Skip to content

Beginner: data foundations · Probability and inference · topic 11 of 62 · 25 min

Probability basics

Explanation

Probability is a number from 0 to 1 that says how likely something is: 0 means it never happens, 1 means it always happens. When all outcomes are equally likely, probability is the number of outcomes you want divided by the number of all outcomes. Simple rules let you combine chances with AND, OR and NOT.

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.

python
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))
Fraction keeps the exact answer. Shows 1/2, then 0.5.

Three rules that do most of the work

  1. 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.
  2. 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.
  3. 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.
Think of it like this: Think of a college canteen with 100 students. 40 drink chai, 30 drink coffee, and 10 drink both. If you add 40 + 30 you get 70, but the 10 who drink both were counted twice. So 60 students drink chai or coffee: P = 0.4 + 0.3 − 0.1 = 0.6.

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

python
import random

random.seed(1)
rolls = [random.randint(1, 6) for _ in range(60000)]
print(round(rolls.count(6) / len(rolls), 3))
With many rolls the share of sixes gets close to 1/6 ≈ 0.167. random.seed(1) makes the run repeatable.

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.
Tip: When a problem confuses you, list the sample space. For two dice there are only 36 pairs; writing them down solves most questions.

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

Probability basics

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.

python
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)
Shows overall: 0.003 and night + new device: 0.05.

Practice (0/3 done)

Task 1 · Code · Python

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.

Task 2 · Code · Python

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).

Task 3 · Code · Python

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

Question 1

1.A bag has 3 red and 7 blue balls. What is P(red) for one ball drawn at random?

Question 2

2.The chance of rain tomorrow is 0.35. What is the chance of no rain?

Question 3

3.You toss a fair coin 3 times. What is P(three heads)?

Question 4

4.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)?

Question 5

5.A fair coin has landed heads 6 times in a row. What is the chance of heads on the next toss?

Read more (free)

Before this, learn

This topic builds on these. If something here feels new, open them first.

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.

From Conditional probability and Bayes' theorem

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.

From Logistic regression and classification

A game pays ₹50 with probability 0.2 and ₹0 otherwise. What is the expected value?Related

Answer: ₹10

50 × 0.2 + 0 × 0.8 = 10.

From Random variables and distributions

What is RLHF used for?Related

Answer: Tuning the model towards answers people prefer

Human preference comparisons guide the model's behaviour.

From How large language models work

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

  1. 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.
  2. Module 3 exam: Probability and inference · 12 questions · 25 min, without help. 5 topics of this module still to finish first.
  3. Then the next module, Cleaning, exploring and visualising data. 62 topics left in the course before the final exam.

← Previous: Correlation: how two things move together