Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Views: 360
Image: ubuntu2004
Kernel: Python 3 (system-wide)

Quick Review

Let's review some important concepts that you might need for this next project.

import random import matplotlib.pyplot as plt %matplotlib inline

Suppose you have an unfair die that comes up according to these probabilities:

Die Face (min)Probability
10.15
20.25
30.1
40.1
50.35
60.05

Recall that we could use conditional statements to simulate this:

number = random.randint(0,99) if number < 15: die = 1 elif 15 <= number < 40: die = 2 elif 40 <= number < 50: die = 3 elif 50 <= number < 60: die = 4 elif 60<= number < 95: die = 5 else: die = 6 print(die)
5

Also recall a shortcut for doing this instead:

dice = [1,2,3,4,5,6] roll = random.choices(dice, weights = [.15, .25, .1, .1, .35, .05], k=1) print(roll)
[5]

Suppose we wanted to turn this work into a function called roll_die_n_times that takes in the die faces, the probabilities of each face, and the number of times that you want to roll the dice and returns a list containing the die rolls. We could do that here:

def roll_die_n_times(faces, weights, times): return random.choices(faces, weights, k=times) roll_die_n_times([1,2,3,4,5,6], [.15, .25, .1, .1, .35, .05], 10)
[5, 5, 4, 1, 6, 5, 2, 4, 1, 1]

What if we wanted to return the average face value that comes up? We could actually create a new function called average_die that calls our previous function roll_die_n_times.

def average_die(faces, weights, times): rolls = roll_die_n_times(faces, weights, times) return sum(rolls)/times average_die([1,2,3,4,5,6], [.15, .25, .1, .1, .35, .05], 10)
4.2

Suppose we want to roll the die 100 times and plot a histogram of the die faces that come up.

rolls = roll_die_n_times([1,2,3,4,5,6], [.15, .25, .1, .1, .35, .05], 100) plt.hist(rolls, bins=6, range=(0.5,6.5), edgecolor='k') plt.xlabel('face value') plt.ylabel('count') plt.title('die rolls')
Text(0.5, 1.0, 'die rolls')
Image in a Jupyter notebook

What if we wanted to plot the simulation number versus the face value? For that, we would need to store another list containing the the simulation numbers. If we want to run this simulation 10 times, we'll want to store the numbers 1 through 10. There are a few ways to do this. Firstly, you can use a for loop.

simulations = [] for i in range(1,11): simulations.append(i) print(simulations)
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Or you could condense this using a list comprehension:

simulations = [i for i in range(1,11)] print(simulations)
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Now, we can create a scatterplot of simulation number versus die face:

rolls = roll_die_n_times([1,2,3,4,5,6], [.15, .25, .1, .1, .35, .05], 10) simulations = [i for i in range(1,11)] plt.plot(simulations, rolls, '.') plt.xlabel('simulation number') plt.ylabel('face value') plt.title('rolling a die')
Text(0.5, 1.0, 'rolling a die')
Image in a Jupyter notebook

Directions...read the Bank Project carefully with your partner. Do NOT start coding. You should just be jotting down ideas, questions, and patterns with pencil and paper for a while.