Minimum Distance and Code Bounds
How good can a code be? For a given length and minimum distance , how many codewords can fit? Several classic bounds answer this, and they show that good codes must balance rate against distance. This lesson computes the Hamming, Singleton and Gilbert-Varshamov bounds and compares them with real codes.
Distance and error correction
The Hamming distance between two words is the number of positions in which they differ. A code with minimum distance corrects errors, because spheres of radius around the codewords do not overlap.
The Hamming (sphere-packing) bound
Each sphere of radius contains words, and the spheres fit in words:
Codes meeting this with equality are perfect: the Hamming codes (), the binary Golay code () and the trivial repetition codes of odd length.
from math import comb, log2
def V(n, t):
return sum(comb(n, i) for i in range(t + 1))
for name, n, k, d in [("Hamming [7,4,3]", 7, 4, 3), ("Hamming [15,11,3]", 15, 11, 3),
("Golay [23,12,7]", 23, 12, 7), ("repetition [5,1,5]", 5, 1, 5),
("ext. Hamming [8,4,4]", 8, 4, 4)]:
t = (d - 1) // 2
print(f"{name:21s}: 2^k V(n,t) = {2**k * V(n, t):7d}, 2^n = {2**n:7d}, perfect: {2**k * V(n, t) == 2**n}")Hamming [7,4,3] : 2^k V(n,t) = 128, 2^n = 128, perfect: True
Hamming [15,11,3] : 2^k V(n,t) = 32768, 2^n = 32768, perfect: True
Golay [23,12,7] : 2^k V(n,t) = 8388608, 2^n = 8388608, perfect: True
repetition [5,1,5] : 2^k V(n,t) = 32, 2^n = 32, perfect: True
ext. Hamming [8,4,4] : 2^k V(n,t) = 144, 2^n = 256, perfect: False
The Singleton bound
Delete positions from every codeword. The shortened words are still distinct, so , that is
Binary codes rarely come close, but Reed-Solomon codes over large alphabets meet it with equality (they are maximum distance separable).
The Gilbert-Varshamov bound
Good codes do exist: a greedy construction (keep adding any word at distance at least from all chosen ones) stops only when the spheres of radius cover everything, so
import numpy as np
from math import comb
def V(n, r):
return sum(comb(n, i) for i in range(r + 1))
def greedy_code(n, d):
chosen = []
for w in range(2 ** n):
if all(bin(w ^ c).count("1") >= d for c in chosen):
chosen.append(w)
return len(chosen)
n = 10
print(" d | GV lower | greedy | Hamming upper")
for d in [3, 4, 5, 6]:
t = (d - 1) // 2
print(f"{d:2d} | {2**n / V(n, d - 1):8.2f} | {greedy_code(n, d):6d} | {2**n // V(n, t):6d}") d | GV lower | greedy | Hamming upper
3 | 18.29 | 64 | 93
4 | 5.82 | 32 | 93
5 | 2.65 | 8 | 18
6 | 1.61 | 4 | 18
Asymptotic rates
For long codes, write . Using , the bounds become rate limits:
| Bound | Rate |
|---|---|
| Gilbert-Varshamov (achievable) | |
| Hamming (upper) |
import numpy as np
H2 = lambda p: 0.0 if p <= 0 else float(-p * np.log2(p) - (1 - p) * np.log2(1 - p))
for delta in [0.05, 0.1, 0.2, 0.3, 0.4]:
print(f"delta = {delta:.2f}: GV rate {max(0.0, 1 - H2(delta)):.3f}, Hamming bound {1 - H2(delta / 2):.3f}")delta = 0.05: GV rate 0.714, Hamming bound 0.831
delta = 0.10: GV rate 0.531, Hamming bound 0.714
delta = 0.20: GV rate 0.278, Hamming bound 0.531
delta = 0.30: GV rate 0.119, Hamming bound 0.390
delta = 0.40: GV rate 0.029, Hamming bound 0.278
Distance is not everything
The bounds talk about worst-case errors. Shannon's theorem is about typical errors: on a BSC with flip probability , a long block has about errors, and random-like codes correct almost all such patterns even when their minimum distance is less than . LDPC and turbo codes have modest minimum distances yet approach capacity because they correct almost all typical error patterns (Low-Density Parity-Check Codes).
In software
- Tables of the best known codes for each are maintained online (for example by Markus Grassl at codetables.de).
- For small codes, compute the minimum distance by enumerating all codewords; for large codes this is infeasible and probabilistic estimates are used.
Exercises
Exercise 1: Correcting power
A code has minimum distance 7. How many errors can it correct, and how many can it detect?
Answer
Correct 3; detect 6.
Solution
and .
Exercise 2: Singleton
Can a binary code have minimum distance 6?
Answer
No.
Solution
Singleton gives .
Exercise 3: Hamming bound check (code)
Does the Hamming bound allow a binary code correcting 2 errors?
Answer
Yes: . (The BCH code exists.)
Solution
from math import comb
V = sum(comb(15, i) for i in range(3))
print(V, 2**7 * V, 2**15, 2**7 * V <= 2**15)121 15488 32768 True
Test yourself
A perfect code is one where…
- the spheres of radius t around codewords fill the whole space
- the minimum distance equals n
- the rate is 1
- every error is detected
The Gilbert-Varshamov bound shows that…
- codes with rate at least exist
- no code beats rate
- all codes are perfect
- Reed-Solomon codes are optimal
The Singleton bound states…