G08CBF (PDF version)
G08 Chapter Contents
G08 Chapter Introduction
NAG Library Manual

NAG Library Routine Document

G08CBF

Note:  before using this routine, please read the Users' Note for your implementation to check the interpretation of bold italicised terms and other implementation-dependent details.

 Contents

    1  Purpose
    7  Accuracy

1  Purpose

G08CBF performs the one sample Kolmogorov–Smirnov test, using one of the distributions provided.

2  Specification

SUBROUTINE G08CBF ( N, X, DIST, PAR, ESTIMA, NTYPE, D, Z, P, SX, IFAIL)
INTEGER  N, NTYPE, IFAIL
REAL (KIND=nag_wp)  X(N), PAR(2), D, Z, P, SX(N)
CHARACTER(*)  DIST
CHARACTER(1)  ESTIMA

3  Description

The data consist of a single sample of n  observations denoted by x 1 , x 2 , , x n . Let S n x i  and F 0 x i  represent the sample cumulative distribution function and the theoretical (null) cumulative distribution function respectively at the point x i  where x i  is the i th smallest sample observation.
The Kolmogorov–Smirnov test provides a test of the null hypothesis H 0 : the data are a random sample of observations from a theoretical distribution specified by you against one of the following alternative hypotheses:
(i) H 1 : the data cannot be considered to be a random sample from the specified null distribution.
(ii) H 2 : the data arise from a distribution which dominates the specified null distribution. In practical terms, this would be demonstrated if the values of the sample cumulative distribution function S n x  tended to exceed the corresponding values of the theoretical cumulative distribution function F 0 x .
(iii) H 3 : the data arise from a distribution which is dominated by the specified null distribution. In practical terms, this would be demonstrated if the values of the theoretical cumulative distribution function F 0 x  tended to exceed the corresponding values of the sample cumulative distribution function S n x .
One of the following test statistics is computed depending on the particular alternative null hypothesis specified (see the description of the parameter NTYPE in Section 5).
For the alternative hypothesis H 1 .
For the alternative hypothesis H 2 .
For the alternative hypothesis H 3 .
The standardized statistic Z = D × n  is also computed where D  may be D n , D n +  or D n -  depending on the choice of the alternative hypothesis. This is the standardized value of D  with no correction for continuity applied and the distribution of Z  converges asymptotically to a limiting distribution, first derived by Kolmogorov (1933), and then tabulated by Smirnov (1948). The asymptotic distributions for the one-sided statistics were obtained by Smirnov (1933).
The probability, under the null hypothesis, of obtaining a value of the test statistic as extreme as that observed, is computed. If n100  an exact method given by Conover (1980), is used. Note that the method used is only exact for continuous theoretical distributions and does not include Conover's modification for discrete distributions. This method computes the one-sided probabilities. The two-sided probabilities are estimated by doubling the one-sided probability. This is a good estimate for small p , that is p0.10 , but it becomes very poor for larger p . If n>100  then p  is computed using the Kolmogorov–Smirnov limiting distributions, see Feller (1948), Kendall and Stuart (1973), Kolmogorov (1933), Smirnov (1933) and Smirnov (1948).

4  References

Conover W J (1980) Practical Nonparametric Statistics Wiley
Feller W (1948) On the Kolmogorov–Smirnov limit theorems for empirical distributions Ann. Math. Statist. 19 179–181
Kendall M G and Stuart A (1973) The Advanced Theory of Statistics (Volume 2) (3rd Edition) Griffin
Kolmogorov A N (1933) Sulla determinazione empirica di una legge di distribuzione Giornale dell' Istituto Italiano degli Attuari 4 83–91
Siegel S (1956) Non-parametric Statistics for the Behavioral Sciences McGraw–Hill
Smirnov N (1933) Estimate of deviation between empirical distribution functions in two independent samples Bull. Moscow Univ. 2(2) 3–16
Smirnov N (1948) Table for estimating the goodness of fit of empirical distributions Ann. Math. Statist. 19 279–281

