Information and entropy: how many bits a message really needs
لم يُترجم بعد؛ يُعرض باللغة الإنجليزية
عن هذا الدرس
A rare symbol carries more information than a common one. Follow Shannon's own example: a source sends A, B, C and D with probabilities 1/2, 1/4, 1/8 and 1/8. A plain 2-bit code spends 16 bits on an 8-symbol message; measuring each symbol's surprise gives the entropy, 1.75 bits per symbol, and a prefix-free code matched to the odds sends the same message in 14 bits. You will see why no code can beat the entropy, meet binary entropy, and compute H in Python with numpy. How to build the best code for any odds is taught in the DSA lesson on Huffman coding.
A source picks symbols
A source sends one of four symbols, A, B, C or D. A comes half the time, B a quarter, C and D an eighth each. A probability is how often a symbol comes, from 0 to 1.
symbol probability p
A 1/2 = 0.5
B 1/4 = 0.25
C 1/8 = 0.125
D 1/8 = 0.125
sum = 1
each symbol is picked on its own
(independent of the ones before)
message: A B A C A B A Dاقرأ الدرس خطوة بخطوة
A source picks symbols
A source sends one of four symbols, A, B, C or D. A comes half the time, B a quarter, C and D an eighth each. A probability is how often a symbol comes, from 0 to 1.
symbol probability p A 1/2 = 0.5 B 1/4 = 0.25 C 1/8 = 0.125 D 1/8 = 0.125 sum = 1 each symbol is picked on its own (independent of the ones before) message: A B A C A B A DFixed length: 2 bits each
Four symbols fit in 2 bits: 00, 01, 10, 11, because 2² = 4. Every symbol costs 2 bits, so the 8-symbol message costs 16 bits. Can we spend fewer?
fixed-length code A 00 B 01 C 10 D 11 bits per symbol = log2(4) = 2 message A B A C A B A D bits 8 × 2 = 16Rare means surprising
A rare symbol tells you more. Shannon measured this as information, or surprise: I = log2(1/p) bits. A (p = 1/2) carries 1 bit, B 2 bits, C and D 3 bits each.
information (surprise) of one symbol I = log2(1 / p) bits A log2(2) = 1 bit B log2(4) = 2 bits C log2(8) = 3 bits D log2(8) = 3 bits p = 1 -> I = 0 (no surprise at all) 1 bit = one fair coin tossEntropy: the average surprise
Weight each surprise by how often it comes and add: H = ½·1 + ¼·2 + ⅛·3 + ⅛·3 = 1.75 bits per symbol. This average is the entropy of the source.
entropy H = Σ p · log2(1 / p) A 0.5 × 1 = 0.5 B 0.25 × 2 = 0.5 C 0.125 × 3 = 0.375 D 0.125 × 3 = 0.375 ----- H = 1.75 bits per symbol all four equally likely: H = 2 bitsA code that fits the odds
Give each symbol as many bits as its surprise: A 0, B 10, C 110, D 111. No codeword is the start of another, so this is a prefix-free code.
codeword length = surprise A 0 1 bit B 10 2 bits C 110 3 bits D 111 3 bits prefix-free: no codeword begins another one ("0" does not begin "10", "110" or "111") (the code from Shannon's 1948 paper)Send it again: 14 bits
Now the common A costs 1 bit and the rare C and D cost 3. The same 8 symbols take 14 bits instead of 16: 12.5% fewer, and nothing is lost.
message A B A C A B A D code 0 10 0 110 0 10 0 111 bits = 4×1 + 2×2 + 1×3 + 1×3 = 14 (fixed code: 16) stream: 01001100100111Reading it back
The receiver needs no gaps between codewords. It reads bits until they match a codeword, writes that letter and starts again. Prefix-free makes the first match the only one.
stream 01001100100111 read "0" -> A read "10" -> B read "0" -> A read "110" -> C read "0" -> A read "10" -> B read "0" -> A read "111" -> D decoded: ABACABADNo code beats the entropy
14 bits / 8 symbols = 1.75 bits per symbol: exactly H. Shannon proved no decodable code (one the receiver can always split back into letters) can average fewer than H bits. Huffman's method (see the DSA lesson) finds the best code.
Shannon's source coding theorem best average length L: H ≤ L < H + 1 here L = Σ p · length = 0.5·1 + 0.25·2 + 0.125·3·2 = 1.75 = H L = H exactly because every p is a power of 1/2Two symbols: binary entropy
A source with two symbols, picked with odds p and 1 − p, has H(p) bits. A fair coin (p = 0.5) gives the most: 1 bit. At p = 0.1 it is only 0.469 bits.
H(p) = −p·log2 p − (1−p)·log2(1−p) p = 0.5 H = 1 bit (the most) p = 0.1 H = 0.1·3.322 + 0.9·0.152 = 0.469 bits p = 0 H = 0 (always the same) predictable source -> few bits neededEntropy in Python
With numpy, entropy is one line. p holds the four probabilities. np.log2 takes the base-2 log of every entry, and −(p·log2 p).sum() adds the weighted surprises: 1.75.
import numpy as np p = np.array([4, 2, 1, 1]) / 8 H = -(p * np.log2(p)).sum() print(H) # 1.75 >>> p array([0.5 , 0.25 , 0.125, 0.125]) >>> -np.log2(p) array([1., 2., 3., 3.]) print(H) shows: 1.75 (Python 3 with numpy)
أسئلة سريعة: 5
اختر إجابة واحدة لكل سؤال. يمكنك تغيير اختياراتك قبل التحقق منها.
01A symbol has probability 1/16. How much information does it carry?
I = log2(1/p) = log2(16) = 4 bits, because 2⁴ = 16. Rarer symbols carry more bits.
02A source sends 8 symbols, all equally likely. What is its entropy?
Each symbol has p = 1/8 and surprise log2(8) = 3 bits; the average of eight 3s is 3. Equal odds give the largest entropy, log2 of the number of symbols.
03Which set of codewords is prefix-free?
In 0, 10, 11 no codeword is the start of another. In the others, 0 begins 01, 1 begins 10, and 0 begins 00, so a receiver could not tell where a codeword ends.
04A source has entropy 1.75 bits per symbol. Someone claims a decodable symbol code averaging 1.5 bits per symbol. What do you say?
Shannon's source coding theorem: the average length of a uniquely decodable code is at least the entropy H. 1.5 is below 1.75, so the claim is wrong.
05A biased coin lands heads with p = 0.1. What is its binary entropy, to 2 decimals?
H(0.1) = −0.1·log2 0.1 − 0.9·log2 0.9 = 0.332 + 0.137 = 0.469 bits. A predictable coin carries less than the 1 bit of a fair one.
المسرد
- Source
- Anything that produces a stream of symbols, here A, B, C and D, each picked with a fixed probability.
- Information (surprise)
- How much a symbol tells you: I = log2(1/p) bits. A certain symbol (p = 1) tells you nothing.
- Bit
- The unit of information with base-2 logs: the information in one fair coin toss. Also a binary digit, 0 or 1.
- Entropy H
- The average information per symbol of a source, H = Σ p·log2(1/p) bits. The fewest bits per symbol any decodable code can average.
- Fixed-length code
- A code that gives every symbol the same number of bits, here 2 bits for 4 symbols.
- Prefix-free code
- A code where no codeword is the start of another, so a receiver can split a bit stream into codewords without gaps.
- Source coding theorem
- Shannon's result that the average codeword length L of a decodable code is at least H, and the best symbol code has L < H + 1.
- Binary entropy H(p)
- The entropy of a two-symbol source with odds p and 1 − p: −p·log2 p − (1−p)·log2(1−p), largest (1 bit) at p = 0.5.
الملاحظات والمصادر
- The source, its probabilities 1/2, 1/4, 1/8, 1/8 and the code 0, 10, 110, 111 are the worked example in Shannon's 1948 paper (§10). The message ABACABAD is chosen to hold the letters in exactly those proportions. Every number in the figure is computed from this model.
- The bound H ≤ L holds for any uniquely decodable code of single symbols; the best such code has L < H + 1. Coding longer blocks of symbols brings the average as close to H as you like (Shannon's §9).
- Bits move along the wire much more slowly than in any real link, so you can follow each codeword.
- C. E. Shannon, A Mathematical Theory of Communication, Bell System Technical Journal 27, 1948 (§6 entropy, §9 noiseless coding theorem, §10 the A/B/C/D example), reprint
- MIT OpenCourseWare 6.02 (Balakrishnan, Verghese, Fall 2012), notes ch. 2: Information, Entropy, and the Motivation for Source Codes
- MIT OpenCourseWare 6.02, readings (ch. 3: Huffman and LZW compression)
- MIT OpenCourseWare 6.450 Principles of Digital Communications I (Gallager, Zheng, Fall 2006), notes ch. 2: Coding for Discrete Sources
- T. M. Cover, J. A. Thomas, Elements of Information Theory, 2nd ed., Wiley, 2006
- Shannon's source coding theorem (Wikipedia): H(X) ≤ E[S] < H(X) + 1 for an optimal symbol code
- Binary entropy function (Wikipedia): maximum of 1 bit at p = 1/2
- NumPy documentation: numpy.log2
اختبار يتاح بعد الخطوة الأخيرة.