Information theory is the mathematical study of the quantification, storage, and communication of information. It was founded by Claude Shannon in 1948.
Entropy
Entropy measures the uncertainty or surprise associated with a random variable .
Usually, for bits (Shannon entropy).
Interactive Lab
import math
def calculate_entropy(probabilities):
entropy = 0
for p in probabilities:
if p > 0:
entropy -= p * math.log2(p)
return entropy
# Fair coin
print(f"Fair coin entropy: {calculate_entropy([0.5, 0.5])} bits")
# Biased coin (90% heads)
print(f"Biased coin entropy: {calculate_entropy([0.9, 0.1]):.3f} bits")
# Single certain outcome
print(f"Certain outcome entropy: {calculate_entropy([1.0])} bits")
Mutual Information and Kullback-Leibler Divergence
Kullback-Leibler (KL) Divergence, or relative entropy, measures the “distance” (though not a true metric) between two probability distributions and :
It quantifies the information lost when is used to approximate .
Mutual Information measures the reduction in uncertainty of given the knowledge of :
It is related to entropy by: .
Interactive Lab
import math
def kl_divergence(P, Q):
# D_KL(P || Q) - Sum P(x) log(P(x)/Q(x))
res = 0
for p, q in zip(P, Q):
if p > 0:
res += p * math.log2(p / q)
return res
P = [0.1, 0.9]
Q = [0.5, 0.5]
print(f"KL Divergence D_KL(P || Q): {kl_divergence(P, Q):.3f}")
def mutual_information(joint_prob, p_x, p_y):
# I(X;Y) = Sum P(x,y) log(P(x,y)/(P(x)P(y)))
mi = 0
for x in range(len(p_x)):
for y in range(len(p_y)):
pxy = joint_prob[x][y]
if pxy > 0:
mi += pxy * math.log2(pxy / (p_x[x] * p_y[y]))
return mi
# Example: X and Y are highly correlated
px = [0.5, 0.5]
py = [0.5, 0.5]
joint = [[0.45, 0.05], [0.05, 0.45]]
print(f"Mutual Information I(X; Y): {mutual_information(joint, px, py):.3f}")
29print(f"Mutual Information I(X; Y): {mutual_information(joint, px, py):.3f}")
Source Coding Theorem
Shannon’s Source Coding Theorem establishes that the minimum number of bits needed to represent a source is its entropy. Huffman Coding is a common algorithm to achieve this bound.
Knowledge Check
If a random variable has 4 equally likely outcomes, what is its entropy?
Answer: 2 bits
If a random variable has 4 equally likely outcomes, what is its entropy?
Channel Capacity
The Shannon-Hartley Theorem defines the maximum rate at which information can be transmitted over a noisy channel with bandwidth and signal-to-noise ratio :
Knowledge Check
What happens to entropy as outcomes become more predictable?
Answer: It decreases.
What happens to entropy as outcomes become more predictable?