Home > wafo > kdetools > chi2gof.m

# chi2gof

## PURPOSE

CHI Squared Goodness Of Fit test.

## SYNOPSIS

[pvalue, test, v] = chi2gof(fs,ft)

## DESCRIPTION

```  CHI2GOF CHI Squared Goodness Of Fit test.

CALL:  P = chi2gof(fs, ft)

P      = P - value
fs     = fitted PDF structure/matrix
evaluated at observed points.
ft     = theoretical PDF structure/matrix
evaluated on a equidistant grid

Large P value -> good fit
Small P value -> lesser fit

Example : Check how well rayleigh data can be described by N(0,1)
xs = wraylrnd(1,500,1);
x  = linspace(-7,7,201);
p  = chi2gof(wnormpdf(xs),wnormpdf(x));

## CROSS-REFERENCE INFORMATION

This function calls:
 qlevels Calculates quantile levels which encloses P% of PDF wchi2cdf Chi squared cumulative distribution function isstruct True for structures.
This function is called by:

## SOURCE CODE

```001 function [pvalue, test, v] = chi2gof(fs,ft)
002 % CHI2GOF CHI Squared Goodness Of Fit test.
003 %
004 % CALL:  P = chi2gof(fs, ft)
005 %
006 %   P      = P - value
007 %   fs     = fitted PDF structure/matrix
008 %            evaluated at observed points.
009 %   ft     = theoretical PDF structure/matrix
010 %            evaluated on a equidistant grid
011 %
012 %  Large P value -> good fit
013 %  Small P value -> lesser fit
014 %
015 %  Example : Check how well rayleigh data can be described by N(0,1)
016 %   xs = wraylrnd(1,500,1);
017 %   x  = linspace(-7,7,201);
018 %   p  = chi2gof(wnormpdf(xs),wnormpdf(x));
019 %
021
022
023 %Tested on: matlab 5.3
024 % History:
025 % revised pab 11.11.2000
026 % - made it independent of stats toolbox only dependent on wstats
027 % by pab 21.09.99
028
029
030
031 % CHI^2 goodness of fit (GOF)   test
032
033  if isstruct(fs) % structure
034      r2=fs.f(:);
035    else
036     r2 = fs(:);
037   end
038 ntresh=length(r2);
039 if ntresh>120 % only valid for samples larger than 120
040    if isstruct(ft)
041      r=ft.f;
042    else
043     r = ft;
044   end
045   k=max([ceil(sqrt(ntresh)),8]);%divide the data into k subsets (greater than 8)
046   pk=100/k;                     % with equal probabilty
047   np=ntresh/k; %the expected number of points in each group (must be greater than 5)
048
049   grpdiv=qlevels(r,(100-pk):-pk:pk); % find dividing levels
050   %grpdiv=normpdf(norminv( (pk:pk:(100-pk))/200))'
051
052
053   test=(sum(grpdiv(1 )>=r2)-np)^2/np +(sum(grpdiv(k-1)<r2)-np)^2/np;
054   for ix=2:k-1,
055     test=test+(sum((grpdiv(ix-1 )<r2).*(grpdiv(ix )>=r2))-np)^2/np;
056   end
057   test %test statistic
058   pvalue=1-wchi2cdf(test,k-1); % pvalue
059   v=k-1;
060 else
061   pvalue=[]
062   disp('to few data')
063   ntresh
064 end
065 return
066
067
068```

Mathematical Statistics
Centre for Mathematical Sciences
Lund University with Lund Institute of Technology

Comments or corrections to the WAFO group

Generated on Thu 06-Oct-2005 02:21:16 for WAFO by m2html © 2003