-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathmab.R
93 lines (69 loc) · 2.03 KB
/
mab.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
library(tidyverse)
ads = read.csv('../data/Ads_CTR_Optimisation.csv')
View(ads)
## Randomly showing ads
N = nrow(ads)
num_ads = ncol(ads)
total_reward = 0
clicks = rep(0, num_ads)
views = rep(0, num_ads)
for(i in 1:N) {
# randomly sample an ad to show
ad_to_show = sample(1:10, 1)
views[ad_to_show] = views[ad_to_show] + 1
# update reward statistics
reward = ads[i, ad_to_show] # either 1 or 0 depending on whether they click
clicks[ad_to_show] = clicks[ad_to_show] + reward
total_reward = total_reward + reward
}
views
clicks/views
total_reward
eps = 0.05
# epsilson-greedy Sampling
# placeholder to track the number of time we showed each ad
clicks = rep(0, num_ads)
views = rep(0, num_ads)
total_reward = 0
for (i in 1:N) {
# Click probabilities from Bayesian posterior distribution (beta)
# mean of each beta = prior_clicks / prior_views
explore = rbinom(1,1,prob=eps)
click_probs = clicks/(views + 1e-6)
if(explore) {
ad_to_show = sample(1:10, 1)
} else {
ad_to_show = which.max(click_probs)
}
views[ad_to_show] = views[ad_to_show] + 1
# Check for reward and update reward statistics
reward = ads[i, ad_to_show]
clicks[ad_to_show] = clicks[ad_to_show] + reward
total_reward = total_reward + reward
}
views
clicks/views
total_reward
## Thompson sampling
N = nrow(ads)
num_ads = ncol(ads)
# placeholder to track the number of time we showed each ad
clicks = rep(0, num_ads)
views = rep(0, num_ads)
# Thompson Sampling
total_reward = 0
for (i in 1:N) {
# Click probabilities from Bayesian posterior distribution (beta)
# mean of each beta = prior_clicks / prior_views
click_probs = rbeta(num_ads, clicks + 1, views - clicks + 1)
# Choose an ad to show based on highest sampled click probability
ad_to_show = which.max(click_probs)
views[ad_to_show] = views[ad_to_show] + 1
# Check for reward and update reward statistics
reward = ads[i, ad_to_show]
clicks[ad_to_show] = clicks[ad_to_show] + reward
total_reward = total_reward + reward
}
views
clicks/views
total_reward