scipy.special.kolmogorov#

scipy.special.kolmogorov(x, out=None) = <ufunc 'kolmogorov'>#

Complementary cumulative distribution function of the Kolmogorov distribution.

Returns the survival function of Kolmogorov’s limiting distribution, i.e., the distribution of \(\sqrt{n}\, D_n\) as \(n \to \infty\), where \(D_n\) is the Kolmogorov–Smirnov statistic:

\[D_n = \sup_x \left| F_n(x) - F(x) \right|\]

with \(F_n\) the empirical cumulative distribution function (ECDF) and \(F\) the theoretical CDF. Specifically, this function computes:

\[\mathbb{P}(\sqrt{n}\, D_n > x)\]
Parameters:
xarray_like

Absolute deviation between the Empirical CDF (ECDF) and the target CDF, multiplied by \(\sqrt{n}\).

outndarray, optional

Optional output array for the function results.

Returns:
scalar or ndarray

The probability that the test statistic exceeds x, in the range \([0, 1]\).

See also

kolmogi

Inverse survival function of the Kolmogorov distribution.

scipy.stats.kstwobign

Provides the functionality as a continuous distribution.

smirnov, smirnovi

Functions for the one-sided distribution.

Notes

kolmogorov is used by scipy.stats.kstest in the application of the Kolmogorov-Smirnov Goodness of Fit test. For historical reasons this function is exposed in scipy.special, but the recommended way to achieve the most accurate CDF/SF/PDF/PPF/ISF computations is to use the scipy.stats.kstwobign distribution.

References

[1]

Marsaglia, G., Tsang, W. W., & Wang, J. (2003). “Evaluating Kolmogorov’s distribution.” Journal of Statistical Software, 8(18), 1-4.

[2]

“Kolmogorov-Smirnov test”, Wikipedia, https://en.wikipedia.org/wiki/Kolmogorov%E2%80%93Smirnov_test

Examples

Show the probability of a gap at least as big as 0, 0.5 and 1.0.

>>> import numpy as np
>>> from scipy.special import kolmogorov
>>> from scipy.stats import kstwobign
>>> kolmogorov([0, 0.5, 1.0])
array([ 1.        ,  0.96394524,  0.26999967])

Compare a sample of size 1000 drawn from a Laplace(0, 1) distribution against the target distribution, a Normal(0, 1) distribution.

>>> from scipy.stats import norm, laplace
>>> rng = np.random.default_rng()
>>> n = 1000
>>> lap01 = laplace(0, 1)
>>> x = np.sort(lap01.rvs(n, random_state=rng))
>>> np.mean(x), np.std(x)
(np.float64(-0.05841730131499543), np.float64(1.3968109101997568))

Construct the Empirical CDF and the K-S statistic Dn.

>>> target = norm(0,1)  # Normal mean 0, stddev 1
>>> cdfs = target.cdf(x)
>>> ecdfs = np.arange(n+1, dtype=float)/n
>>> gaps = np.column_stack([cdfs - ecdfs[:n], ecdfs[1:] - cdfs])
>>> Dn = np.max(gaps)
>>> Kn = np.sqrt(n) * Dn
>>> print(f'Dn={Dn:f}, sqrt(n)*Dn={Kn:f}')
Dn=0.043363, sqrt(n)*Dn=1.371265
>>> print(chr(10).join(['For a Laplace sample tested against N(0, 1):',
...   f' the approximate Kolmogorov probability that sqrt(n)*Dn>={Kn:f} '
...   f'is {kolmogorov(Kn):f}',
...   f' the approximate Kolmogorov probability that sqrt(n)*Dn<={Kn:f} '
...   f'is {kstwobign.cdf(Kn):f}']))
For a Laplace sample tested against N(0, 1):
 the approximate Kolmogorov probability that sqrt(n)*Dn>=1.371265 is 0.046533
 the approximate Kolmogorov probability that sqrt(n)*Dn<=1.371265 is 0.953467

Plot the Empirical CDF against the target N(0, 1) CDF.

>>> import matplotlib.pyplot as plt
>>> plt.step(np.concatenate([[-3], x]), ecdfs, where='post', label='Empirical CDF')
>>> x3 = np.linspace(-3, 3, 100)
>>> plt.plot(x3, target.cdf(x3), label='CDF for N(0, 1)')
>>> plt.ylim([0, 1]); plt.grid(True); plt.legend();
>>> # Add vertical lines marking Dn+ and Dn-
>>> iminus, iplus = np.argmax(gaps, axis=0)
>>> plt.vlines([x[iminus]], ecdfs[iminus], cdfs[iminus],
...            color='r', linestyle='dashed', lw=4)
>>> plt.vlines([x[iplus]], cdfs[iplus], ecdfs[iplus+1],
...            color='r', linestyle='dashed', lw=4)
>>> plt.show()
../../_images/scipy-special-kolmogorov-1.png