Algorithm Search⌘K

Algorithmintro

One-way analysis of variance

Also called one-way ANOVA, ANOVA F-test.

Tests whether k group means are equal by the ratio of the between-group to the within-group mean square. Exact F law under normal errors with equal variances. O(N).

Time
Θ(n) worst · best
Space
Θ(k) extra
Where
k = the number of groups; n = the total number of observations, N in the text
Properties
deterministicexactofflineiterative
Origin
Ronald A. Fisher, George W. Snedecor, Bernard L. Welch, 1925

One-way analysis of variance (ANOVA) is a statistical hypothesis test of the null hypothesis that kk groups have the same mean. It splits the total variation of the data around the grand mean into the variation of the group means around the grand mean (between groups) and the variation of the data around their group means (within groups). It then divides the between mean square by the within mean square. Under normal errors with a common variance, this ratio has the F distribution with k−1k - 1 and N−kN - k degrees of freedom exactly, for every sample size. With two groups it is the square of the pooled Student t statistic. The test is the regression F-test for the model with one indicator column per group against the model with the intercept only.

In plain words

You have several groups, for example plots under three fertilizers, and you want to know if the true averages differ. The group averages always differ a little, because single plots vary. So compare two kinds of spread. The first is how far the group averages lie from each other. The second is how much single plots scatter inside their own group. If the fertilizers do nothing, both kinds of spread come only from plot-to-plot noise, and their ratio is near one. If the ratio is much larger than one, the group averages are farther apart than the noise explains. Fisher worked out how large the ratio can be by chance when the noise follows a bell curve with the same width in every group. The test says only that some averages differ, not which ones.

The problem

Model. Data yijy_{ij} for groups i=1,…,ki = 1, \dots, k (k≥2k \ge 2) and units j=1,…,nij = 1, \dots, n_i, with N=∑ini>kN = \sum_i n_i > k. The model is yij=μi+eijy_{ij} = \mu_i + e_{ij}, where the errors eije_{ij} are iid N(0,σ2)N(0, \sigma^2). The means μi\mu_i and the common variance σ2\sigma^2 are unknown. In a randomized experiment the groups are the treatments and randomization justifies the comparison.

Hypotheses. H0:μ1=μ2=⋯=μkH_0: \mu_1 = \mu_2 = \dots = \mu_k against H1H_1: at least two means differ. The alternative has no direction.

Statistic. With group means yˉi\bar y_i, the grand mean yˉ=1N∑i,jyij\bar y = \frac1N\sum_{i,j}y_{ij}, and

SSB=∑i=1kni(yˉi−yˉ)2,SSW=∑i=1k∑j=1ni(yij−yˉi)2,SSB = \sum_{i=1}^k n_i(\bar y_i - \bar y)^2, \qquad SSW = \sum_{i=1}^k\sum_{j=1}^{n_i}(y_{ij} - \bar y_i)^2,

the statistic is

F=MSBMSW=SSB/(k−1)SSW/(N−k).F = \frac{MSB}{MSW} = \frac{SSB/(k - 1)}{SSW/(N - k)} .

The total sum of squares splits as SST=∑i,j(yij−yˉ)2=SSB+SSWSST = \sum_{i,j}(y_{ij} - \bar y)^2 = SSB + SSW. MSWMSW is the pooled sample variance, an unbiased estimate of σ2\sigma^2 whether H0H_0 holds or not. E[MSB]=σ2+∑ini(μi−μˉ)2/(k−1)E[MSB] = \sigma^2 + \sum_i n_i(\mu_i - \bar\mu)^2/(k - 1) with μˉ=∑niμi/N\bar\mu = \sum n_i\mu_i/N, so MSBMSB estimates σ2\sigma^2 only under H0H_0.

