NAG Library Function Document
nag_1_sample_ks_test (g08cbc)
1 Purpose
nag_1_sample_ks_test (g08cbc) performs the one sample Kolmogorov–Smirnov test, using one of the standard distributions provided.
2 Specification
#include <nag.h> |
#include <nagg08.h> |
void |
nag_1_sample_ks_test (Integer n,
const double x[],
Nag_Distributions dist,
double par[],
Nag_ParaEstimates estima,
Nag_TestStatistics ntype,
double *d,
double *z,
double *p,
NagError *fail) |
|
3 Description
The data consist of a single sample of observations denoted by . Let and represent the sample cumulative distribution function and the theoretical (null) cumulative distribution function respectively at the point where is the th smallest sample observation.
The Kolmogorov–Smirnov test provides a test of the null hypothesis
: the data are a random sample of observations from a theoretical distribution specified by you against one of the following alternative hypotheses:
(i) |
: the data cannot be considered to be a random sample from the specified null distribution. |
(ii) |
: 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 tended to exceed the corresponding values of the theoretical cumulative distribution function . |
(iii) |
: 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 tended to exceed the corresponding values of the sample cumulative distribution function . |
One of the following test statistics is computed depending on the particular alternative null hypothesis specified (see the description of the argument
ntype in
Section 5).
For the alternative hypothesis
.
- – the largest absolute deviation between the sample cumulative distribution function and the theoretical cumulative distribution function. Formally .
For the alternative hypothesis
.
- – the largest positive deviation between the sample cumulative distribution function and the theoretical cumulative distribution function. Formally for both discrete and continuous null distributions.
For the alternative hypothesis
.
- – the largest positive deviation between the theoretical cumulative distribution function and the sample cumulative distribution function. Formally if the null distribution is discrete then and if the null distribution is continuous then .
The standardized statistic
is also computed where
may be
or
depending on the choice of the alternative hypothesis. This is the standardized value of
with no correction for continuity applied and the distribution of
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
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
, that is
, but it becomes very poor for larger
. If
then
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 Arguments
- 1:
n – IntegerInput
On entry: , the number of observations in the sample.
Constraint:
.
- 2:
x[n] – const doubleInput
On entry: the sample observations .
Constraint:
the sample observations supplied must be consistent, in the usual manner, with the null distribution chosen, as specified by the arguments
dist and
par. For further details see
Section 9.
- 3:
dist – Nag_DistributionsInput
On entry: the theoretical (null) distribution from which it is suspected the data may arise.
- The uniform distribution over .
- The Normal distribution with mean and variance .
- The gamma distribution with shape parameter and scale parameter , where the mean .
- The beta distribution with shape parameters and , where the mean .
- The binomial distribution with the number of trials, , and the probability of a success, .
- The exponential distribution with parameter , where the mean .
- The Poisson distribution with parameter , where the mean .
- The negative binomial distribution with the number of trials, , and the probability of success, .
- The generalized Pareto distribution with shape parameter and scale .
Constraint:
, , , , , , , or .
- 4:
par[] – doubleInput/Output
-
On entry: if
,
par must contain the known values of the parameter(s) of the null distribution as follows.
If a uniform distribution is used, then and must contain the boundaries and respectively.
If a Normal distribution is used, then and must contain the mean, , and the variance, , respectively.
If a gamma distribution is used, then and must contain the parameters and respectively.
If a beta distribution is used, then and must contain the parameters and respectively.
If a binomial distribution is used, then and must contain the parameters and respectively.
If an exponential distribution is used, then must contain the parameter .
If a Poisson distribution is used, then must contain the parameter .
If a negative binomial distribution is used, and must contain the parameters and respectively.
If a generalized Pareto distribution is used, and must contain the parameters and respectively.
If
,
par need not be set except when the null distribution requested is either the binomial or the negative binomial distribution in which case
must contain the parameter
.
On exit: if
,
par is unchanged. If
, then
and
are set to values as estimated from the data.
Constraints:
- if , ;
- if , ;
- if , and ;
- if , and and and ;
- if , and and and , where , see nag_machine_precision (X02AJC);
- if , ;
- if , and ;
- if , and and and , where , see nag_machine_precision (X02AJC);
- if , .
- 5:
estima – Nag_ParaEstimatesInput
-
On entry:
estima must specify whether values of the parameters of the null distribution are known or are to be estimated from the data.
- Values of the parameters will be supplied in the array par described above.
- Parameters are to be estimated from the data except when the null distribution requested is the binomial or the negative binomial distribution in which case the first parameter, , must be supplied in and only the second parameter, is estimated from the data.
Constraint:
or .
- 6:
ntype – Nag_TestStatisticsInput
On entry: the test statistic to be calculated, i.e., the choice of alternative hypothesis.
- Computes , to test against ,
- Computes , to test against ,
- Computes , to test against .
Constraint:
, or .
- 7:
d – double *Output
On exit: the Kolmogorov–Smirnov test statistic (
,
or
according to the value of
ntype).
- 8:
z – double *Output
On exit: a standardized value, , of the test statistic, , without any correction for continuity.
- 9:
p – double *Output
On exit: the probability,
, associated with the observed value of
where
may be
or
depending on the value of
ntype (see
Section 3).
- 10:
fail – NagError *Input/Output
-
The NAG error argument (see
Section 3.6 in the Essential Introduction).
6 Error Indicators and Warnings
- NE_ALLOC_FAIL
-
Dynamic memory allocation failed.
- NE_BAD_PARAM
-
On entry,
dist had an illegal value.
On entry,
estima had an illegal value.
On entry,
ntype had an illegal value.
- NE_G08CB_DATA
-
The data supplied in
x could not arise from the chosen null distribution, as specified by the arguments
dist and
par.
- NE_G08CB_INCOMP_GAMMA
-
On entry,
, and in the computation of the incomplete gamma function by
nag_incomplete_gamma (s14bac) the convergence of the Taylor series or Legendre continued fraction fails within
iterations.
- NE_G08CB_PARAM
-
On entry, the parameters supplied for the specified null distribution are out of range. This error will only occur if .
- NE_G08CB_SAMPLE
-
On entry, , , , or , and the whole sample is constant. Thus the variance is zero.
- NE_G08CB_VARIANCE
-
The variance of the binomial distribution exceeds . and .
The variance of the data
x is too small for the generalized Pareto distribution (
).
The variance of the negative binomial distribution () is too large. That is .
- NE_INT_ARG_LT
-
On entry, .
Constraint: .
- NE_INTERNAL_ERROR
-
An internal error has occurred in this function. Check the function call and any array sizes. If the call is correct then please contact
NAG for assistance.
7 Accuracy
The approximation for , given when , 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 , i.e., but very poor for large . The error is always on the conservative side, that is the tail probability, , is over estimated.
8 Parallelism and Performance
Not applicable.
The time taken by nag_1_sample_ks_test (g08cbc) increases with until at which point it drops and then increases slowly with . 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 argument
x must be consistent with the chosen null distribution as follows:
- when , then , for ;
- when , then there are no constraints on the 's;
- when , then , for ;
- when , then , for ;
- when , then , for ;
- when , then , for ;
- when , then , for ;
- when , then , for ;
- when and , then , for ;
- when and , then , for .
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,
, 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
; 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 (g08cbce.c)
10.2 Program Data
Program Data (g08cbce.d)
10.3 Program Results
Program Results (g08cbce.r)