Probability Review for Information Theory
Information theory is built on discrete probability. This lesson recaps the pieces we use constantly: joint distributions as tables, marginals, conditionals, independence, expectation and Bayes' theorem. The full treatment is in the Probability course; here everything is a NumPy array.
Joint distributions as tables
Two discrete random variables and have a joint distribution , which we store as a matrix with one row per value of and one column per value of . Summing rows or columns gives the marginals (Marginals and Independence):
import numpy as np
# X = weather (sun, rain), Y = umbrella (no, yes)
P = np.array([[0.56, 0.14],
[0.06, 0.24]])
print("total:", round(float(P.sum()), 4))
print("P(X):", P.sum(axis=1), " P(Y):", P.sum(axis=0))total: 1.0
P(X): [0.7 0.3] P(Y): [0.62 0.38]
Conditional distributions
The conditional distribution of given is the row of the table divided by its sum:
import numpy as np
P = np.array([[0.56, 0.14],
[0.06, 0.24]])
P_y_given_x = P / P.sum(axis=1, keepdims=True)
P_x_given_y = P / P.sum(axis=0, keepdims=True)
print("P(umbrella | weather):\n", np.round(P_y_given_x, 4))
print("P(weather | umbrella):\n", np.round(P_x_given_y, 4))P(umbrella | weather):
[[0.8 0.2]
[0.2 0.8]]
P(weather | umbrella):
[[0.9032 0.3684]
[0.0968 0.6316]]
Every row of sums to 1, and every column of sums to 1. A channel in chapter 3 is exactly such a conditional table.
Independence
and are independent when for all , that is, when the joint table equals the outer product of its marginals (Independence):
import numpy as np
P = np.array([[0.56, 0.14], [0.06, 0.24]])
Q = np.outer(P.sum(axis=1), P.sum(axis=0))
print("product of marginals:\n", np.round(Q, 4))
print("independent:", bool(np.allclose(P, Q)))product of marginals:
[[0.434 0.266]
[0.186 0.114]]
independent: False
Mutual information (Mutual Information) measures how far a joint table is from this product.
Expectation
The expectation of a function is the probability-weighted average . Entropy itself is an expectation: the average of . The law of large numbers says sample averages converge to expectations, which is the engine behind typical sets (Typical Sets).
import numpy as np
p = np.array([0.5, 0.3, 0.2]); f = np.array([1.0, 4.0, 10.0])
print("E[f(X)] =", round(float(p @ f), 4))
rng = np.random.default_rng(0)
x = rng.choice(3, size=200_000, p=p)
print("sample mean:", round(float(f[x].mean()), 4))E[f(X)] = 3.7
sample mean: 3.6828
Bayes' theorem
Inverting a conditional uses Bayes' theorem (Bayes' Theorem):
Decoding a noisy message is Bayesian inference: we observe the output and ask which input was most likely sent.
import numpy as np
prior = np.array([0.9, 0.1]) # bit 0 is sent 90% of the time
channel = np.array([[0.8, 0.2], # P(y | x): rows x, columns y
[0.2, 0.8]])
y = 1 # we received a 1
posterior = prior * channel[:, y]
posterior /= posterior.sum()
print("P(x | y=1):", np.round(posterior, 4))P(x | y=1): [0.6923 0.3077]
Even after receiving a 1, the sent bit was more likely a 0, because 0s are so much more common.
In software
- Represent joint distributions as 2-D arrays;
sum(axis=...)gives marginals and broadcasting gives conditionals. np.outer(px, py)builds the independent joint with the same marginals.pandas.crosstab(x, y, normalize=True)estimates a joint table from two label columns.
Exercises
Exercise 1: Marginals
For the joint table , what are and ?
Answer
and .
Solution
Row sums are and ; column sums are and .
Exercise 2: Independent?
Is the table in Exercise 1 a product of its marginals?
Answer
No.
Solution
.
Exercise 3: Posterior (code)
In the Bayes example, what is if the channel is correct 95% of the time instead of 80%?
Answer
About 0.679.
Solution
import numpy as np
prior = np.array([0.9, 0.1])
channel = np.array([[0.95, 0.05], [0.05, 0.95]])
post = prior * channel[:, 1]
print(np.round(post / post.sum(), 4))[0.3214 0.6786]
Test yourself
The marginal of a joint table with rows indexed by is…
- the row sums
- the column sums
- the diagonal
- the largest entry in each row
X and Y are independent exactly when…
- the joint table equals the outer product of its marginals
- the joint table is symmetric
- the joint table has no zeros
- the marginals are uniform
Entropy can be written as the expectation of…