Logarithms, surprisal, and units of information
Assistance allowed only after prediction and observed evidence are recorded.
Prerequisites
Complete Day 26 (random variables and sampling); use the listed recovery link (Day 21) if blocked.
Learning Objective
Interpret $-\log p$ as the surprisal (information content) of an outcome, convert between bits ($\log_2$) and nats ($\ln$), and handle $\log(0)$ safely.
Instructions
The Shannon information content of an outcome with probability $p$ is $-\log_2 p$ bits, or $-\ln p$ nats; 1 bit $= \ln 2 \approx 0.6931$ nats. Rarer outcomes carry more information. As a limit $\log p \to -\infty$ as $p \to 0^+$ (the extended-real convention writes $\log(0) = -\infty$) — a limit convention, not an API guarantee: in this environment's Python standard library math.log(0) and math.log1p(-1) raise ValueError, and other numeric libraries may behave differently. Implementations therefore clip probabilities ($p \leftarrow \max(p,\varepsilon)$) or - for arguments near 1 only - use math.log1p(x) = ln(1+x) so log1p(p-1) computes ln p precisely; a distinct p=0 policy is still required (clip to a positive floor, smooth probabilities, or explicitly accept infinite loss) - naming log1p alone is not a zero-probability safeguard.
Practice & Prediction Log
Build a surprisal table in bits and nats for $p\in\{0.5,0.25,0.125,0.8,0.1\}$. Then convert 2 bits to nats and explain why $-\ln(0.1)\approx2.3026$ is about ten times $-\ln(0.8)\approx0.2231$.
$-\log_2 0.5=1$, $-\log_2 0.25=2$, $-\log_2 0.125=3$; nats = bits $\times \ln 2$. Check each cell with math.log2 and math.log.
Bits: $0.5\to1$, $0.25\to2$, $0.125\to3$, $0.8\to0.3219$, $0.1\to3.3219$. Nats: $0.6931, 1.3863, 2.0794, 0.2231, 2.3026$. $2$ bits $=1.3863$ nats. Smaller $p$ means larger surprisal, so an unlikely token costs roughly ten times more loss than a likely one - the asymmetry that punishes overconfident wrong predictions.
Your table matches the verified values in both units, and you state the $\log(0)$ failure plus one concrete safeguard (clipping/a positive floor, or log1p for p near 1 only) plus a stated p=0 policy (floor, smoothing, or declared infinite loss).
Authoritative Source Resources & Provenance
Information Theory, Inference, and Learning Algorithms
Relevant Section / Range: Chapter 2: Probability, Entropy, and Inference (Shannon information content)
Verified Version & Date: Cambridge University Press 2003, authorized free PDF (Verified: 2026-10-30)
Freely available textbook introducing surprisal -log p with worked numerical exercises.
Probability foundations (Day 25-26)
All required definitions, tables, and worked answers are reproduced locally in this lesson.
Compared Candidate Sources & Selection Analysis (3)
The standard rigorous reference; paywalled, so it is cited alongside MacKay only via linking rather than as the sole learner-facing text.
Gentler introduction freely readable on arXiv; less depth on coding and inference connections needed for the language-model-loss bridge.
Historical primary source; valuable citation but dated notation makes it supplementary rather than primary for learners.
Python 3 math: Mathematical functions
Relevant Section / Range: math.log, math.log2, math.log1p (log bases and near-zero precision)
Verified Version & Date: Python 3.12 documentation (Verified: 2026-10-30)
Normative reference for the log-base and log1p calls used in the surprisal table.
Basic Python syntax
Use the installed interpreter's help(math) and the included deterministic commands.
Compared Candidate Sources & Selection Analysis (2)
Vectorized logs arrive with the tensor track; scalar math functions keep Day 27 dependency-free.
Decimal-precision reference rather than binary float math; math remains the correct module for the operators and constants these days use.