Kolmogorov-Smirnov Test

Testing whether a sample comes from a specified distribution, or whether two samples come from the same distribution, using the empirical CDF.

Diagram ยท Empirical CDF vs Theoretical
n =
00.250.50.751-3-2-10123D=0.195Empirical CDFTheoretical (N(0,1))x

The KS statistic D = 0.195 is the largest vertical gap between the empirical CDF (step function) and the theoretical CDF (smooth curve). Larger n โ†’ closer agreement.

Definition

The Kolmogorov-Smirnov (KS) test tests whether a sample comes from a specified distribution (one-sample) or whether two samples come from the same distribution (two-sample).

Empirical CDF: for data x1,โ€ฆ,xnx_1, \ldots, x_n, the empirical CDF is Fn(x)=1n#{i:xiโ‰คx}F_n(x) = \frac{1}{n}\#\{i: x_i \leq x\}.

One-sample test: compare Fn(x)F_n(x) to the hypothesized CDF F0(x)F_0(x): Dn=supโกxโˆฃFn(x)โˆ’F0(x)โˆฃD_n = \sup_x |F_n(x) - F_0(x)|

Two-sample test: compare empirical CDFs from two samples: Dn,m=supโกxโˆฃFn(x)โˆ’Gm(x)โˆฃD_{n,m} = \sup_x |F_n(x) - G_m(x)|

Large DD is evidence against H0H_0 (same distribution).

Testing normality

Data: {0.5,1.2,2.1,2.8,3.5}\{0.5, 1.2, 2.1, 2.8, 3.5\} (n=5n=5). Test H0H_0: standard normal.

The empirical CDF is a step function. We find the largest vertical gap between this step function and ฮฆ\Phi (the standard normal CDF). If D5=0.32D_5 = 0.32, the critical value at ฮฑ=0.05\alpha = 0.05 for n=5n=5 is 0.565. Since 0.32<0.5650.32 < 0.565, fail to reject normality.

Try it

Why should you generally not use the one-sample KS test to test normality when the mean and variance were estimated from the same data?

Solution

The KS test assumes the null distribution F0F_0 is completely specified in advance โ€” not estimated from the data. If you estimate ฮผ\mu and ฯƒ\sigma from the data, then fit F0=N(ฮผ^,ฯƒ^2)F_0 = \mathcal{N}(\hat{\mu}, \hat{\sigma}^2), the estimated distribution will always fit better than a fixed one would. The KS critical values are too conservative (too large), meaning you'll fail to reject normality too often (p-values are too large).

Use the Lilliefors test instead โ€” it's the KS test with corrected critical values for the case where parameters are estimated from the data. Or use the Shapiro-Wilk test, which is generally more powerful for testing normality.

Related concepts