Chapter 1: Exercise 3

Experiment Steps 1. Initialization: Create a data frame to hold the results of each experiment. 2. Simulation Function: Write a function to simulate the rolling of three dice and calculate their sum. 3. Performing Experiments: Conduct 2,000 experiments, starting with 10 rolls, and increasing the number of rolls by 10 with each subsequent experiment. 4. Calculating Proportions: For each experiment, determine the proportion of times the sum of the dice is either 9 or 10. 5. Recording Results: Save the proportion results into the data frame. 6. Visualization: Plot the proportions for the sums of 9 and 10 as the number of rolls increases.

library(ggplot2)
library(gridExtra) # For arranging plots

set.seed(123) # Set a seed for reproducibility

# Define a function to simulate rolling three dice and calculate the sum
roll_dice_once <- function() {
  sum(sample(1:6, size = 3, replace = TRUE))
}

# Initialize a data frame to store the results
simulation_results <- data.frame(
  Experiment = 1:2000,
  NumRolls = seq(10, 2000, by = 10),
  ProportionSum9 = numeric(2000),
  ProportionSum10 = numeric(2000)
)

# Perform simulations
for (i in 1:nrow(simulation_results)) {
  # Roll the dice 'NumRolls' times for the 'i'th experiment
  rolls <- replicate(simulation_results$NumRolls[i], roll_dice_once())
  
  # Calculate the proportions for sum of 9 and sum of 10
  simulation_results$ProportionSum9[i] <- mean(rolls == 9)
  simulation_results$ProportionSum10[i] <- mean(rolls == 10)
}

# View the summary of results
summary(simulation_results[c("ProportionSum9", "ProportionSum10")])
##  ProportionSum9   ProportionSum10 
##  Min.   :0.0000   Min.   :0.0000  
##  1st Qu.:0.1088   1st Qu.:0.1174  
##  Median :0.1159   Median :0.1254  
##  Mean   :0.1159   Mean   :0.1255  
##  3rd Qu.:0.1232   3rd Qu.:0.1328  
##  Max.   :0.2500   Max.   :0.4000
# Plot the proportions for sum of 9 and sum of 10 as the number of rolls increases
ggplot(simulation_results, aes(x = NumRolls)) +
  geom_point(aes(y = ProportionSum9, color = "Sum 9"), alpha = 0.6) +
  geom_point(aes(y = ProportionSum10, color = "Sum 10"), alpha = 0.6) +
  scale_color_manual(values = c("Sum 9" = "blue", "Sum 10" = "red")) +
  theme_minimal() +
  labs(
    title = "Proportion of Sums 9 and 10 as Number of Rolls Increases",
    x = "Number of Rolls",
    y = "Proportion",
    color = "Sum"
  ) +
  theme(legend.position = "bottom")

Analysis of the result

Based on the plot of the simulation results, which shows the proportions of sums 9 and 10 across an increasing number of rolls, we can draw the following conclusions:

In summary, the simulation suggests that while both sums 9 and 10 tend towards their true probabilities with a large number of rolls, sum 10 does indeed seem to occur slightly more frequently than sum 9. This aligns with the gambler’s observation and can be attributed to the distribution of the sums within the sample space of possible dice rolls.