Entropy, Information & Coding
Information theory measures information in bits. Learn self-information, Shannon entropy, joint and conditional entropy, mutual information, the source-coding theorem, Huffman coding, the binary symmetric channel and how these ideas set the limits of compression and communication — with Python entropy calculations.
How many yes/no questions do you need to identify an outcome? That number, averaged over outcomes, is the entropy. A fair coin needs one bit; a heavily biased coin needs less because the answer is usually predictable.
Claude Shannon's 1948 theory showed entropy is the minimum average number of bits for lossless compression, and mutual information bounds how fast data can pass through a noisy channel. Every ZIP file, video codec, 5G link and deep-space probe is designed with these limits in mind.
An analogy
Entropy is surprise. A weather forecast of 'sunny' in the desert carries little information because you expected it; 'snow' in the desert is very informative. Entropy is the average surprise of a source.
Bits as answers to yes/no questions
With M equally likely outcomes you need log₂M bits: 8 outcomes → 3 bits. When outcomes are unequal, ask about likely ones first — the average number of questions drops, and its minimum is exactly the entropy.
Compression and Huffman codes
Give short codewords to frequent symbols and long ones to rare symbols. Huffman's algorithm (repeatedly merge the two least likely symbols) builds an optimal prefix code. For p = {½, ¼, ⅛, ⅛} the codes 0, 10, 110, 111 give average length 1.75 bits = H exactly, because the probabilities are powers of ½.
Mutual information
I(X; Y) is how much knowing Y reduces uncertainty about X. It is symmetric, never negative, and zero only when X and Y are independent. For a channel, maximising I over input distributions gives the capacity C.
Channel coding theorem
Shannon proved that for any rate R < C there exist codes with arbitrarily small error probability, and none for R > C. Modern LDPC and polar codes (5G) operate within a fraction of a dB of this limit. For an AWGN channel the capacity is C = B log₂(1 + S/N) — covered in the next lesson.
Relative entropy (Kullback–Leibler divergence) and cross-entropy extend these ideas to machine learning loss functions.
Formulas
- Entropy:
H(X) = −Σ p(x) log₂ p(x); 0 ≤ H ≤ log₂ M - Binary entropy:
H_b(p) = −p log₂ p − (1 − p) log₂(1 − p) - Chain rule:
H(X, Y) = H(X) + H(Y|X) - Mutual information:
I(X; Y) = H(X) − H(X|Y) = H(X) + H(Y) − H(X, Y) - Source coding theorem:
H(X) ≤ L̄ < H(X) + 1 (optimal prefix code) - Kraft inequality:
Σ 2^(−ℓᵢ) ≤ 1 for any prefix code - BSC capacity:
C = 1 − H_b(ε) bits per channel use - Coding efficiency:
η = H / L̄
Worked example: Entropy of a sensor alphabet
Given: States with p = ½, ¼, ⅛, ⅛
Find: H.
- H = ½·1 + ¼·2 + ⅛·3 + ⅛·3
Answer: H = 1.75 bits/symbol
Worked example: Huffman code
Given: Same source
Find: Code and efficiency.
- Merge ⅛+⅛ → ¼; ¼+¼ → ½; ½+½
- Codes: 0, 10, 110, 111
- L̄ = ½·1 + ¼·2 + ⅛·3·2 = 1.75
Answer: L̄ = 1.75 bits, η = 100%
Worked example: BSC capacity
Given: Bit-flip probability ε = 0.1
Find: C.
- H_b(0.1) = 0.469
- C = 1 − 0.469
Answer: C = 0.531 bits/use
Entropy of sources, text and a noisy channel
import math
from collections import Counter
def H(p):
"""Shannon entropy in bits."""
return -sum(pi * math.log2(pi) for pi in p if pi > 0)
print(f"fair coin H = {H([0.5, 0.5]):.4f} bits")
print(f"biased 0.9 H = {H([0.9, 0.1]):.4f} bits")
print(f"fair die H = {H([1/6]*6):.4f} bits")
text = "ENGINEERING MATHEMATICS"
freq = Counter(text)
p = [c / len(text) for c in freq.values()]
print(f"text entropy = {H(p):.3f} bits/symbol vs {math.log2(len(freq)):.3f} max")
# Binary symmetric channel with crossover probability e
e = 0.1
Hb = H([e, 1 - e])
print(f"BSC e=0.1: capacity C = 1 - H(e) = {1 - Hb:.4f} bits/use")
# fair coin H = 1.0000 bits
# biased 0.9 H = 0.4690 bits
# fair die H = 2.5850 bits
# text entropy = 3.414 bits/symbol vs 3.585 max
# BSC e=0.1: capacity C = 1 - H(e) = 0.5310 bits/useUneven letter frequencies make text compressible (3.41 < 3.585 bits). A channel that flips 10% of bits can still carry 0.531 reliable bits per use — with good enough coding.
Common mistakes
- Wrong: Using natural log and calling it bits. — Right: log₂ → bits; ln → nats (1 nat = 1.443 bits).
- Wrong: Expecting compression below entropy. — Right: Lossless coding cannot beat H on average.
- Wrong: Thinking ε = 1 (all bits flipped) gives zero capacity. — Right: C = 1 — just invert every bit. ε = ½ is the useless channel.
Applications
- Data compression: ZIP, PNG, MP3, H.265 all approach entropy limits.
- Communications: Channel capacity guides modulation and coding choices.
- Machine learning: Cross-entropy loss; information gain in decision trees.
- Cryptography: Password and key entropy measure guessing difficulty.
Key takeaways
- H = −Σp log₂p: average surprise in bits.
- Compression limit = entropy.
- I(X;Y) measures shared information.
- Channel capacity = max mutual information.
Going further
- KL divergence: D(p‖q) = Σp log(p/q) measures mismatch between distributions.
- Rate–distortion: Minimum bits for lossy compression at a given distortion.
- Arithmetic coding: Approaches entropy even for non-dyadic probabilities.