cover

Goodness of fit tests - Kolmogorov-Smirnov, Cramer-von Mises, Anderson-Darling

October 01, 2026 36 min read

Previously we've already considered Chi-sqared goodness-of-fit (GoF) test in great detail. This post is dedicated to a different family of GoF tests, stemming from the same ideas of analysis of Brownian bridge, Karhunen-Loeve decomposition of infinite-dimentional covariance matrix of a stochastic process etc.

Goodness of Fit (GoF) problem

Goodness of fit problem arises when we need to check, if an empirically observed data follow some theoretically known distribution. E.g. we hypothesized that the distribution on the cover of this post is exponential. How to test, if this hypothesis holds?

Previously we’ve already considered derivation of Pearson’s Chi-squared goodness-of-fit test post.

Today we’ll look into a family of its alternatives, based on comparison of empirical distribution function with theoretical one. Then those tests would collect different statistics, all measuring some kind of divergence between theoretical and empirical distributions and will try to say that it is totally improbable to observe such an empirical distribution, if we assumed that indeed it was sampled from theoretical one.

Toolchain

All 3 tests, Kolmogorov-Smirnov, Cramer-von Mises, Anderson-Darling, rely upon the same set of mathematical objects and results. Hence, in order to understand the derivation of those tests and intuition behind them, I’ll first describe those objects and how they pertain to the GoF problem and then will consider specifics of each test in the final steps of their derivation, where they somewhat diverge (hopefully, this will result in intuitions on their relative advantages and disadvantages).

Empirical cumulative distribution function vs true distribution function

Let X1,,XnX_1,\dots,X_n be i.i.d. with unknown CDF F(x)=P(Xx)F(x)=\mathbb{P}(X\le x).

The empirical CDF is built out of samples from that distribution as the fraction of observations that have landed to the left of xx,

Fn(x)=1ni=1n1{Xix}F_n(x) = \frac{1}{n} \sum \limits_{i=1}^{n} \mathbf{1}_{\{X_i \le x\}}.

The left subplot of the figure below is that for a standard Gaussian FF and n=24n=24 samples; the blue curve is theoretical CDF FF, the red ladder is empirical CDF FnF_n.

empirical CDF

Empirical CDF FnF_n versus true Gaussian FF. Left: one staircase (n=24n=24). Right: the random height of that staircase at a fixed xx^\ast.

Now freeze a single abscissa xx^\ast (the dotted vertical line). The height Fn(x)F_n(x^\ast) no longer looks like a function — it is just a number K/nK/n, where K=#{i:Xix}K=\#\{i: X_i\le x^\ast\} counts how many green ticks sit to the left of xx^\ast. In the figure, K=15K=15 out of 2424, so Fn(x)=15/24=0.625F_n(x^\ast)=15/24=0.625, while the true height is F(x)=0.655F(x^\ast)=0.655.

Under the null hypothesis each XiX_i independently falls on the left of xx^\ast with probability F(x)F(x^\ast), so KK is a coin-flip count. That is why the histogram on the right is binomial, and why we next study FnF_n pointwise before treating it as a process.

Binomial distribution of sample at each point

Now take a look at the right subplot of the figure above.

Fix a point xx on the real line and look at a single number: the height of the empirical CDF there, Fn(x)=1ni=1n1{Xix}F_n(x) = \frac{1}{n}\sum_{i=1}^{n} \mathbf{1}_{\{X_i \le x\}}. Under the null, X1,,XnX_1,\dots,X_n are i.i.d. from the hypothesized FF, so each indicator is a coin flip: P(Xix)=F(x)\mathbb{P}(X_i \le x) = F(x). Hence 1{Xix}Bernoulli(F(x))\mathbf{1}_{\{X_i \le x\}} \sim \mathrm{Bernoulli}(F(x)), independent across ii. Their sum is binomial:

nFn(x)Binomial(n,F(x))n F_n(x) \sim \mathrm{Binomial}\bigl(n,\, F(x)\bigr).

That is the whole story: “is the sample x\le x?” is a yes/no trial with success probability F(x)F(x), and we run nn independent trials. Immediately,

E[Fn(x)]=F(x),Var[Fn(x)]=F(x)(1F(x))n\mathbb{E}[F_n(x)] = F(x),\qquad \mathrm{Var}[F_n(x)] = \frac{F(x)\bigl(1-F(x)\bigr)}{n}.

The variance is largest at the median (F(x)=12F(x)=\tfrac12) and vanishes in the tails — the empirical CDF is noisier in the middle than at the edges.

If the curve at the right reminds you a normal distribution, well, that’s because binomial distribution converges to normal in central part as nn \to \infty: after centering and n\sqrt{n}-scaling this binomial becomes approximately N(0,F(x)(1F(x)))\mathcal{N}\bigl(0,\, F(x)(1-F(x))\bigr), which is exactly the pointwise marginal of the so-called Brownian bridge we will meet below.

Note however that this is a pointwise statement: the counts at two different xx and yy are dependent (the same sample is reused), so the process xFn(x)x \mapsto F_n(x) is not a collection of independent binomials.

Quantile transform

The binomial picture above still depends on FF: the success probability at xx is F(x)F(x). Kolmogorov–Smirnov, Cramér–von Mises and Anderson–Darling get rid of that dependence with the quantile transform. If FF is continuous and XFX\sim F, then

U:=F(X)Uniform[0,1]U := F(X) \sim \mathrm{Uniform}[0,1].

Proof is one line: P(F(X)u)=P(XF1(u))=F(F1(u))=u\mathbb{P}(F(X) \le u) = \mathbb{P}(X \le F^{-1}(u)) = F(F^{-1}(u)) = u for u[0,1]u\in[0,1] (for a general continuous CDF take the generalized inverse F1(u)=inf{x:F(x)u}F^{-1}(u)=\inf\{x: F(x)\ge u\}).

