One-sample goodness of fit
Validate whether observed continuous measurements are consistent with a named SciPy distribution.
SciPy · Function
Performs a one-sample or two-sample Kolmogorov–Smirnov test. It compares a sample with a continuous reference distribution or compares two independent continuous samples.
kstest compares one sample with a specified continuous distribution, or compares two independent samples when both inputs represent samples. It returns a result containing the KS statistic and p-value; the selected alternative determines the hypothesis direction, while method controls p-value calculation.
scipy.stats.kstest(rvs, cdf, args=(), N=20, alternative='two-sided', method='auto', *, axis=0, nan_policy='propagate', keepdims=False)| Name | Type | Description |
|---|---|---|
rvs | array_like, str, or callable | Sample data, a distribution name, or a callable generating random variables, depending on the selected form. |
cdf | str, array_like, or callable | Reference distribution name, callable CDF, or the second sample in the two-sample form. |
args | tuple or sequence, optional | Distribution parameters used when rvs or cdf is a string. |
N | int, optional | Sample size when rvs is a string or callable; default is 20. |
alternative | {‘two-sided’, ‘less’, ‘greater’}, optional | Alternative hypothesis: two-sided, less, or greater; default is two-sided. |
method | {‘auto’, ‘exact’, ‘approx’, ‘asymp’}, optional | Method used to calculate the p-value; default is auto. |
axis | int or None, optional | Axis along which to perform the test; default is 0. |
nan_policy | {‘propagate’, ‘omit’, ‘raise’}, optional | How to handle NaN values; default is propagate. |
keepdims | bool, optional | Whether reduced axes are retained as singleton dimensions; default is False. |
KstestResult containing statistic (the KS test statistic, D, D+, or D−) and pvalue (a one-tailed or two-tailed p-value).
less and greater encode directional alternatives.scipy.stats.args when using string specifications.cdf, it must evaluate the hypothesized cumulative distribution function rather than generate observations.from scipy.stats import kstest
sample = [0.12, 0.31, 0.47, 0.66, 0.89]
result = kstest(sample, "uniform")
print(result.statistic, result.pvalue)Tests whether the sample is consistent with a continuous uniform distribution.
Validate whether observed continuous measurements are consistent with a named SciPy distribution.
Compare two independent continuous datasets for evidence of different underlying distributions.
Test a custom continuous model by passing its CDF as a callable.
kstest test?The one-sample form compares a sample's underlying distribution with a specified distribution, while the two-sample form compares two independent samples. In both cases, the distributions must be continuous.
Follow-up: What does the null hypothesis assert? It asserts that the compared distributions are identical.
alternative affect the test?alternative selects two-sided, less, or greater. The default is two-sided; one-sided alternatives compare the empirical CDF against the hypothesized CDF in the corresponding direction.
Follow-up: How would you choose a directional alternative? Use it only when the direction was specified before examining the data.
The function returns a KstestResult with the KS statistic and p-value. The statistic is a KS distance, represented as D, D+, or D− depending on the test and alternative.
Follow-up: What does method control? It selects the calculation method used for the p-value, such as the exact, approximate, asymptotic, or automatic strategy documented for the installed SciPy version.
scipy.stats.ks_1sampscipy.stats.ks_2sampscipy.stats.kstest