Home > . > smirnov.m

smirnov

PURPOSE ^

Smirnov test for 2 distributions

SYNOPSIS ^

function [H,prob,d] = smirnov(x1 , x2 , alpha, iflag )

DESCRIPTION ^

 Smirnov test for 2 distributions
   [H,prob,d] = smirnov(x1 , x2 , alpha, iflag )

 Copyright (C) 2005 Marco Ratto

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [H,prob,d] = smirnov(x1 , x2 , alpha, iflag )
0002 % Smirnov test for 2 distributions
0003 %   [H,prob,d] = smirnov(x1 , x2 , alpha, iflag )
0004 %
0005 % Copyright (C) 2005 Marco Ratto
0006 %
0007 
0008 
0009 
0010 if nargin<3
0011     alpha  =  0.05;
0012 end
0013 if nargin<4,
0014     iflag=0;
0015 end
0016 
0017 % empirical cdfs.
0018 xmix= [x1;x2];
0019 bin = [-inf ; sort(xmix) ; inf];
0020 
0021 ncount1 = histc (x1 , bin);
0022 ncount2 = histc (x2 , bin);
0023 
0024 cum1  =  cumsum(ncount1)./sum(ncount1);
0025 cum1  =  cum1(1:end-1);
0026 
0027 cum2  =  cumsum(ncount2)./sum(ncount2);
0028 cum2  =  cum2(1:end-1);
0029 
0030 n1=  length(x1);
0031 n2=  length(x2);
0032 n =  n1*n2 /(n1+n2);
0033 
0034 % Compute the d(n1,n2) statistics.
0035 
0036 if iflag==0,
0037     d  =  max(abs(cum1 - cum2));
0038 elseif iflag==-1
0039     d  =  max(cum2 - cum1);
0040 elseif iflag==1
0041     d  =  max(cum1 - cum2);
0042 end
0043 %
0044 % Compute P-value check H0 hypothesis
0045 %
0046 
0047 lam =  max((sqrt(n) + 0.12 + 0.11/sqrt(n)) * d , 0);
0048 if iflag == 0        
0049     j       =  [1:101]';
0050     prob  =  2 * sum((-1).^(j-1).*exp(-2*lam*lam*j.^2));
0051     
0052     prob=max(prob,0);
0053     prob=min(1,prob);
0054 else
0055     prob  =  exp(-2*lam*lam);
0056 end
0057 
0058 H  =  (alpha >= prob);

Generated on Fri 16-Jun-2006 09:09:06 by m2html © 2003