Exercise 2. American Roulette Monte Carlo simulation
Create a Monte Carlo simulation that generates 10,000 outcomes of \(S\), the sum of 100 bets.
Compute the average and standard deviation of the resulting list and compare them to the expected value (-5.263158) and standard error (40.19344) for \(S\) that you calculated previously.
This exercise is part of the course
HarvardX Data Science - Probability (PH125.3x)
Exercise instructions
- Use the
replicate
function to replicate the sample code forB <- 10000
simulations. - Within
replicate
, use thesample
function to simulaten <- 100
outcomes of either a win (17) or a loss (-1) for the bet. Use the orderc(17, -1)
and corresponding probabilities. Then, use thesum
function to add up the winnings over all iterations of the model. Make sure to includesum
or DataCamp may crash with a "Session Expired" error. - Use the
mean
function to compute the average winnings. - Use the
sd
function to compute the standard deviation of the winnings.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Assign a variable `p_green` as the probability of the ball landing in a green pocket
p_green <- 2 / 38
# Assign a variable `p_not_green` as the probability of the ball not landing in a green pocket
p_not_green <- 1-p_green
# Define the number of bets using the variable 'n'
n <- 100
# The variable `B` specifies the number of times we want the simulation to run. Let's run the Monte Carlo simulation 10,000 times.
B <- 10000
# Use the `set.seed` function to make sure your answer matches the expected result after random sampling.
set.seed(1)
# Create an object called `S` that replicates the sample code for `B` iterations and sums the outcomes.
# Compute the average value for 'S'
# Calculate the standard deviation of 'S'