Show the code
library(dplyr)
library(ggplot2)
library(tidyr)Load relevant packages.
library(dplyr)
library(ggplot2)
library(tidyr)# Initialize variables
alt = 10 # arms
c = 0 # start value
t_max <- 100 # number of rounds
r_max <- 10000 # number of bandits
result_toss <- array(NA, dim = c(alt, t_max, r_max))
result <- array(NA, dim = c(alt, t_max, r_max))
# Loop through first choice options
for (firstchoice in 1:alt) {
c = c + 1
# Loop through number of simulations (i.e., number of bandits)
for (r in 1:r_max) {
success_prob = sort(runif(alt))
success = rep(1, alt)
failure = rep(1, alt)
# Loop through rounds
for (t in 1:t_max) {
beliefs = success / (success + failure) + rnorm(alt, mean = 0, sd = 1/1000)
# Select the option with the highest belief
select = which.max(beliefs)
# Use the first choice on the first round
if (t == 1) {
select = firstchoice
}
# Simulate the outcome of the selected option
toss = runif(1) < success_prob[select]
# Update success and failure counts
if (toss == 0) {
failure[select] = failure[select] + 1
} else {
success[select] = success[select] + 1
}
result_toss[c, t, r] <- toss %>% as.numeric()
# Record the selected option
result[c, t, r] = select
}
}
}
# Plot the results
mean_success_fuu <- rowMeans(result_toss, dim = 2) %>% as.data.frame() %>%
pivot_longer(everything())
mean_success_fuu$first <- rep(1:10, t_max) %>% sort()
colnames(mean_success_fuu) <- c("round", "success_prob", "first_arm")
mean_success_fuu$round_num <- substring(mean_success_fuu$round, 2)
ggplot(mean_success_fuu %>% as.data.frame(),
aes(as.numeric(round_num),
success_prob)) + # geom_point(alpha = 0.2) +
geom_line(color = "grey50") +
theme_minimal() +
facet_grid(~first_arm) +
theme(legend.position = "none") +
xlab("round")# list to store results
mean_success <- list()
superPlot <- list()
# Initialize variables
alt = 10 # arms
t_max <- 100 # number of rounds
r_max <- 10000 # number of bandits
supernum <- 1:10 # superstition against which number?
for (number in 1:length(supernum)) {
c = 0 # start value
result_toss <- array(NA, dim = c(alt, t_max, r_max))
result <- array(NA, dim = c(alt, t_max, r_max))
# Loop through first choice options
for (firstchoice in 1:alt) {
c = c + 1
# Loop through number of simulations (i.e., number of bandits)
for (r in 1:r_max) {
success_prob = sort(runif(alt))
success = rep(1, alt)
failure = rep(1, alt)
# Loop through rounds
for (t in 1:t_max) {
beliefs = success / (success + failure) + rnorm(alt, mean = 0, sd = 1/1000)
# Select the option with the highest belief
select = which.max(beliefs)
# if you would select the BAD option as best option; rather select the second-best option
if (select == supernum[[number]]) {
select <- which(order(beliefs)==9)
}
# Use the first choice on the first round
if (t == 1) {
select = firstchoice
}
# Simulate the outcome of the selected option
toss = runif(1) < success_prob[select]
# Update success and failure counts
if (toss == 0) {
failure[select] = failure[select] + 1
} else {
success[select] = success[select] + 1
}
result_toss[c, t, r] <- toss %>% as.numeric()
# Record the selected option
result[c, t, r] = select
}
}
}
# Plot the results
mean_success[[number]] <- rowMeans(result_toss, dim = 2) %>% as.data.frame() %>%
pivot_longer(everything())
mean_success[[number]]$first <- rep(1:10, t_max) %>% sort()
colnames(mean_success[[number]]) <- c("round", "success_prob", "first_arm")
mean_success[[number]]$round_num <- substring(mean_success[[number]]$round, 2)
superPlot[[number]] <- ggplot(mean_success[[number]] %>% as.data.frame(),
aes(as.numeric(round_num), success_prob, color = as.character(first_arm))) +
geom_line(color = "#00883a", alpha = 0.5) +
geom_line(data = mean_success_fuu %>% as.data.frame(),
aes(as.numeric(round_num),
success_prob),
color = "grey20", alpha = 0.5) +
theme_minimal() +
facet_grid(~first_arm) +
theme(legend.position = "none") +
xlab("round") +
ggtitle(paste("number: ", number)) +
ylim(0, 1)
}