Guide to AI and Data Courses · Lecture Notes
Chapter 34: Information Theory
In the game of twenty questions we can find a number within twenty steps if every question splits the remaining candidates in half; if the questions do not split them, the same job takes far longer. Learning a message reduces uncertainty, and that reduction can be counted. Information theory measures this with probability: the more unexpected an event, the more information it carries. This note introduces the course and shows its three core ideas, entropy, the compression limit and channel capacity, with small examples you can compute by hand.
Learning outcomes
- Measuring information
- Compute the information of an event and the entropy of a source in bits; say when entropy is largest and smallest.
- Comparing distributions
- Compute KL divergence and cross-entropy for two distributions and show that the divergence is not symmetric.
- Compression
- Build a Huffman code for a source, compare its average length with the entropy and explain why compression has a lower bound.
- Channels
- Compute the capacity of a binary symmetric channel and the error probability of a repetition code.
- Place of the course
- Name the measures that connect information theory to probability, signal processing, machine learning and generative models, and where it differs from them.
34.1What question does the course ask?
How is information measured, and how is it compressed and transmitted with the least loss?
Information theory measures the information in a message by probability: an unlikely event carries more information, and the average information is called entropy. The course derives the limits on how far data can be compressed and how fast it can be sent reliably over a noisy channel.
The question of how much information a message carries seems at first to be about meaning, yet the engineer's job is to carry the message correctly and economically, not to interpret it. Shannon's big step was to separate information from meaning and tie it to probability. In a desert town where rain is rare, the message "it is raining" carries more information than the same message in a town where it rains every day, because it is less expected. Two questions follow: how far can the messages of a source be shortened, and how fast can they be sent reliably over a noisy line?
Both answers are limits, and the course shows how the limits are derived and how practice approaches them. Data cannot be compressed losslessly below its entropy; at rates below the channel capacity the error probability can be made as small as we like. These two results are the theoretical frame for compression programs and for the error-correcting codes in mobile networks.
34.1.1What is a bit?
A bit is the information in learning which of two equally likely outcomes occurred. A fair coin toss carries 1 bit, a fair die roll log2 6 ≈ 2.585 bits. In general an event of probability p carries log2(1/p) bits. If the logarithm base is e instead of 2 the unit is the "nat"; machine learning software usually uses natural logarithms, the theory is the same.
34.1.2At a glance
| Criterion | Information Theory |
|---|---|
| Approach | measuring information with probability |
| Input | source and channel model |
| Output | bounds, codes, compression rate |
| Success measure | bits, channel capacity |
| Math load | high: probability |
| Data needs | none |
34.2A short history
| Year | Milestone |
|---|---|
| 1928 | Hartley proposes measuring information by the logarithm of the number of possible messages. |
| 1948 | Shannon defines entropy and channel capacity in "A Mathematical Theory of Communication". |
| 1951 | Kullback and Leibler publish a divergence measure between two probability distributions. |
| 1952 | Huffman publishes the algorithm that builds the prefix code with minimum average length. |
| 1977 | Lempel and Ziv publish the dictionary-based LZ77 compressor, ancestor of ZIP and PNG. |
| 1993 | Turbo codes arrive: a practical code comes within about half a decibel of the Shannon limit. |
34.3Core concepts
The rest of the course is built on the concepts below. The following sections work through the most important ones with examples.
| Concept | Meaning |
|---|---|
| Entropy | Average information per symbol of a source, in bits. |
| Conditional entropy | Uncertainty left in one variable once the other is known. |
| Mutual information | How much two variables tell about each other. |
| KL divergence | Extra cost of coding one distribution with a code built for another. |
| Cross-entropy | The most common loss function in classification. |
| Source coding | Lossless compression: Huffman, arithmetic coding, LZ. |
| Channel capacity | Highest rate that can pass reliably through a noisy channel. |
| Error-correcting codes | Adding redundancy to detect and correct errors. |
34.4Entropy, cross-entropy and KL divergence
If a source produces symbols with given probabilities, the average information per symbol is called entropy. The information of each symbol is log2(1/p) bits, so entropy is the probability-weighted average of these values.
H(p) = −Σ pi log2 pi bits. It is largest when all symbols are equally likely (log2 n) and zero when the outcome is certain.
The first source produces four symbols with probabilities p = (1/2, 1/4, 1/8, 1/8). The second is a loaded coin: heads has probability 0.9, tails 0.1.
- Information of each symbol. In the first source the symbols carry 1, 2, 3 and 3 bits (log2 2, log2 4, log2 8).
- Average. H = 0.5·1 + 0.25·2 + 0.125·3 + 0.125·3 = 1.75 bits. With four equally likely symbols it would be 2 bits.
- Loaded coin. Heads carries log2(1/0.9) ≈ 0.152 bits, tails log2 10 ≈ 3.322 bits.
- Average. H = 0.9·0.152 + 0.1·3.322 ≈ 0.469 bits, far below the 1 bit of a fair coin.
When the outcome is close to what we expect, there is little surprise: in a world where heads usually comes up, each toss carries less than half a bit.
34.4.1Comparing two distributions
If the true distribution is p but we write the messages with a code built for a distribution q, we pay on average the cross-entropy H(p, q) = −Σ pi log2 qi bits per symbol. The extra amount we pay is the KL divergence: D(p‖q) = H(p, q) − H(p) = Σ pi log2(pi/qi). It is never negative and is zero only when p = q.
Let the true coin be fair, p = (0.5, 0.5), and the wrong model q = (0.9, 0.1).
- Cross-entropy. H(p, q) = −0.5 log2 0.9 − 0.5 log2 0.1 ≈ 0.5·0.152 + 0.5·3.322 ≈ 1.737 bits.
- Divergence. D(p‖q) = 1.737 − 1 ≈ 0.737 bits: with the wrong model we pay 0.737 bits extra per symbol.
- Reverse direction. D(q‖p) = 0.9 log2(0.9/0.5) + 0.1 log2(0.1/0.5) ≈ 0.9·0.848 − 0.1·2.322 ≈ 0.531 bits.
The two directions differ, so KL divergence is not a distance. In classification the true distribution is a vector with a single 1, and the cross-entropy reduces to −log p of the probability given to the correct class: this is the "cross-entropy loss" of machine learning.
Confusing entropy with how "meaningful" a message is. Entropy depends only on the probability distribution: a text of random characters has higher entropy than a meaningful text and therefore compresses less.
34.5Source coding: Huffman codes and the compression limit
When symbols are coded as binary strings, giving frequent symbols short codes and rare ones long codes lowers the average length. A code in which no codeword is the beginning of another is a prefix code; it can be decoded without separators. The codeword lengths li of a prefix code satisfy Σ 2−li ≤ 1 (the Kraft inequality), and the average length cannot fall below the entropy.
Any prefix code that losslessly encodes a source has average length L ≥ H bits per symbol; the best prefix code satisfies H ≤ L < H + 1.
The Huffman algorithm builds this best prefix code greedily: merge the two smallest probabilities, put the sum back in the list and repeat until one node remains. The merges draw a tree; the branches on the path from the root to a symbol give its codeword.
Symbols A, B, C, D with probabilities 0.4, 0.3, 0.2, 0.1.
- Merge. The two smallest are 0.2 and 0.1: sum 0.3. The list is now 0.4, 0.3, 0.3.
- Merge again. The two 0.3 merge into 0.6. The list is 0.4, 0.6.
- Final merge. 0.4 and 0.6 join at the root.
- Lengths. A sits right under the root, length 1 (code 0); B has length 2 (10); C and D length 3 (110, 111). Kraft sum 1/2 + 1/4 + 1/8 + 1/8 = 1.
- Average length. L = 0.4·1 + 0.3·2 + 0.2·3 + 0.1·3 = 1.9 bits. The entropy is H ≈ 1.846 bits; a fixed-length code would need 2 bits.
The Huffman code comes within 0.054 bits of the entropy, but cannot go below it.
A Huffman code spends at least 1 bit per symbol, so for sources with very small entropy the waste is relatively large. The remedy is to code symbols in groups; arithmetic coding takes this idea to its limit.
A two-symbol source has probabilities 0.9 and 0.1; the entropy is 0.469 bits. Coding symbol by symbol, Huffman gives 1 bit. Now group the symbols in pairs.
- Pair probabilities. AA: 0.81; AB: 0.09; BA: 0.09; BB: 0.01.
- Huffman. 0.01 and 0.09 merge (0.10); then 0.09 with 0.10 (0.19); finally 0.81 with 0.19. Lengths 1, 2, 3, 3.
- Average. 0.81·1 + 0.09·2 + 0.09·3 + 0.01·3 = 1.29 bits per pair, that is 0.645 bits per symbol.
We went from 1 bit down to 0.645; with triples or longer groups the value approaches 0.469.
34.6The noisy channel and channel capacity
Compression throws away the redundancy of the source; in transmission, by contrast, redundancy is added on purpose to guard against errors. The simplest noisy channel is the binary symmetric channel (BSC): each transmitted bit is flipped, independently, with probability p. The receiver does not know which bit was corrupted, only how many are corrupted on average.
The highest number of bits per channel use that can be sent reliably: C = max I(X; Y), where I(X; Y) = H(Y) − H(Y | X) is the mutual information the input carries about the output. For the BSC, C = 1 − H(p).
The channel coding theorem makes a two-sided promise: for a code family of rate R < C the error probability can be made as small as we like by lengthening the block; for R > C no code can do it. The theorem does not tell us how, it only draws the limit; Hamming, LDPC, turbo and polar codes are efforts to approach it.
The channel flips each bit with probability p = 0.1.
- Capacity. H(0.1) ≈ 0.469, so C = 1 − 0.469 ≈ 0.531 bits per use. Even though the channel corrupts 10% of the bits, this is the most information that can be sent reliably.
- Repetition code. Send every bit three times (rate 1/3) and decode by majority. The decision is wrong only if at least two of the three bits are corrupted.
- Error probability. 3p2(1 − p) + p3 = 3·0.01·0.9 + 0.001 = 0.028. The error fell from 10% to 2.8%.
- Five repetitions. Wrong if at least three of five are corrupted: 10p3(1 − p)2 + 5p4(1 − p) + p5 = 0.0081 + 0.00045 + 0.00001 ≈ 0.0086; rate 1/5.
The repetition code lowers the error but also the rate: as the error goes to zero, so does the rate. Shannon's proof says the error can be driven to zero while the rate stays fixed below 0.531; clever codes such as Hamming codes open the way.
Believing that "if the rate is below capacity, there are no errors". The theorem says the error probability can be made as small as we like as the block length grows; short blocks still err and encoding and decoding delay grows. If the rate is above capacity the error probability cannot be driven toward zero.
34.7How is the course taught?
34.7.1A typical workflow
A photo sent from a phone is first compressed with JPEG; the wireless channel code then adds controlled redundancy. Bits corrupted in the air are corrected with that redundancy at the receiver and the photo opens. Compression and protection look like opposites, yet both come from the same theory.
34.7.2Topics over the semester
- Entropy, joint and conditional entropy
- Mutual information and KL divergence
- Jensen's and the data processing inequalities
- Typical sets and the AEP
- Source coding theorem and Kraft inequality
- Huffman codes and arithmetic coding
- Universal compression: Lempel-Ziv
- Channel capacity and the channel coding theorem
- Differential entropy and continuous variables
- The Gaussian channel and Shannon-Hartley
- Rate-distortion theory and lossy compression
- Error-correcting codes: Hamming, LDPC
This is a typical semester; the order and coverage vary between universities.
34.7.3Skills you will gain
- computing entropy and mutual information
- writing a Huffman coder
- measuring compression with zlib
- computing channel capacity
- writing proofs with inequalities
34.7.4Try it yourself
The Try It: Send the Message in Fewer Bits slide in the presentation lets you solve a typical problem of this course yourself: A battery-powered sensor sends its messages by radio, and every bit costs energy. Type a text or pick a preset: the Huffman tree gives frequent letters short codes. Click a letter in the tree to see its code.
34.8Are you ready for this course?
34.8.1Prerequisites
| Course | Why it is needed |
|---|---|
| Discrete Mathematics | logarithms, sets, counting and binary representation |
| Algorithms | greedy algorithms and priority queues: building a Huffman code |
34.8.2Ask yourself
For every "no", the right-hand column shows where to start. If you can say "yes" to most of them, you are ready.
| Question | If not, start with |
|---|---|
| Can you use the rules of logarithms and compute in base 2? | Logarithms from high school math |
| Can you explain conditional probability and expected value? | Probability and Statistics |
| Can you write a number in binary and tell how many bits it needs? | Number bases from discrete mathematics |
| Can you write a greedy algorithm that uses a priority queue? | Greedy methods and heaps from an algorithms course |
34.9Neighboring courses
34.9.1Where it overlaps
| Relation | Course | Shared ground |
|---|---|---|
| builds on | Generative Models builds on this course | KL divergence, likelihood |
| shares methods | Shares methods with Machine Learning | cross-entropy loss, decision tree split criterion |
| shares methods | Shares methods with Digital Signal Processing | compression, sampling, channels |
| shares methods | Shares methods with Probability and Statistics | entropy, mutual information |
34.9.2Where it differs
- Probability and Statistics: Probability and statistics describe distributions and uncertainty; this course derives information content and transmission limits from them.
- Digital Signal Processing: Signal processing filters and transforms a signal; this course asks how much information it can carry and how fast.
- Machine Learning: ML uses cross-entropy as a loss function; this course explains where that measure comes from and what it means.
- Generative Models: Generative models minimize KL divergence in training; this course studies the divergence itself and its link to code length.
34.10In practice
34.10.1Applications
| Area | What is done |
|---|---|
| File compression | ZIP, gzip and PNG: lossless compression. |
| Image and audio | JPEG and MP3: lossy compression. |
| Mobile networks | Turbo codes in 4G, LDPC and polar codes in 5G. |
| Data storage | Error-correcting codes in QR codes, CDs and SSDs. |
| Machine learning | Cross-entropy loss, information gain in decision trees. |
| Feature selection | The most informative features by mutual information. |
34.10.2Risks and ethics
| Risk | Explanation |
|---|---|
| Mutual information from little data | Histogram estimates of mutual information are biased upward on small samples; an irrelevant feature looks informative. |
| The wrong KL direction | KL(p‖q) and KL(q‖p) give different models: one pushes to cover every mode, the other to lock onto a single mode. |
| Secrets leaked by compression | Compressing secret data together with attacker-chosen text lets the size reveal the secret; the CRIME attack on TLS did this. |
| Assuming independence | Treating symbols as independent overstates entropy; compression estimates and randomness assessments go wrong. |
These issues are discussed in depth in Explainable AI and AI Ethics.
34.10.3Tools and datasets
Tools: Python, NumPy, SciPy, zlib, scikit-learn, MATLAB.
Classic example problems: Loaded die and coin toss, Binary symmetric channel, Entropy of English text, Building a Huffman tree, Twenty questions game, Canterbury corpus.
Information theory is mostly done with proofs and calculation; code is written to measure entropy and to test compression ratios on real files.
34.11Common misconceptions
Information is measured by the meaning of a message.
Shannon information ignores meaning; it measures how unexpected a message is, that is, its probability.
A good program can shrink any file.
No lossless method shrinks every input; data cannot be compressed below its entropy.
Error-free transmission over a noisy channel is impossible.
Below capacity the error probability can be made as small as desired (Shannon).
KL divergence is a distance.
It is not symmetric and violates the triangle inequality: p to q and q to p give different values.
34.12Summary
- An event of probability p carries log2(1/p) bits; entropy is the average of this information and is largest when the distribution is uniform.
- Cross-entropy is the cost of coding with the wrong distribution; KL divergence is the excess, it is not symmetric, and it underlies the classification loss.
- The average length of a lossless prefix code cannot fall below the entropy; the Huffman code is the best prefix code and is within 1 bit of the entropy, and grouping closes the gap further.
- The capacity of a binary symmetric channel is 1 − H(p) bits; with codes of rate below capacity the error can be made as small as we like.
- Information theory becomes the common language of probability, compression and machine learning losses; it draws the limits, and other courses develop the methods.
34.13Test Yourself
What is the entropy of a fair six-sided die in bits? What is the entropy of a loaded die with probabilities (1/2, 1/4, 1/8, 1/16, 1/32, 1/32)?
Answer
Fair die: log2 6 ≈ 2.585 bits. Loaded die: 0.5·1 + 0.25·2 + 0.125·3 + 0.0625·4 + 2·(0.03125·5) = 0.5 + 0.5 + 0.375 + 0.25 + 0.3125 = 1.9375 bits; loading lowers the entropy.
Two binary variables have joint distribution p(0,0) = p(1,1) = 0.4, p(0,1) = p(1,0) = 0.1. What is the mutual information I(X; Y)?
Answer
Both marginals are (0.5, 0.5), so H(X) = H(Y) = 1. The joint entropy is H(X, Y) = −2·0.4 log2 0.4 − 2·0.1 log2 0.1 ≈ 1.0575 + 0.6644 = 1.722. I = 1 + 1 − 1.722 ≈ 0.278 bits (the same as the capacity of a BSC with error probability 0.2).
Build the Huffman code for four symbols with probabilities 0.5, 0.25, 0.125, 0.125 and compare the average length with the entropy.
Answer
0.125 and 0.125 merge (0.25); then 0.25 with 0.25 (0.5); finally 0.5 with 0.5. Lengths 1, 2, 3, 3. L = 0.5 + 0.5 + 0.375 + 0.375 = 1.75 bits; the entropy is also 1.75. Because the probabilities are negative powers of 2, Huffman reaches the entropy exactly.
A BSC flips a bit with probability 0.2. What is its capacity? Can the error probability be driven to zero at a rate of 0.4 bits per use?
Answer
H(0.2) ≈ 0.722; C = 1 − 0.722 ≈ 0.278 bits per use. A rate of 0.4 exceeds the capacity, so no; it is possible only at rates below 0.278.
A classifier gives probability 0.7 to the correct class and 0.2 and 0.1 to the other two. The true class is the first. What is the cross-entropy loss in bits and in nats? What would it be if the correct class got 0.1?
Answer
The true distribution is (1, 0, 0), so the loss is −log(0.7): log2(1/0.7) ≈ 0.515 bits, ln(1/0.7) ≈ 0.357 nats. With 0.1 it would be log2 10 ≈ 3.322 bits: being confidently wrong is costly.
A friend claims to have written a program that compresses every 1000-bit file to at most 999 bits. Is this possible?
Answer
No. There are 21000 different 1000-bit files, but strings of 999 bits or fewer number at most 21000 − 1. By the pigeonhole principle two files map to the same output and lossless decoding becomes impossible. The source coding theorem says the same thing in terms of probability.
34.14After this course
Information theory is the mathematical gateway to ML: cross-entropy, KL divergence and mutual information reappear in generative models, decision trees and representation learning. On the communications side, continue with digital signal processing and coding theory.
Courses that build on or extend this one: Generative Models.
Related careers: Communications engineer, Compression and codec developer, Machine learning researcher, Cryptography specialist, Data storage engineer.
34.15Further reading
- T. M. Cover and J. A. Thomas, Elements of Information Theory (2nd ed., 2006): the standard textbook, with proofs of every topic in this note from entropy to channel capacity.
- D. J. C. MacKay, Information Theory, Inference, and Learning Algorithms (2003): information theory together with machine learning and inference, with intuitive examples and exercises.
- C. E. Shannon, "A Mathematical Theory of Communication" (1948): the paper that founded the field, where entropy and channel capacity were first defined.
34.16References
- Cover and Thomas, Elements of Information Theory, 2nd ed., Wiley, 2006.
- MacKay, Information Theory, Inference, and Learning Algorithms, Cambridge University Press, 2003.
- Shannon, "A Mathematical Theory of Communication", Bell System Technical Journal 27, 1948.
- Shannon and Weaver, The Mathematical Theory of Communication, University of Illinois Press, 1949.
Related pages: Slides · Slides (PDF) · Course page