Hamming Bound

lean4-proofinformation-theoryvisualization
Ci=0t(ni)2n|C| \cdot \sum_{i=0}^{t} \binom{n}{i} \le 2^n

Statement

A binary code C{0,1}nC \subseteq \{0,1\}^n can correct tt errors if distinct codewords have Hamming distance at least 2t+12t + 1. The Hamming (sphere-packing) bound is

Ci=0t(ni)2n.|C| \cdot \sum_{i=0}^{t} \binom{n}{i} \le 2^n.

A code meeting this bound with equality is called perfect. The [7,4,3][7, 4, 3] binary Hamming code corrects t=1t = 1 error and is perfect:

C=24=16,i=01(7i)=1+7=8,168=128=27.|C| = 2^4 = 16, \quad \sum_{i=0}^{1}\binom{7}{i} = 1 + 7 = 8, \quad 16 \cdot 8 = 128 = 2^7.

Visualization

The [7,4,3][7, 4, 3] Hamming code generator matrix GG (systematic form):

G = [ 1 0 0 0 | 1 1 0 ]
    [ 0 1 0 0 | 1 0 1 ]
    [ 0 0 1 0 | 0 1 1 ]
    [ 0 0 0 1 | 1 1 1 ]

Parity-check matrix HH (3 rows, 7 columns — columns are all nonzero 3-bit strings):

H = [ 1 0 1 0 1 0 1 ]
    [ 0 1 1 0 0 1 1 ]
    [ 0 0 0 1 1 1 1 ]

Sphere-packing count: 16 codewords, each surrounded by a Hamming ball of radius 1 containing 1+7=81 + 7 = 8 binary strings. Total covered: 16×8=128=2716 \times 8 = 128 = 2^7. The balls partition all of {0,1}7\{0,1\}^7 — perfect packing.

Correcting a single error: if received word r=c+ejr = c + e_j (flip of bit jj), compute syndrome HrT=HejT=Hr^T = He_j^T = column jj of HH. That column gives the error position directly.

Proof Sketch

  1. For each codeword cCc \in C, define its Hamming ball B(c,t)={x{0,1}n:d(c,x)t}B(c, t) = \{x \in \{0,1\}^n : d(c, x) \le t\}. Each ball has size i=0t(ni)\sum_{i=0}^{t}\binom{n}{i}.
  2. The minimum distance 2t+1\ge 2t+1 implies balls around distinct codewords are disjoint.
  3. All balls are subsets of {0,1}n\{0,1\}^n, which has 2n2^n elements.
  4. By disjointness: CB(c,t)2n|C| \cdot |B(c, t)| \le 2^n.

Connections

The Hamming bound is the coding-theory analogue of the Pigeonhole PrinciplePigeonhole Principlef1({y})2  for some  yB  when  A>B|f^{-1}(\{y\})| \geq 2 \;\text{for some}\; y \in B \;\text{when}\; |A| > |B|If n+1 objects are placed into n boxes, some box contains at least two objectsRead more →: pack balls in a space, count elements. The parity-check matrix construction links to Rank-Nullity Theorem over F2\mathbb{F}_2.

Lean4 Proof

/-- Verify the Hamming bound for the [7,4,3] binary code:
    16 codewords, t=1 errors, n=7 bits. -/
theorem hamming_bound_74 :
    (16 : ) * (Nat.choose 7 0 + Nat.choose 7 1) = 2 ^ 7 := by
  native_decide