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
| Word | What it means | Example |
|---|---|---|
| Event | something that may or may not happen | a rainy day |
| Probability | a number from 0 to 1 for how likely an event is | |
| Independent | one event does not change the chance of the other | |
| Conditional probability | the chance of an event once you know another has happened | |
| Complement | the 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
| Weather | Days | Chance | Chance it is not this |
|---|---|---|---|
| Rainy | 120 | 120 / 365 = 0.329 | 1 - 0.329 = 0.671 |
| Cloudy | 114 | 114 / 365 = 0.312 | 1 - 0.312 = 0.688 |
| Sunny | 131 | 131 / 365 = 0.359 | 1 - 0.359 = 0.641 |
| All | 365 | 1.000 |
The cells students fill in are left blank in this preview.
Find the share of days that were rainy
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 question | The rule | Worked |
|---|---|---|
| Rainy and then rainy again, independent | Multiply | 0.3 x 0.3 = 0.09 |
| Rainy or sunny, cannot both happen | Add | 120 / 365 + 131 / 365 = 0.688 |
| A or B, can both happen | Add, then take off the overlap | 0.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
| Weather | Days | Busy days (300 or more) | Chance of busy, given this weather |
|---|---|---|---|
| Rainy | 120 | 25 | 25 / 120 = 0.208 |
| Cloudy | 114 | 30 | 30 / 114 = 0.263 |
| Sunny | 131 | 76 | 76 / 131 = 0.580 |
| All days | 365 | 131 | 131 / 365 = 0.359 |
The cells students fill in are left blank in this preview.
Count sunny days and busy sunny days
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
# 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.