Accord.Statistics.Distributions.Univariate.KolmogorovSmirnovDistribution.Durbin C# (CSharp) Method

Durbin() public static method

Durbin's algorithm for computing P[Dn < d]

The method presented by Marsaglia (2003), as stated in the paper, is based on a succession of developments starting with Kolmogorov and culminating in a masterful treatment by Durbin (1972). Durbin's monograph summarized and extended many previous works published in the years 1933-73.

This function implements the small C procedure provided by Marsaglia on his paper with corrections made by Simard (2010). Further optimizations also have been performed.

References: - Marsaglia, G., Tsang, W. W., Wang, J. (2003) "Evaluating Kolmogorov's Distribution", Journal of Statistical Software, 8 (18), 1–4. jstor. Available on: http://www.jstatsoft.org/v08/i18/paper - Durbin, J. (1972) Distribution Theory for Tests Based on The Sample Distribution Function, Society for Industrial & Applied Mathematics, Philadelphia.
public static Durbin ( int n, double d ) : double
n int
d double
return double
        public static double Durbin(int n, double d)
        {
            double s;

            int k = (int)(n * d) + 1;
            int m = 2 * k - 1;
            double h = k - n * d;
            double[,] H = new double[m, m];
            double[,] Q = new double[m, m];
            double[,] B = new double[m, m];


            for (int i = 0; i < m; i++)
                for (int j = 0; j < m; j++)
                    if (i - j + 1 >= 0)
                        H[i, j] = 1;

            for (int i = 0; i < m; i++)
            {
                H[i, 0] -= Math.Pow(h, i + 1);
                H[m - 1, i] -= Math.Pow(h, m - i);
            }

            H[m - 1, 0] += (2 * h - 1 > 0 ? Math.Pow(2 * h - 1, m) : 0);

            for (int i = 0; i < m; i++)
                for (int j = 0; j < m; j++)
                    if (i - j + 1 > 0)
                        for (int g = 1; g <= i - j + 1; g++)
                            H[i, j] /= g;

            int pQ = 0;
            matrixPower(H, 0, Q, ref pQ, m, n, B);

            s = Q[k - 1, k - 1];

            for (int i = 1; i <= n; i++)
            {
                s *= (double)i / n;
                if (s < 1.0e-140)
                {
                    s *= 1.0e140;
                    pQ -= 140;
                }
            }

            return s * Math.Pow(10.0, pQ);
        }