-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathupper_confidence_bound.py
More file actions
60 lines (48 loc) · 1.74 KB
/
Copy pathupper_confidence_bound.py
File metadata and controls
60 lines (48 loc) · 1.74 KB
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
# Upper Confidence Bound (UCB) Algorithm
# Solves the multi-armed bandit problem for ad CTR optimization
import math
import os
import matplotlib.pyplot as plt
import pandas as pd
def main():
# Importing the dataset (resolve path relative to this script)
script_dir = os.path.dirname(os.path.abspath(__file__))
csv_path = os.path.join(script_dir, "Ads_CTR_Optimisation.csv")
if not os.path.isfile(csv_path):
raise FileNotFoundError(f"Dataset not found: {csv_path}")
dataset = pd.read_csv(csv_path)
# Implementing UCB
N = len(dataset)
d = dataset.shape[1]
ads_selected = []
numbers_of_selections = [0] * d
sums_of_rewards = [0] * d
total_reward = 0
for n in range(N):
ad = 0
max_upper_bound = 0
for i in range(d):
if numbers_of_selections[i] > 0:
average_reward = sums_of_rewards[i] / numbers_of_selections[i]
delta_i = math.sqrt(3 / 2 * math.log(n + 1) / numbers_of_selections[i])
upper_bound = average_reward + delta_i
else:
upper_bound = math.inf
if upper_bound > max_upper_bound:
max_upper_bound = upper_bound
ad = i
ads_selected.append(ad)
numbers_of_selections[ad] += 1
reward = dataset.to_numpy()[n, ad]
sums_of_rewards[ad] += reward
total_reward += reward
print(f"Total reward: {total_reward}")
# Visualising the results
plt.hist(ads_selected, bins=range(d + 1), edgecolor="black", align="left")
plt.xticks(range(d))
plt.title("Histogram of ads selections")
plt.xlabel("Ads")
plt.ylabel("Number of times each ad was selected")
plt.show()
if __name__ == "__main__":
main()