Usage of chi2gof: how to derive the "expected" values from the fitting distribution to then use them as argument of chi2gof?
11 views (last 30 days)
Show older comments
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)
0 Comments
Accepted Answer
Aman
on 21 Jun 2023
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 Comments
Aman
on 22 Jun 2023
Hi Sim, Yes, sorry for confusion, that was for PDF, but we needed CDF, so I think expected_values should be used.
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

