TExcelTAD
Home › Information Theory › Probability Review for Information Theory

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 XX and YY have a joint distribution P(x,y)P(x, y), which we store as a matrix with one row per value of XX and one column per value of YY. Summing rows or columns gives the marginals (Marginals and Independence):

P(x)=∑yP(x,y),P(y)=∑xP(x,y)P(x) = \sum_y P(x, y), \qquad P(y) = \sum_x P(x, y)
python · try it yourself
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 YY given X=xX = x is the row of the table divided by its sum:

P(y∣x)=P(x,y)P(x)P(y \mid x) = \frac{P(x, y)}{P(x)}
python · try it yourself
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 P(y∣x)P(y \mid x) sums to 1, and every column of P(x∣y)P(x \mid y) sums to 1. A channel in chapter 3 is exactly such a conditional table.

Independence

XX and YY are independent when P(x,y)=P(x)P(y)P(x, y) = P(x)P(y) for all x,yx, y, that is, when the joint table equals the outer product of its marginals (Independence):

python · try it yourself
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 f(X)f(X) is the probability-weighted average E[f(X)]=∑xP(x)f(x)E[f(X)] = \sum_x P(x) f(x). Entropy itself is an expectation: the average of log⁡21P(x)\log_2 \frac{1}{P(x)}. The law of large numbers says sample averages converge to expectations, which is the engine behind typical sets (Typical Sets).

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

P(x∣y)=P(y∣x)P(x)∑x′P(y∣x′)P(x′)P(x \mid y) = \frac{P(y \mid x)P(x)}{\sum_{x'} P(y \mid x')P(x')}

Decoding a noisy message is Bayesian inference: we observe the output yy and ask which input xx was most likely sent.

python · try it yourself
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 [0.10.20.30.4]\begin{bmatrix} 0.1 & 0.2 \\ 0.3 & 0.4 \end{bmatrix}, what are P(X)P(X) and P(Y)P(Y)?

Answer

P(X)=(0.3,0.7)P(X) = (0.3, 0.7) and P(Y)=(0.4,0.6)P(Y) = (0.4, 0.6).

Solution

Row sums are 0.1+0.20.1 + 0.2 and 0.3+0.40.3 + 0.4; column sums are 0.1+0.30.1 + 0.3 and 0.2+0.40.2 + 0.4.

Exercise 2: Independent?

Is the table in Exercise 1 a product of its marginals?

Answer

No.

Solution

P(X=0)P(Y=0)=0.3×0.4=0.12≠0.1P(X = 0)P(Y = 0) = 0.3 \times 0.4 = 0.12 \ne 0.1.

Exercise 3: Posterior (code)

In the Bayes example, what is P(x=1∣y=1)P(x = 1 \mid y = 1) if the channel is correct 95% of the time instead of 80%?

Answer

About 0.679.

Solution
python · try it yourself
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 P(x)P(x) of a joint table with rows indexed by xx is…

  1. the row sums
  2. the column sums
  3. the diagonal
  4. the largest entry in each row

X and Y are independent exactly when…

  1. the joint table equals the outer product of its marginals
  2. the joint table is symmetric
  3. the joint table has no zeros
  4. the marginals are uniform

Entropy can be written as the expectation of…

  1. log⁡21P(X)\log_2 \frac{1}{P(X)}
  2. P(X)P(X)
  3. X2X^2
  4. log⁡2X\log_2 X