G08 Chapter Contents
G08 Chapter Introduction
NAG Library Manual

# NAG Library Routine DocumentG08CDF

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

G08CDF performs the two sample Kolmogorov–Smirnov distribution test.

## 2  Specification

 SUBROUTINE G08CDF ( N1, X, N2, Y, NTYPE, D, Z, P, SX, SY, IFAIL)
 INTEGER N1, N2, NTYPE, IFAIL REAL (KIND=nag_wp) X(N1), Y(N2), D, Z, P, SX(N1), SY(N2)

## 3  Description

The data consists of two independent samples, one of size ${n}_{1}$, denoted by ${x}_{1},{x}_{2},\dots ,{x}_{{n}_{1}}$, and the other of size ${n}_{2}$ denoted by ${y}_{1},{y}_{2},\dots ,{y}_{{n}_{2}}$. Let $F\left(x\right)$ and $G\left(x\right)$ represent their respective, unknown, distribution functions. Also let ${S}_{1}\left(x\right)$ and ${S}_{2}\left(x\right)$ denote the values of the sample cumulative distribution functions at the point $x$ for the two samples respectively.
The Kolmogorov–Smirnov test provides a test of the null hypothesis ${H}_{0}$: $F\left(x\right)=G\left(x\right)$ against one of the following alternative hypotheses:
 (i) ${H}_{1}$: $F\left(x\right)\ne G\left(x\right)$. (ii) ${H}_{2}$: $F\left(x\right)>G\left(x\right)$. This alternative hypothesis is sometimes stated as, ‘The $x$'s tend to be smaller than the $y$'s’, i.e., it would be demonstrated in practical terms if the values of ${S}_{1}\left(x\right)$ tended to exceed the corresponding values of ${S}_{2}\left(x\right)$. (iii) ${H}_{3}$: $F\left(x\right). This alternative hypothesis is sometimes stated as, ‘The $x$'s tend to be larger than the $y$'s’, i.e., it would be demonstrated in practical terms if the values of ${S}_{2}\left(x\right)$ tended to exceed the corresponding values of ${S}_{1}\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}_{1},{n}_{2}}$ – the largest absolute deviation between the two sample cumulative distribution functions.
For the alternative hypothesis ${H}_{2}$.
• ${D}_{{n}_{1},{n}_{2}}^{+}$ – the largest positive deviation between the sample cumulative distribution function of the first sample, ${S}_{1}\left(x\right)$, and the sample cumulative distribution function of the second sample, ${S}_{2}\left(x\right)$. Formally ${D}_{{n}_{1},{n}_{2}}^{+}=\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left\{{S}_{1}\left(x\right)-{S}_{2}\left(x\right),0\right\}$.
For the alternative hypothesis ${H}_{3}$.
• ${D}_{{n}_{1},{n}_{2}}^{-}$ – the largest positive deviation between the sample cumulative distribution function of the second sample, ${S}_{2}\left(x\right)$, and the sample cumulative distribution function of the first sample, ${S}_{1}\left(x\right)$. Formally ${D}_{{n}_{1},{n}_{2}}^{-}=\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left\{{S}_{2}\left(x\right)-{S}_{1}\left(x\right),0\right\}$.
G08CDF also returns the standardized statistic $Z=\sqrt{\frac{{n}_{1}+{n}_{2}}{{n}_{1}{n}_{2}}}×D$, where $D$ may be ${D}_{{n}_{1},{n}_{2}}$, ${D}_{{n}_{1},{n}_{2}}^{+}$ or ${D}_{{n}_{1},{n}_{2}}^{-}$ depending on the choice of the alternative hypothesis. The distribution of this statistic converges asymptotically to a distribution given by Smirnov as ${n}_{1}$ and ${n}_{2}$ increase; see Feller (1948), Kendall and Stuart (1973), Kim and Jenrich (1973), Smirnov (1933) or Smirnov (1948).
The probability, under the null hypothesis, of obtaining a value of the test statistic as extreme as that observed, is computed. If $\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left({n}_{1},{n}_{2}\right)\le 2500$ and ${n}_{1}{n}_{2}\le 10000$ then an exact method given by Kim and Jenrich (see Kim and Jenrich (1973)) is used. Otherwise $p$ is computed using the approximations suggested by Kim and Jenrich (1973). Note that the method used is only exact for continuous theoretical distributions. This method computes the two-sided probability. The one-sided probabilities are estimated by halving the two-sided probability. This is a good estimate for small $p$, that is $p\le 0.10$, but it becomes very poor for larger $p$.

