Beginner: data foundations · Probability and inference · topic 11 of 62 · 25 min
Probability basics
Explanation
Why data people need probability
Data is never perfectly clean or complete. A model says "this payment is 92% likely to be fraud". A survey of 1,000 people is used to guess what 10 crore people think. To read and build such numbers, you need the language of chance. That language is probability.
The words
- An experiment is something with an uncertain result, like tossing a coin or rolling a die.
- An outcome is one possible result: heads, or the number 4.
- The sample space is the list of all possible outcomes: {1, 2, 3, 4, 5, 6} for a die.
- An event is a group of outcomes you care about: "an even number" is {2, 4, 6}.
- P(A) is the probability of event A, a number from 0 (impossible) to 1 (certain). 0.25 is the same as 25%.
Counting equally likely outcomes
When every outcome is equally likely, P(event) = (outcomes in the event) ÷ (all outcomes). For a fair die, P(even) = 3 ÷ 6 = 0.5. For a card drawn from a full deck, P(ace) = 4 ÷ 52, about 0.077.
from fractions import Fraction
die = [1, 2, 3, 4, 5, 6]
even = [x for x in die if x % 2 == 0]
print(Fraction(len(even), len(die)))
print(len(even) / len(die))Three rules that do most of the work
- NOT (the complement): P(not A) = 1 − P(A). If the chance of rain is 0.3, the chance of no rain is 0.7.
- AND for independent events: P(A and B) = P(A) × P(B). Two events are independent when one does not change the chance of the other, like two separate coin tosses. Two heads: 0.5 × 0.5 = 0.25.
- OR: P(A or B) = P(A) + P(B) − P(A and B). We subtract the overlap so it is not counted twice. If the events cannot happen together, the overlap is 0 and you just add.
Probability from data
In real work we rarely have a perfect die. We count how often something happened in the past. If 12 of 4,000 UPI payments last week were fraud, the observed probability is 12 ÷ 4,000 = 0.003. This is called empirical probability. It is a good guess only if the past looks like the future and the sample is large enough.
Simulation: let the computer roll the dice
import random
random.seed(1)
rolls = [random.randint(1, 6) for _ in range(60000)]
print(round(rolls.count(6) / len(rolls), 3))Common mistakes
- Adding probabilities of events that overlap. Fix: subtract P(A and B), or count the outcomes directly.
- Multiplying probabilities of events that are not independent (rain today and rain tomorrow are related). Fix: use conditional probability, the next topic.
- The gambler's fallacy: "the coin gave 5 heads, so tails is due". Each toss is independent; the chance is still 0.5.
- Getting a probability above 1 or below 0. That always means a mistake in counting.
Need help?
Stuck on a word or an idea? Search this course, or every course, and jump straight to the explanation or a worked question.
Try:
Watch the lesson
Probability basics · 1/8
Welcome. In this lesson you will learn the language of chance: probability.
Prefer a video by a teacher? Find free videos on “probability basics for data science complement addition rule independent events” on YouTube ↗
In real life: A fraud team counts before it models
Before building any fancy model, a payments company first measures simple probabilities from history: what share of payments are fraud, and what share of payments at 3 a.m. from a new device are fraud. If the first number is 0.003 and the second is 0.05, a payment with those features is far riskier than average. These counted probabilities are the baseline every later model must beat.
payments = 4000
fraud = 12
night_new_device = 200
night_new_device_fraud = 10
print("overall:", fraud / payments)
print("night + new device:", night_new_device_fraud / night_new_device)Practice (0/3 done)
Write prob_of(outcomes, wanted) where outcomes is a list of equally likely outcomes and wanted is a set of outcomes you care about. Return the probability (the share of outcomes that are in wanted), rounded to 4 decimal places. For an empty outcomes list return 0.0. Example: prob_of([1, 2, 3, 4, 5, 6], {2, 4, 6}) returns 0.5.
Write two_dice_prob(total) that returns the probability that two fair dice add up to total, rounded to 4 decimal places. List all 36 pairs and count the ones that match. Example: two_dice_prob(7) returns 0.1667 (6 of 36 pairs).
Write and_or(p_a, p_b) for two INDEPENDENT events. Return a tuple (p_and, p_or) where p_and = P(A and B) and p_or = P(A or B), both rounded to 4 decimal places. Example: and_or(0.5, 0.5) returns (0.25, 0.75).
Quick quiz
Read more (free)
Related help
Before this, learn
This topic builds on these. If something here feels new, open them first.
Related topics
- Conditional probability and Bayes' theoremProbability and inference · same module
- Random variables and distributionsProbability and inference · same module
- Samples, the central limit theorem and confidence intervalsProbability and inference · same module
- Logistic regression and classificationThe ML workflow, regression and classification
- How large language models workNLP, embeddings and large language models
- Bias and fairnessEthics, privacy and careers
Questions people ask about this
Open a question to see the answer and why. The first ones are this topic's own quiz, so try the quiz above before you peek.
A bag has 3 red and 7 blue balls. What is P(red) for one ball drawn at random?
Answer: 0.3
3 red out of 10 balls is 3 ÷ 10 = 0.3.
The chance of rain tomorrow is 0.35. What is the chance of no rain?
Answer: 0.65
Complement rule: 1 − 0.35 = 0.65.
You toss a fair coin 3 times. What is P(three heads)?
Answer: 0.125
Tosses are independent: 0.5 × 0.5 × 0.5 = 0.125.
In a class, P(plays cricket) = 0.6, P(plays football) = 0.3 and P(plays both) = 0.2. What is P(cricket or football)?
Answer: 0.7
0.6 + 0.3 − 0.2 = 0.7. Subtract the overlap so the students who play both are counted once.
A fair coin has landed heads 6 times in a row. What is the chance of heads on the next toss?
Answer: Exactly 0.5
Tosses are independent; the coin has no memory. Believing otherwise is the gambler's fallacy.
Of 200 customers who opened a promo email, 30 bought something. What is P(bought | opened)?Related
Answer: 0.15
30 ÷ 200 = 0.15. The bottom is the count of the "given" group.
A hospital wants to miss as few sick patients as possible. Compared with 0.5, the threshold should usually be…Related
Answer: Lower
A lower threshold flags more people, catching more true cases at the cost of more false alarms.
A game pays ₹50 with probability 0.2 and ₹0 otherwise. What is the expected value?Related
What is RLHF used for?Related
Answer: Tuning the model towards answers people prefer
Human preference comparisons guide the model's behaviour.
Pass the quiz (60% or more) or mark this topic as done when you are ready.
Your progress is saved on this device only.
Next steps
- 4 more topics in “Probability and inference” before its exam: Conditional probability and Bayes' theorem, Random variables and distributions, Samples, the central limit theorem and confidence intervals, Hypothesis tests, p-values and A/B testing.
- Module 3 exam: Probability and inference · 12 questions · 25 min, without help. 5 topics of this module still to finish first.
- Then the next module, Cleaning, exploring and visualising data. 62 topics left in the course before the final exam.