# How to use chi2gof within CUPID

4 views (last 30 days)

Show older comments

Two examples of usage of the Matlab's "Chi-square goodness-of-fit test" (chi2gof) function are the following:

First (comparing two frequency distributions):

Population = [996, 749, 370, 53, 9, 3, 1, 0];

Sample = [647, 486, 100, 22, 0, 0, 0, 0];

Population2 = [996, 749, 370, sum(Population(4:8))];

Sample2 = [647, 486, 100, sum(Sample(4:8))];

x = [];

for i = 1:length(Sample2)

x = [x,i*ones(1,Sample2(i))];

end

edges = .5+(0:length(Sample2));

[h,p,k] = chi2gof(x,'Expected',Population2,'Edges',edges)

Second (fit a distribution to data):

bins = 0:5;

obsCounts = [6 16 10 12 4 2];

n = sum(obsCounts);

pd = fitdist(bins','Poisson','Frequency',obsCounts');

expCounts = n * pdf(pd,bins);

[h,p,st] = chi2gof(bins,'Ctrs',bins,...

'Frequency',obsCounts, ...

'Expected',expCounts,...

'NParams',1)

addpath('.../Cupid-master')

% (1) create a "truncated dataset"

pd = makedist('Weibull','a',3,'b',5);

t = truncate(pd,3,inf);

data_trunc = random(t,10000,1);

% (2) fit a distribution (in this case the "Weibull2") to the "truncated test"

fittedDist = TruncatedXlow(Weibull2(2,2),3);

% (3) estimate the Weibull parameters by maximum likelihood, allowing for the truncation.

fittedDist.EstML(data_trunc);

% (4) plot both the "truncated test" (through the histogram) and the "fitting distribution"

% (in this case the "Weibull2" with Weibull's parameters estimated by maximum likelihood)

figure

xgrid = linspace(0,100,1000)';

histogram(data_trunc,100,'Normalization','pdf','facecolor','blue')

line(xgrid,fittedDist.PDF(xgrid),'Linewidth',2,'color','red')

xlim([2.5 6])

##### 0 Comments

### Accepted Answer

Jeff Miller
on 23 Jun 2023

### More Answers (1)

### See Also

### Categories

### Community Treasure Hunt

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

Start Hunting!