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

3 visualizzazioni (ultimi 30 giorni)
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)

Risposta accettata

Aman
Aman il 21 Giu 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 Commenti
Aman
Aman il 22 Giu 2023
Hi Sim, Yes, sorry for confusion, that was for PDF, but we needed CDF, so I think expected_values should be used.

Accedi per commentare.

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by