⚡0 XP🔥0

Probability Basics and Bayes' Theorem

Lesson 1 of 5 ⏱ 5 min read ✎ 2 exercises ❓ 5-question quiz ▶ Runs Python

Experiments, sample spaces and events, the addition and multiplication rules, independence, conditional probability, Bayes' theorem with a medical-test example, and simulating probability in Python.

Why probability?#

Data science is about making decisions under uncertainty - will this customer buy, is this transaction fraud, did the new website design really increase sales? Probability is the language for "how likely".

A probability is a number from 0 (impossible) to 1 (certain).

Experiments, sample spaces and events#

Term Meaning Example (roll a die)
experiment something with an uncertain result rolling a die
sample space S all possible outcomes {1, 2, 3, 4, 5, 6}
event A a set of outcomes we care about "even" =

When all outcomes are equally likely:

P(A) = (number of outcomes in A) ÷ (number of outcomes in S)

▶ Try it YourselfPython
Ctrl + Enter to run
Output
P(even) = 1/2
P(>4)   = 1/3

Rules of probability#

Rule Formula
complement P(not A) = 1 − P(A)
addition (any events) P(A or B) = P(A) + P(B) − P(A and B)
addition (mutually exclusive) P(A or B) = P(A) + P(B)
multiplication (independent) P(A and B) = P(A) × P(B)
multiplication (general) P(A and B) = P(A) × P(B given A)

Example. A card is drawn from a pack of 52. P(king or heart) = 4/52 + 13/52 − 1/52 = 16/52 ≈ 0.31 (the king of hearts was counted twice).

Tip

"At least one" problems are easiest with the complement. P(at least one six in 4 rolls) = 1 − P(no six in 4 rolls) = 1 − (5/6)⁴ ≈ 0.518.

Simulation - checking with Python#

The law of large numbers says that if you repeat an experiment many times, the fraction of times an event happens gets close to its probability. Let's check the "at least one six" answer with 100,000 simulated games.

▶ Try it YourselfPython
Ctrl + Enter to run

Independent events#

Two events are independent if one happening doesn't change the chance of the other - two coin tosses, or two dice. Drawing two cards without putting the first back is not independent.

हिंदी में समझें

Independent का मतलब है एक घटना दूसरी पर असर नहीं डालती। सिक्का पहली बार Head आए या Tail, दूसरी बार Head आने की संभावना फिर भी 1/2 है। लेकिन ताश से बिना वापस रखे दूसरा पत्ता निकालें तो पहली घटना ने पत्ते कम कर दिए - ये dependent हैं।

Conditional probability#

P(A | B) - "probability of A given B" - only looks at the cases where B is true:

P(A | B) = P(A and B) ÷ P(B)

▶ Try it YourselfPython
Ctrl + Enter to run
Output
P(pass)              = 0.78
P(pass | coaching)   = 0.90
P(pass | no coaching) = 0.70
Common Mistake

A difference in conditional probabilities shows an association, not proof of cause. Students who join coaching may also study more at home. Only a controlled experiment (like an A/B test) can show cause.

Bayes' theorem#

Bayes' theorem "reverses" a conditional probability:

P(A | B) = P(B | A) × P(A) ÷ P(B)

  • P(A) - the prior: what we believed before the evidence.
  • P(B | A) - the likelihood of the evidence if A is true.
  • P(A | B) - the posterior: our updated belief.

It powers spam filters, medical diagnosis, fraud detection and the Naive Bayes classifier.

Spam filter

40% of emails are spam. The word "lottery" appears in 25% of spam and 1% of normal emails. An email contains "lottery".

P(lottery) = 0.25 × 0.40 + 0.01 × 0.60 = 0.106

P(spam | lottery) = 0.25 × 0.40 / 0.106 ≈ 0.943 - almost certainly spam.

Summary#

  • P(A) = favourable / total for equally likely outcomes; probabilities lie in [0, 1].
  • Complement, addition (subtract the overlap) and multiplication rules.
  • Independent: P(A and B) = P(A)·P(B). Conditional: P(A|B) = P(A and B)/P(B).
  • Bayes' theorem updates a prior with evidence - and shows why rare events give many false positives.
  • Simulations with NumPy are a quick way to check any probability answer.

Practice Exercises

Write your code, then press Check. Your program is tested automatically.

1

Two dice add up to 7

List every outcome of rolling two dice (36 pairs), count how many add up to 7, and print the probability as a fraction (use Fraction) and as a decimal with 4 places:

1/6
0.1667
Expected output:
1/6
0.1667
2

Bayes for a medical test

1% of people have a disease. The test detects it in 95% of sick people (sensitivity), but also gives a positive result for 5% of healthy people (false positive rate). Compute P(disease | positive) with Bayes' theorem, store it in p, and print it with 3 decimals (0.161).

Expected output:
0.161

Quick Quiz

Q1.

A fair die is rolled. What is P(even number)?

Q2.

If P(A) = 0.3 and P(B) = 0.5 and A, B are independent, P(A and B) = ?

Q3.

P(A or B) = P(A) + P(B) - P(A and B). Why subtract P(A and B)?

Q4.

What does P(A | B) mean?

Q5.

A rare disease affects 1% of people. A test is 95% accurate. A random person tests positive. The chance they actually have the disease is...