TExcelTAD
Home › Information Theory › Repetition Codes

Repetition Codes

The simplest error-correcting code sends every bit NN 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 RNR_N

Encoder: 0 becomes NN zeros, 1 becomes NN ones. Rate 1N\frac{1}{N}.

Decoder: on a BSC with f<12f < \frac{1}{2}, the maximum-likelihood decision is the majority of the received bits (for odd NN). It fails if more than half the bits flip:

pb=∑k>N/2(Nk)fk(1−f)N−kp_b = \sum_{k > N/2} \binom{N}{k} f^k (1 - f)^{N - k}
python · try it yourself
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 ff, the error probability is dominated by the smallest number of flips that fools the decoder, N+12\frac{N + 1}{2}:

pb≈(N(N+1)/2)f(N+1)/2p_b \approx \binom{N}{(N + 1)/2} f^{(N + 1)/2}

For R3R_3 that is 3f23f^2: with f=0.1f = 0.1, about 0.03. Each extra pair of repetitions multiplies the error rate by roughly 4f4f, while the rate keeps falling.

python · try it yourself
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 pb≤10−15p_b \le 10^{-15}, typical for disk drives, on a BSC with f=0.1f = 0.1:

python · try it yourself
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.

python · try it yourself
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 R3R_3 on a BSC with f=0.2f = 0.2?

Answer

0.104.

Solution

3f2(1−f)+f3=3(0.04)(0.8)+0.008=0.096+0.008=0.1043f^2(1 - f) + f^3 = 3(0.04)(0.8) + 0.008 = 0.096 + 0.008 = 0.104.

Exercise 2: Even N

Why are repetition codes usually used with odd NN?

Answer

With even NN a tie is possible.

Solution

With N=2N = 2, receiving 01 gives no majority; the decoder must guess. R2R_2 has the same bit error rate as R1R_1 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 f=0.05f = 0.05 need for pb≤10−6p_b \le 10^{-6}?

Answer

N=15N = 15.

Solution
python · try it yourself
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…

  1. the majority vote
  2. the first bit
  3. the last bit
  4. XOR of all bits

For small f, the bit error rate of R3R_3 is about…

  1. 3f23f^2
  2. ff
  3. f3f^3
  4. 3f3f

What is the main drawback of repetition codes?

  1. Their rate goes to zero as the error rate goes to zero
  2. They cannot correct any errors
  3. They need a complex decoder
  4. They only work on erasure channels