5  Parameters

1:     N – INTEGERInput
On entry: n, the number of observations in the sample.
Constraint: N3 .
2:     XN – REAL (KIND=nag_wp) arrayInput
On entry: the sample observations x 1 , x 2 , , x n .
Constraint: the sample observations supplied must be consistent, in the usual manner, with the null distribution chosen, as specified by the parameters DIST and PAR. For further details see Section 9.
3:     DIST – CHARACTER(*)Input
On entry: the theoretical (null) distribution from which it is suspected the data may arise.
DIST='U'
The uniform distribution over a,b.
DIST='N'
The Normal distribution with mean μ and variance σ2.
DIST='G'
The gamma distribution with shape parameterα and scale parameter β, where the mean =αβ.
DIST='BE'
The beta distribution with shape parameters α and β, where the mean =α/α+β.
DIST='BI'
The binomial distribution with the number of trials, m, and the probability of a success, p.
DIST='E'
The exponential distribution with parameter λ, where the mean =1/λ.
DIST='P'
The Poisson distribution with parameter μ, where the mean =μ.
DIST='NB'
The negative binomial distribution with the number of trials, m, and the probability of success, p.
DIST='GP'
The generalized Pareto distribution with shape parameter ξ and scale β.
Any number of characters may be supplied as the actual parameter, however only the characters, maximum 2, required to uniquely identify the distribution are referenced.
Constraint: DIST='U', 'N', 'G', 'BE', 'BI', 'E', 'P', 'NB' or 'GP'.
4:     PAR2 – REAL (KIND=nag_wp) arrayInput/Output
On entry: if ESTIMA='S', PAR must contain the known values of the parameter(s) of the null distribution as follows.
If a uniform distribution is used, then PAR1 and PAR2 must contain the boundaries a and b respectively.
If a Normal distribution is used, then PAR1 and PAR2 must contain the mean, μ, and the variance, σ2, respectively.
If a gamma distribution is used, then PAR1 and PAR2 must contain the parameters α and β respectively.
If a beta distribution is used, then PAR1 and PAR2 must contain the parameters α and β respectively.
If a binomial distribution is used, then PAR1 and PAR2 must contain the parameters m and p respectively.
If an exponential distribution is used, then PAR1 must contain the parameter λ.
If a Poisson distribution is used, then PAR1 must contain the parameter μ.
If a negative binomial distribution is used, PAR1 and PAR2 must contain the parameters m and p respectively.
If a generalized Pareto distribution is used, PAR1 and PAR2 must contain the parameters ξ and β respectively.
If ESTIMA='E', PAR need not be set except when the null distribution requested is either the binomial or the negative binomial distribution in which case PAR1 must contain the parameter m.
On exit: if ESTIMA='S', PAR is unchanged; if ESTIMA='E', and DIST='BI' or DIST='NB' then PAR2 is estimated from the data; otherwise PAR1 and PAR2 are estimated from the data.
Constraints:
  • if DIST='U', PAR1<PAR2;
  • if DIST='N', PAR2>0.0;
  • if DIST='G', PAR1>0.0 and PAR2>0.0;
  • if DIST='BE', PAR1>0.0 and PAR2>0.0 and PAR1106 and PAR2106;
  • if DIST='BI', PAR11.0 and 0.0<PAR2<1.0 and PAR1×PAR2×1.0-PAR2106 and PAR1<1/eps, where eps=machine precision, see X02AJF;
  • if DIST='E', PAR1>0.0;
  • if DIST='P', PAR1>0.0 and PAR1106;
  • if DIST='NB', PAR11.0 and 0.0<PAR2<1.0 and PAR1× 1.0-PAR2 / PAR2× PAR2 106 and PAR1<1/eps, where eps=machine precision, see X02AJF;
  • if DIST='GP', PAR2>0.