## 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
Kim P J and Jenrich R I (1973) Tables of exact sampling distribution of the two sample Kolmogorov–Smirnov criterion ${D}_{mn}\left(m Selected Tables in Mathematical Statistics 1 80–129 American Mathematical Society
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:     N1 – INTEGERInput
On entry: the number of observations in the first sample, ${n}_{1}$.
Constraint: ${\mathbf{N1}}\ge 1$.
2:     X(N1) – REAL (KIND=nag_wp) arrayInput
On entry: the observations from the first sample, ${x}_{1},{x}_{2},\dots ,{x}_{{n}_{1}}$.
3:     N2 – INTEGERInput
On entry: the number of observations in the second sample, ${n}_{2}$.
Constraint: ${\mathbf{N2}}\ge 1$.
4:     Y(N2) – REAL (KIND=nag_wp) arrayInput
On entry: the observations from the second sample, ${y}_{1},{y}_{2},\dots ,{y}_{{n}_{2}}$.
5:     NTYPE – INTEGERInput
On entry: the statistic to be computed, i.e., the choice of alternative hypothesis.
${\mathbf{NTYPE}}=1$
Computes ${D}_{{n}_{1}{n}_{2}}$, to test against ${H}_{1}$.
${\mathbf{NTYPE}}=2$
Computes ${D}_{{n}_{1}{n}_{2}}^{+}$, to test against ${H}_{2}$.
${\mathbf{NTYPE}}=3$
Computes ${D}_{{n}_{1}{n}_{2}}^{-}$, to test against ${H}_{3}$.
Constraint: ${\mathbf{NTYPE}}=1$, $2$ or $3$.
6:     D – REAL (KIND=nag_wp)Output
On exit: the Kolmogorov–Smirnov test statistic (${D}_{{n}_{1}{n}_{2}}$, ${D}_{{n}_{1}{n}_{2}}^{+}$ or ${D}_{{n}_{1}{n}_{2}}^{-}$ according to the value of NTYPE).
7:     Z – REAL (KIND=nag_wp)Output
On exit: a standardized value, $Z$, of the test statistic, $D$, without any correction for continuity.
8:     P – REAL (KIND=nag_wp)Output
On exit: the tail probability associated with the observed value of $D$, where $D$ may be ${D}_{{n}_{1},{n}_{2}},{D}_{{n}_{1},{n}_{2}}^{+}$ or ${D}_{{n}_{1},{n}_{2}}^{-}$ depending on the value of NTYPE (see Section 3).
9:     SX(N1) – REAL (KIND=nag_wp) arrayOutput
On exit: the observations from the first sample sorted in ascending order.
10:   SY(N2) – REAL (KIND=nag_wp) arrayOutput
On exit: the observations from the second sample 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{N1}}<1$, or ${\mathbf{N2}}<1$.
${\mathbf{IFAIL}}=2$
 On entry, ${\mathbf{NTYPE}}\ne 1$, $2$ or $3$.
${\mathbf{IFAIL}}=3$
The iterative procedure used in the approximation of the probability for large ${n}_{1}$ and ${n}_{2}$ did not converge. For the two-sided test, $p=1$ is returned. For the one-sided test, $p=0.5$ is returned.

## 7  Accuracy

The large sample distributions used as approximations to the exact distribution should have a relative error of less than 5% for most cases.

The time taken by G08CDF increases with ${n}_{1}$ and ${n}_{2}$, until ${n}_{1}{n}_{2}>10000$ or $\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left({n}_{1},{n}_{2}\right)\ge 2500$. At this point one of the approximations is used and the time decreases significantly. The time then increases again modestly with ${n}_{1}$ and ${n}_{2}$.

## 9  Example

This example computes the two-sided Kolmogorov–Smirnov test statistic for two independent samples of size $100$ and $50$ respectively. The first sample is from a uniform distribution $U\left(0,2\right)$. The second sample is from a uniform distribution $U\left(0.25,2.25\right)$. The test statistic, ${D}_{{n}_{1},{n}_{2}}$, the standardized test statistic, $Z$, and the tail probability, $p$, are computed and printed.

### 9.1  Program Text

Program Text (g08cdfe.f90)

### 9.2  Program Data

Program Data (g08cdfe.d)

### 9.3  Program Results

Program Results (g08cdfe.r)