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

Free lesson

Normal and binomial distributions

After this lesson: I can read a normal and a binomial distribution.

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

What is a probability distribution?

A probability distribution shows how likely each possible value of a measurement is. Two shapes turn up again and again: the binomial, for counting successes, and the normal, the bell curve.

Once you know which shape your data has, you can say how surprising a value is without checking every row.

By the end of this lesson

Note

I can read a normal and a binomial distribution.

Key words

WordWhat it meansExample
Distributionhow often each value happens
Trialone go at something that can succeed or failone day, rainy or not
Binomialthe count of successes in a fixed number of trials
Normala bell-shaped distribution set by its mean and spread
Z-scorehow many standard deviations a value is from the mean

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

A binomial counts trials that succeed

Use the binomial when four things hold. The number of trials is fixed. Each trial has two results. The chance of success is the same each time. The trials do not affect each other.

To find the chance of exactly k successes, count the ways to choose which trials succeed. Multiply by the chance of each success and each failure.

Worked example: rain on four days

Rainy daysWays to choose themWorkingChance
010.7 x 0.7 x 0.7 x 0.70.2401
144 x 0.3 x 0.7 x 0.7 x 0.70.4116
266 x 0.3 x 0.3 x 0.7 x 0.70.2646
344 x 0.3 x 0.3 x 0.3 x 0.70.0756
410.3 x 0.3 x 0.3 x 0.30.0081
All1.0000

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

Count rainy days in blocks of five

Command
docker exec jupyter python3 -c "import numpy as np, pandas as pd; h = pd.read_csv('hires.csv'); r = h['weather'].eq('rainy').astype(int).groupby(h.index // 5).sum(); print(np.bincount(r, minlength=6).tolist())"

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

Worked example: real blocks against the binomial

Rainy days in a block of fiveBlocks seenBlocks a binomial expects
0119.9
12124.4
22923.9
3811.7
432.9
510.3

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

Real counts follow the shape loosely

The command splits the year into 73 blocks of five days and counts the rainy days in each. It printed [11, 21, 29, 8, 3, 1].

The right-hand column of the table is the binomial with the year's share of rainy days, 120 of 365. The real counts follow its shape. Chance makes them bumpy, so 29 blocks with two rainy days is a little above the 23.9 expected.

The normal distribution is a bell

The normal distribution is a symmetric bell. The mean says where its middle is. The standard deviation says how wide it is.

A rule of thumb saves a lot of work. About 68 in 100 values lie within one standard deviation of the mean. About 95 lie within two. Almost all lie within three.

Worked example: the 68 and 95 rule

Distance from the meanRule says aboutDays in the lab data
Within 1 standard deviation68 of 100241 of 365 (66 of 100)
Within 2 standard deviations95 of 100355 of 365 (97 of 100)
Within 3 standard deviationsAlmost all365 of 365

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

Count days within one and two deviations

Command
docker exec jupyter python3 -c "import pandas as pd; v = pd.read_csv('hires.csv')['hires']; m = v.mean(); s = v.std(); print(((v - m).abs() <= s).sum(), ((v - m).abs() <= 2 * s).sum())"

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

A z-score says how unusual a value is

A z-score is a value minus the mean, divided by the standard deviation. It counts how many standard deviations the value is from the middle.

A z-score near 0 is a typical day. A z-score beyond 2 or below -2 is unusual: about 1 value in 20.

Work out z-scores in Python

Code: python
# How far is a day from the typical day?
mean = 270.3
spread = 79.7

for hires in [150, 300, 400, 487]:
    z = (hires - mean) / spread
    print(hires, "hires is", round(z, 2), "standard deviations from the mean")
# Add 41, the quietest day of the year, to the list.
# Is it further from the mean than 487?

A common mistake

Warning

Many people think all data forms a bell curve. It does not. Waiting times and incomes pile up on one side. Draw a histogram before you use the 68, 95 rule, and only use it when the shape is roughly a symmetric bell.

Going further

In your own notebook, work out the share of days within one standard deviation of the mean for temp_c. Compare it with 68 in 100.

Then draw a histogram of rain_mm. Say why the 68, 95 rule would be a poor fit for it.

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.