필터 지우기
필터 지우기

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

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

Community Treasure Hunt

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

Start Hunting!

Translated by