Null distribution. Exact: F∼Fk−1,N−kF \sim F_{k-1, N-k} under H0H_0, for every N>kN > k, whatever σ\sigma is. SSBSSB and SSWSSW are squared lengths of the projections of the data onto two orthogonal subspaces, so they are independent scaled chi-square variables (Cochran's theorem in projection form, proved in the correctness proof). Without normality the size is only near α\alpha; see the table under Example.

Rejection rule and size. Reject H0H_0 when F≥F(k−1,N−k,0.95)F \ge F_{(k-1, N-k, 0.95)} at α=0.05\alpha = 0.05, with the lower quantile F(⋅,⋅,p)F_{(\cdot, \cdot, p)}. The size equals α\alpha under the model. For example F(2,12,0.95)=3.885294F_{(2, 12, 0.95)} = 3.885294.

p-value. p=P(Fk−1,N−k>F)p = P(F_{k-1, N-k} > F), the upper tail only. Small values of FF are not evidence against H0H_0, so there is no doubling.

Sensitivity and power. The test reacts to any spread of the means. Under H1H_1, FF has the noncentral F law with noncentrality λ=∑ini(μi−μˉ)2/σ2\lambda = \sum_i n_i(\mu_i - \bar\mu)^2/\sigma^2, and the power depends on the means only through λ\lambda, increasing in it. For three groups of 5 with means 0,0.5,10, 0.5, 1 and σ=1\sigma = 1, λ=2.5\lambda = 2.5 and the measured power is 0.2255 (4000 replications, seed 8). The test spreads its power over all directions. If one ordered pattern or one contrast is of interest, a test of that contrast has more power. The F-test is uniformly most powerful among tests that are invariant under the natural group of transformations (Lehmann and Romano, Chapter 7).

The algorithm

  1. ONE-WAY-ANOVA(groups y[1..k]) ▷ k ≥ 2, N > k
  2. 1N ← n[1] + ... + n[k]
  3. 2for i ← 1 to k: ȳ[i] ← (y[i][1] + ... + y[i][n[i]]) / n[i]
  4. 3ȳ ← (n[1]·ȳ[1] + ... + n[k]·ȳ[k]) / N
  5. 4SSB ← Σ_i n[i]·(ȳ[i] - ȳ)²
  6. 5SSW ← Σ_i Σ_j (y[i][j] - ȳ[i])² ▷ second pass, group mean subtracted first
  7. 6F ← (SSB / (k - 1)) / (SSW / (N - k))
  8. 7p ← F̄(F; k - 1, N - k) ▷ the upper tail of F
  9. 8return (F, k - 1, N - k, p)

Fˉ(x;a,b)=Ib/(b+ax)(b/2,a/2)\bar F(x; a, b) = I_{b/(b + ax)}(b/2, a/2) is the upper tail of the F law, where II is the regularized incomplete beta function. The shared helper evaluates it by its continued fraction, so a small p-value keeps its digits. If every group is constant, SSW=0SSW = 0 and the code raises an error.

Example

Three groups, k=3k = 3, N=15N = 15:

group data nin_i yˉi\bar y_i ∑j(yij−yˉi)2\sum_j (y_{ij} - \bar y_i)^2
A 5, 7, 6, 8 4 6.5 5
B 9, 11, 10, 12, 8 5 10 10
C 6, 8, 7, 5, 9, 7 6 7 10
line value
L3: yˉ=118/15\bar y = 118/15 7.86677.8667
L4: SSB=4(6.5−yˉ)2+5(10−yˉ)2+6(7−yˉ)2=521/15SSB = 4(6.5 - \bar y)^2 + 5(10 - \bar y)^2 + 6(7 - \bar y)^2 = 521/15 34.733334.7333
L5: SSW=5+10+10SSW = 5 + 10 + 10 2525
check: SST=896/15SST = 896/15 59.7333=34.7333+2559.7333 = 34.7333 + 25
L6: MSB=34.7333/2MSB = 34.7333/2, MSW=25/12MSW = 25/12 17.366717.3667, 2.08332.0833
L6: F=1042/125F = 1042/125 8.3368.336
L7: p=P(F2,12>8.336)p = P(F_{2,12} > 8.336) 0.00537450.0053745

F=8.336>3.885F = 8.336 > 3.885, so the test rejects equal means at the 5% level. It does not say which groups differ; that is the job of Tukey's test or Scheffé's method. Welch's version on the same data gives F∗=7.1104F^* = 7.1104 on (2,7.4903)(2, 7.4903) degrees of freedom and p=0.01858p = 0.01858.

Two groups. Groups A and B alone give F=343/27=12.7037F = 343/27 = 12.7037 on (1,7)(1, 7) degrees of freedom and the pooled t statistic t=−3.5642t = -3.5642, with t2=12.7037t^2 = 12.7037 exactly and the same p-value 0.009167.

Size and power by simulation. Monte Carlo with r=4000r = 4000 replications at α=0.05\alpha = 0.05 (standard error of a size near 0.05: 0.0034):

data test measured rejection rate seed
normal, sizes 5, 5, 5, equal means and variances F 0.0510 1
normal, sizes 4, 8, 16, σ=1,2,3\sigma = 1, 2, 3 F 0.0135 2
normal, sizes 4, 8, 16, σ=3,2,1\sigma = 3, 2, 1 F 0.2073 3
normal, sizes 8, 8, 8, σ=1,2,3\sigma = 1, 2, 3 F 0.0658 4
normal, sizes 4, 8, 16, σ=3,2,1\sigma = 3, 2, 1 Welch 0.0608 5
normal, sizes 4, 8, 16, σ=1,2,3\sigma = 1, 2, 3 Welch 0.0495 6
exponential, sizes 5, 5, 5, equal means F 0.0420 7
normal, sizes 5, 5, 5, means 0, 0.5, 1, σ=1\sigma = 1 F (power) 0.2255 8

The F-test keeps its level under skewed errors with equal sizes. With unequal variances and unequal sizes it fails: when the small groups are the noisy ones, MSWMSW is dominated by the quiet large group and the size is 0.21. Welch's test stays near 0.05 in both directions.

Correctness

Let P1P_1 be the projection onto the span of the group indicator vectors and P0P_0 the projection onto the constant vector. Then P1yP_1 y holds the group means, P0yP_0 y the grand mean, SSB=∥(P1−P0)y∥2SSB = \|(P_1 - P_0)y\|^2 and SSW=∥(I−P1)y∥2SSW = \|(I - P_1)y\|^2. The two projections have orthogonal ranges of dimensions k−1k - 1 and N−kN - k. Rotating the data into an orthonormal basis adapted to these ranges gives independent normal coordinates, so SSB/σ2SSB/\sigma^2 and SSW/σ2SSW/\sigma^2 are independent chi-square variables, central for SSWSSW always and for SSBSSB under H0H_0. Their ratio, each divided by its degrees of freedom, is Fk−1,N−kF_{k-1,N-k} by definition. With two groups, SSB=n1n2(yˉ1−yˉ2)2/NSSB = n_1n_2(\bar y_1 - \bar y_2)^2/N, which gives F=t2F = t^2. The full proofs are in the correctness proof below.

Proof: The one-way ANOVA statistic has the F distribution with (k - 1, N - k) degrees of freedom under the normal nullfull

We prove that the total sum of squares splits into the between and within parts, that the two parts are independent scaled chi-square variables under the normal model, that the F statistic computed by the code has the Fk−1,N−kF_{k-1, N-k} law under H0H_0 for every sample size, and that with two groups it is the square of the pooled two-sample t statistic. The data are yijy_{ij} for groups i=1,…,ki = 1, \dots, k and units j=1,…,nij = 1, \dots, n_i, with N=∑ni>kN = \sum n_i > k. Write yˉi\bar y_i for the group means, yˉ=1N∑i,jyij\bar y = \frac1N\sum_{i,j} y_{ij} for the grand mean, SSB=∑ini(yˉi−yˉ)2SSB = \sum_i n_i(\bar y_i - \bar y)^2, SSW=∑i,j(yij−yˉi)2SSW = \sum_{i,j}(y_{ij} - \bar y_i)^2 and SST=∑i,j(yij−yˉ)2SST = \sum_{i,j}(y_{ij} - \bar y)^2. The model is yij=μi+eijy_{ij} = \mu_i + e_{ij} with the eije_{ij} iid N(0,σ2)N(0, \sigma^2).

Lemma 1 (the decomposition). SST=SSB+SSWSST = SSB + SSW. In vector form, let V0⊂RNV_0 \subset \mathbb R^N be the span of the all-ones vector and V1V_1 the span of the kk group indicator vectors, with orthogonal projections P0P_0 and P1P_1. Then P1yP_1 y is the vector that holds yˉi\bar y_i at every unit of group ii, P0y=yˉ 1P_0 y = \bar y\,\mathbf 1, SSB=∥(P1−P0)y∥2SSB = \|(P_1 - P_0)y\|^2, SSW=∥(I−P1)y∥2SSW = \|(I - P_1)y\|^2, and P1−P0P_1 - P_0 and I−P1I - P_1 are orthogonal projections onto orthogonal subspaces of dimensions k−1k - 1 and N−kN - k.

Proof. Write yij−yˉ=(yij−yˉi)+(yˉi−yˉ)y_{ij} - \bar y = (y_{ij} - \bar y_i) + (\bar y_i - \bar y) and square. The cross term is 2∑i(yˉi−yˉ)∑j(yij−yˉi)=02\sum_i(\bar y_i - \bar y)\sum_j(y_{ij} - \bar y_i) = 0, because the inner sum is niyˉi−niyˉi=0n_i\bar y_i - n_i\bar y_i = 0. This gives SST=SSW+SSBSST = SSW + SSB. For the vector form: the group indicators are orthogonal, the ii-th has squared norm nin_i, so projecting yy on V1V_1 gives the coefficient ⟨y,1i⟩/ni=yˉi\langle y, \mathbf 1_i\rangle/n_i = \bar y_i on each indicator. In the same way P0y=yˉ1P_0 y = \bar y\mathbf 1. Because V0⊂V1V_0 \subset V_1, we have P1P0=P0P1=P0P_1P_0 = P_0P_1 = P_0, so (P1−P0)2=P1−P0(P_1 - P_0)^2 = P_1 - P_0 and (P1−P0)⊤=P1−P0(P_1 - P_0)^\top = P_1 - P_0: it is the orthogonal projection onto V1⊖V0V_1 \ominus V_0, of dimension k−1k - 1. I−P1I - P_1 projects onto V1⊥V_1^\perp, of dimension N−kN - k, and (P1−P0)(I−P1)=P1−P1−P0+P0=0(P_1 - P_0)(I - P_1) = P_1 - P_1 - P_0 + P_0 = 0. The squared norms are the two sums by the coordinate formulas above. □\square

Lemma 2 (projections of a normal vector). Let Y∼N(μ,σ2IN)Y \sim N(\mu, \sigma^2 I_N) and let PP and QQ be orthogonal projections with PQ=0PQ = 0, of ranks rr and ss. Then ∥PY∥2/σ2\|PY\|^2/\sigma^2 and ∥QY∥2/σ2\|QY\|^2/\sigma^2 are independent, with noncentral chi-square laws on rr and ss degrees of freedom and noncentralities ∥Pμ∥2/σ2\|P\mu\|^2/\sigma^2 and ∥Qμ∥2/σ2\|Q\mu\|^2/\sigma^2. If Pμ=0P\mu = 0, the first is central χr2\chi^2_r.

Proof. The ranges of PP and QQ are orthogonal, because ⟨Pu,Qv⟩=⟨u,PQv⟩=0\langle Pu, Qv\rangle = \langle u, PQv\rangle = 0. Choose an orthonormal basis h1,…,hNh_1, \dots, h_N of RN\mathbb R^N whose first rr vectors span the range of PP and whose next ss vectors span the range of QQ. Let HH have these rows and set U=HYU = HY. HH is orthogonal, so U∼N(Hμ,σ2IN)U \sim N(H\mu, \sigma^2 I_N): its coordinates are independent normal variables with variance σ2\sigma^2. Now ∥PY∥2=∑l≤rUl2\|PY\|^2 = \sum_{l \le r}U_l^2 and ∥QY∥2=∑r<l≤r+sUl2\|QY\|^2 = \sum_{r < l \le r+s}U_l^2 are functions of disjoint sets of independent coordinates, so they are independent. A sum of rr squares of independent N(ml,σ2)N(m_l, \sigma^2) variables, divided by σ2\sigma^2, is noncentral χr2\chi^2_r with noncentrality ∑ml2/σ2\sum m_l^2/\sigma^2, and here ∑l≤rml2=∥Pμ∥2\sum_{l \le r}m_l^2 = \|P\mu\|^2. When the noncentrality is 0 the law is central. □\square

This is the projection form of Cochran's theorem. The hat matrix of the one-way model is P1P_1.

Lemma 3 (the null law). Under the model, SSW/σ2∼χN−k2SSW/\sigma^2 \sim \chi^2_{N-k} for every μ\mu, and SSBSSB is independent of SSWSSW with SSB/σ2SSB/\sigma^2 noncentral χk−12\chi^2_{k-1} with noncentrality λ=∑ini(μi−μˉ)2/σ2\lambda = \sum_i n_i(\mu_i - \bar\mu)^2/\sigma^2, where μˉ=∑niμi/N\bar\mu = \sum n_i\mu_i/N. Under H0:μ1=⋯=μkH_0: \mu_1 = \dots = \mu_k, λ=0\lambda = 0 and

F=SSB/(k−1)SSW/(N−k)∼Fk−1,N−k.F = \frac{SSB/(k - 1)}{SSW/(N - k)} \sim F_{k-1, N-k}.

Proof. The mean vector μ\mu lies in V1V_1, so (I−P1)μ=0(I - P_1)\mu = 0, and (P1−P0)μ(P_1 - P_0)\mu holds μi−μˉ\mu_i - \bar\mu at the units of group ii. Lemma 2 with P=P1−P0P = P_1 - P_0 and Q=I−P1Q = I - P_1 (Lemma 1) gives the two laws and the independence. Under H0H_0, μ∈V0\mu \in V_0, so λ=0\lambda = 0. The ratio of two independent chi-square variables, each divided by its degrees of freedom, is FF by definition, and σ2\sigma^2 cancels. □\square

Lemma 4 (the code computes FF). Line L1 sets N=∑niN = \sum n_i. L2 sets yˉi\bar y_i. L3 sets ∑niyˉi/N\sum n_i\bar y_i/N, which is yˉ\bar y because niyˉin_i\bar y_i is the sum of group ii. L4 and L5 compute SSBSSB and SSWSSW by their definitions, the second with the group mean subtracted before squaring. L6 forms FF with the degrees of freedom k−1k - 1 and N−kN - k, and L7 returns p=P(Fk−1,N−k>F)p = P(F_{k-1, N-k} > F).

Proof. Each line is the formula it names, evaluated with exact summation of the terms up to floating point rounding (math.fsum). The code rejects N≤kN \le k and SSW=0SSW = 0, where FF is not defined. □\square

Lemma 5 (size). Under H0H_0 the p-value of L7 is uniform on [0,1][0, 1], so the test "reject when p≤αp \le \alpha", which is the test "reject when F≥F(k−1,N−k,1−α)F \ge F_{(k-1, N-k, 1-\alpha)}", has size exactly α\alpha. The power is P(F′>F(k−1,N−k,1−α))P(F' > F_{(k-1, N-k, 1-\alpha)}) for a noncentral F′F' with noncentrality λ\lambda, which increases with λ\lambda.

Proof. FF has a continuous law with distribution function GG under H0H_0 (Lemma 3), and p=1−G(F)p = 1 - G(F). The probability integral transform makes G(F)G(F) uniform. The two rules are the same event because 1−G1 - G is strictly decreasing. The power formula is Lemma 3 under H1H_1. A noncentral chi-square variable with noncentrality λ\lambda is stochastically increasing in λ\lambda: it is (Z+λ)2+W(Z + \sqrt\lambda)^2 + W with Z∼N(0,1)Z \sim N(0,1) and W∼χr−12W \sim \chi^2_{r-1} independent, and P(∣Z+λ∣>c)P(|Z + \sqrt\lambda| > c) increases with λ≥0\lambda \ge 0 for each c>0c > 0, because the normal density is symmetric and unimodal. □\square

Lemma 6 (F=t2F = t^2 for two groups). If k=2k = 2, then F=T2F = T^2, where T=(yˉ1−yˉ2)/(Sp1/n1+1/n2)T = (\bar y_1 - \bar y_2)/(S_p\sqrt{1/n_1 + 1/n_2}) is the pooled Student t statistic with Sp2=SSW/(N−2)S_p^2 = SSW/(N - 2). The p-values of the two-sided t-test and of the F-test are equal.

Proof. Let d=yˉ1−yˉ2d = \bar y_1 - \bar y_2 and N=n1+n2N = n_1 + n_2. Then yˉ=(n1yˉ1+n2yˉ2)/N\bar y = (n_1\bar y_1 + n_2\bar y_2)/N, so yˉ1−yˉ=n2d/N\bar y_1 - \bar y = n_2 d/N and yˉ2−yˉ=−n1d/N\bar y_2 - \bar y = -n_1 d/N. So SSB=n1n22d2/N2+n2n12d2/N2=n1n2d2/NSSB = n_1n_2^2d^2/N^2 + n_2n_1^2d^2/N^2 = n_1n_2d^2/N. With k−1=1k - 1 = 1 and SSW/(N−2)=Sp2SSW/(N - 2) = S_p^2,

F=n1n2d2NSp2=d2Sp2(1/n1+1/n2)=T2,F = \frac{n_1n_2d^2}{N S_p^2} = \frac{d^2}{S_p^2(1/n_1 + 1/n_2)} = T^2,

because 1/n1+1/n2=N/(n1n2)1/n_1 + 1/n_2 = N/(n_1n_2). If T∼tνT \sim t_\nu then T2∼F1,νT^2 \sim F_{1, \nu}, and P(F1,ν>t2)=P(∣T∣>∣t∣)P(F_{1,\nu} > t^2) = P(|T| > |t|), which is the two-sided p-value. □\square

Termination. The code makes one pass to count, one pass for the group sums, one pass over the kk means, and one pass for the within-group squares, so it stops after O(N)O(N) arithmetic operations. The F tail is the incomplete beta continued fraction of the shared helper, which stops at a relative change below 10−1610^{-16} or after a fixed cap of iterations.

Theorem. For k≥2k \ge 2 groups of normal data with a common variance and N>kN > k, the one-way ANOVA code returns F=SSB/(k−1)SSW/(N−k)F = \frac{SSB/(k-1)}{SSW/(N-k)}, with SST=SSB+SSWSST = SSB + SSW, and F∼Fk−1,N−kF \sim F_{k-1, N-k} under H0:μ1=⋯=μkH_0: \mu_1 = \dots = \mu_k for every set of sample sizes, so the test has size exactly α\alpha. With two groups FF is the square of the pooled t statistic and the two tests agree.

Proof. Lemmas 1 to 6. ■\blacksquare

The Welch variant in the same file is not covered by this theorem. Its FF law with estimated degrees of freedom is an approximation, and its size on the page is measured by simulation, not proved.

Complexity

On a real RAM, L2 makes one pass over the NN values and L5 a second pass; L3 and L4 cost O(k)O(k). So the time is Θ(N)\Theta(N), about 5N5N arithmetic operations, and the extra space is Θ(k)\Theta(k) for the group means. The F tail costs a number of continued fraction steps that does not grow with NN.

Numerical stability. The textbook shortcut SSW=∑yij2−∑iniyˉi2SSW = \sum y_{ij}^2 - \sum_i n_i\bar y_i^2 subtracts two nearly equal numbers when the data are large compared with their spread, which is catastrophic cancellation. Add 10810^8 to every value of the example: the shortcut gives F=6.0F = 6.0 in double precision instead of 8.336, and with 10910^9 it gives SSW=0SSW = 0 and a division by zero. L5 subtracts the group mean first and returns F=8.336F = 8.336 in both cases. For data that arrive one at a time, the Welford algorithm gives the same accuracy in one pass. L7 computes the upper tail directly, not as one minus the distribution function.

Properties

  • Exact under the model. Size α\alpha for every set of sample sizes, under normal errors with equal variances.
  • Invariant. FF does not change when all data are shifted by a constant or multiplied by a nonzero constant.
  • Two groups. F=t2F = t^2, so the test is the two-sided pooled t-test.
  • Robust to non-normality, not to unequal variances. With equal or nearly equal group sizes the size stays near α\alpha for moderate non-normality. With unequal sizes and unequal variances it can be far from α\alpha in either direction (table above).
  • Global. A rejection says that the means are not all equal. It does not say which differ, and the follow-up comparisons need a family-wise error rate control.
  • Related to Scheffé. (k−1)F(k - 1)F is the largest squared standardized contrast of the group means, so the F-test rejects exactly when some contrast is significant by Scheffé's method.

Variants and improvements

  • Welch's test. Welch (1951) weights group ii by wi=ni/si2w_i = n_i/s_i^2 and refers F∗=∑wi(yˉi−yˉw)2/(k−1)1+2(k−2)k2−1ΛF^* = \frac{\sum w_i(\bar y_i - \bar y_w)^2/(k-1)}{1 + \frac{2(k-2)}{k^2-1}\Lambda}, with Λ=∑i(1−wi/∑w)2/(ni−1)\Lambda = \sum_i (1 - w_i/\sum w)^2/(n_i - 1), to Fk−1,(k2−1)/(3Λ)F_{k-1, (k^2-1)/(3\Lambda)}. Its law is approximate, but its size stays near α\alpha with unequal variances. With two groups it is the square of the Welch t statistic.
  • Rank test. The Kruskal-Wallis test uses the ranks of the pooled data and has an exact null law for continuous data without normality.
  • Permutation test. Refer FF to its distribution over the permutations of the group labels, a permutation test, which is exact under exchangeability.
  • Blocks and more factors. Two-way ANOVA adds a second factor, and the randomized block design removes the variation between blocks from the error.
  • Random effects. When the groups are a random sample of possible groups, the model yij=μ+ai+eijy_{ij} = \mu + a_i + e_{ij} with ai∼N(0,σa2)a_i \sim N(0, \sigma_a^2) is a linear mixed model. The same F statistic tests σa2=0\sigma_a^2 = 0, and the variance components are estimated by restricted maximum likelihood or by equating mean squares to their expectations.
  • Effect size. η2=SSB/SST\eta^2 = SSB/SST is the fraction of the variation explained by the groups, 0.581 in the example. See effect size.

When to use it

Use the F-test as the first test for k≥3k \ge 3 independent groups when the errors are close to normal and the variances are close to equal, or when the group sizes are equal. Use Welch's test when the variances may differ and the sizes are unequal; many statisticians use it as the default. Use the Kruskal-Wallis test for small samples with clearly non-normal or outlying data. Plan the follow-up comparisons before the data: Tukey's test for all pairs, Scheffé's method for any contrasts chosen after looking at the data, and the Bonferroni correction for a few planned contrasts.

Pitfalls

  • Unequal variances with unequal sizes. The size can be 0.21 or 0.014 instead of 0.05 (table above).
  • Many t-tests instead of one ANOVA. Testing every pair at level α\alpha makes the chance of at least one false rejection grow with kk; this is the multiple comparisons problem.
  • Reading a rejection as "all means differ". The alternative is only that some means differ.
  • Using the shortcut formula. The one-pass formula for SSWSSW cancels catastrophically for large, nearly equal values.
  • Dependent observations. Repeated measures on the same subject, or plots in the same field block, break the independence of the errors. Use a randomized block design analysis or a linear mixed model.
  • A non-significant F as proof of equal means. It shows only that the data cannot rule out equal means; check the statistical power.

Implementations

Each implementation follows the pseudocode above. A comment @line links a line of code to a line of the pseudocode. Point at a line of the pseudocode to see the matching code. The build runs the native tests of each language.

one_way_anova.pyI0RawGitHub
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/one-way-anova/. The authors are listed in prov/QHQ1W6.jsonl.
"""One-way analysis of variance, and Welch's heteroscedastic version.

one_way_anova splits the total sum of squares into the part between the group
means and the part within the groups, and refers the ratio of their mean
squares to the F distribution with (k - 1, N - k) degrees of freedom. Each
group sum of squares uses the two-pass formula (subtract the group mean
first), which does not cancel like sum y^2 - n ybar^2. The p-value comes from
the upper tail f_sf directly, not from 1 - cdf.

welch_anova is Welch's (1951) test, which weights each group by n_i / s_i^2
and does not assume equal variances. With two groups its statistic is the
square of the Welch t statistic.
"""

import math
from collections.abc import Sequence
from typing import NamedTuple

from stdist import f_sf


class AnovaResult(NamedTuple):
    f: float          # the F statistic
    df1: int          # k - 1
    df2: int          # N - k
    p: float          # P(F_(df1, df2) > f)
    ssb: float        # sum of squares between groups
    ssw: float        # sum of squares within groups
    means: list[float]


def one_way_anova(groups: Sequence[Sequence[float]]) -> AnovaResult:
    """Test H_0: all k group means are equal, for normal groups with a common variance."""
    k = len(groups)
    if k < 2 or any(len(g) == 0 for g in groups):
        raise ValueError("need at least two nonempty groups")
    n = [len(g) for g in groups]
    big_n = sum(n)  # @line L1
    if big_n <= k:
        raise ValueError("need N > k, so that some group has two or more values")
    means = [math.fsum(g) / len(g) for g in groups]  # @line L2
    grand = math.fsum(ni * m for ni, m in zip(n, means)) / big_n  # @line L3
    ssb = math.fsum(ni * (m - grand) ** 2 for ni, m in zip(n, means))  # @line L4
    ssw = math.fsum((v - m) ** 2 for g, m in zip(groups, means) for v in g)  # @line L5
    if ssw == 0.0:
        raise ValueError("every group is constant, so the within mean square is 0")
    df1, df2 = k - 1, big_n - k
    f = (ssb / df1) / (ssw / df2)  # @line L6
    p = f_sf(f, df1, df2)  # @line L7
    return AnovaResult(f, df1, df2, p, ssb, ssw, means)  # @line L8


def welch_anova(groups: Sequence[Sequence[float]]) -> tuple[float, float, float, float]:
    """Welch's test of equal means without equal variances: (F*, df1, df2, p)."""
    k = len(groups)
    if k < 2 or any(len(g) < 2 for g in groups):
        raise ValueError("need at least two groups of two or more values")
    n = [len(g) for g in groups]
    means = [math.fsum(g) / len(g) for g in groups]
    var = [math.fsum((v - m) ** 2 for v in g) / (len(g) - 1) for g, m in zip(groups, means)]
    if any(v == 0.0 for v in var):
        raise ValueError("a group is constant, so its weight n / s^2 is infinite")
    w = [ni / v for ni, v in zip(n, var)]
    total_w = math.fsum(w)
    mw = math.fsum(wi * m for wi, m in zip(w, means)) / total_w
    a = math.fsum(wi * (m - mw) ** 2 for wi, m in zip(w, means)) / (k - 1)
    lam = math.fsum((1.0 - wi / total_w) ** 2 / (ni - 1) for wi, ni in zip(w, n))
    f = a / (1.0 + 2.0 * (k - 2) / (k * k - 1) * lam)
    df2 = (k * k - 1) / (3.0 * lam)
    return f, k - 1, df2, f_sf(f, k - 1, df2)
stdist.py 238 lines
# SPDX-License-Identifier: MIT
# Shared helper of the pe1 statistics pages of https://algorithm.tamnd.com/. The authors are listed in prov/.
"""Normal, chi-square, t and F distribution functions in the standard library.

The incomplete gamma and beta functions return a lower tail and an upper
tail, and each tail is computed directly where it is small, so an upper tail
such as chi2_sf(200, 3) does not come from 1 - cdf and does not cancel.
Quantiles use a safeguarded bisection on the CDF (lower half) or on the upper
tail (upper half); the bracket shrinks until floating point stops it, so the
probability error is that of the CDF, below 1e-12 for moderate parameters.
"""

import math
from statistics import NormalDist

_STD = NormalDist()
_EPS = 1e-16
_TINY = 1e-300
_MAXIT = 10000


def norm_cdf(x: float) -> float:
    """P(Z <= x) for a standard normal Z."""
    return 0.5 * math.erfc(-x / math.sqrt(2.0))


def norm_sf(x: float) -> float:
    """P(Z > x), computed with erfc, so it is accurate for large x."""
    return 0.5 * math.erfc(x / math.sqrt(2.0))


def norm_ppf(p: float) -> float:
    """The lower p-quantile z_p of the standard normal, 0 < p < 1."""
    if not 0.0 < p < 1.0:
        raise ValueError("p must be in (0, 1)")
    return _STD.inv_cdf(p)


def _gamma_tails(a: float, x: float) -> tuple[float, float]:
    """(P(a, x), Q(a, x)), the regularized lower and upper incomplete gamma."""
    if a <= 0.0 or x < 0.0:
        raise ValueError("need a > 0 and x >= 0")
    if x == 0.0:
        return 0.0, 1.0
    if math.isinf(x):
        return 1.0, 0.0
    front = math.exp(-x + a * math.log(x) - math.lgamma(a))
    if x < a + 1.0:
        # Series: P = front * sum_{j >= 0} x^j / (a (a + 1) ... (a + j)).
        ap, term = a, 1.0 / a
        total = term
        for _ in range(_MAXIT):
            ap += 1.0
            term *= x / ap
            total += term
            if abs(term) < abs(total) * _EPS:
                break
        p = front * total
        return p, 1.0 - p
    # Continued fraction for Q, modified Lentz.
    b = x + 1.0 - a
    c = 1.0 / _TINY
    d = 1.0 / b
    h = d
    for i in range(1, _MAXIT):
        an = -i * (i - a)
        b += 2.0
        d = an * d + b
        if abs(d) < _TINY:
            d = _TINY
        c = b + an / c
        if abs(c) < _TINY:
            c = _TINY
        d = 1.0 / d
        step = d * c
        h *= step
        if abs(step - 1.0) < _EPS:
            break
    q = front * h
    return 1.0 - q, q


def gamma_p(a: float, x: float) -> float:
    """Regularized lower incomplete gamma P(a, x)."""
    return _gamma_tails(a, x)[0]


def gamma_q(a: float, x: float) -> float:
    """Regularized upper incomplete gamma Q(a, x) = 1 - P(a, x)."""
    return _gamma_tails(a, x)[1]


def _beta_cf(a: float, b: float, x: float) -> float:
    """The continued fraction of I_x(a, b), modified Lentz."""
    qab, qap, qam = a + b, a + 1.0, a - 1.0
    c = 1.0
    d = 1.0 - qab * x / qap
    if abs(d) < _TINY:
        d = _TINY
    d = 1.0 / d
    h = d
    for m in range(1, _MAXIT):
        m2 = 2 * m
        aa = m * (b - m) * x / ((qam + m2) * (a + m2))
        d = 1.0 + aa * d
        if abs(d) < _TINY:
            d = _TINY
        c = 1.0 + aa / c
        if abs(c) < _TINY:
            c = _TINY
        d = 1.0 / d
        h *= d * c
        aa = -(a + m) * (qab + m) * x / ((a + m2) * (qap + m2))
        d = 1.0 + aa * d
        if abs(d) < _TINY:
            d = _TINY
        c = 1.0 + aa / c
        if abs(c) < _TINY:
            c = _TINY
        d = 1.0 / d
        step = d * c
        h *= step
        if abs(step - 1.0) < _EPS:
            break
    return h


def _beta_tails(a: float, b: float, x: float, y: float | None = None) -> tuple[float, float]:
    """(I_x(a, b), 1 - I_x(a, b)), each computed directly where it is small.

    y is 1 - x; pass it when it is known more accurately than 1 - x.
    """
    if a <= 0.0 or b <= 0.0:
        raise ValueError("need a > 0 and b > 0")
    if y is None:
        y = 1.0 - x
    if x <= 0.0:
        return 0.0, 1.0
    if y <= 0.0:
        return 1.0, 0.0
    front = math.exp(
        math.lgamma(a + b) - math.lgamma(a) - math.lgamma(b)
        + a * math.log(x) + b * math.log(y)
    )
    if x < (a + 1.0) / (a + b + 2.0):
        low = front * _beta_cf(a, b, x) / a
        return low, 1.0 - low
    # Symmetry: I_x(a, b) = 1 - I_(1-x)(b, a).
    up = front * _beta_cf(b, a, y) / b
    return 1.0 - up, up


def beta_inc(a: float, b: float, x: float) -> float:
    """Regularized incomplete beta function I_x(a, b)."""
    return _beta_tails(a, b, x)[0]


def chi2_cdf(x: float, k: float) -> float:
    """P(X <= x) for X chi-square with k > 0 degrees of freedom."""
    return 0.0 if x <= 0.0 else gamma_p(k / 2.0, x / 2.0)


def chi2_sf(x: float, k: float) -> float:
    """P(X > x) for X chi-square with k degrees of freedom, without 1 - cdf."""
    return 1.0 if x <= 0.0 else gamma_q(k / 2.0, x / 2.0)


def t_sf(x: float, k: float) -> float:
    """P(T > x) for T Student t with k > 0 degrees of freedom."""
    if math.isfinite(x):
        s = k + x * x
        half = 0.5 * _beta_tails(k / 2.0, 0.5, k / s, x * x / s)[0]  # P(|T| > |x|) / 2
    else:
        half = 0.0
    return half if x >= 0.0 else 1.0 - half


def t_cdf(x: float, k: float) -> float:
    """P(T <= x) for T Student t with k degrees of freedom."""
    return t_sf(-x, k)


def f_cdf(x: float, k1: float, k2: float) -> float:
    """P(F <= x) for F with (k1, k2) degrees of freedom."""
    if x <= 0.0:
        return 0.0
    return _beta_tails(k2 / 2.0, k1 / 2.0, k2 / (k2 + k1 * x), k1 * x / (k2 + k1 * x))[1]


def f_sf(x: float, k1: float, k2: float) -> float:
    """P(F > x) = I_(k2 / (k2 + k1 x))(k2 / 2, k1 / 2)."""
    if x <= 0.0:
        return 1.0
    return _beta_tails(k2 / 2.0, k1 / 2.0, k2 / (k2 + k1 * x), k1 * x / (k2 + k1 * x))[0]


def _quantile(cdf, sf, p: float, lo: float, hi: float) -> float:
    """The x with cdf(x) = p, by bisection on cdf (p <= 1/2) or on sf (p > 1/2).

    lo must satisfy cdf(lo) <= p; hi is doubled until cdf(hi) >= p.
    """
    if not 0.0 < p < 1.0:
        raise ValueError("p must be in (0, 1)")
    if p <= 0.5:
        g = lambda x: cdf(x) - p  # increasing in x
    else:
        q = 1.0 - p
        g = lambda x: q - sf(x)  # increasing in x, and no cancellation in sf
    while g(hi) < 0.0:
        lo, hi = hi, 2.0 * hi if hi > 0.0 else 1.0
    for _ in range(400):
        mid = 0.5 * (lo + hi)
        if mid <= lo or mid >= hi:
            break
        if g(mid) < 0.0:
            lo = mid
        else:
            hi = mid
    return 0.5 * (lo + hi)


def chi2_ppf(p: float, k: float) -> float:
    """The lower p-quantile chi^2_(k, p)."""
    return _quantile(lambda x: chi2_cdf(x, k), lambda x: chi2_sf(x, k), p, 0.0, max(1.0, k))


def t_ppf(p: float, k: float) -> float:
    """The lower p-quantile t_(k, p)."""
    if p == 0.5:
        return 0.0
    if p < 0.5:
        return -t_ppf(1.0 - p, k)
    return _quantile(lambda x: t_cdf(x, k), lambda x: t_sf(x, k), p, 0.0, 1.0)


def f_ppf(p: float, k1: float, k2: float) -> float:
    """The lower p-quantile F_(k1, k2, p)."""
    return _quantile(lambda x: f_cdf(x, k1, k2), lambda x: f_sf(x, k1, k2), p, 0.0, 1.0)
test_one_way_anova.py 120 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/one-way-anova/. The authors are listed in prov/QHQ1W6.jsonl.

import math
import random
from fractions import Fraction

import pytest

from one_way_anova import one_way_anova, welch_anova
from stdist import f_ppf, t_sf

EXAMPLE = [[5, 7, 6, 8], [9, 11, 10, 12, 8], [6, 8, 7, 5, 9, 7]]


def exact_sums(groups: list[list[int]]) -> tuple[Fraction, Fraction, Fraction]:
    """SSB, SSW and SST from their definitions, in exact arithmetic."""
    big_n = sum(len(g) for g in groups)
    grand = Fraction(sum(sum(g) for g in groups), big_n)
    means = [Fraction(sum(g), len(g)) for g in groups]
    ssb = sum(len(g) * (m - grand) ** 2 for g, m in zip(groups, means))
    ssw = sum((v - m) ** 2 for g, m in zip(groups, means) for v in g)
    sst = sum((v - grand) ** 2 for g in groups for v in g)
    return ssb, ssw, sst


def test_example() -> None:
    r = one_way_anova(EXAMPLE)
    ssb, ssw, sst = exact_sums(EXAMPLE)
    assert (ssb, ssw, sst) == (Fraction(521, 15), Fraction(25), Fraction(896, 15))
    assert ssb + ssw == sst
    assert (r.df1, r.df2) == (2, 12)
    assert r.means == [6.5, 10.0, 7.0]
    assert abs(r.ssb - 521 / 15) < 1e-12 and abs(r.ssw - 25.0) < 1e-12
    assert abs(r.f - 1042 / 125) < 1e-12
    assert abs(r.p - 0.005374518329892695) < 1e-12
    assert r.f > f_ppf(0.95, 2, 12)


def test_decomposition_against_fractions() -> None:
    rng = random.Random(1)
    for _ in range(200):
        k = rng.randint(2, 6)
        groups = [[rng.randint(-50, 50) for _ in range(rng.randint(1, 7))] for _ in range(k)]
        if sum(len(g) for g in groups) <= k:
            continue
        ssb, ssw, sst = exact_sums(groups)
        assert ssb + ssw == sst
        if ssw == 0:
            continue
        r = one_way_anova(groups)
        df2 = sum(len(g) for g in groups) - k
        f = (ssb / (k - 1)) / (ssw / df2)
        assert abs(r.ssb - float(ssb)) <= 1e-9 * max(1.0, float(sst))
        assert abs(r.ssw - float(ssw)) <= 1e-9 * max(1.0, float(sst))
        assert abs(r.f - float(f)) <= 1e-9 * max(1.0, float(f))


def test_two_groups_f_is_t_squared() -> None:
    rng = random.Random(2)
    for _ in range(100):
        x = [rng.randint(0, 30) for _ in range(rng.randint(1, 8))]
        y = [rng.randint(0, 30) for _ in range(rng.randint(2, 8))]
        mx, my = Fraction(sum(x), len(x)), Fraction(sum(y), len(y))
        ss = sum((v - mx) ** 2 for v in x) + sum((v - my) ** 2 for v in y)
        if ss == 0:
            continue
        sp2 = ss / (len(x) + len(y) - 2)
        t2 = (mx - my) ** 2 / (sp2 * (Fraction(1, len(x)) + Fraction(1, len(y))))
        assert exact_sums([x, y])[0] / (sp2) == t2  # SSB / MSE = t^2, exactly
        r = one_way_anova([x, y])
        assert abs(r.f - float(t2)) <= 1e-10 * max(1.0, float(t2))
        assert abs(r.p - 2.0 * t_sf(math.sqrt(r.f), len(x) + len(y) - 2)) < 1e-12


def test_shift_does_not_cancel() -> None:
    shifted = [[1e9 + v for v in g] for g in EXAMPLE]
    assert abs(one_way_anova(shifted).f - 8.336) < 1e-6


def test_size_under_the_null() -> None:
    rng = random.Random(3)
    reps, alpha = 3000, 0.05
    rej = sum(one_way_anova([[rng.gauss(0.0, 1.0) for _ in range(n)] for n in (4, 6, 9)]).p <= alpha
              for _ in range(reps))
    se = math.sqrt(alpha * (1 - alpha) / reps)
    assert abs(rej / reps - alpha) < 4.0 * se


def test_welch_two_groups_is_welch_t_squared() -> None:
    rng = random.Random(4)
    for _ in range(50):
        x = [rng.gauss(0.0, 1.0) for _ in range(rng.randint(2, 9))]
        y = [rng.gauss(0.5, 3.0) for _ in range(rng.randint(2, 9))]
        mx, my = sum(x) / len(x), sum(y) / len(y)
        a = sum((v - mx) ** 2 for v in x) / (len(x) - 1) / len(x)
        b = sum((v - my) ** 2 for v in y) / (len(y) - 1) / len(y)
        t2 = (mx - my) ** 2 / (a + b)
        df = (a + b) ** 2 / (a * a / (len(x) - 1) + b * b / (len(y) - 1))
        f, df1, df2, p = welch_anova([x, y])
        assert df1 == 1
        assert abs(f - t2) <= 1e-10 * t2 and abs(df2 - df) <= 1e-10 * df
        assert abs(p - 2.0 * t_sf(math.sqrt(t2), df)) < 1e-12


def test_welch_example() -> None:
    f, df1, df2, p = welch_anova(EXAMPLE)
    assert df1 == 2
    assert abs(f - 7.110393149302828) < 1e-9
    assert abs(df2 - 7.490339568443729) < 1e-9
    assert abs(p - 0.01858036702427885) < 1e-9


def test_errors() -> None:
    with pytest.raises(ValueError):
        one_way_anova([[1.0, 2.0]])
    with pytest.raises(ValueError):
        one_way_anova([[1.0], [2.0]])
    with pytest.raises(ValueError):
        one_way_anova([[1.0, 1.0], [2.0, 2.0]])
test_stdist.py 60 lines
# SPDX-License-Identifier: MIT
# Shared helper of the pe1 statistics pages of https://algorithm.tamnd.com/. The authors are listed in prov/.

import math

from stdist import (
    beta_inc, chi2_cdf, chi2_ppf, chi2_sf, f_cdf, f_ppf, f_sf, gamma_p, gamma_q,
    norm_cdf, norm_ppf, norm_sf, t_cdf, t_ppf, t_sf,
)

XS = [0.001, 0.01, 0.1, 0.3, 0.5, 1.0, 1.7, 2.5, 4.0, 7.0, 12.0, 30.0, 80.0]


def test_chi2_two_degrees_of_freedom() -> None:
    for x in XS:
        assert abs(chi2_cdf(x, 2) - (1.0 - math.exp(-x / 2.0))) < 1e-13
        assert abs(chi2_sf(x, 2) - math.exp(-x / 2.0)) <= 1e-12 * math.exp(-x / 2.0)


def test_t_one_and_two_degrees_of_freedom() -> None:
    for x in XS + [-v for v in XS] + [0.0]:
        assert abs(t_cdf(x, 1) - (0.5 + math.atan(x) / math.pi)) < 1e-13
        assert abs(t_cdf(x, 2) - (0.5 + x / (2.0 * math.sqrt(2.0 + x * x)))) < 1e-13
        assert abs(t_cdf(x, 3) + t_sf(x, 3) - 1.0) < 1e-14


def test_f_is_square_of_t() -> None:
    for k in [1, 2, 3, 5, 10, 30]:
        for x in XS:
            two_sided = 2.0 * t_sf(math.sqrt(x), k)
            assert abs(f_sf(x, 1, k) - two_sided) < 1e-13
            assert abs(f_cdf(x, 1, k) + f_sf(x, 1, k) - 1.0) < 1e-14


def test_gamma_and_beta_identities() -> None:
    for a in [0.5, 1.0, 2.5, 10.0, 50.0]:
        for x in XS:
            assert abs(gamma_p(a, x) + gamma_q(a, x) - 1.0) < 1e-14
    # P(1, x) = 1 - e^(-x); I_x(a, 1) = x^a; I_x(1, b) = 1 - (1 - x)^b.
    for x in [0.05, 0.3, 0.5, 0.8, 0.99]:
        assert abs(gamma_p(1.0, x) - (1.0 - math.exp(-x))) < 1e-14
        for a in [0.5, 2.0, 7.0]:
            assert abs(beta_inc(a, 1.0, x) - x**a) < 1e-13
            assert abs(beta_inc(1.0, a, x) - (1.0 - (1.0 - x) ** a)) < 1e-13


def test_upper_tails_do_not_cancel() -> None:
    assert abs(norm_sf(10.0) / 7.619853024160527e-24 - 1.0) < 1e-12
    assert abs(chi2_sf(200.0, 2) / math.exp(-100.0) - 1.0) < 1e-12
    assert norm_cdf(-30.0) > 0.0

def test_quantiles() -> None:
    assert abs(norm_ppf(0.975) - 1.959963985) < 1e-8
    assert abs(chi2_ppf(0.95, 1) - 3.841458821) < 1e-8
    assert abs(t_ppf(0.975, 10) - 2.228138852) < 1e-8
    for p in [1e-6, 0.01, 0.2, 0.5, 0.8, 0.99, 1.0 - 1e-6]:
        for k in [1, 4, 25]:
            assert abs(chi2_cdf(chi2_ppf(p, k), k) - p) < 1e-12
            assert abs(t_cdf(t_ppf(p, k), k) - p) < 1e-12
            assert abs(f_cdf(f_ppf(p, k, 7), k, 7) - p) < 1e-12

History

Ronald A. Fisher introduced the word "variance" in 1918 and developed the analysis of variance at Rothamsted Experimental Station for agricultural field trials. Statistical Methods for Research Workers (1925) presented the method with tables for z=12ln⁡(MSB/MSW)z = \frac12\ln(MSB/MSW). George W. Snedecor tabulated the variance ratio itself and named it F in honor of Fisher in 1934. Bernard L. Welch gave the test without equal variances in 1951.

References

  1. Ronald A. Fisher, Statistical Methods for Research Workers, Oliver and Boyd, 1925, online text of the first edition, Chapter VII (intraclass correlations and the analysis of variance)
  2. B. L. Welch, On the comparison of several mean values: an alternative approach, Biometrika 38(3-4), 1951, pp. 330 to 336 (the weighted statistic and its approximate F law with estimated denominator degrees of freedom)
  3. George Casella and Roger L. Berger, Statistical Inference, second edition, Duxbury, 2002, Section 11.2 (oneway analysis of variance, the F-test, contrasts and the partition of sums of squares)
  4. Douglas C. Montgomery, Design and Analysis of Experiments, tenth edition, Wiley, 2019, Chapter 3 (experiments with a single factor, the analysis of variance, model adequacy checking)
  5. George A. F. Seber and Alan J. Lee, Linear Regression Analysis, second edition, Wiley, 2003, Chapter 2 (quadratic forms of normal vectors, Cochran's theorem) and Chapter 8 (analysis of variance; chapter-level locators)
  6. E. L. Lehmann and Joseph P. Romano, Testing Statistical Hypotheses, third edition, Springer, 2005, Chapter 7 (linear hypotheses, the F-test as a UMP invariant test; chapter-level locator)

Where this leads

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH1 sketch
  • a full proof of time.best
  • a full proof of time.worst
  • a full proof of space.extra
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
Code (python)I0 not checked yet
  • the tests did not run in this build
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW2 basic
  • a scene preset for the example