Computing Capacity: Blahut-Arimoto
For a binary input, capacity is a one-dimensional maximization. For larger alphabets we need an algorithm. The Blahut-Arimoto algorithm (1972) computes the capacity of any discrete memoryless channel with simple alternating updates, each of which has a closed form. It is an example of alternating maximization, like the EM algorithm (The EM Algorithm).
The idea
Mutual information can be written as a maximum over an auxiliary "backward channel" :
with the maximum reached at the true posterior . So capacity is a maximum over two things, and , and we can alternate:
- Fix , update : set to the posterior, .
- Fix , update : .
Combining both steps gives the usual form: , normalized, where .
Bounds that tell you when to stop
At every iteration, with as above,
The lower bound is the current ; the upper bound comes from duality. Stop when the gap is below the tolerance.
import numpy as np
def blahut_arimoto(Q, tol=1e-9, max_iter=10_000):
n = Q.shape[0]
p = np.ones(n) / n
for it in range(max_iter):
py = p @ Q
with np.errstate(divide="ignore", invalid="ignore"):
D = np.sum(np.where(Q > 0, Q * np.log2(Q / py), 0.0), axis=1) # KL of each row from P_Y
lower, upper = float(p @ D), float(D.max())
if upper - lower < tol:
break
p = p * np.exp2(D); p /= p.sum()
return lower, upper, p, it
Z = np.array([[1.0, 0.0], [0.15, 0.85]])
C_low, C_up, p, it = blahut_arimoto(Z)
print(f"Z channel: {C_low:.6f} <= C <= {C_up:.6f}, input {np.round(p, 4)}, {it} iterations")
T = np.array([[0.8, 0.2, 0.0], [0.1, 0.8, 0.1], [0.0, 0.2, 0.8]])
C_low, C_up, p, it = blahut_arimoto(T)
print(f"ternary: {C_low:.6f} <= C <= {C_up:.6f}, input {np.round(p, 4)}, {it} iterations")Z channel: 0.685418 <= C <= 0.685418, input [0.5551 0.4449], 14 iterations
ternary: 0.826916 <= C <= 0.826916, input [0.4299 0.1402 0.4299], 73 iterations
Watching it converge
import numpy as np
Q = np.array([[0.6, 0.3, 0.1, 0.0],
[0.0, 0.5, 0.4, 0.1],
[0.1, 0.1, 0.1, 0.7],
[0.25, 0.25, 0.25, 0.25]]) # the last input is useless
p = np.ones(4) / 4
for it in range(1, 41):
py = p @ Q
with np.errstate(divide="ignore", invalid="ignore"):
D = np.sum(np.where(Q > 0, Q * np.log2(Q / py), 0.0), axis=1)
if it in (1, 2, 5, 10, 20, 40):
print(f"iter {it:2d}: lower {p @ D:.6f}, upper {D.max():.6f}, P = [{' '.join(f'{v:.4f}' for v in p)}]")
p = p * np.exp2(D); p /= p.sum()iter 1: lower 0.487651, upper 0.711895, P = [0.2500 0.2500 0.2500 0.2500]
iter 2: lower 0.535613, upper 0.689117, P = [0.2870 0.2702 0.2664 0.1763]
iter 5: lower 0.614005, upper 0.654694, P = [0.3478 0.3021 0.2961 0.0540]
iter 10: lower 0.644147, upper 0.648350, P = [0.3682 0.3149 0.3105 0.0063]
iter 20: lower 0.648097, upper 0.648169, P = [0.3704 0.3170 0.3126 0.0001]
iter 40: lower 0.648147, upper 0.648147, P = [0.3704 0.3170 0.3126 0.0000]
The useless input (its output does not depend on anything) gets less and less weight; the capacity-achieving distribution never uses it.
The KKT conditions
At the optimum, every input that is used has the same divergence , and unused inputs have . These are the KKT conditions of the concave maximization (The KKT Conditions). They give a quick check of a claimed solution.
import numpy as np
Q = np.array([[0.8, 0.2, 0.0], [0.1, 0.8, 0.1], [0.0, 0.2, 0.8]])
p = np.ones(3) / 3
for _ in range(2000):
py = p @ Q
with np.errstate(divide="ignore", invalid="ignore"):
D = np.sum(np.where(Q > 0, Q * np.log2(Q / py), 0.0), axis=1)
p = p * np.exp2(D); p /= p.sum()
print("D(x) for each input:", np.round(D, 6), " (all equal: every input is used)")D(x) for each input: [0.826916 0.826916 0.826916] (all equal: every input is used)
In software
- Blahut-Arimoto needs only matrix-vector products, so it scales to channels with thousands of symbols; work in log space if some probabilities are tiny.
- The same alternating scheme with a distortion penalty computes the rate-distortion function (Rate-Distortion Theory).
Exercises
Exercise 1: Starting point
Why does Blahut-Arimoto start from a distribution with all entries positive?
Answer
The multiplicative update can never revive an input that has probability 0.
Solution
keeps zeros at zero, so starting with a zero could miss the optimum.
Exercise 2: Symmetric channels
What does Blahut-Arimoto do on a BSC when started from the uniform distribution?
Answer
It stops at once: uniform is already optimal.
Solution
By symmetry , so the update does not change and the lower and upper bounds coincide.
Exercise 3: Erasure channel check (code)
Run Blahut-Arimoto on a binary erasure channel with and compare with .
Answer
Both give 0.65.
Solution
import numpy as np
Q = np.array([[0.65, 0.35, 0.0], [0.0, 0.35, 0.65]])
p = np.array([0.3, 0.7])
for _ in range(200):
py = p @ Q
with np.errstate(divide="ignore", invalid="ignore"):
D = np.sum(np.where(Q > 0, Q * np.log2(Q / py), 0.0), axis=1)
p = p * np.exp2(D); p /= p.sum()
print(round(float(p @ D), 6), np.round(p, 4))0.65 [0.5 0.5]
Test yourself
Blahut-Arimoto computes capacity by…
- alternating closed-form updates of the input distribution and a backward channel
- enumerating all codes
- gradient descent on the error probability
- Huffman coding the outputs
At the capacity-achieving input, every used input x has…
- the same divergence
- probability 1/N
- zero divergence
- the largest entropy
Which quantity is an upper bound on capacity at every iteration?
- 0