GGR274 Lab 7: Statistical Distributions#
Logistics#
As usual, your lab grade will be based on lab attendance and submission the following tasks to MarkUs by 23:59 on Thursday.
Complete the tasks in this Jupyter notebook and submit your completed file to MarkUs. Here are the instructions for submitting to MarkUs (same as last week):
Download this file (
Lab_7.ipynb) from JupyterHub. (See our JupyterHub Guide for detailed instructions.)Submit this file to MarkUs under the lab7 assignment. (See our MarkUs Guide for detailed instructions.)
Note: You can use autotests with this week’s lab to see if you are on the right track. Your TA and instructors can look in more detail to see if you answered all questions. The submission is a part of your Lab attendance grade.
Introduction#
For this lab, you will be simulating the distribution of outcomes associated with rolling standard six-sided dice.
Task 1: Roll a Die#
a) Create a list of possible outcomes from rolling a six-sided die named die_list. Then, turn this list into a pandas Series named die_series.
import pandas as pd
# Write your code here
b) Using .sample(), simulate rolling this die 10 times in a row. Assign this result to the object roll10.
Be sure to use the argument
replace = True, so that the same number can be selected more than once. Otherwise, you will get an error!Also include the argument
random_state = 1, so that you get the exact same result every time.
# Write your code here
c) What is the mean of roll10? Calculate this value and assign it to the object roll10_mean.
Calculate this manually, without using
.mean()
# Write your code here
d) What is the standard deviation of roll10? Using the formula below, calculate this value and assign it to the object roll10_sd.
Calculate this manually, following the formula above and the example from lecture!
Remember that square root is the same as the exponent 0.5
# Write your code here
Task 2: Rolling Two Dice#
Now, let’s simulate rolling multiple dice at the same time!
a) Use .sample() to simulate rolling two dice, again using replace = True and random_state = 1. Then, sum these values and save them as roll_sum
# Write your code here
b) Now, simulate rolling those two dice a bunch of times! Follow these steps:
Create an empty list named
resultsCreate a
forloop which loops through the valuei10,000 timesWithin this
forloop:Create an object that contains the results of randomly rolling these two dice (as in Task 2a), but set
random_state = i, so that each iteration of the for loop will produce a different dice roll!Sum the values of the two dice together
Append the value of this sum to
results
Finally, convert
resultsinto a pandasSeries
# Write your code here
c) Count the number of times each result appears, then sort this count in order of the dice roll sum. Save this result as results_count_sorted
# Write your code here
d) Plot the values in results_count_sorted
# Write your code here
Task 3: Rolling Many Dice#
a) Repeat the instructions in Task 2b (feel free to copy and modify your existing code), but this time run 10,000 simulations, rolling ten dice each time! Save these results in results10.
# Write your code here
b) Count the values in results10 and sort them by the dice roll sum. Save this result as results10_count_sorted. Finally, plot the contents of results10_count_sorted
# Write your code here
The result of this plot should look pretty similar to a normal distribution, as introduced in class!
Because the outcome for each individual die is random and independent, adding together more dice will get us closer and closer to a perfectly normal distribution.