Sample size

Sample size calculator for the Kolmogorov–Smirnov test

Calculate the number of observations needed to detect a deviation d from the theoretical distribution with a target power, using an approximation based on the asymptotic Kolmogorov distribution.

Calculate the number of observations needed for the Kolmogorov–Smirnov test to detect, with the desired power, a maximum deviation \(d\) from a fully specified theoretical distribution.

Parameters

Enter the maximum CDF deviation (d), the significance level and the desired power to obtain the minimum sample size.

Result pending…

Power curve by sample size

Check the parameters: the chart will appear once they are valid.

Explanation

The one-sample Kolmogorov–Smirnov test checks whether the data come from a fully specified theoretical distribution \(F_0\). The test statistic is the maximum absolute deviation between the empirical distribution function \(\hat{F}_n\) and the theoretical one:

\( D_n = \sup_x \left| \hat{F}_n(x) - F_0(x) \right| \)

The effect size is \(d = \max_x |F_{\text{true}}(x) - F_0(x)|\), that is, the maximum vertical distance between the real CDF and the hypothesized one. Under \(H_0\), the asymptotic distribution of \(\sqrt{n}\,D_n\) is the Kolmogorov distribution.

The actual power depends on the full shape of the alternative, not just on \(d\). That is why the calculator uses a lower bound (Massey, 1951): it computes the size that guarantees at least the requested power for any distribution that is \(d\) away from the theoretical one. It is a sufficient size: for many specific alternatives less data will be enough (see the note in the example).

Power formula

The CDF of the Kolmogorov distribution is:

\( P(K \leq x) = 1 - 2\sum_{k=1}^{\infty} (-1)^{k+1} e^{-2k^2 x^2}, \quad x > 0 \)

The critical quantile \(k_\alpha\) is defined by \(P(K > k_\alpha) = \alpha\), that is, \(1 - P(K \leq k_\alpha) = \alpha\). It is found by binary search.

If the true distribution function is \(d\) away from the theoretical one at some point, the empirical one at that point departs from the theoretical by \(d\) plus a sampling error with standard deviation at most \(1/(2\sqrt{n})\). Since \(D_n\) is at least that departure, the power satisfies:

\( \text{Power}(n) \;\geq\; \Phi\!\left(2\,(d\sqrt{n} - k_\alpha)\right) \)

where \(\Phi\) is the standard normal distribution function. Setting the bound equal to the target power \(1-\beta\) gives the sample size:

\( n = \left\lceil \left( \dfrac{k_\alpha + z_{1-\beta}/2}{d} \right)^{2} \right\rceil \)

With \(\alpha = 0.05\) and 80% power, \(k_\alpha \approx 1.358\) and \(z_{0.80} \approx 0.842\), so \(n \approx (1.779/d)^2\).

Quick setup

  • d = 0.10: small effect. The real distribution deviates from the theoretical one by at most 0.10 in the CDF. Requires large samples.
  • d = 0.20: medium effect. Deviation clearly visible on a P-P or Q-Q plot. Moderate sample size.
  • d = 0.30: large effect. Substantial difference between distributions; detectable with relatively small samples.
  • α: 0.05 is the usual standard; use 0.01 if the cost of a false positive is high.
  • Power: 0.80 is the usual minimum; 0.90 if false negatives have important consequences.
  • Fully specified distribution: if you estimate parameters from the data (mean, variance…), the distribution of the statistic changes and the nominal level is no longer valid; in that case use the Lilliefors test.

Worked example

A process engineer wants to test whether the failure times of a component follow an exponential distribution with rate \(\lambda_0 = 0.1\) (fully specified). Based on historical data, they believe the real CDF deviates by at most \(d = 0.15\) from the theoretical exponential.

With \(d = 0.15\), \(\alpha = 0.05\) and 80% power, the calculator finds:

  1. The critical quantile \(k_{0.05} \approx 1.358\) via binary search on the Kolmogorov distribution.
  2. The size \(n = \lceil ((1.358 + 0.842/2)/0.15)^2 \rceil = \lceil 11.86^2 \rceil = \lceil 140.7 \rceil\).

The result is n = 141 observations. With that sample, if the real CDF deviates by 0.15 from the theoretical exponential, the test will detect it at least 80% of the time, whatever the shape of the deviation.

Note on the bound: the size is conservative. It corresponds to the least favourable case, in which the deviation is concentrated at a point near the median. With more usual alternatives, the actual power is higher: in simulations against a shift in the mean with \(d = 0.15\), power with \(n = 141\) was 97%, and 80% was reached with about 75–80 observations. If the sample size is critical or costly, refine it with a Monte Carlo simulation for the specific alternative you expect.

Sensitivity analysis: with \(d = 0.10\) (small effect) 317 observations are needed; with \(d = 0.20\), 80; and with \(d = 0.30\) (large effect), 36. The required sample grows as \(1/d^2\): detecting a deviation half as large takes four times as much data.

Model assumptions

  • Random sample of a continuous variable, with independent and identically distributed observations.
  • The reference theoretical distribution is fully specified (its parameters are not estimated from the data).
  • The effect size is the maximum deviation \(d\) between the theoretical distribution and the alternative.
  • The critical value uses the asymptotic Kolmogorov distribution and power uses Massey's lower bound with a normal approximation; the exact distribution of \(D_n\) for finite \(n\) is not used.

How to interpret the result

The value \(n\) is the number of observations with which, according to Massey's bound, the Kolmogorov-Smirnov (KS) test reaches at least the indicated power at level \(\alpha\) against any deviation of the specified size. It is a sufficient, conservative size: against many specific alternatives less data will be enough. Always round up. If you anticipate data loss or exclusions, divide \(n\) by \((1 - \text{dropout rate})\) to obtain the number of units to collect. A critical assumption is that the theoretical distribution under \(H_0\) must be fully specified (all its parameters known a priori): if instead the parameters are estimated from the data themselves (as in the Lilliefors test for normality), the standard KS critical tables are not valid and the p-value will be inflated.

The power of the KS test depends on the maximum distance \(D = \sup_x |F_n(x) - F_0(x)|\) you want to detect. The KS test is relatively powerful for detecting differences in the central region of the distribution, but has low power for differences in the tails. If the deviations you care about are mainly in the extremes (heavy tails, skewness), the Anderson-Darling test or the Cramér-von Mises test are more powerful and require a smaller \(n\) for the same effectiveness. Perform a sensitivity analysis by varying the minimum detectable distance \(D\) by ±0.05 to see how much \(n\) changes.

Development and additional details

If the computed \(n\) exceeds a few hundred, keep in mind that with very large samples the KS test will detect trivial deviations from the theoretical distribution that have no practical relevance: a \(p < 0.05\) does not imply the distribution is "too different" for the purposes of the analysis. In that context, complement the test with Q-Q plots and assess whether the deviations are substantive. Once you have collected the data, run the test with the Kolmogorov-Smirnov calculator; if the goal is specifically to test normality, use the Shapiro-Wilk calculator for small or moderate samples.

References

  • Kolmogorov, A. N. (1933). Sulla determinazione empirica di una legge di distribuzione. Giornale dell'Istituto Italiano degli Attuari, 4, 83–91.
  • Smirnov, N. V. (1948). Table for estimating the goodness of fit of empirical distributions. The Annals of Mathematical Statistics, 19(2), 279–281.
  • Conover, W. J. (1999). Practical Nonparametric Statistics (3rd ed.). Wiley.
  • Massey, F. J. (1951). The Kolmogorov-Smirnov test for goodness of fit. Journal of the American Statistical Association, 46(253), 68–78.