-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathThomspon Sampling.R
50 lines (40 loc) · 1.37 KB
/
Thomspon Sampling.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
# For steps and algorithm check Machine Learning with Python.
# ----------------------------------------------------- Importing Data -------------------------------------------- #
dataset = read.csv('Ads_CTR_Optimisation.csv')
# --------------------------------------------------- Implementating UCB ------------------------------------------ #
# Total Number of rounds to show User
N = 10000
# Number of ads
d = 10
# Full list of rounds
ads_selected = integer(0)
numbers_of_rewards_1 = integer(d)
numbers_of_rewards_0 = integer(d)
total_reward = 0
for (n in 1:N) {
ad = 0
max_random = 0
for (i in 1:d) {
random_beta = rbeta(n = 1,
shape1 = numbers_of_rewards_1[i] + 1,
shape2 = numbers_of_rewards_0[i] + 1)
if (random_beta > max_random) {
max_random = random_beta
ad = i
}
}
ads_selected = append(ads_selected, ad)
reward = dataset[n, ad]
if (reward == 1) {
numbers_of_rewards_1[ad] = numbers_of_rewards_1[ad] + 1
} else {
numbers_of_rewards_0[ad] = numbers_of_rewards_0[ad] + 1
}
total_reward = total_reward + reward
}
# ---------------------------------------------- Visualising the Result ------------------------------------------- #
hist(ads_selected,
col = 'blue',
main = 'Histogram of Ads Selections',
xlab = 'Ads',
ylab = 'Number of times each ad was selected')