# NAG Library Routine Document

## 1Purpose

g08ccf performs the one sample Kolmogorov–Smirnov distribution test, using a user-specified distribution.

## 2Specification

Fortran Interface
 Subroutine g08ccf ( n, x, cdf, d, z, p, sx,
 Integer, Intent (In) :: n, ntype Integer, Intent (Inout) :: ifail Real (Kind=nag_wp), External :: cdf Real (Kind=nag_wp), Intent (In) :: x(n) Real (Kind=nag_wp), Intent (Out) :: d, z, p, sx(n)
#include <nagmk26.h>
 void g08ccf_ (const Integer *n, const double x[], double (NAG_CALL *cdf)(const double *x),const Integer *ntype, double *d, double *z, double *p, double sx[], Integer *ifail)

## 3Description

The data consists 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 (in cdf) 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 hypothesis specified (see the description of the argument 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 the alternative hypothesis ${H}_{3}$:
• ${D}_{n}^{-}$ – the largest positive deviation between the theoretical cumulative distribution function and the sample cumulative distribution function. Formally ${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\}$. This is only true for continuous distributions. See Section 9 for comments on discrete distributions.
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 continuity correction 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).

## 4References

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

## 5Arguments

1:     $\mathbf{n}$ – IntegerInput
On entry: $n$, the number of observations in the sample.
Constraint: ${\mathbf{n}}\ge 1$.
2:     $\mathbf{x}\left({\mathbf{n}}\right)$ – Real (Kind=nag_wp) arrayInput
On entry: the sample observations, ${x}_{1},{x}_{2},\dots ,{x}_{n}$.
3:     $\mathbf{cdf}$ – real (Kind=nag_wp) Function, supplied by the user.External Procedure
cdf must return the value of the theoretical (null) cumulative distribution function for a given value of its argument.
The specification of cdf is:
Fortran Interface
 Function cdf ( x)
 Real (Kind=nag_wp) :: cdf Real (Kind=nag_wp), Intent (In) :: x
#include <nagmk26.h>
 double cdf (const double *x)
1:     $\mathbf{x}$ – Real (Kind=nag_wp)Input
On entry: the argument for which cdf must be evaluated.
cdf must either be a module subprogram USEd by, or declared as EXTERNAL in, the (sub)program from which g08ccf is called. Arguments denoted as Input must not be changed by this procedure.
Note: cdf should not return floating-point NaN (Not a Number) or infinity values, since these are not handled by g08ccf. If your code inadvertently does return any NaNs or infinities, g08ccf is likely to produce unexpected results.
Constraint: ${\mathbf{cdf}}$ must always return a value in the range $\left[0.0,1.0\right]$ and cdf must always satify the condition that ${\mathbf{cdf}}\left({x}_{1}\right)\le {\mathbf{cdf}}\left({x}_{2}\right)$ for any ${x}_{1}\le {x}_{2}$.
4:     $\mathbf{ntype}$ – IntegerInput
On entry: the 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$.
5:     $\mathbf{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).
6:     $\mathbf{z}$ – Real (Kind=nag_wp)Output
On exit: a standardized value, $Z$, of the test statistic, $D$, without the continuity correction applied.
7:     $\mathbf{p}$ – Real (Kind=nag_wp)Output
On exit: the probability, $p$, associated with the observed value of $D$, where $D$ may ${D}_{n}$, ${D}_{n}^{+}$ or ${D}_{n}^{-}$ depending on the value of ntype (see Section 3).
8:     $\mathbf{sx}\left({\mathbf{n}}\right)$ – Real (Kind=nag_wp) arrayOutput
On exit: the sample observations, ${x}_{1},{x}_{2},\dots ,{x}_{n}$, sorted in ascending order.
9:     $\mathbf{ifail}$ – IntegerInput/Output
On entry: ifail must be set to $0$, . If you are unfamiliar with this argument you should refer to Section 3.4 in How to Use the NAG Library and its Documentation for details.
For environments where it might be inappropriate to halt program execution when an error is detected, the value  is recommended. If the output of error messages is undesirable, then the value $1$ is recommended. Otherwise, if you are not familiar with this argument, the recommended value is $0$. When the value  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).

## 6Error Indicators and Warnings

If on entry ${\mathbf{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:
${\mathbf{ifail}}=1$
On entry, ${\mathbf{n}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{n}}\ge 1$.
${\mathbf{ifail}}=2$
On entry, ${\mathbf{ntype}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{ntype}}=1$, $2$ or $3$.
${\mathbf{ifail}}=3$
On entry, at $x=〈\mathit{\text{value}}〉$, ${F}_{0}\left(x\right)=〈\mathit{\text{value}}〉$.
Constraint: $0.0\le {F}_{0}\left(x\right)\le 1$, where ${F}_{0}$ is supplied in cdf.
${\mathbf{ifail}}=4$
On entry, at $x=〈\mathit{\text{value}}〉$, ${F}_{0}\left(x\right)=〈\mathit{\text{value}}〉$ and at $y=〈\mathit{\text{value}}〉$, ${F}_{0}\left(y\right)=〈\mathit{\text{value}}〉$
Constraint: when $x, ${F}_{0}\left(x\right)\le {F}_{0}\left(y\right)$, where ${F}_{0}$ is supplied in cdf.
${\mathbf{ifail}}=-99$
See Section 3.9 in How to Use the NAG Library and its Documentation for further information.
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
See Section 3.8 in How to Use the NAG Library and its Documentation for further information.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.
See Section 3.7 in How to Use the NAG Library and its Documentation for further information.

## 7Accuracy

For most cases the approximation for $p$ given when $n>100$ has a relative error of less than $0.01$. The two-sided probability is approximated by doubling the one-sided probability. This is only good for small $p$, that is $p<0.10$, but very poor for large $p$. The error is always on the conservative side.

## 8Parallelism and Performance

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

The time taken by g08ccf increases with $n$ until $n>100$ at which point it drops and then increases slowly.
For a discrete theoretical cumulative distribution function ${F}_{0}\left(x\right)$, ${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\}$. Thus if you wish to provide a discrete distribution function the following adjustment needs to be made,
• for ${D}_{n}^{+}$, return $F\left(x\right)$ as $x$ as usual;
• for ${D}_{n}^{-}$, return $F\left(x-d\right)$ at $x$ where $d$ is the discrete jump in the distribution. For example $d=1$ for the Poisson or binomial distributions.

## 10Example

The following example performs the one sample Kolmogorov–Smirnov test to test whether a sample of $30$ observations arise firstly from a uniform distribution $U\left(0,1\right)$ or secondly from a Normal distribution with mean $0.75$ and standard deviation $0.5$. The two-sided test statistic, ${D}_{n}$, the standardized test statistic, $Z$, and the upper tail probability, $p$, are computed and then printed for each test.

### 10.1Program Text

Program Text (g08ccfe.f90)

### 10.2Program Data

Program Data (g08ccfe.d)

### 10.3Program Results

Program Results (g08ccfe.r)