# Thompson Sampling Algorithm
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

# Import the dataset
dataset = pd.read_csv("Ads_CTR_Optimisation.csv")

# Implement Thompson Sampling Algorithm
import random
d = 10
N = 10000
ad_selected = []
numbers_of_rewards_1 = [0] * d
numbers_of_rewards_0 = [0] * d
total_reward = 0
for n in range(0,N):
    ad = 0
    max_random = 0
    for i in range(0,d):
        random_beta = random.betavariate(numbers_of_rewards_1[i]+1,numbers_of_rewards_0[i]+1)
        if random_beta > max_random:
            max_random = random_beta
            ad = i
    ad_selected.append(ad)
    reward = dataset.iloc[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
        
# Plot the Histogram of distribution
plt.hist(ad_selected)
plt.title("Histogram of Ads Selected")
plt.xlabel("Ads")
plt.ylabel("Number of times shown")
plt.show