Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
from scipy.stats import beta
import random
ads_selected = []
# d = number of ads
number_of_wins = [0] * d
number_of_losses = [0] * d
total_reward = 0
# For each user
for n in range(0, N):
ad = 0
max_random = 0
# Iterate over ads
for i in range(0, d):
# Randomly pull a value from the betavariate distribution for that ad
random_beta = random.betavariate(number_of_wins[i] + 1, number_of_losses[i] + 1)
# Selecting the highest value
if random_beta > max_random:
max_random = random_beta
ad = i
# Selecting the winning ad for the current user
ads_selected.append(ad)
# If the user has clicked, update the number of successes for that ad
# Else update number of failures
reward = df.values[n, ad]
if reward == 1:
number_of_wins[ad] = number_of_wins[ad] + 1
else:
number_of_losses[ad] = number_of_losses[ad] + 1
total_reward = total_reward + reward
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment