Repetition Codes
The simplest error-correcting code sends every bit times and decodes by majority vote. Repetition codes are too wasteful for real use, but they introduce every idea of the chapter: redundancy, decoding as inference, error probability, and the trade-off between rate and reliability.
The code
Encoder: 0 becomes zeros, 1 becomes ones. Rate .
Decoder: on a BSC with , the maximum-likelihood decision is the majority of the received bits (for odd ). It fails if more than half the bits flip:
import numpy as np
from scipy.stats import binom
f = 0.1
rng = np.random.default_rng(0)
bits = rng.integers(0, 2, 200_000)
for N in [1, 3, 5, 7]:
received = np.repeat(bits, N) ^ (rng.random(bits.size * N) < f)
decoded = (received.reshape(-1, N).sum(axis=1) > N // 2).astype(int)
print(f"R_{N}: rate {1 / N:.3f}, simulated pb {np.mean(decoded != bits):.5f}, exact {binom.sf(N // 2, N, f):.5f}")R_1: rate 1.000, simulated pb 0.10010, exact 0.10000
R_3: rate 0.333, simulated pb 0.02804, exact 0.02800
R_5: rate 0.200, simulated pb 0.00840, exact 0.00856
R_7: rate 0.143, simulated pb 0.00265, exact 0.00273
The dominant term
For small , the error probability is dominated by the smallest number of flips that fools the decoder, :
For that is : with , about 0.03. Each extra pair of repetitions multiplies the error rate by roughly , while the rate keeps falling.
from math import comb
from scipy.stats import binom
f = 0.01
for N in [3, 5, 7, 9]:
approx = comb(N, (N + 1) // 2) * f ** ((N + 1) // 2)
print(f"N = {N}: exact {binom.sf(N // 2, N, f):.3e}, dominant term {approx:.3e}")N = 3: exact 2.980e-04, dominant term 3.000e-04
N = 5: exact 9.851e-06, dominant term 1.000e-05
N = 7: exact 3.417e-07, dominant term 3.500e-07
N = 9: exact 1.219e-08, dominant term 1.260e-08
How many repetitions for a target?
Suppose we want , typical for disk drives, on a BSC with :
from scipy.stats import binom
f = 0.1
N = 1
while binom.sf(N // 2, N, f) > 1e-15:
N += 2
print(f"R_{N} reaches pb = {binom.sf(N // 2, N, f):.2e} at rate {1 / N:.4f}")
print("Shannon: rate up to 1 - H2(0.1) = 0.531 is possible with arbitrarily small error")R_63 reaches pb = 3.90e-16 at rate 0.0159
Shannon: rate up to 1 - H2(0.1) = 0.531 is possible with arbitrarily small error
The repetition code needs about 60 transmissions per bit; Shannon's theorem says about 2 would be enough. This gap motivated the search for better codes.
Soft-decision decoding
If the receiver has a confidence for each received bit (for example the analog voltage), the majority vote should be replaced by a weighted vote: add up the log-likelihood ratios (Sum-Product (Belief Propagation) Decoding). For a Gaussian channel, that means summing the received voltages.
import numpy as np
rng = np.random.default_rng(1)
N, sigma = 3, 1.0
bits = rng.integers(0, 2, 200_000)
x = 1 - 2 * np.repeat(bits, N) # 0 -> +1, 1 -> -1
y = x + sigma * rng.normal(size=x.size)
hard = ((y < 0).reshape(-1, N).sum(axis=1) > N // 2).astype(int)
soft = (y.reshape(-1, N).sum(axis=1) < 0).astype(int)
print(f"hard majority: pb = {np.mean(hard != bits):.4f}")
print(f"soft sum: pb = {np.mean(soft != bits):.4f}")hard majority: pb = 0.0676
soft sum: pb = 0.0420
Soft decoding uses the information that hard decisions throw away.
In software
- Repetition is used where simplicity matters more than efficiency: triple modular redundancy in spacecraft computers (three CPUs vote), and storing critical metadata in several copies.
- Most of the rate is wasted, so storage and communication systems use Reed-Solomon, BCH or LDPC codes instead.
Exercises
Exercise 1: R3 by hand
What is the exact bit error probability of on a BSC with ?
Answer
0.104.
Solution
.
Exercise 2: Even N
Why are repetition codes usually used with odd ?
Answer
With even a tie is possible.
Solution
With , receiving 01 gives no majority; the decoder must guess. has the same bit error rate as but half the rate (it can detect one error but not correct it).
Exercise 3: Repetitions for 1e-6 (code)
How many repetitions does a BSC with need for ?
Answer
.
Solution
from scipy.stats import binom
f, N = 0.05, 1
while binom.sf(N // 2, N, f) > 1e-6:
N += 2
print(N, f"{binom.sf(N // 2, N, f):.2e}")15 1.83e-07
Test yourself
The maximum-likelihood decoder for a repetition code on a BSC with f < 0.5 is…
- the majority vote
- the first bit
- the last bit
- XOR of all bits
For small f, the bit error rate of is about…
What is the main drawback of repetition codes?
- Their rate goes to zero as the error rate goes to zero
- They cannot correct any errors
- They need a complex decoder
- They only work on erasure channels