5:     ESTIMA – CHARACTER(1)Input
On entry: ESTIMA must specify whether values of the parameters of the null distribution are known or are to be estimated from the data.
ESTIMA='S'
Values of the parameters will be supplied in the array PAR described above.
ESTIMA='E'
Parameters are to be estimated from the data except when the null distribution requested is the binomial distribution or the negative binomial distribution in which case the first parameter, m, must be supplied in PAR1 and only the second parameter, p, is estimated from the data.
Constraint: ESTIMA='S' or 'E'.
6:     NTYPE – INTEGERInput
On entry: the test statistic to be calculated, i.e., the choice of alternative hypothesis.
NTYPE=1
Computes D n , to test H 0  against H 1 ,
NTYPE=2
Computes D n + , to test H 0  against H 2 ,
NTYPE=3
Computes D n - , to test H 0  against H 3 .
Constraint: NTYPE=1, 2 or 3.
7:     D – REAL (KIND=nag_wp)Output
On exit: the Kolmogorov–Smirnov test statistic ( D n , D n +  or D n -  according to the value of NTYPE).
8:     Z – REAL (KIND=nag_wp)Output
On exit: a standardized value, Z , of the test statistic, D , without any correction for continuity.
9:     P – REAL (KIND=nag_wp)Output
On exit: the probability, p , associated with the observed value of D  where D  may be D n , D n +  or D n -  depending on the value of NTYPE (see Section 3).
10:   SXN – REAL (KIND=nag_wp) arrayOutput
On exit: the sample observations, x1,x2,,xn, sorted in ascending order.
11:   IFAIL – INTEGERInput/Output
On entry: IFAIL must be set to 0, -1​ or ​1. If you are unfamiliar with this parameter you should refer to Section 3.3 in the Essential Introduction for details.
For environments where it might be inappropriate to halt program execution when an error is detected, the value -1​ or ​1 is recommended. If the output of error messages is undesirable, then the value 1 is recommended. Otherwise, if you are not familiar with this parameter, the recommended value is 0. When the value -1​ or ​1 is used it is essential to test the value of IFAIL on exit.
On exit: IFAIL=0 unless the routine detects an error or a warning has been flagged (see Section 6).

6  Error Indicators and Warnings

