Information Through a Channel
A channel together with an input distribution defines a joint distribution of input and output, and so a mutual information : the number of bits per use that the output reveals about the input. This lesson computes it for general channels and shows how it depends on the input distribution, which is the step before maximizing it to get the capacity.
Joint, output and posterior
Given an input distribution and a channel matrix (rows indexed by input):
| Quantity | Formula | NumPy |
|---|---|---|
| joint | px[:, None] * Q |
|
| output | px @ Q |
|
| mutual information | see below |
import numpy as np
def channel_mi(px, Q):
py = px @ Q
joint = px[:, None] * Q
nz = joint > 0
ratio = Q / np.where(py > 0, py, 1)[None, :]
return float(np.sum(joint[nz] * np.log2(ratio[nz])))
bsc = np.array([[0.9, 0.1], [0.1, 0.9]])
bec = np.array([[0.8, 0.2, 0.0], [0.0, 0.2, 0.8]])
z = np.array([[1.0, 0.0], [0.15, 0.85]])
px = np.array([0.5, 0.5])
for name, Q in [("BSC f=0.1", bsc), ("BEC e=0.2", bec), ("Z f=0.15", z)]:
print(f"{name:10s}: I(X;Y) with uniform input = {channel_mi(px, Q):.4f} bits")BSC f=0.1 : I(X;Y) with uniform input = 0.5310 bits
BEC e=0.2 : I(X;Y) with uniform input = 0.8000 bits
Z f=0.15 : I(X;Y) with uniform input = 0.6788 bits
Two views of the same number
- : the output's total uncertainty minus the part caused by noise. This form is convenient for computing.
- : what we wanted to send minus what remains unknown after receiving. is the equivocation, the information the channel destroyed.
import numpy as np
def H(p):
p = np.asarray(p, float).ravel(); p = p[p > 0]
return float(-np.sum(p * np.log2(p))) + 0.0
Q = np.array([[1.0, 0.0], [0.15, 0.85]]) # Z channel
px = np.array([0.6, 0.4])
joint = px[:, None] * Q
HX, HY, HXY = H(px), H(px @ Q), H(joint)
print(f"H(Y) - H(Y|X) = {HY:.4f} - {HXY - HX:.4f} = {HY - (HXY - HX):.4f}")
print(f"H(X) - H(X|Y) = {HX:.4f} - {HXY - HY:.4f} = {HX - (HXY - HY):.4f} (equivocation {HXY - HY:.4f})")H(Y) - H(Y|X) = 0.9248 - 0.2439 = 0.6809
H(X) - H(X|Y) = 0.9710 - 0.2901 = 0.6809 (equivocation 0.2901)
Dependence on the input distribution
For a fixed channel, is a concave function of the input distribution, so it has a single maximum and no other local maxima. For a binary input we can simply scan :
import numpy as np
def channel_mi(px, Q):
py = px @ Q; joint = px[:, None] * Q; nz = joint > 0
return float(np.sum(joint[nz] * np.log2((Q / np.where(py > 0, py, 1))[nz])))
Q = np.array([[1.0, 0.0], [0.15, 0.85]]) # Z channel
ps = np.linspace(0, 1, 1001)
mi = np.array([channel_mi(np.array([1 - p, p]), Q) for p in ps])
for p in [0.0, 0.2, 0.4, 0.5, 0.6, 0.8, 1.0]:
print(f"P(X=1) = {p:.1f}: I = {mi[int(round(p * 1000))]:.4f}")
print(f"best: P(X=1) = {ps[mi.argmax()]:.3f}, I = {mi.max():.4f} bits")P(X=1) = 0.0: I = 0.0000
P(X=1) = 0.2: I = 0.5357
P(X=1) = 0.4: I = 0.6809
P(X=1) = 0.5: I = 0.6788
P(X=1) = 0.6: I = 0.6338
P(X=1) = 0.8: I = 0.4165
P(X=1) = 1.0: I = 0.0000
best: P(X=1) = 0.445, I = 0.6854 bits
For the asymmetric Z channel the best input sends 1 less than half the time, because 1s are the unreliable symbol.
Estimating from simulation
When the channel is only available as a black box (a simulator or measured hardware), estimate the joint table from input-output pairs and compute from it:
import numpy as np
rng = np.random.default_rng(0)
n = 200_000
x = rng.integers(0, 2, n)
y = np.where(x == 1, (rng.random(n) > 0.15).astype(int), 0) # Z channel
table = np.zeros((2, 2)); np.add.at(table, (x, y), 1)
P = table / n
Q = np.outer(P.sum(axis=1), P.sum(axis=0)); nz = P > 0
print("estimated I(X;Y):", round(float(np.sum(P[nz] * np.log2(P[nz] / Q[nz]))), 4))estimated I(X;Y): 0.6792
In software
- Keep channels as row-stochastic matrices and inputs as row vectors;
px @ Qis the output distribution, and the same function works for any alphabet sizes. - Concavity means any local maximization over the input simplex (projected gradient, Blahut-Arimoto) finds the global optimum (Computing Capacity: Blahut-Arimoto).
Exercises
Exercise 1: A perfect channel
For the identity channel on 4 symbols with uniform input, what is ?
Answer
2 bits.
Solution
, so .
Exercise 2: A useless channel
If every row of is the same, what is for any input distribution?
Answer
Solution
The output distribution does not depend on the input, so and are independent.
Exercise 3: Noisy typewriter (code)
Compute for the 3-key noisy typewriter (each key produces itself or the next key with probability ) with uniform input.
Answer
bits.
Solution
import numpy as np
Q = np.array([[0.5, 0.5, 0.0], [0.0, 0.5, 0.5], [0.5, 0.0, 0.5]])
px = np.ones(3) / 3
py = px @ Q; joint = px[:, None] * Q; nz = joint > 0
print(round(float(np.sum(joint[nz] * np.log2((Q / py)[nz]))), 4), round(np.log2(3) - 1, 4))0.585 0.585
and the noise adds exactly 1 bit for every input.
Test yourself
The equivocation of a channel is…
- , the uncertainty about the input left after receiving
- the capacity
For a fixed channel, mutual information as a function of the input distribution is…
- concave
- convex
- linear
- constant
In NumPy, with a row-stochastic channel matrix Q and input vector px, the output distribution is…
- px @ Q
- Q @ px
- Q.T @ Q
- px * Q.sum(axis=0)