PDF version (NAG web site
, 64-bit version, 64-bit version)
NAG Toolbox: nag_nonpar_test_ks_1sample_user (g08cc)
Purpose
nag_nonpar_test_ks_1sample_user (g08cc) performs the one sample Kolmogorov–Smirnov distribution test, using a user-specified distribution.
Syntax
Description
The data consists 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 (in
cdf) 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 hypothesis specified (see the description of the argument
ntype in
Arguments).
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 the alternative hypothesis
:
- – the largest positive deviation between the theoretical cumulative distribution function and the sample cumulative distribution function. Formally . This is only true for continuous distributions. See Further Comments for comments on discrete distributions.
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 continuity correction 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).
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:
– double array
-
The sample observations, .
- 2:
– function handle or string containing name of m-file
-
cdf must return the value of the theoretical (null) cumulative distribution function for a given value of its argument.
[result] = cdf(x)
Input Parameters
- 1:
– double scalar
-
The argument for which
cdf must be evaluated.
Output Parameters
- 1:
– double scalar
-
The value of the theoretical (null) cumulative distribution function evaluated at
x.
Constraint:
must always return a value in the range
and
cdf must always satify the condition that
for any
.
- 3:
– int64int32nag_int scalar
-
The 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 .
Optional Input Parameters
- 1:
– int64int32nag_int scalar
-
Default:
the dimension of the array
x.
, the number of observations in the sample.
Constraint:
.
Output Parameters
- 1:
– double scalar
-
The Kolmogorov–Smirnov test statistic (
,
or
according to the value of
ntype).
- 2:
– double scalar
-
A standardized value, , of the test statistic, , without the continuity correction applied.
- 3:
– double scalar
-
The probability,
, associated with the observed value of
, where
may
,
or
depending on the value of
ntype (see
Description).
- 4:
– double array
-
The sample observations, , sorted in ascending order.
- 5:
– int64int32nag_int scalar
unless the function detects an error (see
Error Indicators and Warnings).
Error Indicators and Warnings
Errors or warnings detected by the function:
-
-
-
-
On entry, | , or . |
-
-
The supplied theoretical cumulative distribution function returns a value less than or greater than , thereby violating the definition of the cumulative distribution function.
-
-
The supplied theoretical cumulative distribution function is not a nondecreasing function thereby violating the definition of a cumulative distribution function, that is for some .
-
An unexpected error has been triggered by this routine. Please
contact
NAG.
-
Your licence key may have expired or may not have been installed correctly.
-
Dynamic memory allocation failed.
Accuracy
For most cases the approximation for given when has a relative error of less than . The two-sided probability is approximated by doubling the one-sided probability. This is only good for small , that is , but very poor for large . The error is always on the conservative side.
Further Comments
The time taken by nag_nonpar_test_ks_1sample_user (g08cc) increases with until at which point it drops and then increases slowly.
For a discrete theoretical cumulative distribution function
,
. Thus if you wish to provide a discrete distribution function the following adjustment needs to be made,
- for , return as as usual;
- for , return at where is the discrete jump in the distribution. For example for the Poisson or binomial distributions.
Example
The following example performs the one sample Kolmogorov–Smirnov test to test whether a sample of observations arise firstly from a uniform distribution or secondly from a Normal distribution with mean and standard deviation . The two-sided test statistic, , the standardized test statistic, , and the upper tail probability, , are computed and then printed for each test.
Open in the MATLAB editor:
g08cc_example
function g08cc_example
fprintf('g08cc example results\n\n');
global xmean std;
xmean = 0.75;
std = 0.5;
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];
ntype = int64(1);
[d, z, p, sx, ifail] = g08cc( ...
x, @cdf, ntype);
fprintf('Test against normal distribution:\n');
fprintf(' mean = %7.2f\n', xmean);
fprintf(' standard deviation = %7.2f\n', std);
fprintf('\n\nTest statistic D = %8.4f\n', d);
fprintf('Z statistic = %8.4f\n', z);
fprintf('Tail probability = %8.4f\n', p);
function [result] = cdf(x)
global xmean std;
z = (x-xmean)/std;
[result,ifail] = s15ab(z);
g08cc example results
Test against normal distribution:
mean = 0.75
standard deviation = 0.50
Test statistic D = 0.1439
Z statistic = 0.7882
Tail probability = 0.5262
PDF version (NAG web site
, 64-bit version, 64-bit version)
© The Numerical Algorithms Group Ltd, Oxford, UK. 2009–2015