IT·01
Start Here: What Information Is
What "information" means to an engineer: not meaning but resolved uncertainty; Shannon's 1948 picture of a source, an encoder, a noisy channel and a receiver; why an unlikely message carries more information than a likely one; surprise measured in bits, as minus the logarithm of a probability, and why it has to be a logarithm; bits as yes-or-no questions; and the difference between the bits a file stores and the information it carries, measured on Shakespeare's plays and random data.
Start here
What this chapter answers
What does "information" mean, precisely enough to measure? Why does a weather forecast of "sunny" in a desert tell you less than "snow"? Why does the measure have to be a logarithm, and why is its unit the bit? How is a bit of information related to the bits a computer stores? And why does a 1 MB file of zeros hold almost no information while a 1 MB file of random bytes can't be shrunk at all?
Information in one picture
You're waiting for a friend's exam result, which will be "pass" or "fail". Before the message arrives you don't know which; after it arrives you do. The message has resolved your uncertainty, and that's what information is in this book. If you already knew they'd pass (the exam was easy, they always pass), the message tells you almost nothing. If failure was likely and they passed, the message is a surprise, and it tells you a lot. Information is measured by how much uncertainty a message removes, which depends on what you expected, not on what the words mean.
Information is not meaning
The engineering problem
Shannon began by separating two questions. One is what a message means, whether it's true, and what the reader does about it. The other is whether the exact message chosen at one end arrives at the other. The telephone company only has to solve the second: it must deliver your words faithfully whether they're a love letter, a stock price or nonsense. Shannon's paper says outright that meaning is irrelevant to that engineering problem. What matters is that the message was chosen from a set of possible messages, and that the receiver must work out which one was chosen.
That's why information theory works for anything: text, speech, pictures, DNA, the state of a sensor. A message is a choice among possibilities, and the amount of information is a property of the set of possibilities and their probabilities, not of the particular message's content. Warren Weaver's introduction to the 1949 book edition separated three levels: the technical problem (are the symbols delivered accurately?), the semantic problem (do they carry the intended meaning?), and the effectiveness problem (does the meaning change behaviour as intended?). Information theory solves the first, and its answers limit the other two: you can't convey meaning you didn't deliver.
The communication system
Every system in this book fits the picture. In a phone call the source is a speaker, the transmitter is the phone's microphone, codec and radio, the channel is the air and the network, the noise is interference and lost packets, and the receiver is the other phone. In a zip file the "channel" is the disk and the time between saving and opening: the transmitter is the compressor and the receiver is the decompressor. Shannon's insight was that the transmitter can be split into two jobs, which the book takes in turn: source coding (remove redundancy, so the message takes as few symbols as possible; chapters 5 and 6) and channel coding (add redundancy back, carefully, so noise can be corrected; chapters 8 and 9).
Measuring information
Counting the possibilities
Hartley's idea is the starting point. If a message is one of equally likely possibilities, the amount of information it carries should depend only on . And it should add across independent messages: two messages, one from 8 possibilities and one from 4, together pick one of combinations, and should carry the information of the first plus that of the second. The function that turns multiplication into addition is the logarithm (Mathematics Book 1, chapter 10: ), so Hartley measured information as . With base 2:
is also the number of yes-or-no questions you need to single out one of equally likely possibilities, when is a power of two: each well-chosen question halves the possibilities, and you halve times to get down to one.
Surprise
Hartley's measure treats every possibility as equally likely. Shannon's step was to measure each outcome by how unlikely it was. An outcome with probability is "one of equally likely possibilities" in the sense that it's as rare as a particular choice among of them, so it carries bits.
A fair coin landing heads () carries 1 bit. A fair die showing six () carries bits. A certain outcome () carries bits: being told what you already knew tells you nothing. And as shrinks towards 0, the surprise grows without limit: winning a 6-from-49 lottery, a chance of 1 in 13,983,816, carries about 23.7 bits. The lab computes these.
Why it has to be a logarithm
You might ask whether is a sensible choice or just a convenient one. It turns out to be the only choice, given three things anyone would want from a measure of surprise:
- It depends only on the probability: .
- Less likely means more surprising: gets bigger as gets smaller, and a certain event has .
- Surprises of independent events add. If two independent events have probabilities and , both happening has probability (chapter 3 explains why), and its surprise should be the sum: .
Units: bits, nats and bans
The constant is fixed by the base of the logarithm, and each base gives a unit:
| Unit | Logarithm | One unit is the surprise of | Used in |
|---|---|---|---|
| bit (shannon) | a fair coin toss | computing, communications, this book | |
| nat | an event of probability | mathematics, physics, machine learning | |
| ban (hartley, dit) | one of 10 equally likely digits | Hartley's paper; Bletchley Park |
They convert like any units (Mathematics Book 1, chapter 11): 1 nat bits, and 1 ban bits. Alan Turing's team at Bletchley Park, breaking German naval codes in the 1940s, scored evidence in "bans" and "decibans" (tenths of a ban) and added the scores up, using exactly the additivity above. This book uses bits unless it says otherwise.
Bits as yes-or-no questions
A bit is the information in the answer to one fair yes-or-no question. That gives a practical reading of the formula: bits is the number of halving questions needed to pick out one of equally likely possibilities. For a number between 1 and a million, , so 20 questions of the form "is it above ?" always suffice, and some numbers need only 19. The lab plays the game for every number up to a million.
When isn't a power of two, isn't a whole number: a die roll carries 2.58 bits, but you can't ask 2.58 questions. Chapter 5 shows how to get the average down to the fractional value anyway, by asking questions about many rolls at once.
Stored bits and information bits
The word "bit" has two meanings, and the difference is the whole subject of compression. A binary digit is a 0 or 1 in memory: a file of 1,000,000 bytes stores 8,000,000 binary digits. A bit of information is the resolution of one fair yes-or-no question. A stored binary digit carries one bit of information only if it was equally likely to be 0 or 1 and independent of its neighbours. If it's predictable, it carries less.
A file of a million zero bytes stores 8 million binary digits but carries almost no information: once you know "it's a million zeros", every digit is certain, and the file could be replaced by that sentence. A million truly random bytes carry 8 million bits of information, and no program can shrink them. English text is in between: letters aren't equally likely ("e" is common, "z" rare), and each letter depends on the ones before ("q" is nearly always followed by "u"). The lab measures all three with real compressors. Making the "in between" precise is the job of entropy, in chapter 4.
Lab: surprise and information in Python
Experiment 1: the surprise of everyday events
import math
events = [("a fair coin lands heads", 1 / 2),
("a fair die shows a six", 1 / 6),
("a card drawn from a shuffled deck is the queen of hearts", 1 / 52),
("a 6-from-49 lottery ticket wins the jackpot", 1 / math.comb(49, 6)),
("the sun rises tomorrow (as good as certain)", 1.0)]
for name, p in events:
print(f"{name:<58} p = {p:<12.3g} surprise = {-math.log2(p) + 0.0:6.2f} bits")
# Independent events: probabilities multiply, surprises add
p_two_heads = (1 / 2) * (1 / 2)
print("\ntwo heads in a row:", -math.log2(p_two_heads), "bits =", -math.log2(1 / 2), "+", -math.log2(1 / 2))
print("a six and then heads:", round(-math.log2((1 / 6) * (1 / 2)), 4), "bits =",
round(-math.log2(1 / 6), 4), "+", round(-math.log2(1 / 2), 4))
a fair coin lands heads p = 0.5 surprise = 1.00 bits
a fair die shows a six p = 0.167 surprise = 2.58 bits
a card drawn from a shuffled deck is the queen of hearts p = 0.0192 surprise = 5.70 bits
a 6-from-49 lottery ticket wins the jackpot p = 7.15e-08 surprise = 23.74 bits
the sun rises tomorrow (as good as certain) p = 1 surprise = 0.00 bits
two heads in a row: 2.0 bits = 1.0 + 1.0
a six and then heads: 3.585 bits = 2.585 + 1.0
The rarer the event, the more bits: 1 for a coin, 2.58 for a die, 5.70 for a particular card, and 23.74 for a jackpot that comes up once in 13,983,816 draws. A certain event carries nothing. The last two lines check the property that forced the logarithm: independent events' probabilities multiply and their surprises add (a six and then heads: bits).
Experiment 2: twenty questions
import math
def questions(secret, lo, hi):
"""Find secret in lo..hi with yes/no questions 'is it above m?'; return how many were asked."""
asked = 0
while lo < hi:
mid = (lo + hi) // 2
asked += 1
if secret > mid:
lo = mid + 1
else:
hi = mid
return asked
for n in (2, 8, 1000, 1_000_000):
counts = [questions(s, 1, n) for s in range(1, n + 1)]
print(f"{n:>9,} possibilities: log2 = {math.log2(n):6.2f}, questions needed: {min(counts)} to {max(counts)}")
2 possibilities: log2 = 1.00, questions needed: 1 to 1
8 possibilities: log2 = 3.00, questions needed: 3 to 3
1,000 possibilities: log2 = 9.97, questions needed: 9 to 10
1,000,000 possibilities: log2 = 19.93, questions needed: 19 to 20
For 2 and 8 possibilities, exactly questions are needed every time. For 1,000 and 1,000,000, which aren't powers of two, the halving search needs either the whole number just below or the one just above, never more: 20 questions are always enough for a million (checked for every one of the million secrets).
Experiment 3: surprising letters
import math
from collections import Counter
text = open("../data/shakespeare.txt", encoding="utf-8").read().lower()
letters = [c for c in text if "a" <= c <= "z"]
count = Counter(letters)
n = len(letters)
print(f"{n:,} letters")
for ch in "etaqjz":
p = count[ch] / n
print(f"'{ch}': {count[ch]:>7,} times, p = {p:.5f}, surprise = {-math.log2(p):5.2f} bits")
avg = sum(count[c] / n * -math.log2(count[c] / n) for c in count)
print(f"average surprise per letter: {avg:.3f} bits (26 equally likely letters would give {math.log2(26):.3f})")
851,078 letters
'e': 100,652 times, p = 0.11826, surprise = 3.08 bits
't': 74,024 times, p = 0.08698, surprise = 3.52 bits
'a': 63,326 times, p = 0.07441, surprise = 3.75 bits
'q': 840 times, p = 0.00099, surprise = 9.98 bits
'j': 948 times, p = 0.00111, surprise = 9.81 bits
'z': 554 times, p = 0.00065, surprise = 10.59 bits
average surprise per letter: 4.194 bits (26 equally likely letters would give 4.700)
Treating each letter as a random draw with the frequencies found in the plays, "e" (12 % of letters) carries 3.08 bits and "z" (0.065 %) carries 10.59. The average surprise per letter, each letter's surprise weighted by how often it occurs, is 4.19 bits, less than the 4.70 bits of 26 equally likely letters. That average is the entropy of chapter 4, and the gap between 4.19 and 4.70 is the first source of redundancy a compressor can remove.
Experiment 4: stored bits against information
import lzma, random, zlib
random.seed(1948)
text = open("../data/shakespeare.txt", "rb").read()
n = len(text)
samples = {"all zeros": bytes(n),
"the four bytes abcd, repeated": (b"abcd" * (n // 4 + 1))[:n],
"Shakespeare's plays": text,
"random bytes": random.randbytes(n)}
print(f"each sample is {n:,} bytes = {8 * n:,} stored bits")
for name, data in samples.items():
z, x = len(zlib.compress(data, 9)), len(lzma.compress(data, preset=9))
print(f"{name:<36} zlib {z:>9,} bytes lzma {x:>9,} bytes ({8 * x / n:.3f} bits per byte)")
each sample is 1,115,394 bytes = 8,923,152 stored bits
all zeros zlib 1,104 bytes lzma 292 bytes (0.002 bits per byte)
the four bytes abcd, repeated zlib 1,109 bytes lzma 296 bytes (0.002 bits per byte)
Shakespeare's plays zlib 434,702 bytes lzma 364,052 bytes (2.611 bits per byte)
random bytes zlib 1,115,740 bytes lzma 1,115,508 bytes (8.001 bits per byte)
Four files of exactly the same size, 8,923,152 stored binary digits each, compress very differently. A million zeros and a repeating four-byte pattern shrink to about 300 bytes with lzma: they carry almost no information. Shakespeare shrinks to 364,052 bytes, about 2.6 bits per character: less than the 4.19 bits of experiment 3, because the compressor also exploits which letters follow which. Random bytes don't shrink at all; both compressors make them slightly bigger (they add a little bookkeeping), because there's no redundancy to remove. Chapter 6 proves no compressor can do better on random data.
Worked examples
Surprise of outcomes
Questions for a code
Converting units
Exercises
Practice
- How many bits of surprise in: a fair coin landing heads 5 times in a row; a card being an ace; a card being any card except the ace of spades?
- How many yes-or-no questions are always enough to identify one of 52 cards? One of 10,000 students? One of 8 billion people?
- A message is one of 64 equally likely messages. How many bits does it carry? How many if it's one of 65?
- Convert 1 bit into nats and into bans.
- An event's surprise is 7 bits. What's its probability?
- Which carries more information: being told the result of a fair die roll, or the results of two fair coin tosses?
- A 10-megabyte file compresses to 2 megabytes. What does that tell you about the information it carries, roughly, and what does it not tell you?
Answers
- 5 bits; bits; bits.
- 6 (); 14 (); 33 ( billion).
- 6 bits; bits (a seventh question is needed in the worst case).
- 1 bit = nats = bans.
- .
- The die: bits against 2 bits.
- That it carries at most about 2 megabytes of information as that compressor could find it, so at least 80 % of the stored digits were redundant. It doesn't tell you the true information content: a better compressor might shrink it further. Compression gives an upper bound.
Harder
- Show from the definition that the surprise of " and then ", for independent and , is the sum of their surprises. Why does this fail if depends on ? (Think of "it's raining" and "the ground is wet".)
- A password is 8 characters, each chosen at random from the 62 letters and digits. How many bits is it? How many characters would give 128 bits?
- In a game of twenty questions about one of a million numbers, someone asks "is it even?" first. Is that a good question? What about "is it 7?"
- Hartley measured information by and Shannon by . Show that they agree when the possibilities are equally likely.
- Why can't a "bit of information" be negative? Can the surprise of an outcome be less than 1 bit? Give an example.
Answers
- Both happen with probability , so the surprise is . If depends on , the probability of both isn't : if it's raining, a wet ground is almost certain, so learning "wet" after "raining" adds almost nothing. The right measure is the surprise of given (chapter 7's conditional information).
- bits. , so 22 characters.
- "Even?" is a good question: it's yes or no with probability each, so its answer is worth a full bit. "Is it 7?" is a bad one: almost always "no" (probability 999,999 in a million), worth about bits on average (chapter 4 shows how to average it); on the rare "yes" it carries about 20 bits, but you can't count on that.
- Each possibility has , so .
- , so and . Yes, surprise can be below 1 bit: any outcome with probability above , such as "no rain" at (0.046 bits).
Challenge
- You have 12 coins, one of which is either heavier or lighter than the others, and a balance scale that tips left, tips right or balances. Use information to argue that 3 weighings might be enough to find the odd coin and say whether it's heavier or lighter, and that 2 can never be.
Discussion
There are possibilities (which coin, and heavier or lighter), so finding the answer needs bits. A weighing has 3 outcomes, so it gives at most bits (when all three are equally likely). Two weighings give at most bits: not enough, since . Three give at most bits, and , so they might suffice, and in fact they do with a careful plan (weigh 4 against 4 first, so each outcome is about equally likely: 8 possibilities when it balances, 8 for each tilt). The counting shows what's possible; the plan has to make every weighing's outcomes nearly equally likely to get close to the limit, which is the same principle as a good compression code.
In real life
Misconceptions and limits
The model to keep
Common misconceptions
| Misconception | What's actually true | Where it's shown |
|---|---|---|
| "Information means meaning, or truth." | In this theory it's resolved uncertainty; a false or meaningless message from a set of possibilities carries information too. | Information is not meaning |
| "A message carries information on its own." | The amount depends on the set of possible messages and their probabilities. | The engineering problem |
| "A bit is a 0 or 1 in memory." | That's a binary digit; a bit of information can take many stored digits, or a fraction of one. | Stored bits and information bits |
| "A 1 MB file holds 1 MB of information." | Only if it's unpredictable; a megabyte of zeros holds almost none. | Experiment 4 |
| "The logarithm is just a convenient choice." | It's the only measure that's additive for independent events. | Why it has to be a logarithm |
| "Rare messages are more important." | They're more informative; importance is a question of meaning, which the theory leaves out. | Information is not meaning |
| "Compression can always shrink a file a bit." | Random data doesn't shrink; compressors made it slightly larger. | Experiment 4 |
| "You need a whole number of questions, so information is a whole number of bits." | Surprise can be any non-negative number; averages over many messages get close to it. | Bits as yes-or-no questions |
Limits and trade-offs
- Probabilities have to come from somewhere. The surprise of a message depends on a probability, which depends on a model of the source: the same letter is more surprising to a model that knows only letter frequencies than to one that knows English. Chapters 3, 4 and 10 deal with where probabilities come from.
- Meaning really is left out. "The bridge is safe" and "the bridge is unsafe" can carry the same number of bits; information theory can't say which matters more. Theories of semantic information exist, but none is as widely used.
- A compressed size is only an upper bound on information content. A better model of the data compresses further; the true limit is the entropy of the source, which chapters 4 and 6 define and prove.
Revision sheet
Key ideas
- Information is resolved uncertainty; a message is a choice from a set of possibilities.
- Shannon's system: source → transmitter → channel (with noise) → receiver → destination; source coding removes redundancy, channel coding adds it back to fight noise.
- Hartley (1928): equally likely possibilities carry .
- Surprise (self-information): bits; 0 for certainty; adds for independent events.
- Only a logarithm makes surprises of independent events add.
- Units: bit (), nat (), ban (); 1 nat ≈ 1.443 bits, 1 ban ≈ 3.322 bits.
- Questions: yes-or-no questions always find one of possibilities.
- Binary digit ≠ bit of information: predictable data carries less than its size.
Glossary
- Information: what a message provides by resolving uncertainty about which of several possibilities happened.
- Self-information / Surprise: the information carried by an outcome of probability , bits.
- Bit (of information): the unit of information; the surprise of one outcome of a fair coin toss, or the answer to one fair yes-or-no question.
- Binary digit: a stored 0 or 1, which carries up to one bit of information.
- Nat: the unit of information measured with natural logarithms; about 1.443 bits.
- Ban / Hartley: the unit of information measured with base-10 logarithms; about 3.322 bits.
- Information source: whatever chooses the messages to be sent, described by the possible messages and their probabilities.
- Channel: the medium a signal crosses from transmitter to receiver, where noise can change it.
- Noise: random disturbance that changes a signal in a channel.
- Source coding: turning messages into as few symbols as possible by removing redundancy.
- Channel coding: adding structured redundancy to a signal so the receiver can correct errors caused by noise.
- Redundancy: the part of a message's length that carries no information, because it could have been predicted.