Definitionadvanced
Balanced incomplete block design
Also called BIBD, 2-design.
Puts v treatments into b blocks of size k < v so that each pair of treatments shares exactly lambda blocks. Then bk = vr, lambda(v-1) = r(k-1), and b >= v (Fisher).
A balanced incomplete block design (BIBD) is used when a block cannot hold every treatment: a day allows three runs but there are seven catalysts, a judge can taste four wines out of thirteen. It places treatments in blocks of units so that every treatment appears in the same number of blocks and every pair of treatments appears together in the same number of blocks. Because all pairs are treated alike, all treatment differences are estimated with the same precision after the block effects are removed. In combinatorics the same object is a 2- design; with it is a Steiner system . Its parameters must satisfy two counting identities and Fisher's inequality .
Definition
Let be a set of treatments. A balanced incomplete block design with parameters is a list of subsets of , the blocks, such that
- every block has elements, ;
- every treatment lies in exactly blocks;
- every pair of distinct treatments lies together in exactly blocks.
The incidence matrix is the 0/1 matrix with when treatment is in block . In an experiment, the blocks are assigned to the actual blocks at random and the treatments to the units of each block at random.
Theorem 1 (the counting identities). and .
Proof. Count the pairs (treatment, block containing it). By blocks there are ; by treatments there are . For the second identity, fix a treatment and count the pairs with and . Each of the blocks that contain gives choices of ; each of the treatments gives blocks.
So and follow from : and , and both must be integers.
Theorem 2 (Fisher's inequality). .
Proof. The entry of counts the blocks that contain both and , so , where is the all-ones matrix. has the eigenvalue on the all-ones vector and 0 on its orthogonal complement, so has the eigenvalues once and with multiplicity , and
Since , Theorem 1 gives , so the determinant is positive. Then .
A design with (so ) is symmetric; in a symmetric design any two blocks meet in exactly treatments. The test file checks the two identities and the determinant formula in exact arithmetic for every design on this page.
The intrablock analysis. The model is with iid errors and . Let be the total of treatment , the total of block , and
the treatment total adjusted for the blocks it is in.
Theorem 3 (the estimates). The least squares estimates are , and for every pair .
Proof. The normal equation of block is . Solve it for and put that into the normal equation of treatment , . This gives with
using and . For , , so . is , where and are the treatment and block indicator matrices and is the projection onto the block indicators, so its covariance is . Then .
The adjusted treatment sum of squares is on degrees of freedom, the error sum of squares is what remains after the blocks (unadjusted) and the adjusted treatments, on degrees of freedom, and their ratio of mean squares has the exact F law under by the same projection argument as in the randomized block design. A complete block design with the same would give , so the efficiency factor is , the price of incomplete blocks when is the same. With random block effects, a linear mixed model recovers extra information from the block totals (Yates, 1940).
What is cited, not proved. The Bruck-Ryser-Chowla theorem gives further conditions for symmetric designs; for example, it rules out the projective plane of order 6, a symmetric design, although those parameters pass Theorems 1 and 2.
Examples and non-examples
Example: four treatments in blocks of three. All four 3-subsets of form a design, with . The responses:
| block | treatments | responses |
|---|---|---|
| 1 | 1, 2, 3 | 71, 76, 70 |
| 2 | 1, 2, 4 | 70, 77, 72 |
| 3 | 1, 3, 4 | 75, 73, 70 |
| 4 | 2, 3, 4 | 80, 77, 73 |
The treatment totals are , and the adjusted totals are , so ; the test file checks that these equal exact least squares.
| source | sum of squares | df | mean square | F | p |
|---|---|---|---|---|---|
| blocks (unadjusted) | 3 | ||||
| treatments (adjusted) | 3 | 22.69 | 0.0296 | ||
| error | 5 | 3.183 | |||
| total | 11 |
The treatments differ at the 5% level. Ignoring the blocks, the one-way F on the raw treatment means 72, 77.67, 73.33 and 71.67 is on , .
Example: the Fano plane. Developing the base block modulo 7 gives the seven blocks , a symmetric design: the projective plane of order 2. It works because the differences of , namely , cover every nonzero residue modulo 7 once. Any two blocks meet in exactly one treatment, and .
Example: Kirkman's schoolgirls. Fifteen girls walk in five rows of three on seven days, and no two girls walk in the same row twice. The 35 rows form a design whose blocks split into 7 parallel classes, one per day: a resolvable design, a Kirkman triple system. The code takes the 35 lines of the projective space and finds the days by backtracking:
| day | rows |
|---|---|
| 1 | 1 2 3, 4 8 12, 5 10 15, 6 11 13, 7 9 14 |
| 2 | 1 4 5, 2 8 10, 3 13 14, 6 9 15, 7 11 12 |
| 3 | 1 6 7, 2 9 11, 3 12 15, 4 10 14, 5 8 13 |
| 4 | 1 8 9, 2 12 14, 3 5 6, 4 11 15, 7 10 13 |
| 5 | 1 10 11, 2 13 15, 3 4 7, 5 9 12, 6 8 14 |
| 6 | 1 12 13, 2 4 6, 3 9 10, 5 11 14, 7 8 15 |
| 7 | 1 14 15, 2 5 7, 3 8 11, 4 9 13, 6 10 12 |
Example: designs from algebra. The quadratic residues modulo 11, developed cyclically, give a symmetric design (see quadratic residue); modulo 13 gives the projective plane of order 3, ; and the 12 lines of the affine plane over give , a resolvable design whose four parallel classes come from the rows, the columns and the two Latin squares of order 3.
Non-example: a cyclic block that is not a difference set. Developing modulo 7 gives seven blocks of size 3 with , but its differences are : pairs at distance 1 meet twice, pairs at distance 3 never. It is not balanced.
Non-example: parameters that pass the identities. gives the integers and , so Theorem 1 holds, but violates Fisher's inequality. No such design exists.
Related definitions
- Randomized block design: the complete case , where the treatment means need no adjustment.
- Latin square design: complete blocks in two directions; affine planes and finite fields give both Latin squares and BIBDs.
- Orthogonal array: strength-2 arrays balance pairs of levels, as BIBDs balance pairs of treatments.
- Hamming code: the seven codewords of weight 3 of the [7, 4] Hamming code are the lines of the Fano plane.
- Golay code: its codewords of weight 8 form the Steiner system , a 5-design.
- Exact cover: finding a design or a resolution, such as Kirkman's seven days, is an exact cover search.
Code
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.
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/balanced-incomplete-block-design/. The authors are listed in prov/JG5X5V.jsonl.
"""Balanced incomplete block designs: checking, construction and intrablock analysis.
A BIBD(v, b, r, k, lambda) has v treatments in b blocks of size k < v, each treatment in
r blocks and each pair of treatments together in lambda blocks. Then b k = v r,
lambda (v - 1) = r (k - 1) and b >= v (Fisher's inequality).
"""
import math
from collections.abc import Sequence
from itertools import combinations
Block = Sequence[int]
def parameters(v: int, blocks: Sequence[Block]) -> tuple[int, int, int, int, int]:
"""(v, b, r, k, lambda) if the blocks on treatments 0..v-1 form a BIBD, else ValueError."""
b = len(blocks)
sizes = {len(set(blk)) for blk in blocks} | {len(blk) for blk in blocks}
if len(sizes) != 1:
raise ValueError("blocks must have one size and no repeated treatment")
k = sizes.pop()
reps = [sum(t in blk for blk in blocks) for t in range(v)]
if len(set(reps)) != 1:
raise ValueError("treatments are not equally replicated")
pair = {}
for blk in blocks:
for x, y in combinations(sorted(blk), 2):
pair[x, y] = pair.get((x, y), 0) + 1
counts = {pair.get(xy, 0) for xy in combinations(range(v), 2)}
if len(counts) != 1:
raise ValueError("pairs do not occur together equally often")
return v, b, reps[0], k, counts.pop()
def develop(base_blocks: Sequence[Block], v: int) -> list[list[int]]:
"""Cyclic development: each base block B gives the blocks B + i (mod v), i = 0..v-1."""
return [sorted((x + i) % v for x in blk) for blk in base_blocks for i in range(v)]
def fano() -> list[list[int]]:
"""The Fano plane, a (7, 7, 3, 3, 1) design: the difference set {0, 1, 3} mod 7."""
return develop([[0, 1, 3]], 7)
def kirkman_15() -> list[list[list[int]]]:
"""A resolution of a (15, 35, 7, 3, 1) design into 7 parallel classes of 5 blocks
(Kirkman's schoolgirl problem). Points are the nonzero vectors of GF(2)^4 (as the
integers 1..15, shifted to 0..14), blocks are the lines {x, y, x xor y} of PG(3, 2),
and a backtracking search packs the 56 spreads (sets of 5 disjoint lines) into 7
spreads that use every line once."""
lines = sorted({tuple(sorted((x, y, x ^ y))) for x in range(1, 16) for y in range(1, 16) if x != y})
spreads = []
for combo in combinations(range(len(lines)), 5):
pts = [p for i in combo for p in lines[i]]
if len(set(pts)) == 15:
spreads.append(frozenset(combo))
def search(chosen, used):
if len(chosen) == 7:
return chosen
first = min(set(range(len(lines))) - used) # the first unused line must be covered
for s in spreads:
if first in s and not s & used:
found = search(chosen + [s], used | s)
if found:
return found
return None
days = search([], frozenset())
return sorted([sorted([p - 1 for p in lines[i]] for i in s) for s in days])
def efficiency_factor(v: int, k: int, lam: int, r: int) -> float:
"""E = lambda v / (r k): the variance ratio of a complete block design to the BIBD."""
return lam * v / (r * k)
def intrablock_estimates(v: int, blocks: Sequence[Block], y: Sequence[Sequence[float]]) -> list[float]:
"""Treatment effects tau_hat_i = k Q_i / (lambda v), with sum tau_hat_i = 0, where
Q_i = T_i - (1/k) (sum of the totals of the blocks that hold i). y[j][m] is the
response of treatment blocks[j][m] in block j."""
v, b, r, k, lam = parameters(v, blocks)
t = [0.0] * v
adj = [0.0] * v
for blk, ys in zip(blocks, y):
total = math.fsum(ys)
for trt, val in zip(blk, ys):
t[trt] += val
adj[trt] += total / k
return [k * (t[i] - adj[i]) / (lam * v) for i in range(v)]
test_balanced_incomplete_block_design.py 134 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/balanced-incomplete-block-design/. The authors are listed in prov/JG5X5V.jsonl.
import random
from fractions import Fraction
from itertools import combinations
import pytest
from balanced_incomplete_block_design import (
develop, efficiency_factor, fano, intrablock_estimates, kirkman_15, parameters,
)
def determinant(m: list[list[Fraction]]) -> Fraction:
m = [row[:] for row in m]
n, det = len(m), Fraction(1)
for c in range(n):
piv = next((r for r in range(c, n) if m[r][c] != 0), None)
if piv is None:
return Fraction(0)
if piv != c:
m[c], m[piv] = m[piv], m[c]
det = -det
det *= m[c][c]
for r in range(c + 1, n):
f = m[r][c] / m[c][c]
m[r] = [x - f * z for x, z in zip(m[r], m[c])]
return det
def check_identities(v: int, blocks) -> None:
v, b, r, k, lam = parameters(v, blocks)
assert b * k == v * r and lam * (v - 1) == r * (k - 1) and b >= v
inc = [[Fraction(int(t in blk)) for blk in blocks] for t in range(v)]
nnt = [[sum(a * c for a, c in zip(inc[i], inc[j])) for j in range(v)] for i in range(v)]
assert all(nnt[i][j] == (r if i == j else lam) for i in range(v) for j in range(v))
assert determinant(nnt) == (r - lam) ** (v - 1) * (r + (v - 1) * lam) != 0
def test_fano_plane() -> None:
blocks = fano()
assert blocks[:3] == [[0, 1, 3], [1, 2, 4], [2, 3, 5]]
assert parameters(7, blocks) == (7, 7, 3, 3, 1)
check_identities(7, blocks)
# any two lines meet in exactly one point (a symmetric design)
assert all(len(set(a) & set(b)) == 1 for a, b in combinations(blocks, 2))
def test_kirkman_schoolgirls() -> None:
days = kirkman_15()
assert len(days) == 7 and all(len(d) == 5 for d in days)
for day in days:
assert sorted(p for blk in day for p in blk) == list(range(15)) # each girl once a day
blocks = [blk for day in days for blk in day]
assert parameters(15, blocks) == (15, 35, 7, 3, 1)
check_identities(15, blocks)
def test_other_designs() -> None:
check_identities(4, [list(c) for c in combinations(range(4), 3)]) # (4, 4, 3, 3, 2)
check_identities(6, [list(c) for c in combinations(range(6), 2)]) # (6, 15, 5, 2, 1)
check_identities(13, develop([[0, 1, 3, 9]], 13)) # projective plane of order 3
check_identities(11, develop([[1, 3, 4, 5, 9]], 11)) # quadratic residues mod 11
assert parameters(9, [[0, 1, 2], [3, 4, 5], [6, 7, 8], [0, 3, 6], [1, 4, 7], [2, 5, 8],
[0, 4, 8], [1, 5, 6], [2, 3, 7], [0, 5, 7], [1, 3, 8], [2, 4, 6]]) == (9, 12, 4, 3, 1)
def test_not_a_bibd() -> None:
with pytest.raises(ValueError):
parameters(7, develop([[0, 1, 2]], 7)) # differences 1, 1, 2: pairs at distance 3 never meet
with pytest.raises(ValueError):
parameters(4, [[0, 1], [2, 3]])
def least_squares_effects(v, blocks, y) -> list[Fraction]:
"""Treatment effects (sum zero) of the model y = mu + tau_t + beta_block + e, solved exactly."""
obs = [(t, j, Fraction(val)) for j, (blk, ys) in enumerate(zip(blocks, y)) for t, val in zip(blk, ys)]
b = len(blocks)
cols = [[Fraction(1)] * len(obs)]
cols += [[Fraction((t == i) - (t == v - 1)) for t, _, _ in obs] for i in range(v - 1)]
cols += [[Fraction((j == i) - (j == b - 1)) for _, j, _ in obs] for i in range(b - 1)]
k = len(cols)
m = [[sum(a * c for a, c in zip(cols[i], cols[jj])) for jj in range(k)]
+ [sum(a * o[2] for a, o in zip(cols[i], obs))] for i in range(k)]
for c in range(k):
piv = next(r for r in range(c, k) if m[r][c] != 0)
m[c], m[piv] = m[piv], m[c]
for r in range(k):
if r != c and m[r][c] != 0:
f = m[r][c] / m[c][c]
m[r] = [x - f * z for x, z in zip(m[r], m[c])]
beta = [m[i][k] / m[i][i] for i in range(k)]
tau = beta[1:v]
return tau + [-sum(tau)]
def test_intrablock_estimates_are_least_squares() -> None:
rng = random.Random(1)
for v, blocks in ((7, fano()), (4, [list(c) for c in combinations(range(4), 3)])):
y = [[rng.randint(0, 30) for _ in blk] for blk in blocks]
got = intrablock_estimates(v, blocks, y)
exact = least_squares_effects(v, blocks, y)
assert all(abs(g - float(e)) < 1e-9 for g, e in zip(got, exact))
def test_example() -> None:
blocks = [list(c) for c in combinations(range(4), 3)]
y = [[71, 76, 70], [70, 77, 72], [75, 73, 70], [80, 77, 73]]
v, b, r, k, lam = parameters(4, blocks)
assert (v, b, r, k, lam) == (4, 4, 3, 3, 2)
values = [Fraction(x) for ys in y for x in ys]
grand = sum(values) / len(values)
sst = sum((x - grand) ** 2 for x in values)
ss_blocks = k * sum((Fraction(sum(ys), k) - grand) ** 2 for ys in y)
totals = [sum(x for blk, ys in zip(blocks, y) for t, x in zip(blk, ys) if t == i) for i in range(v)]
q = [totals[i] - sum(Fraction(sum(ys), k) for blk, ys in zip(blocks, y) if i in blk) for i in range(v)]
ss_treat_adj = k * sum(qi * qi for qi in q) / (lam * v)
sse = sst - ss_blocks - ss_treat_adj
assert totals == [216, 233, 220, 215] and q == [-2, 11, Fraction(-5, 3), Fraction(-22, 3)]
assert (sst, ss_blocks, ss_treat_adj, sse) == (Fraction(362, 3), Fraction(110, 3), Fraction(817, 12), Fraction(191, 12))
assert (ss_treat_adj / 3) / (sse / 5) == Fraction(4085, 573)
tau = [Fraction(-3, 4), Fraction(33, 8), Fraction(-5, 8), Fraction(-11, 4)]
assert all(abs(g - float(e)) < 1e-12 for g, e in zip(intrablock_estimates(v, blocks, y), tau))
assert least_squares_effects(v, blocks, y) == tau
# the one-way analysis that ignores the blocks
ss_treat_unadj = sum(r * (Fraction(t, r) - grand) ** 2 for t in totals)
assert ss_treat_unadj == Fraction(206, 3)
assert (ss_treat_unadj / 3) / ((sst - ss_treat_unadj) / 8) == Fraction(206, 9) / Fraction(13, 2)
def test_efficiency() -> None:
assert efficiency_factor(7, 3, 1, 3) == 7 / 9
assert abs(efficiency_factor(15, 3, 1, 7) - 5 / 7) < 1e-15
References
- Frank Yates, Incomplete randomized blocks, Annals of Eugenics 7(2), 1936, pp. 121 to 140 (balanced incomplete blocks and their intrablock analysis)
- Ronald A. Fisher, An examination of the different possible solutions of a problem in incomplete blocks, Annals of Eugenics 10(1), 1940, pp. 52 to 75 (the inequality b >= v and the enumeration of designs)
- George E. P. Box, J. Stuart Hunter and William G. Hunter, Statistics for Experimenters, second edition, Wiley, 2005, Section 4.5 (balanced incomplete block designs), starting on p. 162
- Douglas C. Montgomery, Design and Analysis of Experiments, tenth edition, Wiley, 2019, Chapter 4 (the balanced incomplete block design and its intrablock analysis; chapter-level locator)
- J. H. van Lint and R. M. Wilson, A Course in Combinatorics, second edition, Cambridge University Press, 2001, the chapter on block designs (Fisher's inequality, symmetric designs, the Bruck-Ryser-Chowla theorem; general locator, chapter number not confirmed)
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.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Code (python) | I0 not checked yet |
|
| Visual | V0 none |
|
| Writing | W4 complete |
|
Tag JG5X5V Edit this page History Report an error Markdown