TExcelTAD
Home › Information Theory › Information Through a Channel

Information Through a Channel

A channel together with an input distribution defines a joint distribution of input and output, and so a mutual information I(X;Y)I(X; Y): 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 PXP_X and a channel matrix QQ (rows indexed by input):

Quantity Formula NumPy
joint P(x,y)=P(x)Q(y∣x)P(x, y) = P(x)Q(y \mid x) px[:, None] * Q
output P(y)=∑xP(x)Q(y∣x)P(y) = \sum_x P(x)Q(y \mid x) px @ Q
mutual information I(X;Y)=∑x,yP(x,y)log⁡2Q(y∣x)P(y)I(X; Y) = \sum_{x, y} P(x, y)\log_2 \frac{Q(y \mid x)}{P(y)} see below
python · try it yourself
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

I(X;Y)=H(Y)−H(Y∣X)=H(X)−H(X∣Y)I(X; Y) = H(Y) - H(Y \mid X) = H(X) - H(X \mid Y)
  • H(Y)−H(Y∣X)H(Y) - H(Y \mid X): the output's total uncertainty minus the part caused by noise. This form is convenient for computing.
  • H(X)−H(X∣Y)H(X) - H(X \mid Y): what we wanted to send minus what remains unknown after receiving. H(X∣Y)H(X \mid Y) is the equivocation, the information the channel destroyed.
python · try it yourself
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, I(X;Y)I(X; Y) 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 P(X=1)P(X = 1):

python · try it yourself
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 II from it:

python · try it yourself
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 @ Q is 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 I(X;Y)I(X; Y)?

Answer

2 bits.

Solution

H(X∣Y)=0H(X \mid Y) = 0, so I=H(X)=log⁡24I = H(X) = \log_2 4.

Exercise 2: A useless channel

If every row of QQ is the same, what is I(X;Y)I(X; Y) for any input distribution?

Answer
Solution

The output distribution does not depend on the input, so XX and YY are independent.

Exercise 3: Noisy typewriter (code)

Compute I(X;Y)I(X; Y) for the 3-key noisy typewriter (each key produces itself or the next key with probability 12\frac{1}{2}) with uniform input.

Answer

log⁡23−1≈0.585\log_2 3 - 1 \approx 0.585 bits.

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

H(Y)=log⁡23H(Y) = \log_2 3 and the noise adds exactly 1 bit for every input.

Test yourself

The equivocation of a channel is…

  1. H(X∣Y)H(X \mid Y), the uncertainty about the input left after receiving
  2. H(Y∣X)H(Y \mid X)
  3. H(X)H(X)
  4. the capacity

For a fixed channel, mutual information as a function of the input distribution is…

  1. concave
  2. convex
  3. linear
  4. constant

In NumPy, with a row-stochastic channel matrix Q and input vector px, the output distribution is…

  1. px @ Q
  2. Q @ px
  3. Q.T @ Q
  4. px * Q.sum(axis=0)