So under the null, the mapped sample Ui=F(Xi)U_i = F(X_i) is i.i.d. uniform on [0,1][0,1]. Write its empirical CDF as

Gn(u)=1ni=1n1{Uiu},u[0,1]G_n(u) = \frac{1}{n} \sum \limits_{i=1}^{n} \mathbf{1}_{\{U_i \le u\}},\qquad u\in[0,1].

The original ECDF is just this process reparametrized by the hypothesized CDF: Fn(x)=Gn(F(x))F_n(x) = G_n(F(x)). Comparing FnF_n to FF is therefore the same as comparing GnG_n to the identity map uuu\mapsto u. In particular, from the previous section,

nGn(u)Binomial(n,u),E[Gn(u)]=u,Var[Gn(u)]=u(1u)nn G_n(u) \sim \mathrm{Binomial}(n,\, u),\qquad \mathbb{E}[G_n(u)]=u,\qquad \mathrm{Var}[G_n(u)]=\frac{u(1-u)}{n}.

The whole GoF problem now lives on the unit interval, with a distribution-free null: no leftover FF. That is why the same critical values work for every continuous hypothesized law, and why the limiting object below is a Brownian bridge on [0,1][0,1] rather than some FF-dependent process on R\mathbb{R}.

If the null is false, F(X)F(X) is not uniform, GnG_n drifts away from the diagonal, and that is what the tests detect. (If FF is estimated from the same sample, the UiU_i are only approximately uniform — that is a different, composite, story.)

Brownian bridge

Pointwise, n(Gn(u)u)\sqrt{n}(G_n(u)-u) is a centered binomial, hence asymptotically N(0,u(1u))\mathcal{N}(0,\,u(1-u)).

GoF tests, however, are not questions about one uu: Kolmogorov–Smirnov looks at supuGn(u)u\sup_u |G_n(u)-u|, Cramér–von Mises at (Gnu)2\int (G_n-u)^2, Anderson–Darling at a weighted version. Those are functionals of the whole path. Two hard constraints come with it: Gn(0)=0G_n(0)=0 and Gn(1)=1G_n(1)=1 always, so the centered process is pinned at both ends of [0,1][0,1]. A Wiener process starts at 00 but is free at t=1t=1. The Gaussian process that is Wiener-like and tied down at both endpoints is the Brownian bridge; Donsker’s theorem (next) says the empirical process converges to it.

Intuition: take a Wiener process WtW_t (W0=0W_0=0, independent Gaussian increments, Var(Wt)=t\mathrm{Var}(W_t)=t). It typically ends at some W10W_1\neq 0. Draw the chord Lt=tW1L_t = t W_1 from (0,0)(0,0) to (1,W1)(1,W_1) and subtract it,

Bt=WttW1B_t = W_t - t W_1.

Then B0=B1=0B_0=B_1=0: the path is a “bridge” between the two banks of the interval. Equivalently, BB is Brownian motion conditioned on W1=0W_1=0. That is the same geometry as the empirical process after the quantile transform: n(Gn(u)u)\sqrt{n}(G_n(u)-u) starts at 00 (no mass before 00) and returns to 00 (all mass is accounted for by u=1u=1). The figure below is exactly this construction.

Brownian bridge

A Brownian bridge is a Wiener path with its chord subtracted. Top: one Wiener path WtW_t and the straight chord Lt=tW1L_t = t W_1 from start to finish. Bottom: Bt=WttW1B_t = W_t - t W_1, pinned at 00 at both endpoints. The marked gap WtLtW_{t^\ast}-L_{t^\ast} is exactly the bridge height BtB_{t^\ast}.

The object GoF actually computes with is this staircase, centered and blown up to CLT scale. Call it the empirical process

αn(u)=n(Gn(u)u),u[0,1]\alpha_n(u) = \sqrt{n}\bigl(G_n(u)-u\bigr),\qquad u\in[0,1].

Pointwise it is the standardized binomial from earlier; as a function of uu it is a stochastic process that, like the green curve above, starts at 00 and dies at 00. Without the n\sqrt{n}, Glivenko–Cantelli says αn/n0\alpha_n/\sqrt{n}\to 0 uniformly — the staircase hugs FF and there is nothing left to test. The n\sqrt{n} keeps the typical fluctuations of order 11, so questions like “is supuαn(u)\sup_u|\alpha_n(u)| too large?” have a non-degenerate answer. Donsker’s theorem, next, is the functional CLT that identifies the limiting path: αnB\alpha_n\Rightarrow B, a Brownian bridge. That is the whole reason to care about bridges — every KS/CvM/AD statistic is a continuous functional of αn\alpha_n, hence in the limit a functional of BB, whose law we can actually compute.

Donsker’s theorem

Here the previous pieces become a theorem: we have an empirical staircase GnG_n, a pinned fluctuation process αn=n(Gnu)\alpha_n=\sqrt{n}(G_n-u), and a named Gaussian limit BB (Brownian bridge) that has the same covariance and the same endpoints. Donsker’s theorem is the statement that this is actually convergence in distribution: αnB\alpha_n\Rightarrow B as stochastic processes on [0,1][0,1]. The double arrow \Rightarrow means convergence in distribution. In case of discrete random variable that is the statement FXn(x)FX(x)F_{X_n}(x)\to F_X(x) at every continuity point of FXF_X — equivalently, E[f(Xn)]E[f(X)]\mathbb{E}[f(X_n)]\to\mathbb{E}[f(X)] for every bounded continuous f:RRf:\mathbb{R}\to\mathbb{R}. In a continuous case of a stochastic process it is the same thing, with ff a bounded continuous functional of the whole curve: E[φ(αn)]E[φ(B)]\mathbb{E}[\varphi(\alpha_n)]\to\mathbb{E}[\varphi(B)] for every such φ\varphi. In particular it does not say that one realized staircase αn(ω)\alpha_n(\omega) converges to one realized bridge B(ω)B(\omega); only that probabilities of nice events about the path settle to those of BB.

