Probability Basics
Probability measures how likely an event is, on a scale from 0 (impossible) to 1 (certain). It is the language behind every p-value and confidence level in this course, so it is worth knowing its basic rules.
Events and the sample space
- An experiment is any process with an uncertain outcome: rolling a die, testing a part, asking a customer a question.
- The sample space
Sis the set of all possible outcomes:{1, 2, 3, 4, 5, 6}for a die. - An event is a set of outcomes: "an even number" =
{2, 4, 6}.
If all outcomes are equally likely, the classical definition applies:
P(A) = (number of outcomes in A) / (number of outcomes in S)from fractions import Fraction
die = {1, 2, 3, 4, 5, 6}
even = {x for x in die if x % 2 == 0}
greater_than_4 = {x for x in die if x > 4}
P = lambda event: Fraction(len(event), len(die))
print("P(even) =", P(even))
print("P(> 4) =", P(greater_than_4))
print("P(even and >4) =", P(even & greater_than_4))
print("P(even or >4) =", P(even | greater_than_4))
print("P(not even) =", P(die - even))P(even) = 1/2
P(> 4) = 1/3
P(even and >4) = 1/6
P(even or >4) = 2/3
P(not even) = 1/2
Probability as long-run frequency
Probability can also be seen as the share of times an event happens if the experiment is repeated very often. This is the law of large numbers: the relative frequency settles down to the probability as the number of trials grows.
import numpy as np
rng = np.random.default_rng(2)
rolls = rng.integers(1, 7, size=100_000)
for n in [10, 100, 1_000, 10_000, 100_000]:
share = np.mean(rolls[:n] == 6)
print(f"after {n:6d} rolls: share of sixes = {share:.4f}")
print(f"theory: 1/6 = {1 / 6:.4f}")after 10 rolls: share of sixes = 0.1000
after 100 rolls: share of sixes = 0.1700
after 1000 rolls: share of sixes = 0.1660
after 10000 rolls: share of sixes = 0.1701
after 100000 rolls: share of sixes = 0.1668
theory: 1/6 = 0.1667
The basic rules
| Rule | Formula |
|---|---|
| Complement | P(not A) = 1 − P(A) |
| Addition (general) | P(A or B) = P(A) + P(B) − P(A and B) |
| Addition (mutually exclusive) | P(A or B) = P(A) + P(B) |
| Conditional probability | P(A | B) = P(A and B) / P(B) |
| Multiplication (general) | P(A and B) = P(A) × P(B | A) |
| Multiplication (independent) | P(A and B) = P(A) × P(B) |
Independent events don't influence each other: one coin flip says nothing about the next. Mutually exclusive events can't happen together: a single die roll can't be both 2 and 5.
The complement rule is a great shortcut for "at least one" questions. What is the chance of at least one six in four rolls?
p_no_six = (5 / 6) ** 4
print(f"P(no six in 4 rolls) = {p_no_six:.4f}")
print(f"P(at least one six) = {1 - p_no_six:.4f}")
# the famous birthday problem: at least two of 23 people share a birthday
p_all_different = 1.0
for k in range(23):
p_all_different *= (365 - k) / 365
print(f"P(shared birthday, 23 people) = {1 - p_all_different:.4f}")P(no six in 4 rolls) = 0.4823
P(at least one six) = 0.5177
P(shared birthday, 23 people) = 0.5073
Conditional probability from a table
A gym surveyed its 500 members:
| Uses app | No app | Total | |
|---|---|---|---|
| Renewed | 210 | 140 | 350 |
| Cancelled | 30 | 120 | 150 |
| Total | 240 | 260 | 500 |
import pandas as pd
table = pd.DataFrame({"app": [210, 30], "no_app": [140, 120]},
index=["renewed", "cancelled"])
total = table.values.sum()
p_renew = table.loc["renewed"].sum() / total
p_renew_given_app = table.loc["renewed", "app"] / table["app"].sum()
p_renew_given_no_app = table.loc["renewed", "no_app"] / table["no_app"].sum()
print(f"P(renew) = {p_renew:.3f}")
print(f"P(renew | app) = {p_renew_given_app:.3f}")
print(f"P(renew | no app) = {p_renew_given_no_app:.3f}")P(renew) = 0.700
P(renew | app) = 0.875
P(renew | no app) = 0.538
Renewal clearly depends on app use, so the two events are not independent (if they were, both conditional probabilities would equal 0.70). Whether the app causes renewals is another question, as you know from Study Types.
Bayes' theorem
Bayes' theorem reverses a conditional probability:
P(A | B) = P(B | A) × P(A) / P(B)A classic example: a disease affects 1% of people. A test detects it 95% of the time (sensitivity) and wrongly flags 4% of healthy people (false positive rate). If you test positive, how likely are you to be ill?
p_ill = 0.01
p_pos_given_ill = 0.95
p_pos_given_healthy = 0.04
p_pos = p_pos_given_ill * p_ill + p_pos_given_healthy * (1 - p_ill)
p_ill_given_pos = p_pos_given_ill * p_ill / p_pos
print(f"P(positive) = {p_pos:.4f}")
print(f"P(ill | positive) = {p_ill_given_pos:.4f}")P(positive) = 0.0491
P(ill | positive) = 0.1935
Only about 19% of positive results come from ill people, because healthy people vastly outnumber ill ones. Confusing P(ill | positive) with P(positive | ill) is a common reasoning error. The same confusion is behind misreading a p-value as "the probability that H₀ is true".
Test yourself
P(A) = 0.3. What is P(not A)?
- 0.3
- 0.7
- 1.3
- 0.09
Two independent events have P(A) = 0.5 and P(B) = 0.2. What is P(A and B)?
- 0.7
- 0.1
- 0.3
- 0.25
What does the law of large numbers say?
- Large samples are always unbiased
- Relative frequencies approach the true probability as the number of trials grows
- After many tails, heads becomes more likely
- Probabilities grow with sample size