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)); See also wchi2cdf, qlevels
Calculates quantile levels which encloses P% of PDF | |
Chi squared cumulative distribution function | |
True for structures. |
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 % 020 % See also wchi2cdf, qlevels 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
Comments or corrections to the WAFO group