That arrow is the hinge of the rest of the post. Write TT for a test statistic: a continuous map from a path on [0,1][0,1] to a real number, fT(f)f\mapsto T(f) (the arrow \mapsto just names the function: “the path ff is sent to the number T(f)T(f)”).

Kolmogorov–Smirnov, Cramér–von Mises and Anderson–Darling are three such maps:

TKS(α)=supuα(u)T_{\mathrm{KS}}(\alpha)=\sup_u|\alpha(u)|

TCvM(α)=01α(u)2duT_{\mathrm{CvM}}(\alpha)=\int_0^1 \alpha(u)^2\,du

TAD(α)=01α(u)2u(1u)duT_{\mathrm{AD}}(\alpha)=\int_0^1 \frac{\alpha(u)^2}{u(1-u)}\,du

The continuous mapping theorem plus Donsker turn T(αn)T(\alpha_n) into T(B)T(B), whose laws we then compute (Kolmogorov’s distribution of supB\sup|B|; Karhunen–Loève for the L2L^2 and weighted L2L^2 maps in case of Cramer - von Mises and Anderson - Darling). Without Donsker we would only have the pointwise binomial CLT from earlier — enough for one uu, useless for a supremum or an integral over all uu.

The classical CLT watches only the endpoint: Sn/nN(0,σ2)S_n/\sqrt{n}\to\mathcal{N}(0,\sigma^2). (A single arrow \to is the same idea as \Rightarrow, but for a random number rather than a stochastic process.) Donsker’s theorem is the same statement for the whole path. We will prove two claims.

Donsker’s theorem (functional CLT).

Donsker’s theorem for the partial sums. Let ξi\xi_i be i.i.d. with mean 00 and variance 11, Sk=ξ1++ξkS_k=\xi_1+\cdots+\xi_k, and Wn(t)=Snt/nW_n(t)=S_{\lfloor nt\rfloor}/\sqrt{n} on [0,1][0,1]. Then WnWW_n\Rightarrow W, a standard Wiener process.

Donsker’s theorem for the empirical process. Let UiU_i be i.i.d. Uniform[0,1][0,1] and αn(u)=n(Gn(u)u)\alpha_n(u)=\sqrt{n}\bigl(G_n(u)-u\bigr). Then αnB\alpha_n\Rightarrow B, a standard Brownian bridge on [0,1][0,1]. Equivalently αn\alpha_n is a Donsker walk with the chord subtracted: Bt=WttW1B_t=W_t-t W_1.

The first claim is steps 1–3; the second is step 4.

1. Rescale the walk. Let ξi\xi_i be i.i.d. with E[ξi]=0\mathbb{E}[\xi_i]=0, Var(ξi)=1\mathrm{Var}(\xi_i)=1, and (for the tightness step) a finite fourth moment. Put Sk=ξ1++ξkS_k=\xi_1+\cdots+\xi_k, S0=0S_0=0, and

Wn(t)=Sntn,t[0,1]W_n(t) = \dfrac{S_{\lfloor nt\rfloor}}{\sqrt{n}},\qquad t\in[0,1]

(linearly interpolate between k/nk/n if you prefer a continuous path). Time kk sits at t=k/nt=k/n; height is the CLT scale 1/n1/\sqrt{n}. Ordinary CLT is Wn(1)N(0,1)W_n(1)\to\mathcal{N}(0,1).

2. Finite-dimensional distributions. Fix 0t1<<tm10\le t_1<\cdots<t_m\le 1. The increments Wn(tj)Wn(tj1)W_n(t_j)-W_n(t_{j-1}) are functions of disjoint blocks of the ξi\xi_i, hence independent, and each block is a sum of about n(tjtj1)n(t_j-t_{j-1}) terms. Lindeberg CLT gives

Wn(tj)Wn(tj1)N(0,tjtj1)W_n(t_j)-W_n(t_{j-1}) \to \mathcal{N}\bigl(0,\, t_j-t_{j-1}\bigr),

independently across jj. Therefore (Wn(t1),,Wn(tm))(W_n(t_1),\dots,W_n(t_m)) converges to a centered Gaussian vector with Cov=min(ti,tj)\mathrm{Cov}=\min(t_i,t_j) — exactly the finite-dimensional laws of Wiener process WW.

3. Tightness (paths, not just coordinates). The finite-dimensional distributions (f.d.d.s) of a process XX are the laws of the random vectors (X(t1),,X(tm))\bigl(X(t_1),\dots,X(t_m)\bigr) for every finite grid t1,,tmt_1,\dots,t_m. Step 2 says those vectors, for WnW_n, converge to those of WW. That is not enough for WnWW_n\Rightarrow W as paths: two processes can agree on every finite grid and still differ wildly in the gaps (imagine a spike of height 11 and width 1/n1/n sitting between the tjt_j; no finite grid will see it, but supWn\sup|W_n| will). Tightness rules those spikes out. A sequence of random paths is tight if, uniformly in nn, the path stays bounded and does not oscillate too fast: for every ε>0\varepsilon>0 there is a modulus of continuity δ\delta such that

