Home > wafo > kdetools > chi2gof2.m

# chi2gof2

## PURPOSE

CHI Squared Goodness Of Fit test.

## SYNOPSIS

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

## DESCRIPTION

```  CHI2GOF2 CHI Squared Goodness Of Fit test.

CALL:  P = chi2gof2(fs, ft)

P      = P - value
fs     = fitted PDF matrix
evaluated at observed points.
ft     = theoretical PDF matrix
evaluated at simulated points

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);
xs2 = wnormrnd(0,1,100000,1);
p   = chi2gof2(wnormpdf(xs),wnormpdf(xs2));

See also  wchi2cdf, qlevels2```

## CROSS-REFERENCE INFORMATION

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

## SOURCE CODE

```001 function [pvalue, test, v] = chi2gof2(fs,ft)
002 % CHI2GOF2 CHI Squared Goodness Of Fit test.
003 %
004 % CALL:  P = chi2gof2(fs, ft)
005 %
006 %   P      = P - value
007 %   fs     = fitted PDF matrix
008 %            evaluated at observed points.
009 %   ft     = theoretical PDF matrix
010 %            evaluated at simulated points
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 %   xs2 = wnormrnd(0,1,100000,1);
018 %   p   = chi2gof2(wnormpdf(xs),wnormpdf(xs2));
019 %
020 % See also  wchi2cdf, qlevels2
021
022
023 %Tested on: matlab 5.3
024 % History:
025 % revised pab feb2005
026 % -updated see also line
027 % revised pab 11.11.2000
028 % - made it independent of stats toolbox only dependent on wstats
029 % by pab 21.09.99
030
031
032
033 % CHI^2 goodness of fit (GOF)   test
034
035  if isstruct(fs) % structure
036      r2=fs.f(:);
037    else
038     r2 = fs(:);
039   end
040 ntresh=length(r2);
041 if ntresh>120 % only valid for samples larger than 120
042    if isstruct(ft)
043      r=ft.f;
044    else
045     r = ft;
046   end
047   k=max([ceil(sqrt(ntresh)),8]);%divide the data into k subsets (greater than 8)
048   pk=100/k;                     % with equal probabilty
049   np=ntresh/k; %the expected number of points in each group (must be greater than 5)
050
051   grpdiv=qlevels2(r,(100-pk):-pk:pk); % find dividing levels
052   %grpdiv=normpdf(norminv( (pk:pk:(100-pk))/200))'
053
054
055   test=(sum(grpdiv(1 )>=r2)-np)^2/np +(sum(grpdiv(k-1)<r2)-np)^2/np;
056   for ix=2:k-1,
057     test=test+(sum((grpdiv(ix-1 )<r2).*(grpdiv(ix )>=r2))-np)^2/np;
058   end
059   test %test statistic
060   pvalue=1-wchi2cdf(test,k-1); % pvalue
061   v=k-1;
062 else
063   pvalue=[]
064   disp('to few data')
065   ntresh
066 end
067 return
068
069
070```

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