S830 of 56
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

S8 · Lesson 30 · Entropy and source coding

Measuring information in bits, entropy and the binary entropy function, the source coding theorem, Huffman codes and their efficiency

Shannon's 1948 paper began by asking how much information a message really contains, and answered with a formula: a symbol that occurs with probability p carries −log₂ p bits, and a source's average, its entropy, is the least number of bits per symbol any lossless code can use. That one number explains why text compresses, why random data doesn't, and how close a practical code can get. Huffman's 1952 algorithm builds the best possible prefix code for any set of probabilities, often within a few percent of the entropy, and it still sits inside JPEG, MP3 and ZIP.

  • The binary entropy function H(p)=−plog2⁡p−(1−p)log2⁡(1−p): the information per symbol of a source emitting 1 with probability p.
  • A fair coin (p = 0.5) carries a full bit per toss. A biased one carries less: at p = 0.1, only 0.469 bits, so long runs of its output can be compressed to under half their length.
  • At p = 0 or 1 the outcome is certain and carries nothing.

Predict

A source emits A, B, C and D with probabilities 0.5, 0.25, 0.125 and 0.125. How many bits per symbol does the best lossless code need, on average?

Name it

Information and entropy

A symbol of probability p carries self-information I=−log2⁡p bits: rare events carry more. A source's entropy is the average:

H=−∑ipilog2⁡pi bits/symbol

with 0≤H≤log2⁡M for M symbols, the maximum when all are equally likely. For two symbols it's the binary entropy function H(p), peaking at 1 bit for p = ½.

The source coding theorem

No uniquely decodable code can average fewer than H bits per symbol, and codes exist that come within 1 bit of it, or arbitrarily close by coding blocks of symbols together. A code's efficiency is H/L for average length L.

Huffman coding

Repeatedly merge the two least probable symbols into one node whose probability is their sum, until one node remains; reading the branches from the root gives an optimal prefix code. For probabilities 0.4, 0.3, 0.2 and 0.1: merge 0.1 and 0.2 → 0.3, then 0.3 and 0.3 → 0.6, then 0.4 and 0.6, giving lengths 1, 2, 3, 3 and L = 1.9 bits, against H = 1.846: 97.2% efficient.

Prefix codes: no codeword is the start of another, so a stream decodes without separators.

Worked example

Find the entropy of a source with probabilities 0.4, 0.3, 0.2 and 0.1, and the efficiency of its Huffman code.

Show the solution
  1. Entropy. The four terms −plog2⁡p are H=0.529+0.521+0.464+0.332=1.846bits
  2. Huffman code. The lengths are 1, 2, 3 and 3, so the average is L¯=0.4+0.6+0.6+0.3=1.9bits
  3. Efficiency. 1.8461.9=97.2%
  4. Doing better. Coding pairs of symbols would approach 1.846 more closely.

Answer the entropy is 1.846 bits, and the Huffman code, at 1.9 bits, is 97.2% efficient.

Worked example

A binary source emits 1s with probability 0.1. How much could a million of its bits be compressed?

Show the solution
  1. Entropy. H(0.1)=0.469bits per symbol
  2. A million bits. On average they need only 106×0.469=469000bits less than half.
  3. In practice. Run-length or arithmetic coding gets close to that bound for long sequences; a fair coin's output (H = 1) can't be compressed at all.

Answer to about 469,000 bits on average, less than half.

Worked example

A binary source emits 1s with probability 0.1. How efficient is Huffman coding of single symbols, and of pairs?

Show the solution
  1. Entropy. H(0.1)=0.469bits per symbol
  2. Single symbols. They can't be coded in less than 1 bit each, so the efficiency is 47%.
  3. Pairs. The pairs have probabilities 0.81, 0.09, 0.09 and 0.01, and Huffman gives them lengths 1, 2, 3 and 3: L¯=0.81+0.18+0.27+0.03=1.29bits per pair which is 0.645 per symbol.
  4. Efficiency of pairs. 0.4690.645=73% Longer blocks approach the entropy.

Answer 47% efficient for single symbols and 73% for pairs.

Check

An event has probability 0.1. How much information does its occurrence carry?

Check

A four-symbol source has probabilities 0.5, 0.2, 0.15 and the remainder. What's its entropy?

Check

What's the binary entropy H(0.11)?

Check

A source has entropy 2.2 bits, and a code for it averages 2.25 bits per symbol. How efficient is the code?

Check

A source has 26 equally likely symbols. What's its entropy?

Check

How many bits, at least, does a lossless code need on average for 1000 outputs of a binary source with P(1) = 0.02?

Check

A three-symbol source has probabilities 0.6, 0.2 and the remainder (with 0.6 the largest). Huffman gives the largest a 1-bit code and the others 2 bits each.

(a)

What is its entropy?

(b)

How efficient is the Huffman code?

Check

A binary source emits 1s with probability p.

Step 1

What is its entropy?

Type it the way you would write it, like 3sin(2πft) or 3*sin(2*pi*f*t). Angles can be in radians or carry a degree sign, like 45°.

Explain why

Why is entropy the limit of lossless compression?

Practice

Keep practicing

10 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.