Part 01 · Mathematics for AI

Probability and Statistics

ProbabilityDistributionsBayes' ruleMean & varianceSampling

A model never says yes or no, it says how likely. Probability, distributions, Bayes' rule, and the mean and variance you need to tell a real result from noise.

⏱ 1-2 hours · Station 2 of 92

What it is and why it matters

Ask a spam filter "is this spam?" and a good one never says just yes or no. It says "92% likely". Ask an LLM for the next word after "for lunch I had" and it doesn't pick one word; it gives every word in its vocabulary a chance, and then one is drawn.

Every AI system speaks in probabilities, and every result you measure about it has noise. This lesson gives you the four ideas you'll use again and again:

  • Probability and distributions: how likely each outcome is, and the full table of outcomes that adds up to 1.
  • Bayes' rule: how to update a belief when you see evidence.
  • Mean and variance: the typical value, and how much values jump around it.
  • Sampling: drawing outcomes from a distribution, which is what an LLM does for every token.

How it works

Probability is a number from 0 (never) to 1 (always). A distribution lists every possible outcome with its probability, and all of them sum to 1. A model's final layer produces raw scores called logits, and softmax turns them into a distribution: raise e to each score, then divide by the total.

Conditional probability P(A | B) means "the chance of A, counting only the cases where B is true". Bayes' rule turns one around into the other:

P(spam | "free") = P("free" | spam) × P(spam) / P("free")

The easiest way to trust it is to count. Out of 1,000 emails, 200 are spam. "Free" shows up in 60% of spam (120 emails) and in 5% of normal mail (40 emails). An email with "free" is one of those 160, and 120 of them are spam: 75%. The prior (20%) moved to a posterior (75%) because of the evidence.

Mean and variance. The mean is the average. The variance is the average squared distance from the mean; its square root is the standard deviation. When you average n results, the average itself wobbles by about σ/√n, the standard error. Accuracy is just the mean of 1s (right) and 0s (wrong), so the same rule tells you how much to trust it.

Sampling. Drawing from a distribution means outcomes with higher probability come up more often, but not always. Run the same prompt twice and you can get different answers for this reason (Part 4 shows how temperature changes it).

Predict first

A disease affects 1 in 100 people. A test catches 99% of sick people, and wrongly flags 1% of healthy people. You test positive. Roughly how likely are you to be sick?

Visual

1,000 emails200 spam20%800 normal80%120 say free60% of spam40 say free5% of normal75%spam | freestart: 1,000 emails in the inboxprior: 1 in 5 is spamevidence: keep only emails containing "free" (160)posterior: 120 of those 160 are spam = 75%
Bayes' rule as counting. Start with 1,000 emails, split them by the prior (200 spam, 800 normal), then keep only the emails that contain "free": 120 spam and 40 normal. Among those 160, spam is 120, so P(spam | free) is 75%.

Practical example

Three small calculations you'll meet in real work: the spam filter's Bayes update, softmax turning scores into a next-word distribution, and a check on whether one model's accuracy is really higher than another's.

import math

# 1) Bayes' rule on a spam filter, counted out of 1,000 emails.
p_spam = 0.20             # 200 of 1,000 emails are spam
p_free_given_spam = 0.60  # "free" appears in 60% of spam
p_free_given_ham = 0.05   # and in 5% of normal mail
p_free = p_free_given_spam * p_spam + p_free_given_ham * (1 - p_spam)
p_spam_given_free = p_free_given_spam * p_spam / p_free
print(f"P(free)        = {p_free:.3f}")
print(f"P(spam | free) = {p_spam_given_free:.3f}")

# 2) A model's output is a distribution: softmax turns scores into probabilities.
words = ["bhat", "ruti", "pasta"]
logits = [2.0, 1.0, 0.1]
exps = [math.exp(z) for z in logits]
probs = [e / sum(exps) for e in exps]
for w, p in zip(words, probs):
    print(f"P(next = {w:<5}) = {p:.3f}")
print(f"sum = {sum(probs):.3f}")

# 3) Is model B really better? Accuracy is a mean of 0/1 results, so it has noise.
def interval(correct, n):
    p = correct / n
    se = math.sqrt(p * (1 - p) / n)  # standard error of the mean
    return p, p - 1.96 * se, p + 1.96 * se

for name, correct, n in [("A", 82, 100), ("B", 84, 100), ("A", 8200, 10000), ("B", 8400, 10000)]:
    p, lo, hi = interval(correct, n)
    print(f"model {name} n={n:<6} accuracy {p:.3f}  95% range [{lo:.3f}, {hi:.3f}]")

Output:

P(free)        = 0.160
P(spam | free) = 0.750
P(next = bhat ) = 0.659
P(next = ruti ) = 0.242
P(next = pasta) = 0.099
sum = 1.000
model A n=100    accuracy 0.820  95% range [0.745, 0.895]
model B n=100    accuracy 0.840  95% range [0.768, 0.912]
model A n=10000  accuracy 0.820  95% range [0.812, 0.828]
model B n=10000  accuracy 0.840  95% range [0.833, 0.847]

Reading it:

  • P(spam | free) = 0.750, the same 75% we counted in the diagram.
  • Softmax gives "bhat" 66%, "ruti" 24%, "pasta" 10%, and they sum to 1. A model sampling from this will usually say bhat, but about 1 time in 10 it says pasta.
  • With 100 test questions, model A scores 82% and B 84%, but their 95% ranges overlap heavily ([0.745, 0.895] and [0.768, 0.912]). You can't say B is better. With 10,000 questions the ranges shrink to under ±1% and no longer overlap. The 2% gap is real only when you have enough data to see it.

In production

  • Small eval sets lie. A "2% improvement" on 100 examples is noise. Before you ship a new prompt or model (Part 8), check the gap against the standard error, or run more examples.
  • Base rates matter. A fraud model that is "99% accurate" on a problem where 0.1% of payments are fraud can still flag mostly honest customers. Always ask for the prior.
  • Probabilities aren't always honest. A model's 0.9 is not guaranteed to mean "right 90% of the time". Checking that is called calibration, and modern LLMs are often overconfident.
  • Numerical safety. Softmax with large logits overflows (e^1000). Real code subtracts the largest logit first, which gives the same result safely. Multiplying many small probabilities underflows to 0, so systems add log-probabilities instead.

Key takeaway

Models output distributions, Bayes' rule updates a prior with evidence, and every measured number has noise of about σ/√n, so compare results against their uncertainty, not just against each other.

Practice

A support bot routes messages. 30% of messages are about refunds. The word "money" appears in 50% of refund messages and in 10% of other messages. A message contains "money". What is the chance it's about refunds? Count it out of 1,000 messages first, then check with the formula.

Answer

300 refund messages, 150 contain "money". 700 others, 70 contain "money". Of 220 messages with "money", 150 are refunds: 150 / 220 ≈ 0.682, about 68%. Formula: 0.5 × 0.3 / (0.5 × 0.3 + 0.1 × 0.7) = 0.15 / 0.22 ≈ 0.682.

Station stamp

Finish the chapter

Done reading? Press Complete to save your progress. XP is Pro-only — on Pro this chapter is worth +10 XP.

Go Pro to earn XP →