Model Answers: Monte Carlo
7.9 Problem: MC accuracy
First let’s increase the number of simulations and out the accuracy
<- c(10, 50, 100, 250, 500, 1000) # try different sample sizes
sample_sizes <- length(sample_sizes) # number of sample sizes to try
n_sample_sizes <- 100 # number of repeats for each sample size
rpts <- rep(0, n_sample_sizes) # vector to record accuracy values
accuracy <- rep(0, n_sample_sizes) # vector to record accuracy sd values
accuracy_sd
# for each sample size
for (i in 1:n_sample_sizes) {
<- sample_sizes[i] # select a sanmple size to use
sample_sz
# vector to store results from each repeat
<- rep(0, rpts)
mc_integral for (j in 1:rpts){
# simulated normally distributed numbers
<- rnorm(sample_sz, mean = 1, sd = 2)
sims # find proportion of values between 1-3
<- sum(sims >= 1 & sims <= 3) / sample_sz
mc_integral[j]
}
# compute average difference between integral estimate and real value
<- mean(mc_integral - mc_exact)
accuracy[i] # compute sd difference between integral estimate and real value
<- sd(mc_integral - mc_exact)
accuracy_sd[i]
}
print(accuracy)
## [1] 0.0036552539 0.0060552539 -0.0019447461 -0.0021847461 -0.0008047461
## [6] -0.0027447461
print(accuracy_sd)
## [1] 0.15658928 0.06640281 0.04039752 0.03365563 0.02103168 0.01486029
print(accuracy + accuracy_sd)
## [1] 0.16024453 0.07245807 0.03845277 0.03147088 0.02022694 0.01211555
Next, we will plot the results. Here we will make use of ggplot2
a library to create nice plots without much effort. The input need to be a data.frame
so we will need to create one based on the data.
# load ggplot
library(ggplot2)
# create a data frame for plotting
<- data.frame(sample_sizes, accuracy, accuracy_sd)
df
print(df)
## sample_sizes accuracy accuracy_sd
## 1 10 0.0036552539 0.15658928
## 2 50 0.0060552539 0.06640281
## 3 100 -0.0019447461 0.04039752
## 4 250 -0.0021847461 0.03365563
## 5 500 -0.0008047461 0.02103168
## 6 1000 -0.0027447461 0.01486029
# use ggplot to plot lines for the mean accuracy and error bars
# using the std dev
ggplot(df, aes(x = sample_sizes, y = accuracy)) +
geom_line() +
geom_point() +
geom_errorbar(
aes(ymin = accuracy - accuracy_sd, ymax = accuracy + accuracy_sd),
width = .2,
position = position_dodge(0.05)) +
ylab("Estimate-Exact") +
xlab("Run")
This shows that as the number of Monte Carlo samples is increased, the accuracy increases (i.e. the difference between the estimated integral value and real values converges to zero). In addition, the variability in the integral estimates across different simulation runs reduces.
7.10 Problem: MC Expectation
7.10.1 Problem 1
# simulates a game of 20 spins
<- function() {
play_game # picks a number from the list (1, -1, 2)
# with probability 50%, 25% and 25% twenty times
<- sample(c(1, -1, 2), 20, replace = TRUE, prob = c(0.5, 0.25, 0.25))
results return(sum(results)) # function returns the sum of all the spins
}
= rep(0, runs) # vector to store outcome of each game
score_per_game for (it in 1:runs) {
<- play_game() # play the game by calling the function
score_per_game[it]
}= mean(score_per_game) # average over all simulations
expected_score
print(expected_score)
## [1] 14.28
7.10.2 Problem 2
# simulates a game of up to 20 spins
<- function() {
play_game # picks a number from the list (1, -1, 2)
# with probability 50%, 25% and 25% twenty times
<- sample(c(1, -1, 2), 20, replace = TRUE, prob = c(0.5, 0.25, 0.25))
results <- cumsum(results) # compute a running sum of points
results_sum # check if the game goes to zero at any point
if (sum(results_sum <= 0)) {
return(0) # return zero
else {
} return(results_sum[20]) # returns the final score
}
}
<- rep(0, runs) # vector to store scores in each game played
game_score
# for each game
for (it in 1:runs) {
<- play_game()
game_score[it]
}
print(mean(game_score))
## [1] 11.13
plot(game_score)
The games with score zero now corresponds to the number of games where we went bust (or genuinely ended the game with zero).