Notes

Entropy

Can you guess the next marble?

Reach into a bag without looking. The mix of colours decides how good your guess can be. Put a number on that uncertainty, then watch a computer use it to choose which question to ask.

Overview: bag A holds ten red marbles and has an entropy of 0 bits. Bag B holds five red and five blue and has an entropy of 1 bit.BAG A10 RED0 bitsENTROPYBAG B5 RED · 5 BLUE1 bitENTROPY

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.

Two open containers of ten marbles. Bag A: ten red. Bag B: five red and five striped blue. Each draw is shown in a slot and added to a row of the last ten draws.BAG A10 REDDRAWNLAST 10 DRAWSred 0 · blue 0BAG B5 RED · 5 BLUEDRAWNLAST 10 DRAWSred 0 · blue 0
Guess the colour you will get from each bag, then draw. Each marble goes back after it is drawn.

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.

Ten marbles above a bar of ten cells. Each cell is one tenth. The red cells make up p(red), the striped blue cells p(blue), and the two always add up to 1.THE BAG · 10 MARBLES00.51P(RED)9/10 = 0.9P(BLUE)1/10 = 0.1together = 1
9 red and 1 blue. Red comes out 9 times in 10 on average, so p(red) = 0.9 and p(blue) = 0.1.

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?

Eight equally likely options numbered 1 to 8. Each yes or no answer rules out half of them: 8, then 4, then 2, then 1. The chance of the answers so far halves each time, from 1 to 1/2, 1/4 and 1/8, and the surprise grows by one bit each time, to 3 bits.QUESTIONCANDIDATESCHANCESURPRISEStart1234567810 bitsIs it 5 to 8?→ yes1122334456781/21 bitIs it 7 or 8?→ no12345677881/42 bitsIs it 6?→ yes1234556781/83 bitssurprise = log₂(1/p) = the number of halvings
Three answers narrow eight options down to one. An outcome with chance 1/8 carries 3 bits of surprise.

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.

Left: for each colour, a rectangle as wide as its chance and as tall as its surprise. The dashed line shows their combined area spread over the full width: the entropy. Right: the entropy for every mix from 0 to 10 red marbles, peaking at 1 bit for five and five, with a point at the current mix. Below, the sum written out.CHANCE × SURPRISEENTROPY OF EVERY MIX0123bits01CHANCE P0.46901010RED MARBLESCOLOURCHANCE PSURPRISEP × SURPRISEred0.9×0.152=0.137blue0.1×3.322=0.332Entropy, the sum0.469bits
9 red, 1 blue. Each colour's chance × surprise is an area; together they make 0.469 bits of entropy per draw.

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.

A question tree. Q1 asks: red or blue? Yes leads to Q2: is it red? No leads to Q2: is it yellow? The four leaves are red, blue, yellow and purple, each with chance 1/4, each reached after exactly two questions. Entropy = 4 × 1/4 × 2 = 2 bits.yesnoyesnoyesnoQ1Red or blue?Q2Is it red?Q2Is it yellow?REDp = 1/4 · 2 bitsBLUEp = 1/4 · 2 bitsYELLOWp = 1/4 · 2 bitsPURPLEp = 1/4 · 2 bits2 QUESTIONSentropy = 4 × 1/4 × 2 = 2 bits
Four equally likely colours. Every colour takes exactly two yes or no answers to pin down.

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?

A table of ten marble records with their colour, size and shelf. One question sorts the records into a yes tray and a no tray, each labelled with its count of red and blue and its entropy.RECORDCOLOURLARGETOP SHELF1yesyes2yesyes3yesno4yesno5nono6noyes7noyes8nono9nono10nonoPick a question belowYESNO
Five red, five blue: 1 bit of uncertainty before any question.

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.

Information gain. Before the question the ten records hold 1 bit of uncertainty. After it, each group is a block as wide as its share of the records and as tall as its own entropy. Their combined area, spread over the full width, is the uncertainty left. The gain is the drop from 1 bit.UNCERTAINTY LEFT AFTER THE QUESTION00.51bitsBEFORE · 1 BITLARGE4 of 10 records0 bitsSMALL6 of 10 records0.650 bitsAFTER · 0.390 BITSGAIN0.610 bitsafter = 4/10 × 0 + 6/10 × 0.650 = 0.390 bitsgain = 1 − 0.390 = 0.610 bits
Large records are all red: 0 bits. Small ones keep 0.650 bits, but they are 6 of the 10, so they count for 6/10 of it. The question removes 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.

How entropy reached machine learning+63 y1865Clausius names entropyin physics: heat that can no longer do work+20 y1928Hartley measures informationthe logarithm of the number of choices+4 y1948Shannon's information entropyuncertainty measured in bits+34 y1952Huffman codingcompression close to the entropy limit1986ID3 decision treesRoss Quinlan picks questions by information gainTodayCross-entropy trains modelsspam filters, image classifiers, language models
One idea, three jobs: a law of heat, a limit on compression, and the score a model learns to lower.

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