P(supts<δWn(t)Wn(s)>ε)\mathbb{P}\bigl(\sup_{|t-s|<\delta}\bigl|W_n(t)-W_n(s)\bigr| > \varepsilon\bigr) is small, independently of nn. Then every subsequential limit is a continuous path, and together with the f.d.d.s it must be Wiener process.

The Kolmogorov–Chentsov theorem turns a moment bound on increments into that modulus. If a process satisfies, for some α>0\alpha>0 and β>0\beta>0,

EX(t)X(s)αCts1+β\mathbb{E}\bigl|X(t)-X(s)\bigr|^\alpha \le C\,|t-s|^{1+\beta},

then it admits a version whose paths are almost surely Hölder-γ\gamma for every γ<β/α\gamma<\beta/\alpha (locally: X(t)X(s)Ktsγ|X(t)-X(s)|\le K|t-s|^\gamma). In particular the oscillations on small intervals are under control, which is tightness in Hölder geometry. For our walk the block of length ntsn|t-s| and a finite fourth moment of ξi\xi_i give

E[Wn(t)Wn(s)]4Cts2\mathbb{E}\bigl[W_n(t)-W_n(s)\bigr]^4 \le C\,|t-s|^2,

which is Kolmogorov–Chentsov with α=4\alpha=4, β=1\beta=1, hence Hölder up to (but not including) exponent 1/41/4 — the same budget Brownian motion itself has (Wiener increments satisfy EWtWs4=3ts2\mathbb{E}|W_t-W_s|^4=3|t-s|^2). Tightness plus f.d.d.s \Rightarrow WnWW_n\Rightarrow W as processes. (If you only assume two moments the same conclusion holds with a maximal inequality in place of Kolmogorov–Chentsov.)

4. Empirical process: the same two ingredients. After the quantile transform, αn(u)=n1/2i=1n(1{Uiu}u)\alpha_n(u)=n^{-1/2}\sum_{i=1}^n \bigl(\mathbf{1}_{\{U_i\le u\}}-u\bigr). This is not a walk of independent increments in uu (each UiU_i is reused), but it is a walk in the sample index, and the two Donsker steps still fire.

Finite-dimensional laws. For fixed u1,,umu_1,\dots,u_m, the vector of indicators is multinomial. We already computed its covariance: it equals min(s,t)st\min(s,t)-st at every finite nn. Multivariate CLT therefore sends (αn(u1),,αn(um))(\alpha_n(u_1),\dots,\alpha_n(u_m)) to the corresponding Brownian-bridge vector. Also αn(0)=αn(1)=0\alpha_n(0)=\alpha_n(1)=0 exactly.

Tightness. An increment αn(t)αn(s)\alpha_n(t)-\alpha_n(s) is a centered binomial of success probability tst-s, hence the same fourth-moment bound Eαn(t)αn(s)4Cts2\mathbb{E}|\alpha_n(t)-\alpha_n(s)|^4\le C|t-s|^2. Tightness follows as in step 3.

