hide long namesshow long names
hide short namesshow short names
Integer type:  int32  int64  nag_int  show int32  show int32  show int64  show int64  show nag_int  show nag_int

PDF version (NAG web site, 64-bit version, 64-bit version)
Chapter Contents
Chapter Introduction
NAG Toolbox

NAG Toolbox: nag_nonpar_test_ks_1sample (g08cb)

 Contents

    1  Purpose
    2  Syntax
    7  Accuracy
    9  Example

Purpose

nag_nonpar_test_ks_1sample (g08cb) performs the one sample Kolmogorov–Smirnov test, using one of the distributions provided.

Syntax

[par, d, z, p, sx, ifail] = g08cb(x, dist, par, estima, ntype, 'n', n)
[par, d, z, p, sx, ifail] = nag_nonpar_test_ks_1sample(x, dist, par, estima, ntype, 'n', n)

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 argument ntype in Arguments).
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).

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

Parameters

Compulsory Input Parameters

1:     xn – double array
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 arguments dist and par. For further details see Further Comments.
2:     dist – string
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 argument, 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'.
3:     par2 – double array
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.
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 nag_machine_precision (x02aj);
  • 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 nag_machine_precision (x02aj);
  • if dist='GP', par2>0.
4:     estima – string (length ≥ 1)
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'.
5:     ntype int64int32nag_int scalar
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.

Optional Input Parameters

1:     n int64int32nag_int scalar
Default: the dimension of the array x.
n, the number of observations in the sample.
Constraint: n3 .

Output Parameters

1:     par2 – double array
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.
2:     d – double scalar
The Kolmogorov–Smirnov test statistic ( D n , D n + or D n - according to the value of ntype).
3:     z – double scalar
A standardized value, Z , of the test statistic, D , without any correction for continuity.
4:     p – double scalar
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 Description).
5:     sxn – double array
The sample observations, x1,x2,,xn, sorted in ascending order.
6:     ifail int64int32nag_int scalar
ifail=0 unless the function detects an error (see Error Indicators and Warnings).

Error Indicators and Warnings

Errors or warnings detected by the function:
   ifail=1
Constraint: n3.
   ifail=2
On entry, dist=_ was an illegal value.
   ifail=3
Constraint: ntype=1, 2 or 3.
   ifail=4
On entry, estima=_ was an illegal value.
   ifail=5
Constraint: for the beta distribution, 0<par1 and par21000000.
Constraint: for the binomial distribution, 0<par2<1.
Constraint: for the binomial distribution, 1par1<1/eps, where eps=machine precision, see nag_machine_precision (x02aj).
Constraint: for the exponential distribution, par1>0.
Constraint: for the gamma distribution, par1 and par2>0.
Constraint: for the generalized Pareto distribution, par2>0.
Constraint: for the generalized Pareto distribution with par1<0, 0xi-par2/par1, for i=1,2,,n.
Constraint: for the negative binomial distribution, 0<par2<1.
Constraint: for the negative binomial distribution, 1par1<1/eps, where eps=machine precision, see nag_machine_precision (x02aj).
Constraint: for the Normal distribution, par2>0.
Constraint: for the Poisson distribution, 0<par1<1000000.
Constraint: for the uniform distribution, par1<par2.
   ifail=6
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='G', 'E', 'P', 'NB' or 'GP' and at least one observation is negative.
Constraint: xi0, 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.
On entry, dist='U' and at least one observation is illegal.
Constraint: par1xipar2, for i=1,2,,n.
   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=_, .
The variance par1×par2×1-par2 exceeds 1000000.
On entry, dist='NB', par1=_, .
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 nag_specfun_gamma_incomplete (s14ba) 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.
   ifail=-399
Your licence key may have expired or may not have been installed correctly.
   ifail=-999
Dynamic memory allocation failed.

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.

Further Comments

The time taken by nag_nonpar_test_ks_1sample (g08cb) 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 argument x must be consistent with the chosen null distribution as follows:

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.
function g08cb_example


fprintf('g08cb example results\n\n');

x = [0.01; 0.30; 0.20; 0.90; 1.20; 0.09; 1.30; 0.18; 0.90; 0.48;
     1.98; 0.03; 0.50; 0.07; 0.70; 0.60; 0.95; 1.00; 0.31; 1.45;
     1.04; 1.25; 0.15; 0.75; 0.85; 0.22; 1.56; 0.81; 0.57; 0.55];

% Parameters
dist   = 'Normal';
estima = 'Estimate';
ntype  = int64(1);
par = [0; 0];

[par, d, z, p, sx, ifail] = g08cb( ...
                                   x, dist, par, estima, ntype);

fprintf('K-S Test\n');
fprintf('Distribution: %s\n',dist);
fprintf('Parameters  :');
fprintf('%7.4f', par);
fprintf('\n\nTest statistic D = %8.4f\n', d);
fprintf('Z statistic      = %8.4f\n', z);
fprintf('Tail probability = %8.4f\n', p);


g08cb example results

K-S Test
Distribution: Normal
Parameters  : 0.6967 0.2564

Test statistic D =   0.1108
Z statistic      =   0.6068
Tail probability =   0.8925

PDF version (NAG web site, 64-bit version, 64-bit version)
Chapter Contents
Chapter Introduction
NAG Toolbox

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