Probability Basics
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)
| Term | Notation | Description |
|---|---|---|
| Probability | P(A) | It is used to say how likely A is, from 0 to 1 |
| Conditional probability | P(A | B) | It is used for the chance of A when B is known to be true |
| Bayes' rule | P(A | B) = P(B | A) P(A) / P(B) | It is used to update a belief with new evidence |
| Distribution | Depends on the kind | It is used to describe the chances of all possible values, such as test scores |
Example: A Spam Filter
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}")P(spam) = 0.20
P(free | spam) = 0.75
P(free) = 0.19
P(spam | free) = 0.79
Simulated P(spam | free) = 0.79Explanation: 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
| Distribution | Description | Example |
|---|---|---|
| Bernoulli | It is used for one yes-or-no outcome | Is this email spam? |
| Binomial | It is used to count yes outcomes in n tries | How many of 100 parents pay on time? |
| Normal | It is used for values that cluster around an average | Heights, many measurement errors |
| Categorical | It is used for one choice among several | Which 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:
- What does P(free | spam) mean?
- Why is P(spam | free) different from P(free | spam)?
- How did the simulation check the math?
Key Takeaways
Quick reference
| Need | Formula |
|---|---|
| From counts | P(A) = count of A ÷ total |
| Conditional | P(A | B) = count of A and B ÷ count of B |
| Bayes | P(A | B) = P(B | A) P(A) ÷ P(B) |
Was this tutorial helpful?