The unique continuous Gaussian limit with that covariance and those endpoints is the Brownian bridge. Equivalently: the chord map π(x)(t)=x(t)tx(1)\pi(x)(t)=x(t)-t\,x(1) is continuous, π(W)=B\pi(W)=B, and αn=π(Wn)+op(1)\alpha_n=\pi(W_n')+o_p(1) for a Donsker walk WnW_n' built from the same indicators, so continuous mapping gives αnB\alpha_n\Rightarrow B directly from WnWW_n'\Rightarrow W.

Continuous functionals of αn\alpha_n are then functionals of BB — which is how KS, CvM and AD get their null distributions.

Kolmogorov-Smirnov

The Kolmogorov–Smirnov statistic is the largest vertical gap between the staircase and the hypothesized CDF,

Dn=supxFn(x)F(x)=supu[0,1]Gn(u)uD_n = \sup_x \bigl|F_n(x)-F(x)\bigr| = \sup_{u\in[0,1]} \bigl|G_n(u)-u\bigr|.

In the notation of the previous section this is just TKST_{\mathrm{KS}} applied to the empirical process: nDn=TKS(αn)=supuαn(u)\sqrt{n}\,D_n = T_{\mathrm{KS}}(\alpha_n) = \sup_u|\alpha_n(u)|. The supremum of a step function is attained at (or just before) a jump, so in practice one evaluates i/nF(X(i))|i/n - F(X_{(i)})| and (i1)/nF(X(i))|(i-1)/n - F(X_{(i)})| at the order statistics and takes the max.

Donsker gives αnB\alpha_n\Rightarrow B. The map fsupff\mapsto\sup|f| is continuous, so the continuous mapping theorem yields the whole null distribution in one line:

nDnsupu[0,1]Bu\sqrt{n}\,D_n \Rightarrow \sup_{u\in[0,1]} |B_u|.

The law of that supremum is Kolmogorov’s distribution. For x>0x>0,

K(x):=P(supuBux)=12k=1(1)k1e2k2x2K(x) := \mathbb{P}\bigl(\sup_u |B_u| \le x\bigr) = 1 - 2\sum_{k=1}^{\infty} (-1)^{k-1} e^{-2k^2 x^2}.

(The series is the reflection principle in disguise: P(supB>x)\mathbb{P}(\sup|B|>x) is the probability that a bridge ever exits [x,x][-x,x], obtained by summing signed Wiener paths that hit ±x,±3x,\pm x, \pm 3x, \ldots and still end at 00.) Thus, under a continuous simple null,

P(nDnx)K(x)\mathbb{P}(\sqrt{n}\,D_n \le x) \to K(x).

Reject H0H_0 when nDn\sqrt{n}\,D_n exceeds the (1α)(1-\alpha)-quantile of KK — about 1.361.36 at 5%5\%, 1.631.63 at 1%1\%. Because of the quantile transform, those numbers do not depend on FF.

KS asks only how far the worst point of αn\alpha_n wandered. That makes it a clean, distribution-free test of any continuous FF, and unusually sensitive to a single large bump. It is not equally sensitive everywhere: Var(Bu)=u(1u)\mathrm{Var}(B_u)=u(1-u) peaks at the median, so a deviation in the bulk moves supB\sup|B| more easily than the same vertical gap in the tails. Cramér–von Mises will average the squared gap instead of taking the max; Anderson–Darling will reweight the tails. If FF is estimated from the same sample, αn\alpha_n is no longer a free bridge and these critical values are too conservative (Lilliefors’ tables for normality, etc.).

Cramer-von Mises family of tests

Kolmogorov–Smirnov judged the empirical process by its LL^\infty size: one number, the worst vertical gap. The Cramér–von Mises family judges it in L2L^2 instead. Average the squared gap along the whole curve, optionally with a weight ψ\psi that says which uu you care about:

ωn2(ψ)=01αn(u)2ψ(u)du=Tψ(αn)\omega_n^2(\psi) = \int_0^1 \alpha_n(u)^2\,\psi(u)\,du = T_\psi(\alpha_n).

After the quantile transform this is the same as n(FnF)2ψ(F)dF\int n(F_n-F)^2\psi(F)\,dF on the original scale. Two members of the family will occupy the rest of the post:

  • Cramér–von Mises: ψ1\psi\equiv 1, so TCvM(αn)=01αn(u)2duT_{\mathrm{CvM}}(\alpha_n)=\int_0^1\alpha_n(u)^2\,du;
  • Anderson–Darling: ψ(u)=1/(u(1u))\psi(u)=1/(u(1-u)), so TAD(αn)=01αn(u)2/(u(1u))duT_{\mathrm{AD}}(\alpha_n)=\int_0^1 \alpha_n(u)^2/(u(1-u))\,du.

The AD weight is not a whim: Var(Bu)=u(1u)\mathrm{Var}(B_u)=u(1-u), so ψ=1/Var(Bu)\psi=1/\mathrm{Var}(B_u) puts the noisy middle and the quiet tails on equal footing. Both statistics are continuous maps of the path, so Donsker plus continuous mapping give ωn2(ψ)01Bu2ψ(u)du\omega_n^2(\psi)\Rightarrow\int_0^1 B_u^2\,\psi(u)\,du under a continuous simple null — still distribution-free. What they share, and what KS did not need, is that the limit is a quadratic functional of a Gaussian process.

That is awkward at first glance. The random variables BuB_u at different uu are dependent (the bridge covariance min(s,t)st\min(s,t)-st is not diagonal), so B2ψ\int B^2\psi is not the square of one Gaussian and not an ordinary χ2\chi^2. The way out is the same move that turns a correlated Gaussian vector XN(0,Σ)X\sim\mathcal{N}(0,\Sigma) into independent coordinates: diagonalize Σ\Sigma, write X=λkZkvkX=\sum \sqrt{\lambda_k} Z_k v_k with ZkZ_k i.i.d. standard normal. For a process the covariance matrix is the kernel KB(s,t)K_B(s,t), the eigenvectors become eigenfunctions, and the finite sum becomes a series

Bu=k=1λkZkφk(u),ZkN(0,1) i.i.d.B_u = \sum_{k=1}^{\infty} \sqrt{\lambda_k}\,Z_k\,\varphi_k(u),\qquad Z_k\sim\mathcal{N}(0,1)\ \text{i.i.d.}

That expansion is Karhunen–Loève — PCA in L2L^2, with interdependent values of BB rotated into independent random coefficients in front of orthogonal eigenfunctions. Plug it into the quadratic statistic and the cross terms die:

01Bu2ψ(u)du=k=1λkZk2\int_0^1 B_u^2\,\psi(u)\,du = \sum_{k=1}^{\infty} \lambda_k Z_k^2,

a weighted sum of independent χ12\chi^2_1 random variables. The eigenvalues {λk}\{\lambda_k\} depend on ψ\psi (plain L2L^2 for CvM, the 1/(u(1u))1/(u(1-u)) inner product for AD), which is why the two tests get different series and different critical values — but they share this machinery. The next section builds that decomposition; then we return to the two choices of ψ\psi.

Karhunen-Loeve decomposition of a stochastic process

A finite-dimensional random vector is described by a covariance matrix Σij=Cov(Xi,Xj)\Sigma_{ij}=\mathrm{Cov}(X_i,X_j). A standard way to diagonalize that covariance matrix is to perform a PCA or truncated PCA (knowing that the covariance matrix is Gram, i.e. symmetric positive definite, i.e. all of its eigenvalues are real and positive numbers and eigenvector correspond to the axes of ellipsoid of revolution). See post on PCA.

Karhunen-Loeve is a functional analysis-style generalization of PCA from discrete random vectors case to a continuous random function/stochastic process case. You can defined a covariance matrix for the stochastic process, describing how inter-correlated points of this process at a certain distance are.

In discrete case you sample a process at times t1,,tkt_1,\dots,t_k and you get a random vector; let the grid get dense and you’re transitioning to a continuous random function case, where matrix becomes a covariance kernel K(s,t)=Cov(Xs,Xt)K(s,t)=\mathrm{Cov}(X_s,X_t). This is the same leap as from the discrete Fourier transform (eigenbasis of a circulant matrix on nn points) to Fourier series (eigenbasis of a translation-invariant kernel on [0,1][0,1]). In functional-analysis language, KK is a compact self-adjoint operator on L2[0,1]L^2[0,1], (Kf)(s)=01K(s,t)f(t)dt(Kf)(s)=\int_0^1 K(s,t)\,f(t)\,dt; its eigen-decomposition is Karhunen–Loève — PCA for paths — which Cramér–von Mises and Anderson–Darling will use.

What’s counter-intuitive in this transition is that compared to discrete case axes change the roles: normally your data matrix XX would consist of nn data points, each of which is a pp-dimensional predictor. By covariance matrix you’d understand n×nn \times n matrix of covariances between the data points, not between coordinates of each data point. Center the columns of XX (subtract each predictor’s mean) to get X~\tilde{X}. The n×nn\times n Gram matrix is the product of an n×pn\times p matrix with a p×np\times n one, C=X~X~TC=\tilde{X}\tilde{X}^T. Below n=4n=4, p=5p=5: the two blue rows of X~\tilde{X} become the two blue columns of X~T\tilde{X}^T, and their inner product is the blue entry of CC.

Cn×n=X~n×pX~p×nT=[x11x12x13x14x15x21x22x23x24x25x31x32x33x34x35x41x42x43x44x45][x11x21x31x41x12x22x32x42x13x23x33x43x14x24x34x44x15x25x35x45]=[c11c12c13c14c21c22c23c24c31c32c33c34c41c42c43c44]C_{n \times n} = \tilde{X}_{n \times p}\, \tilde{X}^T_{p \times n} = \begin{bmatrix} x_{11} & x_{12} & x_{13} & x_{14} & x_{15} \\ \textcolor{blue}{x_{21}} & \textcolor{blue}{x_{22}} & \textcolor{blue}{x_{23}} & \textcolor{blue}{x_{24}} & \textcolor{blue}{x_{25}} \\ x_{31} & x_{32} & x_{33} & x_{34} & x_{35} \\ \textcolor{blue}{x_{41}} & \textcolor{blue}{x_{42}} & \textcolor{blue}{x_{43}} & \textcolor{blue}{x_{44}} & \textcolor{blue}{x_{45}} \end{bmatrix} \begin{bmatrix} x_{11} & \textcolor{blue}{x_{21}} & x_{31} & \textcolor{blue}{x_{41}} \\ x_{12} & \textcolor{blue}{x_{22}} & x_{32} & \textcolor{blue}{x_{42}} \\ x_{13} & \textcolor{blue}{x_{23}} & x_{33} & \textcolor{blue}{x_{43}} \\ x_{14} & \textcolor{blue}{x_{24}} & x_{34} & \textcolor{blue}{x_{44}} \\ x_{15} & \textcolor{blue}{x_{25}} & x_{35} & \textcolor{blue}{x_{45}} \end{bmatrix} = \begin{bmatrix} c_{11} & c_{12} & c_{13} & c_{14} \\ c_{21} & c_{22} & c_{23} & \textcolor{blue}{c_{24}} \\ c_{31} & c_{32} & c_{33} & c_{34} \\ c_{41} & \textcolor{blue}{c_{42}} & c_{43} & c_{44} \end{bmatrix}

In case of random functions and covariance kernel roles of axes swap. One path is what used to be a pp-dimensional data point, as pp \to \infty. And so covariance kernel actually reflects correlations between what used to be your predictors. Flip the product: K=X~TX~K=\tilde{X}^T\tilde{X} is p×np\times n times n×pn\times p. The two orange columns of X~\tilde{X} become the two orange rows of X~T\tilde{X}^T, and their inner product is the orange entry of the p×pp\times p kernel — the object that becomes K(s,t)K(s,t) as pp\to\infty.

Kp×p=X~p×nTX~n×p=[x11x21x31x41x12x22x32x42x13x23x33x43x14x24x34x44x15x25x35x45][x11x12x13x14x15x21x22x23x24x25x31x32x33x34x35x41x42x43x44x45]=[k11k12k13k14k15k21k22k23k24k25k31k32k33k34k35k41k42k43k44k45k51k52k53k54k55]K_{p \times p} = \tilde{X}^T_{p \times n}\, \tilde{X}_{n \times p} = \begin{bmatrix} x_{11} & x_{21} & x_{31} & x_{41} \\ \textcolor{orange}{x_{12}} & \textcolor{orange}{x_{22}} & \textcolor{orange}{x_{32}} & \textcolor{orange}{x_{42}} \\ x_{13} & x_{23} & x_{33} & x_{43} \\ \textcolor{orange}{x_{14}} & \textcolor{orange}{x_{24}} & \textcolor{orange}{x_{34}} & \textcolor{orange}{x_{44}} \\ x_{15} & x_{25} & x_{35} & x_{45} \end{bmatrix} \begin{bmatrix} x_{11} & \textcolor{orange}{x_{12}} & x_{13} & \textcolor{orange}{x_{14}} & x_{15} \\ x_{21} & \textcolor{orange}{x_{22}} & x_{23} & \textcolor{orange}{x_{24}} & x_{25} \\ x_{31} & \textcolor{orange}{x_{32}} & x_{33} & \textcolor{orange}{x_{34}} & x_{35} \\ x_{41} & \textcolor{orange}{x_{42}} & x_{43} & \textcolor{orange}{x_{44}} & x_{45} \end{bmatrix} = \begin{bmatrix} k_{11} & k_{12} & k_{13} & k_{14} & k_{15} \\ k_{21} & k_{22} & k_{23} & \textcolor{orange}{k_{24}} & k_{25} \\ k_{31} & k_{32} & k_{33} & k_{34} & k_{35} \\ k_{41} & \textcolor{orange}{k_{42}} & k_{43} & k_{44} & k_{45} \\ k_{51} & k_{52} & k_{53} & k_{54} & k_{55} \end{bmatrix}

(The usual sample-covariance 1/n1/n or 1/(n1)1/(n-1) factor does not change the eigenstructure; it is omitted from the pictures.)

From practical standpoint for the Cramer - von Mises family of tests we’d need covariance kernels for Wiener process KW(s,t)=min(s,t)K_W(s,t)=\min(s,t) and for the Brownian bridge process Bt=WttW1B_t=W_t-t W_1. For the latter a short calculation gives

KB(s,t)=Cov(Bs,Bt)=min(s,t)stK_B(s,t)=\mathrm{Cov}(B_s,B_t)=\min(s,t)-st.

In particular Var(Bu)=u(1u)\mathrm{Var}(B_u)=u(1-u). Not a coincidence: for sts\le t,

Cov(n(Gn(s)s),n(Gn(t)t))=Cov(1Us,1Ut)=s(1t)=min(s,t)st\mathrm{Cov}\bigl(\sqrt{n}(G_n(s)-s),\,\sqrt{n}(G_n(t)-t)\bigr)=\mathrm{Cov}(\mathbf{1}_{U\le s},\,\mathbf{1}_{U\le t})=s(1-t)=\min(s,t)-st.

The empirical process on [0,1][0,1] already has exactly the Brownian-bridge covariance at every finite nn (the multinomial structure of the indicators); only the marginals are still binomial rather than Gaussian. That is why this kernel is the right object: every path-functional of n(Gnu)\sqrt{n}(G_n-u) becomes, in the limit, the same functional of a Brownian bridge.

Cramer - von Mises

Cramér–von Mises is the unweighted member of the family, ψ1\psi\equiv 1:

ωn2=TCvM(αn)=01αn(u)2du=n(FnF)2dF\omega_n^2 = T_{\mathrm{CvM}}(\alpha_n) = \int_0^1 \alpha_n(u)^2\,du = n\int_{-\infty}^{\infty}(F_n-F)^2\,dF.

Every uu contributes equally to the L2L^2 energy of the empirical process. Relative to KS, a single spike is diluted by the rest of the curve; relative to AD, the tails are not up-weighted, so CvM is a bulk test — most of its power sits where Var(Bu)=u(1u)\mathrm{Var}(B_u)=u(1-u) is already large.

The staircase again turns the integral into a sum over U(i)=F(X(i))U_{(i)}=F(X_{(i)}):

ωn2=112n+i=1n(U(i)2i12n)2\omega_n^2 = \frac{1}{12n} + \sum_{i=1}^{n}\left(U_{(i)}-\frac{2i-1}{2n}\right)^2.

(The term (2i1)/(2n)(2i-1)/(2n) is the midpoint of the ii-th ECDF step; ωn2\omega_n^2 is the squared distance from the uniform order statistics to those midpoints, plus a 1/(12n)1/(12n) continuity correction.)

Donsker sends ωn2\omega_n^2 to 01Bu2du\int_0^1 B_u^2\,du. Karhunen–Loève in ordinary L2[0,1]L^2[0,1] diagonalizes the bridge covariance KB(s,t)=min(s,t)stK_B(s,t)=\min(s,t)-st. The eigenfunctions and eigenvalues are the sine modes that vanish at both endpoints,

φk(u)=2sin(kπu),λk=1k2π2,k=1,2,\varphi_k(u)=\sqrt{2}\sin(k\pi u),\qquad \lambda_k=\frac{1}{k^2\pi^2},\qquad k=1,2,\ldots

hence

ωn2k=1Zk2k2π2,ZkN(0,1) i.i.d.\omega_n^2 \Rightarrow \sum_{k=1}^{\infty} \frac{Z_k^2}{k^2\pi^2},\qquad Z_k\sim\mathcal{N}(0,1)\ \text{i.i.d.}

This is a weighted generalized chi-squared kλkχ1,k2\sum_k \lambda_k\chi^2_{1,k} with weights 1/(k2π2)1/(k^2\pi^2). The k=1k=1 mode (a single arch on [0,1][0,1]) carries most of the mass; higher harmonics decay as 1/k21/k^2, faster than AD’s 1/(k(k+1))1/(k(k+1)), which is another way of saying CvM is less interested in the wiggly, tail-heavy part of the path.

Reject H0H_0 when ωn2\omega_n^2 is large. For a fully specified continuous FF the 5%5\% and 1%1\% points are about 0.4610.461 and $0.743.

Anderson–Darling, next, keeps this quadratic/KL story and only changes the inner product.

Anderson-Darling

Anderson–Darling is Cramér–von Mises with the weight that flattens the bridge’s variance, ψ(u)=1/(u(1u))\psi(u)=1/(u(1-u)):

An2=TAD(αn)=01αn(u)2u(1u)du=n(FnF)2F(1F)dFA_n^2 = T_{\mathrm{AD}}(\alpha_n) = \int_0^1 \frac{\alpha_n(u)^2}{u(1-u)}\,du = n\int_{-\infty}^{\infty}\frac{(F_n-F)^2}{F(1-F)}\,dF.

Near u=0u=0 and u=1u=1, Var(Bu)=u(1u)\mathrm{Var}(B_u)=u(1-u) vanishes, so an ordinary L2L^2 gap (CvM) or a raw vertical gap (KS) can hide a large relative misfit in the tails. Dividing by u(1u)u(1-u) is exactly studentizing each uu: the integrand is the square of the pointwise zz-score of αn(u)\alpha_n(u). That is why AD is the member of the family people reach for when tail discrepancies matter (normality with outliers, exponential vs heavier tails, and so on).

Because GnG_n is a staircase, the integral collapses to a sum over the order statistics U(i)=F(X(i))U_{(i)}=F(X_{(i)}) of the quantile-transformed sample:

An2=n1ni=1n(2i1)[lnU(i)+ln(1U(n+1i))]A_n^2 = -n -\frac{1}{n}\sum_{i=1}^{n}(2i-1)\bigl[\ln U_{(i)}+\ln\bigl(1-U_{(n+1-i)}\bigr)\bigr].

Under a continuous simple null, Donsker sends An2A_n^2 to Bu2/(u(1u))du\int B_u^2/(u(1-u))\,du. Karhunen–Loève in the weighted inner product f,gψ=fg/(u(1u))du\langle f,g\rangle_\psi=\int f g/(u(1-u))\,du diagonalizes that quadratic form. The eigenvalues of the corresponding operator on the bridge are λk=1/(k(k+1))\lambda_k=1/(k(k+1)), so

An2k=1Zk2k(k+1),ZkN(0,1) i.i.d.A_n^2 \Rightarrow \sum_{k=1}^{\infty} \frac{Z_k^2}{k(k+1)},\qquad Z_k\sim\mathcal{N}(0,1)\ \text{i.i.d.}

Each Zk2χ12Z_k^2\sim\chi^2_1, so the limit is a weighted generalized chi-squared: an infinite linear combination kλkχ1,k2\sum_k \lambda_k \chi^2_{1,k} of independent chi-squares, here with weights λk=1/(k(k+1))\lambda_k=1/(k(k+1)). It is “generalized” because it is not a single χν2\chi^2_\nu (the weights are unequal), and “weighted” because those λk\lambda_k are exactly the KL eigenvalues of the AD inner product. The same statement holds for CvM with a different sequence {λk}\{\lambda_k\}; ordinary χ2\chi^2 would require all weights equal.

(The eigenfunctions are Jacobi / shifted Legendre polynomials, orthonormal in that weighted L2L^2 — the same PCA story as CvM, different inner product, different spectrum. CvM’s eigenvalues decay like 1/(πk)21/(\pi k)^2; AD’s 1/(k(k+1))1/(k(k+1)) put relatively more mass on the first few modes.)

Reject H0H_0 when An2A_n^2 is large. For a fully specified continuous FF the 5%5\% and 1%1\% points of the limiting law are about 2.492.49 and 3.863.86. As always, if mean or variance of FF is estimated from the same sample the process is no longer a free bridge and one must use the adjusted tables (Stephens); for normality that actually helps power, which is why AD is a standard normality test.

The three tests are now three functionals of the same αn\alpha_n: KS takes supαn\sup|\alpha_n|, CvM takes αn2\int\alpha_n^2, AD takes αn2/Var(B)\int\alpha_n^2/\mathrm{Var}(B). Same Donsker limit BB, three different ways of asking whether that bridge wandered too far.

Honorable mention: Shapiro-Wilk test of normality

Everything above was omnibus GoF: after the quantile transform, KS/CvM/AD do not care which continuous FF you hypothesized, and their null limits are functionals of one Brownian bridge. Shapiro–Wilk asks a narrower question — “is this sample Gaussian?” — and therefore throws away the empirical-process machinery. The mathematics is mostly different. The problem is the same, and the contrast is useful.

The catch we kept postponing is the composite null. In practice one almost never knows μ\mu and σ\sigma; one estimates them from the same sample. Then Ui=F^(Xi)U_i=\hat F(X_i) are not uniform, αn\alpha_n is not a free bridge, and the KS/CvM/AD critical values we quoted are wrong (Stephens / Lilliefors). Shapiro–Wilk is built for that case. It is location–scale invariant: W(aX+b)=W(X)W(aX+b)=W(X), so unknown μ,σ\mu,\sigma are not a nuisance — they are quotiented out.

The picture is a QQ-plot, which is the inverse of the ECDF story. Order the sample X(1)X(n)X_{(1)}\le\cdots\le X_{(n)}. Under normality the order statistics have known means mi=E[Z(i)]m_i=\mathbb{E}[Z_{(i)}] and covariance VV for a standard normal sample ZZ. The best linear unbiased estimator of σ\sigma from those order statistics is mV1X()m^\top V^{-1} X_{(\cdot)} (up to a constant). Shapiro–Wilk compares that Gaussian-specific estimate of scale to the ordinary sample variance:

W=(i=1naiX(i))2i=1n(XiXˉ)2,aV1m.W = \frac{\bigl(\sum_{i=1}^{n} a_i X_{(i)}\bigr)^2}{\sum_{i=1}^{n}(X_i-\bar X)^2},\qquad a \propto V^{-1}m.

W(0,1]W\in(0,1]; W=1W=1 means the ordered sample lies exactly on the normal scores line. One rejects normality for small WW (the opposite direction from DnD_n, ωn2\omega_n^2, An2A_n^2). The Shapiro–Francia cousin is literally the squared correlation of (X(i))(X_{(i)}) with mm, i.e. R2R^2 of the QQ-plot; WW is that idea with the covariance VV of the order statistics baked into the weights aia_i.

So: KS/CvM/AD measure how far the ECDF staircase wanders from FF, in LL^\infty or (weighted) L2L^2, and need Donsker to know what “far” means. Shapiro–Wilk measures how straight the quantile plot is against Gaussian order-statistic means, and “far” is a finite-nn ratio of quadratic forms — no bridge, no Karhunen–Loève, no generalized χ2\chi^2 series. The payoff for specializing is power: for the composite normal null, WW typically beats even Anderson–Darling. The cost is that it does not test exponentiality, uniformity, or any other FF. Those remain jobs for αn\alpha_n.

References:


Boris Burkov

Written by Boris Burkov who lives in Moscow, Russia, loves to take part in development of cutting-edge technologies, reflects on how the world works and admires the giants of the past. You can follow me in Telegram