Probability Basics

Updated · 5 min read
mediumPython 3.13aimathprobabilitybayesnumpy

Probability is a number from 0 to 1 that measures how likely something is: 0 means impossible, 1 means certain. Machine learning uses it to express uncertainty, such as "this email is 79% likely to be spam", and many models are built directly on probability rules.

Conditional Probability and Bayes' Rule

Conditional probability, written P(A | B), is the probability of A given that B is true. Bayes' rule turns one conditional probability around into the other:

P(spam | free) = P(free | spam) × P(spam) ÷ P(free)

Turning P(free | spam) into P(spam | free)
TermNotationDescription
ProbabilityP(A)It is used to say how likely A is, from 0 to 1
Conditional probabilityP(A | B)It is used for the chance of A when B is known to be true
Bayes' ruleP(A | B) = P(B | A) P(A) / P(B)It is used to update a belief with new evidence
DistributionDepends on the kindIt is used to describe the chances of all possible values, such as test scores

Example: A Spam Filter

Python
import numpy as np
 
# 1,000 school emails: 200 are spam. "free" appears in 150 spam and 40 normal emails.
total, spam = 1000, 200
free_in_spam, free_in_normal = 150, 40
 
p_spam = spam / total
p_free_given_spam = free_in_spam / spam
p_free = (free_in_spam + free_in_normal) / total
p_spam_given_free = p_free_given_spam * p_spam / p_free   # Bayes' rule
 
print(f"P(spam) = {p_spam:.2f}")
print(f"P(free | spam) = {p_free_given_spam:.2f}")
print(f"P(free) = {p_free:.2f}")
print(f"P(spam | free) = {p_spam_given_free:.2f}")
 
# Check by simulation: draw 100,000 emails with the same rates
rng = np.random.default_rng(1)
is_spam = rng.random(100_000) < p_spam
has_free = np.where(is_spam, rng.random(100_000) < 0.75, rng.random(100_000) < 40 / 800)
print(f"Simulated P(spam | free) = {is_spam[has_free].mean():.2f}")
Output
P(spam) = 0.20
P(free | spam) = 0.75
P(free) = 0.19
P(spam | free) = 0.79
Simulated P(spam | free) = 0.79

Explanation: Of 1,000 emails, 200 are spam, so P(spam) = 0.20. "Free" appears in 150 of the spam emails, so P(free | spam) = 0.75, and in 190 emails overall, so P(free) = 0.19. Bayes' rule gives P(spam | free) = 0.75 × 0.20 ÷ 0.19 = 0.79. The simulation draws 100,000 emails with the same rates and counts how many with "free" are spam: also 0.79.

Distributions

DistributionDescriptionExample
BernoulliIt is used for one yes-or-no outcomeIs this email spam?
BinomialIt is used to count yes outcomes in n triesHow many of 100 parents pay on time?
NormalIt is used for values that cluster around an averageHeights, many measurement errors
CategoricalIt is used for one choice among severalWhich word comes next (a language model's output)

Common Mistakes

Swapping the condition. P(spam | free) ≠ P(free | spam).

Forgetting the base rate. Rare events stay fairly unlikely even after positive evidence; P(spam) matters.

Treating probabilities as certainties. 0.79 means wrong about one time in five.

Interview Corner

What is Bayes' rule?

P(A | B) = P(B | A) × P(A) / P(B). It updates a prior belief P(A) with evidence B, and underlies classifiers such as naive Bayes spam filters.

What is conditional probability?

The probability of an event given that another event is known to have happened, written P(A | B).

Practice

Exercise: a second word. Suppose "winner" appears in 60 of the 200 spam emails and 4 of the 800 normal ones. Compute P(spam | winner) by hand with Bayes' rule, then check it by changing the program.

Check your understanding:

  1. What does P(free | spam) mean?
  2. Why is P(spam | free) different from P(free | spam)?
  3. How did the simulation check the math?

Quick check: probability

4 questions
1What is P(spam | free) in the example?
2What does P(A | B) mean?
3Which distribution counts yes outcomes in n tries?
4What does a language model output for the next token?

Key Takeaways

Quick reference

NeedFormula
From countsP(A) = count of A ÷ total
ConditionalP(A | B) = count of A and B ÷ count of B
BayesP(A | B) = P(B | A) P(A) ÷ P(B)

Was this tutorial helpful?

Lesson 117 of 186