Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

Probability and conditional probability

After this lesson: I can work out the chance of an event.

55 min lesson2 commandsJupyterLab data science lab: pandas, Matplotlib and scikit-learn in the browser

How do you work out a chance?

Probability is a number from 0 to 1 that says how likely an event is. 0 means it cannot happen and 1 means it must. It turns "it might rain" into a figure you can plan with.

A bike shop uses it to decide how many bikes to put out on a day that looks wet.

By the end of this lesson

Note

I can work out the chance of an event.

Key words

WordWhat it meansExample
Eventsomething that may or may not happena rainy day
Probabilitya number from 0 to 1 for how likely an event is
Independentone event does not change the chance of the other
Conditional probabilitythe chance of an event once you know another has happened
Complementthe chance that an event does not happen

The cells students fill in are left blank in this preview.

A probability comes from counting outcomes

Count the days when the event happened. Divide by all the days you looked at. That share is the probability.

The chance that an event does not happen is 1 minus its probability. This is the complement. Probabilities for all the possible outcomes add up to 1.

Worked example: weather over 365 days

WeatherDaysChanceChance it is not this
Rainy120120 / 365 = 0.3291 - 0.329 = 0.671
Cloudy114114 / 365 = 0.3121 - 0.312 = 0.688
Sunny131131 / 365 = 0.3591 - 0.359 = 0.641
All3651.000

The cells students fill in are left blank in this preview.

Find the share of days that were rainy

Command
docker exec jupyter python3 -c "import pandas as pd; h = pd.read_csv('hires.csv'); print(round((h['weather'] == 'rainy').mean(), 3))"

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Multiply for "and", add for "or"

If two events are independent, the chance of both is their chances multiplied. Two fair coins both land heads with chance 0.5 x 0.5 = 0.25.

For "or", add the chances, but only if the events cannot both happen. If they can, you counted the overlap twice, so take it off once.

Worked example: three rules

The questionThe ruleWorked
Rainy and then rainy again, independentMultiply0.3 x 0.3 = 0.09
Rainy or sunny, cannot both happenAdd120 / 365 + 131 / 365 = 0.688
A or B, can both happenAdd, then take off the overlap0.5 + 0.4 - 0.2 = 0.7

The cells students fill in are left blank in this preview.

A common mistake

Warning

Many people think a long run of heads makes tails more likely next. For independent events it does not. A coin has no memory, so the chance of tails stays 0.5 after ten heads in a row.

Conditional probability narrows the days first

Sometimes you know something already. It is sunny, so how likely is a busy day? That is a conditional probability: the chance of busy, given sunny.

Look only at the sunny days. Count how many of them were busy. Divide by the number of sunny days, not by all 365.

Worked example: a tree written as a table

WeatherDaysBusy days (300 or more)Chance of busy, given this weather
Rainy1202525 / 120 = 0.208
Cloudy1143030 / 114 = 0.263
Sunny1317676 / 131 = 0.580
All days365131131 / 365 = 0.359

The cells students fill in are left blank in this preview.

Count sunny days and busy sunny days

Command
docker exec jupyter python3 -c "import pandas as pd; h = pd.read_csv('hires.csv'); s = h['weather'] == 'sunny'; print((s & (h['hires'] >= 300)).sum(), s.sum())"

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Work out three chances in Python

Code: python
# Weather on ten days, and whether each was busy (300 hires or more).
weather = ["sunny", "rainy", "sunny", "cloudy", "sunny",
           "rainy", "cloudy", "sunny", "rainy", "sunny"]
busy = [True, False, True, False, False, False, True, True, False, True]

sunny = 0
sunny_and_busy = 0
for n in range(len(weather)):
    if weather[n] == "sunny":
        sunny = sunny + 1
        if busy[n]:
            sunny_and_busy = sunny_and_busy + 1

print("chance of sunny:", sunny / len(weather))
print("chance of sunny and busy:", sunny_and_busy / len(weather))
print("chance of busy, given sunny:", sunny_and_busy / sunny)
# Change "sunny" to "rainy" in both places and run again.

Going further

In your own notebook, work out the chance that a day is busy given that it is a weekend. Then the chance of a weekend given that a day is busy.

The two numbers are not equal. Explain in one sentence why dividing by a different group of days changes the answer.

About this lab and its source: JupyterLab data science lab: pandas, Matplotlib and scikit-learn in the browser

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.