If on entry IFAIL=0 or -1, explanatory error messages are output on the current error message unit (as defined by X04AAF).
Errors or warnings detected by the routine:
IFAIL=1
On entry, N=value.
Constraint: N3.
IFAIL=2
On entry, DIST=value was an illegal value.
IFAIL=3
On entry, NTYPE=value.
Constraint: NTYPE=1, 2 or 3.
IFAIL=4
On entry, ESTIMA=value was an illegal value.
IFAIL=5
On entry, DIST='BI' and m=PAR1=value.
Note that m must always be supplied.
Constraint: for the binomial distribution, 1PAR1<1/eps, where eps=machine precision, see X02AJF.
On entry, DIST='NB' and m=PAR1=value.
Note that m must always be supplied.
Constraint: for the negative binomial distribution, 1PAR1<1/eps, where eps=machine precision, see X02AJF.
On entry, ESTIMA='S' and PAR1=value; PAR2=value.
Constraint: for the beta distribution, 0<PAR1 and PAR21000000.
On entry, ESTIMA='S' and PAR1=value; PAR2=value.
Constraint: for the gamma distribution, PAR1 and PAR2>0.
On entry, ESTIMA='S' and PAR1=value; PAR2=value.
Constraint: for the generalized Pareto distribution with PAR1<0, 0Xi-PAR2/PAR1, for i=1,2,,N.
On entry, ESTIMA='S' and PAR1=value; PAR2=value.
Constraint: for the uniform distribution, PAR1<PAR2.
On entry, ESTIMA='S' and PAR1=value.
Constraint: for the exponential distribution, PAR1>0.
On entry, ESTIMA='S' and PAR1=value.
Constraint: for the Poisson distribution, 0<PAR1<1000000.
On entry, ESTIMA='S' and PAR2=value.
Constraint: for the binomial distribution, 0<PAR2<1.
On entry, ESTIMA='S' and PAR2=value.
Constraint: for the generalized Pareto distribution, PAR2>0.
On entry, ESTIMA='S' and PAR2=value.
Constraint: for the negative binomial distribution, 0<PAR2<1.
On entry, ESTIMA='S' and PAR2=value.
Constraint: for the Normal distribution, PAR2>0.
IFAIL=6
On entry, DIST='U' and at least one observation is illegal.
Constraint: PAR1XiPAR2, for i=1,2,,N.
On entry, DIST='G', 'E', 'P', 'NB' or 'GP' and at least one observation is negative.
Constraint: Xi0, for i=1,2,,N.
On entry, DIST='BE' and at least one observation is illegal.
Constraint: 0Xi1, for i=1,2,,N.
On entry, DIST='BI' and all observations are zero or m.
Constraint: at least one 0.0<Xi<PAR1, for i=1,2,,N.
On entry, DIST='BI' and at least one observation is illegal.
Constraint: 0XiPAR1, for i=1,2,,N.
On entry, DIST='E' or 'P' and all observations are zero.
Constraint: at least one Xi>0, for i=1,2,,N.
On entry, DIST='GP' and ESTIMA='E'.
The parameter estimates are invalid; the data may not be from the generalized Pareto distribution.
IFAIL=7
On entry, DIST='U', 'N', 'G', 'BE' or 'GP', ESTIMA='E' and the whole sample is constant. Thus the variance is zero.
IFAIL=8
On entry, DIST='BI', PAR1=value, PAR2=value.
The variance PAR1×PAR2×1-PAR2 exceeds 1000000.
On entry, DIST='NB', PAR1=value, PAR2=value.
The variance PAR1×1-PAR2/PAR2×PAR2 exceeds 1000000.
IFAIL=9
On entry, DIST='G' and in the computation of the incomplete gamma function by S14BAF the convergence of the Taylor series or Legendre continued fraction fails within 600 iterations.
IFAIL=-99
An unexpected error has been triggered by this routine. Please contact NAG.
See Section 3.8 in the Essential Introduction for further information.
IFAIL=-399
Your licence key may have expired or may not have been installed correctly.
See Section 3.7 in the Essential Introduction for further information.
IFAIL=-999
Dynamic memory allocation failed.
See Section 3.6 in the Essential Introduction for further information.

7  Accuracy

The approximation for p , given when n>100 , has a relative error of at most 2.5% for most cases. The two-sided probability is approximated by doubling the one-sided probability. This is only good for small p , i.e., p<0.10  but very poor for large p . The error is always on the conservative side, that is the tail probability, p , is over estimated.

8  Parallelism and Performance

G08CBF is threaded by NAG for parallel execution in multithreaded implementations of the NAG Library.
Please consult the X06 Chapter Introduction for information on how to control and interrogate the OpenMP environment used within this routine. Please also consult the Users' Note for your implementation for any additional implementation-specific information.

9  Further Comments

The time taken by G08CBF increases with n  until n>100  at which point it drops and then increases slowly with n . The time may also depend on the choice of null distribution and on whether or not the parameters are to be estimated.
The data supplied in the parameter X must be consistent with the chosen null distribution as follows:

10  Example

The following example program reads in a set of data consisting of 30 observations. The Kolmogorov–Smirnov test is then applied twice, firstly to test whether the sample is taken from a uniform distribution, U0,2, and secondly to test whether the sample is taken from a Normal distribution where the mean and variance are estimated from the data. In both cases we are testing against H1; that is, we are doing a two tailed test. The values of D, Z and P are printed for each case.

10.1  Program Text

Program Text (g08cbfe.f90)

10.2  Program Data

Program Data (g08cbfe.d)

10.3  Program Results

Program Results (g08cbfe.r)


G08CBF (PDF version)
G08 Chapter Contents
G08 Chapter Introduction
NAG Library Manual

© The Numerical Algorithms Group Ltd, Oxford, UK. 2015