Math for AILoading progress…

Events and distributions

Calculate conditional probabilities, Bayes updates, expectations, variance and softmax probabilities.

A model of what can happen

Probability starts with a list of everything that could happen: the sample space. For one die it is six faces. For two dice it is 36 pairs, drawn below as a grid. An event is any set of outcomes you care about, such as "the sum is 7" or "the dice match". The probability of an event is the total probability of the outcomes inside it. With fair dice every cell has probability 1/36, so P(event) = (cells in the event) / 36.

d1\d21122334455661,11,21,31,41,51,62,12,22,32,42,52,63,13,23,33,43,53,64,14,24,34,44,54,65,15,25,35,45,55,66,16,26,36,46,56,6
cells in the event: 0 of 36
P(event) = 0/36 = 0 = 0%
P(not event) = 36/36 = 100%
Roll the pair of dice:
rolls so far: 0 (seed 2024)
frequency of the event: no rolls yet
exact probability: 0
Click cells to build an event and read its probability. Then roll with a seeded generator and watch the frequency settle toward the exact value. Same seed, same rolls, every time.

Predict first. There are 11 possible sums (2 to 12). So P(sum = 7) = 1/11, right? What is it really?

Frequencies approach probabilities

A probability is not a promise about the next roll. It is the long-run share. Roll the grid above ten times and the frequency of an event can be far from its probability. After a thousand it is much closer, and after ten thousand closer still. This is the law of large numbers, and it is why simulation is a legitimate way to estimate probabilities you cannot easily count.

The two rules every distribution obeys

A list of probabilities over mutually exclusive, exhaustive outcomes is a distribution when (1) none is negative and (2) they sum to exactly 1. Everything that can happen has been given some share of one unit of probability, no more, no less.

face 120%face 220%face 320%face 420%face 520%face 620%
0.210.220.230.240.250.26
total = 0.2 + 0.2 + 0.2 + 0.2 + 0.2 + 0.2 = 1.2 Not valid: 0.2 too much probability.
Six sliders for the faces of a die. They start at the case from the quiz: 0.2 on every face.

Predict first. A die gives each of its six faces probability 0.2. Is that a valid distribution?

Common mistakes

Adding overlapping events. "At least one six" is not 1/6 + 1/6 = 12/36, because the cell (6,6) sits in both the first-die-six row and the second-die-six column. Count it once: 11/36. In general, P(A or B) = P(A) + P(B) - P(A and B). Doubles (6 cells) or a sum of at least 10 (6 cells) share (5,5) and (6,6), so the union has 10 cells: 5/18, not 12/36.

Density is not probability. For a continuous quantity, probability is area under a curve. The height at a single point is a density and can exceed 1: a narrow bell curve with standard deviation 0.2 is about 1.99 tall at its centre, yet the total area is still 1.

Chapter context

Probability quantifies uncertainty relative to a model of possible outcomes. Conditioning changes the relevant population. Before inserting numbers into a formula, say which event is given and which event is being predicted.

Chapter reference: math, worked example & code

This section belongs to the whole chapter, so it is the same on every lesson in it and may cover a different lesson's topic. The code for the experiment you just ran is inside the experiment itself.

Math and notation

P(A∣B)=P(B∣A)P(A)P(B),E[X]=∑xxP(X=x).P(A\mid B)=\frac{P(B\mid A)P(A)}{P(B)},\quad E[X]=\sum_x xP(X=x). Var⁡(X)=E[(X−E[X])2],pi=ezi−m∑jezj−m,  m=max⁡jzj.\operatorname{Var}(X)=E[(X-E[X])^2],\quad p_i=\frac{e^{z_i-m}}{\sum_j e^{z_j-m}},\;m=\max_jz_j.

Bayes' denominator must be nonzero. The softmax normalization runs over the candidate classes.

Worked example

In a fictional population, illness prevalence is 1%, sensitivity is 90% and the false-positive rate is 10%. True positives account for 0.01×0.9=0.009 of the population; false positives account for 0.99×0.1=0.099. Among positives, the illness fraction is 0.009/0.108=1/12≈8.33%. This is an arithmetic example, not medical guidance.

Reference code

The following example uses the Python standard library and does not call a remote service.

from math import exp, log, isclose
posterior = .01*.9 / (.01*.9 + .99*.1)
assert isclose(posterior, 1/12)
scores = [0, log(2), log(3)]
weights = [exp(z-max(scores)) for z in scores]
probs = [w/sum(weights) for w in weights]
assert all(isclose(a,b) for a,b in zip(probs, [1/6,2/6,3/6]))

Python examples are provided for study and local execution; this page does not execute Python.

Common mistakes

P(A given B) and P(B given A) are different quantities. A high uncalibrated model confidence is not evidence of a correct probability model.