Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
goodness of fit test for geometric distribution

Subject: goodness of fit test for geometric distribution

From: Erica Silva

Date: 7 Jul, 2003 16:46:42

Message: 1 of 2

Does anybody have a function that performs a goodness of fit test for
discrete samples and geometric and poisson distributions?

Subject: goodness of fit test for geometric distribution

From: Peter Perkins

Date: 11 Jul, 2003 14:09:13

Message: 2 of 2

> Does anybody have a function that performs a goodness of fit test for
> discrete samples and geometric and poisson distributions?

Hi Erica -

Would a chi-squared goodness of fit test help? There's no MATLAB function for
that per se, but the code do do the test is only a few lines long.

First fit a geometric distribution to your data:

 >> x = [0 7 15 1 2 5 4 19 0 0 6 2 8 2 4 1 0 2 0 1 8 0 7 2 1]; % made-up data
 >> n = length(x);
 >> phat = mle('geometric',x)
phat =
     0.2049
 >> q = 1; % one estimated parameter


Next, figure out how to group the data so that the expected number of
observations in each group is at least 5. The first column here is k from
zero up to 10, the second column is the expected number of observations equal
to k under the estimated model, Geo(phat), i.e., the sample size times the
probability of getting k. After looking at this table, I decide to group
observations into {0}, {1,2}, {3,4,5}, and {6 or higher}:

 >> [(0:10); geopdf((0:10),phat)*n]'
ans =
          0 5.1230
     1.0000 4.0732
     2.0000 3.2385
     3.0000 2.5749
     4.0000 2.0472
     5.0000 1.6277
     6.0000 1.2942
     7.0000 1.0290
     8.0000 0.8181
     9.0000 0.6505
    10.0000 0.5172
 >> binEdges = [0 1 3 6 Inf]; % left edge of each bin, plus Inf


Next, compute the expected number of observations in each group, and the
observed number:

 >> binProbs = diff([0 geocdf(binEdges(2:(end-1))-1,phat) 1]);
 >> e = n*binProbs
e =
     5.1230 7.3117 6.2498 6.3156
 >> o = histc(x,binEdges); o(end) = []
o =
      6 9 3 7
 >> chi2 = sum((o-e).^2 ./ e)


Finally, compute the chi-squareed statistic, its degrees of freedom, and its
p-value:
chi2 =
     2.3040
 >> nbins = length(binEdges) - 1;
 >> dof = nbins - q - 1
dof =
      2
 >> pval = 1-chi2cdf(chi2,dof)
pval =
     0.3160

So, this test does not provide strong evidence against the geometric model. A
chi-quared test for the Poisson is more or less the same, just use poisspdf
and poisscdf.

Hope this helps.

- Peter Perkins
   The MathWorks, Inc.

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us