TMC #0012: Information Theory: Shannon Entropy & Channel Capacity
A pure-Python implementation of Claude Shannon's 1948 information theory: Shannon entropy, mutual information, Shannon-Hartley channel capacity, BSC capacity, and optimal Huffman source coding.
Shannon's 1948 paper gave engineers the first rigorous answers to three questions: what is information, how efficiently can a source be encoded, and what is the maximum rate at which a noisy channel can carry information? This toolkit implements all three results.
What It Includes
Entropy functions: entropy(probs), joint_entropy(joint_probs), conditional_entropy(joint_probs), mutual_information(joint_probs). Demonstrates the entropy of a fair coin (1.0 bit), fair die (2.585 bits), and English letter distribution (~4.11 bits/letter vs a theoretical max of 4.70 bits).
Channel capacity: shannon_hartley_capacity(bandwidth_hz, snr_linear) implementing . Sweeps SNR from 0 dB to 40 dB for a 1 MHz channel; compares against practical modem speeds by decade. bsc_capacity(p) computes the Binary Symmetric Channel capacity .
Huffman coder: huffman_codebook(frequencies) and average_code_length(codebook, frequencies). Builds an optimal prefix-free code for English letter frequencies and measures how close the average code length is to the entropy bound. Demonstrates that 'E' gets a 3-bit code and 'Z' gets an 8-bit code.
Running It
python information_theory.py # all demos
python information_theory.py --demo entropy # entropy examples only
python information_theory.py --demo capacity # channel capacity sweep
python information_theory.py --demo huffman # Huffman coding demo
No external dependencies: pure Python 3.10+.