ML Systems Lab Open interactive version →
Foundational 28 min read probabilitybayesfoundations

Probability Fundamentals

Sample spaces, Bayes' theorem, conditional probability

An email arrives. Is it spam? You know before opening it that 30% of all email is spam — that is your prior. You open it and find the word "FREE" five times. Words like "FREE" appear five times more often in spam than in legitimate email. How do you combine what you already knew with this new evidence?

The naive move is to just count: look at all emails containing "FREE" in your dataset and compute the fraction that were spam. This ignores the prior and breaks when your dataset is small or skewed. What you actually want is a principled update: start from P(spam) = 0.3, observe the word "FREE," and revise. The mechanism for this is conditional probability. P(spam|FREE) asks: among emails that contain "FREE," what fraction are spam? This is not the same as P(FREE|spam), which asks: among spam emails, what fraction contain "FREE?" Those two quantities are different, and confusing them is the base rate fallacy.

To go from the latter to the former, you need Bayes' theorem: $P(spam|FREE) = P(FREE|spam) \cdot P(spam) / P(FREE)$. The numerator multiplies the likelihood — how often "FREE" appears in spam — by the prior probability of spam. The denominator, $P(FREE)$, normalises so everything sums to 1. It equals $P(FREE|spam) \cdot P(spam) + P(FREE|ham) \cdot P(ham)$: the law of total probability.

This is the mechanism behind every probabilistic ML system. The prior $P(spam) = 0.3$ encodes what you knew before. The likelihood $P(FREE|spam)$ is what your model learns from data. The posterior $P(spam|FREE)$ is what you actually want — your updated belief after seeing evidence.

Concretely: suppose "FREE" appears in 10% of spam emails but only 2% of legitimate ones — a 5x likelihood ratio, matching what you observed. Then $P(FREE) = P(FREE|spam) cdot P(spam) + P(FREE|ham) cdot P(ham) = 0.10 imes 0.3 + 0.02 imes 0.7 = 0.044$, and $P(spam|FREE) = (0.10 imes 0.3) / 0.044 = 0.03 / 0.044 approx 0.68$. A single word moved your belief from a 30% prior to a 68% posterior.

NOT this. Most people think $P(A|B) = P(B|A)$. They do not. A test that correctly identifies 99% of sick patients — $P(positive|disease) = 0.99$ — does not mean a positive result means you are 99% likely to have the disease. Suppose the disease affects only 1 in 1000 people ($P(disease) = 0.001$) and the test also has a 1% false-positive rate ($P(positive|no\ disease) = 0.01$, i.e. 99% specificity). Then $P(positive) = 0.99 \times 0.001 + 0.01 \times 0.999 \approx 0.0110$, so $P(disease|positive) = 0.00099 / 0.0110 \approx 9\%$. The false positive rate, applied to the large healthy population, swamps the true positives. Dropping the prior — treating $P(positive|disease)$ as $P(disease|positive)$ — is the error. The formal rules: conditional probability $P(A|B) = P(A \cap B)/P(B)$. Independence: $P(A \cap B) = P(A)P(B)$. Chain rule: $P(A,B,C) = P(A)P(B|A)P(C|A,B)$.

Key points

Takeaway

Posterior = likelihood × prior / evidence. The prior is not optional — it determines whether a model output is meaningful or misleading.

Recap

Check your understanding

Q1. You roll two fair dice. Event A = 'first die shows 6', Event B = 'sum equals 7'. Are A and B independent? Compute P(A), P(B), P(A∩B) and verify your answer.

Q2. You have a biased coin: P(H)=0.7. You flip it 3 times. What is the probability of getting exactly 2 heads, and why does the binomial formula give the right answer?

Q3. In a medical test, P(Disease)=0.01, P(+|Disease)=0.95, P(+|No Disease)=0.05. You test positive. Which TWO of the following options correctly compute P(Disease|+) AND correctly interpret what the result implies?

Try it interactively

ML Systems Lab is a free interview-prep platform for ML engineers — work through the full interactive module, quizzes, and drills.

Open ML Systems Lab →