Information Theory ExercisesInformation Theory · Exercises
0 / 0

Workbench / Information Theory Exercises

B1·04

Entropy: the Average Surprise of a Source

Exercises for chapter 4: entropy as an average in your own words, why uniform is the maximum, independence and grouping, entropy against counting, redundancy as protection, and what an entropy figure is relative to; inverting the binary entropy function, the order-0 entropy of Shakespeare, Python's source code, a compiled program and compressed output, letter entropy estimated from short windows, question strategies for a sensor, what adding two dice loses, and senior exercises on encoding columns by their entropy, a token generator with too little entropy, and choosing a first question.

20 topics · ~5 min read
Updated Suggest a correction

Start here: how these exercises work

Write your answer in the box first; hints open one at a time as you write, and the solution a couple of minutes after the last hint. Theory is in your own words, practice and programs come with real output (Python 3.13), cumulative projects use chapters 1 to 4 together, what the chapter leaves out explains what it skips, and senior exercises put the ideas into designs and reviews.

Theory

T1. Entropy as an average

Explain entropy to someone who understands chapter 1's surprise but not the formula. Use the 90/10 coin, and say why its entropy is closer to the surprise of heads than to the surprise of tails.

T2. Why equally likely is the most uncertain

Explain in words why a source with K symbols has the most entropy when all are equally likely, and why making any symbol more likely than another lowers it.

T3. Entropy against counting

Chapter 2 measured information by counting possibilities, log2K. Chapter 4 measures it by H. Explain when they agree, when they differ, and which one a compressor should be judged against.

T4. Grouping as a question strategy

Use the grouping property to explain how a question strategy's average number of questions relates to entropy. Why does a strategy whose questions all have answers close to 50/50 come close to the entropy?

T5. Redundancy as protection

A friend says English's redundancy is "wasted letters" that a better language would remove. Argue the other side.

T6. What is an entropy figure relative to?

"The entropy of this file is 4.78 bits per byte." Explain everything this sentence assumes, and give two different entropy figures for the same file that would both be correct.

Practice and programs

P1. By hand

(a) Find the entropy of the probabilities 12,18,18,18,18. (b) A source has entropy 3 bits and 8 symbols. What can you say about its probabilities? (c) Is H(0.3,0.3,0.4) more or less than log23? (d) A source with 2 symbols has redundancy 50 %. What's its entropy, and what (to two decimal places) is the probability of its likelier symbol?

P2. Inverting the binary entropy function

Write h_inverse(target) that finds the p≤12 with h(p)= target by halving, and tabulate it for targets 0.01, 0.1, 0.5, 0.9 and 0.99 bits. What do the results say about how biased a coin must be to carry only a tenth of a bit?

P3. Four kinds of data

Compute the order-0 byte entropy and lzma's bits per byte for: Shakespeare; the first 2 MB of Python's standard library source (.py files); the first 2 MB of the python3 program itself; and lzma's own compressed output of Shakespeare. Explain each pair of numbers.

P4. Estimating letter entropy from short windows

Estimate the entropy of the plays' letters from windows of 50, 200, 1,000, 5,000 and 50,000 letters taken from 40 places in the file. Compare the average estimate with the whole-file figure, with and without Miller's correction, and look at the spread.

Cumulative projects (chapters so far: 01–04)

C1. Questions for a sensor

A soil sensor reports one of 8 states with probabilities 0.65, 0.2, 0.1, 0.02, 0.01, 0.01, 0.005, 0.005. Compute the entropy, then the average number of questions for three strategies: fixed-length (3 questions), "is it the likeliest remaining state?" one at a time, and "dry?" followed by halving the rest. Which comes closest to the entropy, and why can none reach it?

C2. What adding two dice loses

Two fair dice are rolled. Compute the entropy of: the pair; the sum; the larger; the difference; whether it's a double; and the sum and difference together. Which descriptions lose information, and which keep it all?

What the chapter leaves out

Differential entropy

For quantities with a continuous range (a voltage, a temperature), the formula becomes an integral, −∫f(x)log2f(x)dx over a probability density, called differential entropy. It behaves differently: it can be negative, and it changes if you change the units. What's meaningful are differences between differential entropies, which chapter 9 uses for the capacity of a channel with Gaussian noise.

Rényi and other entropies

Alfréd Rényi (1961) defined a family of entropies, Hα =11−αlog2∑pα, of which Shannon's is the limit as α→1. H0 is log2 of the number of possible outcomes (Hartley's measure), H2 is the collision entropy (the chance two draws match, which matters for hashing), and H∞=−log2maxp is the min-entropy, the right measure of how hard a secret is to guess in one try. Security standards use min-entropy for random number generators, because an attacker targets the likeliest value (chapter 10).

The maximum entropy principle

E. T. Jaynes (1957) proposed that, when you know some facts about a distribution (its mean, say) and nothing else, you should choose the distribution with the largest entropy consistent with them: it assumes the least. It recovers the uniform distribution when nothing is known, the exponential and geometric distributions when the mean is known, and the normal distribution when the mean and variance are known, and it gives statistical mechanics in physics as an inference problem.

Entropy rate

Order-0 entropy treats symbols as independent. A source with memory has an entropy rate: the limit of the entropy of n symbols divided by n. For English it's far below the order-0 figure. Chapter 7 develops conditional entropy, which computes it for Markov sources, and chapter 10 estimates it for real text.

Senior exercises

S1. Choosing encodings by entropy

A service writes 100,000 request records a day with a timestamp, method, path, status and user id. Compute each column's order-0 entropy and its cost as text, and propose an encoding for each. One column's entropy figure is misleading: which, and why?

S2. A token generator with too little entropy

A legacy service makes password-reset tokens by hashing the user's email together with the current time in milliseconds, and outputs a 64-character hex string. A reviewer calls it "256 bits of entropy". Estimate the real entropy against an attacker, and say what to replace it with.

S3. Choosing a first question

A support team triages tickets into 6 categories with frequencies: login 40 %, billing 25 %, bug 15 %, feature request 10 %, account deletion 6 %, other 4 %. Their form asks one yes-or-no question first. Compare "Is it about logging in?" with "Is it about money or your account (billing or account deletion)?" and with "Is it about logging in or billing?" by how much each reduces the entropy of the category, and recommend one.