scipy.special.kolmogorov#
- scipy.special.kolmogorov(x, out=None) = <wrapped_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
kolmogiInverse survival function of the Kolmogorov distribution.
scipy.stats.kstwobignProvides the functionality as a continuous distribution.
smirnov,smirnoviFunctions for the one-sided distribution.
Notes
kolmogorovis used byscipy.stats.kstestin the application of the Kolmogorov-Smirnov Goodness of Fit test. For historical reasons this function is exposed inscipy.special, but the recommended way to achieve the most accurate CDF/SF/PDF/PPF/ISF computations is to use thescipy.stats.kstwobigndistribution.Array API Standard Support
kolmogorovhas support for Python Array API Standard compatible backends in addition to NumPy. The following combinations of backend and device (or other capability) are supported.Library
CPU
GPU
NumPy
✅
n/a
CuPy
n/a
⛔
PyTorch
✅
⛔
JAX
✅
⛔
Dask
✅
n/a
For the NumPy backend, this function supports all NumPy ufunc keyword arguments. Other backends may support
out, but typically none of the other ufunc kwargs.outis typically supported for CuPy and PyTorch, but not currently in cases where SciPy relies on a generic Array API implementation or, for PyTorch on CPU, falls back to the NumPy backend.outis never supported for JAX because JAX arrays are immutable.kolmogorovdoes not currently supportoutfor the PyTorch backend.See Support for the array API standard for more information.
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()