##### Gambler's Fallacy
# If you have 40 rolls in a game session and have a streak of 10 rolls that are 10 are lower,
# does that affect the remaining 30 rolls that evening?
#
# Are you DUE for a high roll?
# Load Packages
library(tidyverse)
library(purrr)
# Set randomization seed. This makes sure the results are the same every time.
set.seed(201201) # Sets the seed for reproduction
#### If you desire to "replicate" you can change the seed or the number of sequences below.
# Define Variables
die <- seq(1,20) # creates the evenly distributed 20 sided die
n_sequences <- 1000000 # The number of simulations of 40 rolls that will be run.
n_rolls <- 40 # The Number of rolls in the simulation
watching_window <- 1:10 # Creating a variable that has us look at the first ten "rolls" in each sequence
bet_window <- (1:n_rolls)[- watching_window] # A variable representing the remaining numbers
n_low <- 10 # Setting the boundary for what a "low roll" is, in this case 10 or less
# Roll the Dice
sequences <- map(1:n_sequences, ~sample(die, n_rolls, replace = TRUE)) # Rolling 40 d20 1 Million times.
# Create a list of all the sets where the first 10 rolls are low. In this case 1% or 1006 were low.
lowroll <- sequences[map_lgl(sequences, function(sequence) sum(sequence[watching_window] <= 10) >= n_low)]
# Tally the rolls that would be considered "high" in order to get a proportion.
high_tally <- map_dbl(lowroll, function(sequence) sum(sequence[bet_window] >= 11 ))
### Get Proportion of the rolls that were high in the remaining 30 rolls
p_high <- high_tally/30
results <- tibble(p_high)
results
proportion_high <- mean(p_high)
## Illustrate the Density of High Outcomes showing what percentage were high vs. low
tibble(p_high) %>%
ggplot(aes(x = p_high)) +
geom_density(fill = "lightblue", alpha = 0.8, colour = "lightblue") +
geom_vline(xintercept = 0.5, linetype = 1) +
geom_vline(xintercept =proportion_high, linetype = 2) +
annotate("segment", x = proportion_high, xend = 0.64, y = 3, yend = 3, linetype = 2) +
annotate("segment", x = 0.5, xend = 0.64, y = 2.7, yend = 2.7, linetype = 1) +
annotate("text", x = 0.67, y = 3, label = proportion_high, size = 8, hjust = 0, family = "serif") +
annotate("text", x = 0.67, y = 2.7, label = "p = 0.5", size = 8, hjust = 0, family = "serif") +
theme_minimal() +
theme(
plot.title = element_blank(),
axis.title = element_blank(),
axis.text.y = element_blank(),
axis.text.x = element_text(margin = margin(0.5, 0, 0.5, 0)),
plot.subtitle = element_text(lineheight = 0.3, margin(0,0,0,50), hjust = 0)
) +
scale_x_continuous(breaks = round(seq(5,25,5)/30, 1), labels = round(seq(5,25,5)/30,1))