# Upper Confidence Bound

import numpy as np # mathematical/ statistical  models 
import matplotlib.pyplot as plt #Great visual graphs plotting 
import pandas as pd # importing & managing datasets 
import math

# Importing dataset
dataset = pd.read_csv('Ads_CTR_Optimisation.csv')

# Implementing UCB 
N = 10000
d = 10
ads_selected = []
numbers_of_selections = [0] * d # vector of 0s of size d 
sums_of_rewards = [0] * d
total_reward = 0

for n in range(0, N): # For each user
    ad = 0
    max_upper_bound = 0
    
    for i in range(0, d): # For each ad
        if (numbers_of_selections[i] > 0):
            average_reward = sums_of_rewards[i]/numbers_of_selections[i] #relative ad performance 
            delta_i = math.sqrt(3/2 * math.log(n + 1) / numbers_of_selections[i])
            upper_bound = average_reward + delta_i
        else:
            upper_bound = 1e400 #10^400, but not sure why
        
        if upper_bound > max_upper_bound: # finding the max upper bound every roundd
            max_upper_bound = upper_bound 
            ad = i 
            
    ads_selected.append(ad)
    numbers_of_selections[ad] += 1
    reward = dataset.values[n, ad] #getting real reward 
    sums_of_rewards[ad] += reward
    total_reward += reward 

# Visualising the results
plt.hist(ads_selected)
plt.title('Histogram of ads selections')
plt.xlabel('Ads')
plt.ylabel('Number of times each ad was selected')
plt.show()
d09c64161d8916f23125672ac019efcb64a81ce2
<mask token>

<mask token>
for n in range(0, N):
    ad = 0
    max_upper_bound = 0
    for i in range(0, 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 = 1e1000
        if upper_bound > max_upper_bound:
            max_upper_bound = upper_bound
            ad = i
    ads_selected.append(ad)
    numbers_of_selections[ad] += 1
    reward = dataset.values[n, ad]
    sums_of_rewards[ad] += reward
    total_reward += reward
plt.hist(ads_selected)
plt.title('Histogram of ads selections')
plt.xlabel('Ads')
plt.ylabel('Number of times each ad was selected')
plt.show()

<mask token>
dataset = pd.read_csv('Ads_CTR_Optimisation.csv')
N = 10000
d = 10
ads_selected = []
numbers_of_selections = [0] * d
sums_of_rewards = [0] * d
total_reward = 0
for n in range(0, N):
    ad = 0
    max_upper_bound = 0
    for i in range(0, 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 = 1e1000
        if upper_bound > max_upper_bound:
            max_upper_bound = upper_bound
            ad = i
    ads_selected.append(ad)
    numbers_of_selections[ad] += 1
    reward = dataset.values[n, ad]
    sums_of_rewards[ad] += reward
    total_reward += reward
plt.hist(ads_selected)
plt.title('Histogram of ads selections')
plt.xlabel('Ads')
plt.ylabel('Number of times each ad was selected')
plt.show()

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import math
dataset = pd.read_csv('Ads_CTR_Optimisation.csv')
N = 10000
d = 10
ads_selected = []
numbers_of_selections = [0] * d
sums_of_rewards = [0] * d
total_reward = 0
for n in range(0, N):
    ad = 0
    max_upper_bound = 0
    for i in range(0, 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 = 1e1000
        if upper_bound > max_upper_bound:
            max_upper_bound = upper_bound
            ad = i
    ads_selected.append(ad)
    numbers_of_selections[ad] += 1
    reward = dataset.values[n, ad]
    sums_of_rewards[ad] += reward
    total_reward += reward
plt.hist(ads_selected)
plt.title('Histogram of ads selections')
plt.xlabel('Ads')
plt.ylabel('Number of times each ad was selected')
plt.show()

# Upper Confidence Bound

import numpy as np # mathematical/ statistical  models 
import matplotlib.pyplot as plt #Great visual graphs plotting 
import pandas as pd # importing & managing datasets 
import math

# Importing dataset
dataset = pd.read_csv('Ads_CTR_Optimisation.csv')

# Implementing UCB 
N = 10000
d = 10
ads_selected = []
numbers_of_selections = [0] * d # vector of 0s of size d 
sums_of_rewards = [0] * d
total_reward = 0

for n in range(0, N): # For each user
    ad = 0
    max_upper_bound = 0
    
    for i in range(0, d): # For each ad
        if (numbers_of_selections[i] > 0):
            average_reward = sums_of_rewards[i]/numbers_of_selections[i] #relative ad performance 
            delta_i = math.sqrt(3/2 * math.log(n + 1) / numbers_of_selections[i])
            upper_bound = average_reward + delta_i
        else:
            upper_bound = 1e400 #10^400, but not sure why
        
        if upper_bound > max_upper_bound: # finding the max upper bound every roundd
            max_upper_bound = upper_bound 
            ad = i 
            
    ads_selected.append(ad)
    numbers_of_selections[ad] += 1
    reward = dataset.values[n, ad] #getting real reward 
    sums_of_rewards[ad] += reward
    total_reward += reward 

# Visualising the results
plt.hist(ads_selected)
plt.title('Histogram of ads selections')
plt.xlabel('Ads')
plt.ylabel('Number of times each ad was selected')
plt.show()