Probability week

First meeting

After this meeting students should:

  • Understand how to use the random library to simulate random events

  • Know what they need to do to prepare for their sixth tutorial.


Explain to students that we will be solving the following problem:

A delivery company delivers fragile items. If a delivery is on time it is usually because it was rushed. The probability that an item is delivered on time is \(0.75\). The probability that an item is broken given that it arrived on time is \(0.3\) and if it is late \(0.2\).

  1. What is the probability that an item is late?

  2. Given that an item is broken what is the probability that it was on time?


Group exercise (breakout rooms of 3): ask students to spend 5 minutes writing a plan to tackle that problem (not necessarily carrying out each step).

Now explain that we are going get a computer to simulate the described events from which we can measure the probabilities (and theoretically compare to the expected results).

Now show how to get code to first write a function to simulate a delivery:

>>> import random
>>> def is_delivery_late():
...     """
...     A function to randomly simulate if a delivery is late or not.
...     """
...     return random.random() > 0.75

Spend some time explaining what is happening here, including:

  • The random library

  • Functions (importance of white space, importance of the docstring, the help statement…)

  • The return statement

Now we will use that function to create a number of experiments:

>>> number_of_repetitions = 10000
>>> samples = [is_delivery_late() for repetition in range(number_of_repetitions)]
>>> samples  
[True, False, True, ..., False, True, False]

We can confirm the number of samples:

>>> len(samples)

We can now confirm the probability:

>>> sum(sample for sample in samples) / number_of_repetitions  

Now explain that we will cover the entire question by writing a function to simulate both the delivery and whether or not the item is broken:

>>> def sample_experiment():
...     """
...     This samples a delivery and depending on whether or not it is late
...     selects whether or not the item is broken.
...     """
...     is_late = is_delivery_late()
...     if is_late is True:
...         probability_of_broken = 0.2
...     else:
...         probability_of_broken = 0.3
...     is_broken = random.random() < probability_of_broken
...     return is_late, is_broken

We can use this to confirm the previous result:

>>> samples = [sample_experiment() for repetition in range(number_of_repetitions)]
>>> sum(is_broken for is_late, is_broken in samples) / number_of_repetitions 

Now we can compute the probability of an item being on time if it is broken:

>>> samples_with_broken = [(is_late, is_broken) for is_late, is_broken in samples if is_broken is True]
>>> sum(is_late for is_late, is_broken in samples_with_broken) / len(samples_with_broken)  

Note that we can use Bayes’ theorem to confirm this theoretically:

\[P(\text{On time}|\text{Broken}) = \frac{P(\text{Broken} | \text{On time})P(\text{On time})}{P(\text{Broken})}\]

We have:

\[P(\text{Broken} | \text{On time}) = 0.3\]
\[P(\text{On time}) = 0.75\]
\[P(\text{Broken}) = 0.3 \times 0.75 + 0.2 \times 0.25\]

We can compute this:

>>> probability_of_on_time_if_broken = 0.3 * 0.75 / (0.3 * 0.75 + 0.2 * 0.25)
>>> probability_of_on_time_if_broken  

Thus the probability for the question is:

>>> 1 - probability_of_on_time_if_broken  

Come back: with time take any questions.

Point at resources.

After class email

Send the following email after class:

Hi all,

A recording of today's class is available at <>.

In this class I went over a demonstration of using Python to solve a
probabilitistic problem. I demontrated how to simulate random events and
measure probabilities directly. We did this using the following Python

- Writing functions.
- List comprehensions.

In preparation for your tutorial tomorrow please work through the sixth
chapter of the Python for mathematics book:

Please get in touch if I can assist with anything,

Post meeting

Here is a video recording of a short review given in the 2020/2021 academic year: