Quiz: information, entropy and entropy codes
Before this unit: Day 7: Wavelets.
See also: Day 3: Bit depth and noise; Day 46: Colour; Day 18: Moments.
Short questions to test your understanding. Try each on paper first, then open the answer. Information I = -\log_2 p, entropy H = -\sum_k p_k \log_2 p_k bits per symbol.
Q1. An 8-bit image uses all 256 grey levels equally often. What is its entropy, and how much can an entropy coder save?
What is H for a flat histogram of K values?
H = \log_2 256 = 8 bits per pixel, exactly what the raw image uses: an entropy coder that treats pixels independently saves nothing. Only a method that uses the neighbours (prediction, transforms) can help, and only if the neighbours are related.
Q2. Which carries more information: a grey level that occurs in 50% of the pixels, or one that occurs in 1%? By how much?
-\log_2 0.5 and -\log_2 0.01.
The rare one: -\log_2 0.01 = 6.64 bits against -\log_2 0.5 = 1 bit, 5.64 bits more. A good code therefore gives the common grey level a short code word and the rare one a long one.
Q3. You shuffle the pixels of a micrograph at random. What happens to the entropy of its grey-level histogram? And to the size of a file compressed with a method that looks at neighbouring pixels?
Does shuffling change the histogram?
The entropy stays exactly the same: the histogram does not change. The compressed file grows, because neighbouring pixels are no longer alike (in the lecture’s example from 44 457 to 58 519 bytes). The histogram entropy is a lower bound only for coding pixels independently; neighbours carry extra structure that it ignores.
Q4. Is the code {A = 0, B = 01, C = 11} prefix-free? Can the bit string 011 be decoded uniquely?
Is any code word the beginning of another?
No: 0 (A) is the beginning of 01 (B). Here 011 can only be read as A C (0 11), because 01 followed by 1 leaves an incomplete word; but the decoder has to look ahead to see that: after the first 0 it cannot tell an A from the start of a B. With a prefix-free code (every symbol a leaf of the tree) each symbol is known as soon as its last bit arrives.
Q5. In a Huffman code, why is the code word of the most probable symbol never longer than that of a less probable symbol?
Which symbols are merged first, and what does each merge add to their code words?
Every merge adds one bit to all code words below it. The least probable trees are merged first, so their symbols take part in the most merges and get the longest code words; the most probable symbol joins late and gets a short one. If a more probable symbol had a longer word, swapping the two words would lower \bar L, so the code would not be optimal.
Q6. A binary mask has 99% background pixels. Its entropy is 0.08 bits per pixel. What is the best a Huffman code on single pixels can do, and what would you use instead?
How short can a code word be?
1 bit per pixel: each pixel needs a code word of at least one bit, more than 12 times the entropy. Better: run-length coding (code the lengths of the long background runs), Huffman on blocks of pixels, or arithmetic coding, which spends about -\log_2 0.99 = 0.015 bits on a background pixel.
Q7. Name the three redundancies of an image and say which kind of codec removes each.
Coding, between pixels, and what the eye does not need.
Coding redundancy (all values get the same number of bits): removed by entropy codes such as Huffman or arithmetic coding. Inter-pixel redundancy (neighbours are alike): removed by prediction, transforms or dictionary methods. Psychovisual redundancy (detail the eye does not see): removed by quantisation. Lossless codecs remove only the first two; only lossy codecs remove the third.
Q8. (math) Show that for two symbols with probabilities p and 1 - p the entropy is largest at p = \tfrac12. What is its value there?
Differentiate h(p) = -p \log_2 p - (1-p)\log_2(1-p).
h'(p) = -\log_2 p - \frac{1}{\ln 2} + \log_2(1 - p) + \frac{1}{\ln 2} = \log_2 \frac{1-p}{p}, zero at p = \tfrac12; h''(p) = -\frac{1}{\ln 2}\big(\frac1p + \frac1{1-p}\big) < 0, so it is a maximum: h(\tfrac12) = 1 bit.