Usage of chi2gof: how to derive the "expected" values from the fitting distribution to then use them as argument of chi2gof?

2 次查看(过去 30 天)
How to derive the "expected" values from the fitting distribution to then use them as argument of chi2gof?
observed_data = exprnd(2, 100, 1);
xgrid = linspace(0,100,1000)';
pd = fitdist(observed_data,'Exponential'); % <-- fitting distribution
hold on
line(xgrid,pdf(pd,xgrid),'Linewidth',2,'color','b')
histogram(observed_data,100,'Normalization','pdf','facecolor','blue')
hold off
% Desired output
% [h, p] = chi2gof(observed_data, 'Expected', expected_counts)

采纳的回答

Aman
Aman 2023-6-21
Hi Sim,
To generate the expected counts from a fitted distribution, you can use the probability density function (PDF) of the fitted distribution to generate the expected values for each bin.
Here's an example for the same,
observed_data = exprnd(2, 100, 1);
pd = fitdist(observed_data,'Exponential');
num_bins = 10;
bin_edges = linspace(min(observed_data), max(observed_data), num_bins+1);
expected_values = numel(observed_data) * diff(cdf(pd,bin_edges));
% since the area of the pdf must be 1, it is better to obtain the expected_counts by multiplying with the total number of observations accumulated on every single bin
expected_counts = expected_values * numel(observed_data);
[h, p] = chi2gof(observed_data, 'Expected', expected_counts);
Hope this helps!
  3 个评论

请先登录,再进行评论。

更多回答(0 个)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by