Search Knowledge

© 2026 LIBREUNI PROJECT

Information Theory

Information Theory

Information theory is the mathematical study of the quantification, storage, and communication of information. It was founded by Claude Shannon in 1948.

Entropy

Entropy H(X)H(X) measures the uncertainty or surprise associated with a random variable XX. H(X)=iP(xi)logbP(xi)H(X) = -\sum_{i} P(x_i) \log_b P(x_i) Usually, b=2b=2 for bits (Shannon entropy).

python
1import math
2 
3def calculate_entropy(probabilities):
4 entropy = 0
5 for p in probabilities:
6 if p > 0:
7 entropy -= p * math.log2(p)
8 return entropy
9 
10# Fair coin
11print(f"Fair coin entropy: {calculate_entropy([0.5, 0.5])} bits")
12 
13# Biased coin (90% heads)
14print(f"Biased coin entropy: {calculate_entropy([0.9, 0.1]):.3f} bits")
15 
16# Single certain outcome
17print(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 PP and QQ: DKL(PQ)=xXP(x)log2(P(x)Q(x))D_{KL}(P || Q) = \sum_{x \in \mathcal{X}} P(x) \log_2 \left( \frac{P(x)}{Q(x)} \right)

It quantifies the information lost when QQ is used to approximate PP.

Mutual Information I(X;Y)I(X; Y) measures the reduction in uncertainty of XX given the knowledge of YY: I(X;Y)=xXyYP(x,y)log2(P(x,y)P(x)P(y))I(X; Y) = \sum_{x \in \mathcal{X}} \sum_{y \in \mathcal{Y}} P(x, y) \log_2 \left( \frac{P(x, y)}{P(x)P(y)} \right)

It is related to entropy by: I(X;Y)=H(X)H(XY)=H(X)+H(Y)H(X,Y)I(X; Y) = H(X) - H(X|Y) = H(X) + H(Y) - H(X, Y).

python
1import math
2 
3def kl_divergence(P, Q):
4 # D_KL(P || Q) - Sum P(x) log(P(x)/Q(x))
5 res = 0
6 for p, q in zip(P, Q):
7 if p > 0:
8 res += p * math.log2(p / q)
9 return res
10 
11P = [0.1, 0.9]
12Q = [0.5, 0.5]
13print(f"KL Divergence D_KL(P || Q): {kl_divergence(P, Q):.3f}")
14 
15def mutual_information(joint_prob, p_x, p_y):
16 # I(X;Y) = Sum P(x,y) log(P(x,y)/(P(x)P(y)))
17 mi = 0
18 for x in range(len(p_x)):
19 for y in range(len(p_y)):
20 pxy = joint_prob[x][y]
21 if pxy > 0:
22 mi += pxy * math.log2(pxy / (p_x[x] * p_y[y]))
23 return mi
24 
25# Example: X and Y are highly correlated
26px = [0.5, 0.5]
27py = [0.5, 0.5]
28joint = [[0.45, 0.05], [0.05, 0.45]]
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.

If a random variable has 4 equally likely outcomes, what is its entropy?

Channel Capacity

The Shannon-Hartley Theorem defines the maximum rate CC at which information can be transmitted over a noisy channel with bandwidth BB and signal-to-noise ratio S/NS/N: C=Blog2(1+S/N)C = B \log_2(1 + S/N)

What happens to entropy as outcomes become more predictable?