Learn machine learning from scratch

Start at chapter 1 and build up: what a model is, how it measures its own mistakes, and how it learns. Everything is worked out in plain Python, with the maths shown rather than assumed. Two shorter tracks hold reference notes on the maths and on modern architectures.

Track Progress: 0 Completed
0%
Maths for MLLEVEL 1 · BEGINNERModule 5 · Probability and statistics

Probability, and probability given something

A probability is a number from 0 to 1. The bar in P(A | B) means “given”: count only where B happened. Almost every model is one of these.

9 min Prerequisites: Module 0 (reading notation)

1. The idea

A probability is a number between 0 and 1 that says how likely an event is: P(A) = 0.3 means A happens about 30% of the time. A random variable is a quantity whose value depends on chance, like tomorrow's temperature or the class of the next image, and P(X = x) is the probability that it takes the value x.

The bar in P(A | B) reads “given”: the probability of A once you know B happened. You get it by counting only inside B: P(A | B) = P(A and B) / P(B). Knowing B can change the odds a lot (umbrellas are far more common when it rains) or not at all, in which case A and B are independent and P(A | B) = P(A).

Almost every model in machine learning is a conditional probability. A classifier outputs P(class | image); a language model outputs P(next token | the tokens so far). A lowercase p(x) usually means a density or a whole distribution rather than a single probability, which lesson 25 comes back to.

2. Try it: count inside B

A hundred people: blue ones carry an umbrella (A), red rings are standing in the rain (B). Switch to counting only where it rains and compare P(A | B) with P(A). Then set the link to 0 and see when they agree.

A grid of 100 circles. Filled ones carry an umbrella, ringed ones are in the rain. Sliders set how many of each and how strongly the two go together; a toggle fades everyone outside the rain. The text gives P(A), P(B), P(A and B) and P(A given B).

3. The math

A probability
0≤P(A)≤1,P(not A)=1−P(A)0 \le P(A) \le 1, \qquad P(\text{not } A) = 1 - P(A)
0: never. 1: certain.
Conditional probability
P(A∣B)=P(A∩B)P(B)P(A \mid B) = \frac{P(A \cap B)}{P(B)}
Of the cases where B happens, the share where A happens too.
The product rule
P(A∩B)=P(A∣B) P(B)P(A \cap B) = P(A \mid B)\,P(B)
The same formula, multiplied out.
Independence
P(A∣B)=P(A)  ⟺  P(A∩B)=P(A) P(B)P(A \mid B) = P(A) \iff P(A \cap B) = P(A)\,P(B)
Knowing B tells you nothing about A.
A model is a conditional distribution
pθ(y∣x)p_\theta(y \mid \mathbf{x})
The probability of output y given input 𝐱, with parameters θ.

4. The code (python)

mm5_conditional.py
import numpy as np

rng = np.random.default_rng(0)
n = 100_000
rain = rng.random(n) < 0.25                                   # P(B) = 0.25
umbrella = np.where(rain, rng.random(n) < 0.8, rng.random(n) < 0.1)

print(round(umbrella.mean(), 3))                              # 0.276
print(round(umbrella[rain].mean(), 3))                        # 0.801
print(round((umbrella & rain).mean() / rain.mean(), 3))       # 0.801

6. Check yourself

Answer all 4 questions correctly to complete the chapter · 0 / 4 done
Q1/4 P(A) = 0.3. What is P(not A)?
Q2/4 P(A and B) = 0.12 and P(B) = 0.4. What is P(A | B)?
Q3/4 What does it mean for A and B to be independent?
Q4/4 What does a language model's output give you?

Finished this chapter?

Weekly newsletter

What changed in ML architectures this week

New models worth knowing, benchmark moves, and one architecture explained in plain words. Free, once a week, unsubscribe anytime.