G08 Chapter Contents
G08 Chapter Introduction
NAG Library Manual

# NAG Library Routine DocumentG08CBF

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.

## 1  Purpose

G08CBF performs the one sample Kolmogorov–Smirnov test, using one of the standard 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},\dots ,{x}_{n}$. Let ${S}_{n}\left({x}_{\left(i\right)}\right)$ and ${F}_{0}\left({x}_{\left(i\right)}\right)$ represent the sample cumulative distribution function and the theoretical (null) cumulative distribution function respectively at the point ${x}_{\left(i\right)}$ where ${x}_{\left(i\right)}$ 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}\left(x\right)$ tended to exceed the corresponding values of the theoretical cumulative distribution function ${F}_{0}\left(x\right)$. (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}\left(x\right)$ tended to exceed the corresponding values of the sample cumulative distribution function ${S}_{n}\left(x\right)$.
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}$.
• ${D}_{n}$ – the largest absolute deviation between the sample cumulative distribution function and the theoretical cumulative distribution function. Formally ${D}_{n}=\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left\{{D}_{n}^{+},{D}_{n}^{-}\right\}$.
For the alternative hypothesis ${H}_{2}$.
• ${D}_{n}^{+}$ – the largest positive deviation between the sample cumulative distribution function and the theoretical cumulative distribution function. Formally ${D}_{n}^{+}=\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left\{{S}_{n}\left({x}_{\left(i\right)}\right)-{F}_{0}\left({x}_{\left(i\right)}\right),0\right\}$ for both discrete and continuous null distributions.
For the alternative hypothesis ${H}_{3}$.
• ${D}_{n}^{-}$ – the largest positive deviation between the theoretical cumulative distribution function and the sample cumulative distribution function. Formally if the null distribution is discrete then ${D}_{n}^{-}=\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left\{{F}_{0}\left({x}_{\left(i\right)}\right)-{S}_{n}\left({x}_{\left(i\right)}\right),0\right\}$ and if the null distribution is continuous then ${D}_{n}^{-}=\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left\{{F}_{0}\left({x}_{\left(i\right)}\right)-{S}_{n}\left({x}_{\left(i-1\right)}\right),0\right\}$.
The standardized statistic $Z=D×\sqrt{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 $n\le 100$ 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 $p\le 0.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: ${\mathbf{N}}\ge 3$.
2:     X(N) – REAL (KIND=nag_wp) arrayInput
On entry: the sample observations ${x}_{1},{x}_{2},\dots ,{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 8.
3:     DIST – CHARACTER(*)Input
On entry: the theoretical (null) distribution from which it is suspected the data may arise.
${\mathbf{DIST}}=\text{'U'}$
The uniform distribution over $\left(a,b\right)-U\left(a,b\right)$.
${\mathbf{DIST}}=\text{'N'}$
The Normal distribution with mean $\mu$ and variance ${\sigma }^{2}-N\left(\mu ,{\sigma }^{2}\right)$.
${\mathbf{DIST}}=\text{'G'}$
The gamma distribution with shape parameter $\alpha$ and scale parameter $\beta$, where the mean $\text{}=\alpha \beta$.
${\mathbf{DIST}}=\text{'BE'}$
The beta distribution with shape parameters $\alpha$ and $\beta$, where the mean $\text{}=\alpha /\left(\alpha +\beta \right)$.
${\mathbf{DIST}}=\text{'BI'}$
The binomial distribution with the number of trials, $m$, and the probability of a success, $p$.
${\mathbf{DIST}}=\text{'E'}$
The exponential distribution with parameter $\lambda$, where the mean $\text{}=1/\lambda$.
${\mathbf{DIST}}=\text{'P'}$
The Poisson distribution with parameter $\mu$, where the mean $\text{}=\mu$.
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: ${\mathbf{DIST}}=\text{'U'}$, $\text{'N'}$, $\text{'G'}$, $\text{'BE'}$, $\text{'BI'}$, $\text{'E'}$ or $\text{'P'}$.
4:     PAR($2$) – REAL (KIND=nag_wp) arrayInput/Output
On entry: if ${\mathbf{ESTIMA}}=\text{'S'}$, PAR must contain the known values of the parameter(s) of the null distribution as follows.
If a uniform distribution is used, then ${\mathbf{PAR}}\left(1\right)$ and ${\mathbf{PAR}}\left(2\right)$ must contain the boundaries $a$ and $b$ respectively.
If a Normal distribution is used, then ${\mathbf{PAR}}\left(1\right)$ and ${\mathbf{PAR}}\left(2\right)$ must contain the mean, $\mu$, and the variance, ${\sigma }^{2}$, respectively.
If a gamma distribution is used, then ${\mathbf{PAR}}\left(1\right)$ and ${\mathbf{PAR}}\left(2\right)$ must contain the parameters $\alpha$ and $\beta$ respectively.
If a beta distribution is used, then ${\mathbf{PAR}}\left(1\right)$ and ${\mathbf{PAR}}\left(2\right)$ must contain the parameters $\alpha$ and $\beta$ respectively.
If a binomial distribution is used, then ${\mathbf{PAR}}\left(1\right)$ and ${\mathbf{PAR}}\left(2\right)$ must contain the parameters $m$ and $p$ respectively.
If an exponential distribution is used, then ${\mathbf{PAR}}\left(1\right)$ must contain the parameter $\lambda$.
If a Poisson distribution is used, then ${\mathbf{PAR}}\left(1\right)$ must contain the parameter $\mu$.
If ${\mathbf{ESTIMA}}=$, PAR need not be set except when the null distribution requested is the binomial distribution in which case ${\mathbf{PAR}}\left(1\right)$ must contain the parameter $m$.
On exit: if ${\mathbf{ESTIMA}}=\text{'S'}$, PAR is unchanged. If ${\mathbf{ESTIMA}}=$, then ${\mathbf{PAR}}\left(1\right)$ and ${\mathbf{PAR}}\left(2\right)$ are set to values as estimated from the data.
Constraints:
• if ${\mathbf{DIST}}=\text{'U'}$, ${\mathbf{PAR}}\left(1\right)<{\mathbf{PAR}}\left(2\right)$;
• if ${\mathbf{DIST}}=\text{'N'}$, ${\mathbf{PAR}}\left(2\right)>0.0$;
• if ${\mathbf{DIST}}=\text{'G'}$, ${\mathbf{PAR}}\left(1\right)>0.0$ and ${\mathbf{PAR}}\left(2\right)>0.0$;
• if ${\mathbf{DIST}}=\text{'BE'}$, ${\mathbf{PAR}}\left(1\right)>0.0$ and ${\mathbf{PAR}}\left(2\right)>0.0$ and ${\mathbf{PAR}}\left(1\right)\le {10}^{6}$ and ${\mathbf{PAR}}\left(2\right)\le {10}^{6}$;
• if ${\mathbf{DIST}}=\text{'BI'}$, ${\mathbf{PAR}}\left(1\right)\ge 1.0$ and $0.0<{\mathbf{PAR}}\left(2\right)<1.0$ and ${\mathbf{PAR}}\left(1\right)×{\mathbf{PAR}}\left(2\right)×\left(1.0-{\mathbf{PAR}}\left(2\right)\right)\le {10}^{6}$ and ${\mathbf{PAR}}\left(1\right)<1/\mathrm{eps}$, where , see X02AJF;
• if ${\mathbf{DIST}}=\text{'E'}$, ${\mathbf{PAR}}\left(1\right)>0.0$;
• if ${\mathbf{DIST}}=\text{'P'}$, ${\mathbf{PAR}}\left(1\right)>0.0$ and ${\mathbf{PAR}}\left(1\right)\le {10}^{6}$.
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.
${\mathbf{ESTIMA}}=\text{'S'}$
Values of the parameters will be supplied in the array PAR described above.
${\mathbf{ESTIMA}}=\text{'E'}$
Parameters are to be estimated from the data except when the null distribution requested is the binomial distribution in which case the first parameter, $m$, must be supplied in ${\mathbf{PAR}}\left(1\right)$ and only the second parameter, $p$ is estimated from the data.
Constraint: ${\mathbf{ESTIMA}}=\text{'S'}$ or $\text{'E'}$.
6:     NTYPE – INTEGERInput
On entry: the test statistic to be calculated, i.e., the choice of alternative hypothesis.
${\mathbf{NTYPE}}=1$
Computes ${D}_{n}$, to test ${H}_{0}$ against ${H}_{1}$,
${\mathbf{NTYPE}}=2$
Computes ${D}_{n}^{+}$, to test ${H}_{0}$ against ${H}_{2}$,
${\mathbf{NTYPE}}=3$
Computes ${D}_{n}^{-}$, to test ${H}_{0}$ against ${H}_{3}$.
Constraint: ${\mathbf{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:   SX(N) – REAL (KIND=nag_wp) arrayOutput
On exit: the sample observations, ${x}_{1},{x}_{2},\dots ,{x}_{n}$, sorted in ascending order.
11:   IFAIL – INTEGERInput/Output
On entry: IFAIL must be set to $0$, $-1\text{​ 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\text{​ 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 $-\mathbf{1}\text{​ or ​}\mathbf{1}$ is used it is essential to test the value of IFAIL on exit.
On exit: ${\mathbf{IFAIL}}={\mathbf{0}}$ unless the routine detects an error or a warning has been flagged (see Section 6).

## 6  Error Indicators and Warnings

If on entry ${\mathbf{IFAIL}}={\mathbf{0}}$ or $-{\mathbf{1}}$, explanatory error messages are output on the current error message unit (as defined by X04AAF).
Errors or warnings detected by the routine:
${\mathbf{IFAIL}}=1$
On entry, ${\mathbf{N}}<3$.
${\mathbf{IFAIL}}=2$
On entry, an invalid code for DIST has been specified.
${\mathbf{IFAIL}}=3$
On entry, ${\mathbf{NTYPE}}\ne 1$, $2$ or $3$.
${\mathbf{IFAIL}}=4$
On entry, ${\mathbf{ESTIMA}}\ne \text{'S'}$ or $\text{'E'}$.
${\mathbf{IFAIL}}=5$
On entry, the parameters supplied for the specified null distribution are out of range (see Section 5). Apart from a check on the first parameter for the binomial distribution (${\mathbf{DIST}}=\text{'BI'}$) this error will only occur if ${\mathbf{ESTIMA}}=\text{'S'}$.
${\mathbf{IFAIL}}=6$
The data supplied in X could not arise from the chosen null distribution, as specified by the parameters DIST and PAR. For further details see Section 8.
${\mathbf{IFAIL}}=7$
The whole sample is constant, i.e., the variance is zero. This error may only occur if (${\mathbf{DIST}}=\text{'U'}$, $\text{'N'}$, $\text{'G'}$ or $\text{'BE'}$) and ${\mathbf{ESTIMA}}=\text{'E'}$.
${\mathbf{IFAIL}}=8$
The variance of the binomial distribution (${\mathbf{DIST}}=\text{'BI'}$) is too large. That is, $\mathit{mp}\left(1-p\right)>1000000$.
${\mathbf{IFAIL}}=9$
When ${\mathbf{DIST}}=\text{'G'}$, in the computation of the incomplete gamma function by S14BAF the convergence of the Taylor series or Legendre continued fraction fails within $600$ iterations. This is an unlikely error exit.

## 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.

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:
• when ${\mathbf{DIST}}=\text{'U'}$, then ${\mathbf{PAR}}\left(1\right)\le {x}_{i}\le {\mathbf{PAR}}\left(2\right)$, for $i=1,2,\dots ,n$;
• when ${\mathbf{DIST}}=\text{'N'}$, then there are no constraints on the ${x}_{i}$'s;
• when ${\mathbf{DIST}}=\text{'G'}$, then ${x}_{i}\ge 0.0$, for $i=1,2,\dots ,n$;
• when ${\mathbf{DIST}}=\text{'BE'}$, then $0.0\le {x}_{i}\le 1.0$, for $i=1,2,\dots ,n$;
• when ${\mathbf{DIST}}=\text{'BI'}$, then $0.0\le {x}_{i}\le {\mathbf{PAR}}\left(1\right)$, for $i=1,2,\dots ,n$;
• when ${\mathbf{DIST}}=\text{'E'}$, then ${x}_{i}\ge 0.0$, for $i=1,2,\dots ,n$;
• when ${\mathbf{DIST}}=\text{'P'}$, then ${x}_{i}\ge 0.0$, for $i=1,2,\dots ,n$.

## 9  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, $U\left(0,2\right)$, 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 ${H}_{1}$; that is, we are doing a two tailed test. The values of D, Z and P are printed for each case.

### 9.1  Program Text

Program Text (g08cbfe.f90)

### 9.2  Program Data

Program Data (g08cbfe.d)

### 9.3  Program Results

Program Results (g08cbfe.r)