Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

SciPy · Function

stats.kstest

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.

How it works

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.

Syntax

scipy.stats.kstest(rvs, cdf, args=(), N=20, alternative='two-sided', method='auto', *, axis=0, nan_policy='propagate', keepdims=False)

Parameters

NameTypeDescription
rvsarray_like, str, or callableSample data, a distribution name, or a callable generating random variables, depending on the selected form.
cdfstr, array_like, or callableReference distribution name, callable CDF, or the second sample in the two-sample form.
argstuple or sequence, optionalDistribution parameters used when rvs or cdf is a string.
Nint, optionalSample size when rvs is a string or callable; default is 20.
alternative{‘two-sided’, ‘less’, ‘greater’}, optionalAlternative hypothesis: two-sided, less, or greater; default is two-sided.
method{‘auto’, ‘exact’, ‘approx’, ‘asymp’}, optionalMethod used to calculate the p-value; default is auto.
axisint or None, optionalAxis along which to perform the test; default is 0.
nan_policy{‘propagate’, ‘omit’, ‘raise’}, optionalHow to handle NaN values; default is propagate.
keepdimsbool, optionalWhether reduced axes are retained as singleton dimensions; default is False.

Returns

KstestResult containing statistic (the KS test statistic, D, D+, or D−) and pvalue (a one-tailed or two-tailed p-value).

Key points

  • Use it for either one-sample goodness-of-fit testing or two-sample distribution comparison.
  • In one-sample mode, provide observations and a named distribution or CDF representing the reference model.
  • Two-sample use requires independent samples and tests whether their continuous distributions agree.
  • The default checks for any difference; less and greater encode directional alternatives.
  • A string distribution argument refers to a distribution available in scipy.stats.
  • Pass distribution shape or location parameters through args when using string specifications.
  • The result includes a p-value whose sidedness follows the selected alternative.

Common mistakes

  • Do not apply the test directly to discrete distributions; the documented validity assumption is continuity.
  • An array input for a sample must be one-dimensional.
  • When supplying a callable as cdf, it must evaluate the hypothesized cumulative distribution function rather than generate observations.

Example: One-sample goodness-of-fit test

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.

When to use it

One-sample goodness of fit

Validate whether observed continuous measurements are consistent with a named SciPy distribution.

Two-sample comparison

Compare two independent continuous datasets for evidence of different underlying distributions.

Custom CDF validation

Test a custom continuous model by passing its CDF as a callable.

Interview questions

QWhat does 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.

QHow does 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.

QWhat does the returned result contain?

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_1samp
  • scipy.stats.ks_2samp
  • scipy.stats.kstest

Source: scipy.stats.kstest — SciPy v1.18.0 Manual. Checked 2026-10-15.