-
Notifications
You must be signed in to change notification settings - Fork 0
/
3_offline_bootstrapped_persuasion.R
executable file
·68 lines (48 loc) · 2.89 KB
/
3_offline_bootstrapped_persuasion.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
library(contextual)
library(here)
library(data.table)
setwd(here::here("demo","replication_kruijswijk_2018"))
source("./policy_pooled_egreedy.R")
source("./policy_pooled_ucb.R")
source("./bandit_bootstrapped_replay.R")
simulations <- 100
horizon <- 570000
csv_url <- "http://d1ie9wlkzugsxr.cloudfront.net/data_persuasion_api/persuasion_api_simple.csv"
# If csv file is not yet available, use csv_url instead
csv <- "./persuasion_api_simple.csv"
data <- fread(csv, nrows = horizon)
tab <- table(data$user)
data <- data[data$user %in% names(tab)[tab>5 & tab<50],]
data$user <- as.numeric(as.factor(data$user))
users <- data[, .N, keyby = user]
max_repeat <- max(users$N)
n_users <- length(users$user)
print(max_repeat)
print(n_users)
# table(data$choice)
# 1 2 3 4
# 142628 142860 142439 142073
##################### Bandit ###########################
bandit <- DependentObservationsBootstrappedBandit$new(data, 4)
##################### Policies #########################
agents <- list(Agent$new(UnpooledEgreedyPolicy$new(epsilon = 0.1, n_subjects = n_users), bandit),
Agent$new(PooledEgreedyPolicy$new(epsilon = 0.1), bandit),
Agent$new(RandomPolicy$new(), bandit),
Agent$new(PartiallyBBPooledEgreedyPolicy$new(epsilon = 0.1, n_subjects = n_users), bandit),
Agent$new(PartiallyPooledEgreedyPolicy$new(epsilon = 0.1, n_subjects = n_users), bandit))
history <- Simulator$new(agents = agents, horizon = horizon, do_parallel = TRUE,
simulations = simulations, save_interval = 50, include_packages = c("plyr"))$run()
history$save("emp_eg_100reps.RData")
history$clear_data_table()
rm(agents,history)
plot(history, type = "cumulative", regret = FALSE, rate = TRUE, ylim = c(0.01,0.0375), legend_border = FALSE, legend_position = "topright", ylab = "Click-through rate", legend_labels = c("No pooling", "Complete pooling", "Random", "Partial BB Pooling", "Partial Pooling"))
agents <- list(Agent$new(UnpooledUCBPolicy$new( n_subjects = n_users), bandit),
Agent$new(PooledUCBPolicy$new(), bandit),
Agent$new(RandomPolicy$new(), bandit),
Agent$new(PartiallyBBPooledUCBPolicy$new( n_subjects = n_users), bandit),
Agent$new(PartiallyPooledUCBPolicy$new(n_subjects = n_users), bandit)
)
history <- Simulator$new(agents = agents, horizon = horizon, do_parallel = TRUE,
simulations = simulations, save_interval = 50, include_packages = c("plyr"))$run()
history$save("emp_ucb_100reps.RData")
plot(history, type = "cumulative", regret = FALSE, rate = TRUE, ylim = c(0.01,0.0375), legend_border = FALSE, legend_position = "topright", ylab = "Click-through rate", legend_labels = c("No pooling", "Complete pooling", "Random", "Partial BB Pooling", "Partial Pooling"))