comparison m-toolbox/classes/+utils/@math/KSpValue.m @ 0:f0afece42f48

Import.
author Daniele Nicolodi <nicolodi@science.unitn.it>
date Wed, 23 Nov 2011 19:22:13 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:f0afece42f48
1 % KSpValue Compute p-Value of the Kolmogorov - Smirnov distribution
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %
4 % Compute p-Value of the Kolmogorov - Smirnov distribution
5 %
6 % CALL
7 %
8 % pValue = utils.math.KSpValue(KSstatistic,n1,n2);
9 %
10 %
11 % INPUT
12 %
13 % - KSstatistic, value of the statistic of the KS distribution.
14 % Corresponding at KSstatistic = max(abs(CD1-CD2))
15 % - length of the first data series
16 % - length of the second data series
17 %
18 % References:
19 % Marsaglia, G., W.W. Tsang, and J. Wang (2003), "Evaluating Kolmogorov`s
20 % Distribution", Journal of Statistical Software, vol. 8, issue 18.
21 %
22 %
23 % L Ferraioli 06-12-2010
24 %
25 % $Id: KSpValue.m,v 1.3 2011/07/14 07:10:16 mauro Exp $
26 %
27 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
28 function pValue = KSpValue(KSstatistic, n1, n2)
29
30 if isempty(n2)
31 n = n1; % test against theoretical distribution
32 else
33 n = n1*n2/(n1+n2); % test of two empirical distributions
34 end
35 s = n*KSstatistic^2;
36
37 % Following the recipe described in described in Marsaglia, et al.
38 % For d values that are in the far tail of the distribution (i.e.
39 % p-values > .999), the following lines will speed up the computation
40 % significantly, and provide accuracy up to 7 digits.
41 if s == 0
42 pValue = 0;
43 else
44 if (s > 7.24) || ((s > 3.76) && (n > 99))
45 pValue = 2*exp(-(2.000071+.331/sqrt(n)+1.409/n)*s);
46 else
47 % Express d as d = (k-h)/n, where k is a +ve integer and 0 < h < 1.
48 k = ceil(KSstatistic*n);
49 h = k - KSstatistic*n;
50 m = 2*k-1;
51
52 % Create the H matrix, which describes the CDF, as described in Marsaglia,
53 % et al.
54 if m > 1
55 c = 1./gamma((1:m)' + 1);
56
57 r = zeros(1,m);
58 r(1) = 1;
59 r(2) = 1;
60
61 T = toeplitz(c,r);
62
63 T(:,1) = T(:,1) - (h.^[1:m]')./gamma((1:m)' + 1);
64
65 T(m,:) = fliplr(T(:,1)');
66 T(m,1) = (1 - 2*h^m + max(0,2*h-1)^m)/gamma(m+1);
67 else
68 T = (1 - 2*h^m + max(0,2*h-1)^m)/gamma(m+1);
69 end
70
71 % Scaling before raising the matrix to a power
72 if ~isscalar(T)
73 lmax = max(eig(T));
74 T = (T./lmax)^n;
75 else
76 lmax = 1;
77 end
78
79 % Pr(Dn < d) = n!/n * tkk , where tkk is the kth element of Tn = T^n.
80 % p-value = Pr(Dn > d) = 1-Pr(Dn < d)
81 pValue = (1 - exp(gammaln(n+1) + n*log(lmax) - n*log(n)) * T(k,k));
82 end
83 end
84 pValue = abs(pValue);
85
86 end