In the last exercise, we simulated a random sample of 500 visitors, where each visitor had a 10% chance of making a purchase. When we pressed “Run” a few times, we saw that the number of purchases varied from sample to sample, but was around 50.
Similarly, we simulated a single random sample of 10 coin flips, where each flip had a 50% chance of coming up heads. We saw that the number of simulated heads was not necessarily 5, but somewhere around 5.
By running the same simulated experiment many times, we can get a sense for how much a particular outcome (like the number of purchases, or heads) varies by random chance. Consider the following code:
outcomes =  for i in range(10000): flips = np.random.choice(['heads', 'tails'], size=10, p=[0.5, 0.5]) num_heads = np.sum(flips == 'heads') outcomes.append(num_heads) print(outcomes) ## output is something like: [3, 4, 5, 8, 5, 6, 4, 5, 3, 2, 8, 5, 7, 4, 4, 5, 4, 3, 6, 5,...]
In this code chunk, we’ve done the following:
- initialized an empty list named
outcomesto store the number of ‘heads’ from simulated samples of coin flips
- set up a for-loop to repeat the steps below 10000 times:
- flip a fair coin 10 times
- calculate the number of those 10 flips that came up heads
- append that number onto
Note that 10000 is an arbitrarily chosen large number — it’s big enough that it will yield almost all possible outcomes of our experiment, and small enough that the simulation still runs quickly. From inspecting the output, we can see that the number of ‘heads’ varied between 0 and 10:
min_heads = np.min(outcomes) print(min_heads) #output: 0 max_heads = np.max(outcomes) print(max_heads) #output: 10
Thus, if we flip a fair coin 10 times, we could observe anywhere between 0 and 10 heads by random chance.
Now let’s ask the same question with regards to purchases: if we run an experiment where we simulate a sample of 500 visitors, each with a 10% chance of making a purchase, and record the number of purchases for that imaginary sample — then repeat that experiment a bunch of times — what are the minimum and maximum number of purchases that we’ll observe by random chance?
The solution code from the last exercise is already saved for you in script.py commented out. This is the code to randomly sample 500 visitors who each have a 10% chance of making a purchase, then save the actual number who made a purchase as
Use a for-loop to repeat this process 10000 times. Each time, append the number of simulated visitors who made a purchase to the list
null_outcomes (which has also been initialized for you in script.py).
Outside of your loop, save the minimum and maximum values in
null_max respectively, then print them out. How much does the number of purchases seem to be varying by random chance?
Run your code a couple of times to see how variable these numbers are.