Before you look
Someone hands you two closed bags. Bag A holds ten red marbles. Bag B holds five red and five blue. The marbles feel alike, so any one of them is equally likely to come out. Which colour would you bet on?
From bag A, you can say “red” before your hand goes in. From bag B, red and blue are equally good guesses. Draw a few times from each and watch the row of past draws fill up.
Bag A never surprises you. Bag B does, every time you open your hand. This article puts a number on that difference. The number is called entropy. Here we only care about the colour you get, not which particular marble.
Chance as a fraction
Before we can measure surprise, we need to say how likely each colour is. Swap one red marble in bag A for a blue one. Now nine of the ten marbles are red.
If you draw many times, putting the marble back each time, red comes out
nine times in ten on average. We write that as a fraction:
p(red) = 0.9. The letter p stands for
probability, a fancy word for chance. Blue gets
p(blue) = 0.1. Move the slider and watch the two
shares trade places.
The two chances always add up to 1, because every draw gives you one colour or the other. A chance of 1 means certain. A chance of 0 means it never happens.
Surprise
A rare outcome tells you more than a common one. With nine red and one blue, getting red is no news. Getting blue is.
To put a number on surprise, think of yes-or-no questions. Say there are eight equally likely options, numbered 1 to 8. Each answer can rule out half of what's left. How many answers until you know it was 6?
Each halving is worth one bit. An outcome with chance 1/2
carries 1 bit of surprise, one with chance 1/4 carries 2
bits, and one with chance 1/8 carries 3 bits. The general rule is
surprise = log₂(1/p): how many times you halve 1 to get down
to p.
The rule works for chances that aren't neat halves too. Blue, at 0.1, carries about 3.322 bits of surprise. Red, at 0.9, carries only about 0.152 bits.
Entropy is the average surprise
You don't know in advance which colour you'll get, so ask instead: how surprised will I be, on average, per draw? Weight each colour's surprise by its chance, then add.
In the drawing, each colour is a rectangle. Its width is the chance, its height is the surprise, so its area is chance × surprise. The blue one is tall but thin: very surprising, rarely seen. Add the two areas and you get the entropy.
For nine red and one blue, that's 0.9 × 0.152 + 0.1 × 3.322, about 0.469 bits per draw. Slide toward an even mix and watch the point climb the curve. Five and five gives the peak, exactly 1 bit. Ten of one colour gives 0 bits: bag A, where nothing is ever a surprise.
Bits as yes-or-no questions
Why call the unit a bit? Because entropy also counts questions. Take a bag with four colours in equal numbers: red, blue, yellow and purple. Each has chance 1/4, so each carries 2 bits of surprise.
Now find the colour of a draw with yes-or-no questions. “Red or blue?” splits the four in half. A second question splits the remaining pair. Whatever you drew, two answers are enough.
The entropy of this bag is 2 bits, and you needed exactly two questions. For bag B it is 1 bit: one question, “red?”. For lopsided bags the entropy falls below one. You can't ask a fraction of a question about one draw, but over a long run of draws, a clever scheme needs about 0.469 answers per draw for the nine-to-one bag, and no scheme can do with fewer.
A computer asks a question
Back to bag B, five red and five blue. A toy shop keeps a record for each of those ten marbles: its colour, whether it is large, and whether it sat on the top shelf. The shop wants a rule that guesses the colour of a new marble from its other details.
A computer can build that rule by trying questions on the records. Here are two. Which one leaves each group easier to predict?
Both shelves keep an even mix, so knowing the shelf tells you nothing about colour. Size helps. All 4 large marbles are red. Of the 6 small ones, 5 are blue. The large group has 0 bits of uncertainty left; the small group has about 0.650.
Information gain
To compare the questions, the computer needs one number per question. It takes the uncertainty left in each group and averages it, weighting each group by its share of the records. A bigger group counts for more, because a new marble is more likely to land in it.
For size, that's 4/10 × 0 + 6/10 × 0.650, about 0.390 bits left, down from 1 bit. The drop is the question's information gain: about 0.610 bits.
The shelf question leaves 1 bit, a gain of 0. A decision tree is built this way: at each step it asks the question with the largest gain, then repeats inside each group. With only ten records the rule is a guess; more records would show whether it holds for the next batch.
Where entropy came from
Physicists used the word first. In 1865 Rudolf Clausius named entropy after the Greek tropē, transformation, to describe heat that can no longer do useful work. In 1928 Ralph Hartley, at Bell Labs, proposed measuring information as the logarithm of the number of possible choices: the equally likely case you saw with four colours.
In 1948 Claude Shannon, also at Bell Labs, published A Mathematical Theory of Communication. He extended Hartley's measure to outcomes with unequal chances, the average surprise you just computed, and called it entropy too. The word bit, short for binary digit, came from his colleague John Tukey.
Shannon's entropy is also a limit. On average, no lossless compression can store a source in fewer bits than its entropy. Huffman coding, from 1952, gets close, and still works inside ZIP files and JPEG images. In 1986 Ross Quinlan published ID3, a decision tree that picks each question by information gain, the measure you just used on the marbles.
Today a close relative, cross-entropy, trains many machine learning
models. It measures how surprised a model is by the right answer. A spam
filter, a photo classifier and a language model guessing the next word all
learn by making that surprise smaller. In scikit-learn, a decision tree
created with criterion="entropy" scores its questions by
information gain, exactly as the shop's computer did.
Filed under Machine learning