Skip to content

Instantly share code, notes, and snippets.

@SidJain1412
Last active May 22, 2024 08:44
Show Gist options
  • Save SidJain1412/a2d2d06ef7e45c3cde15556bc8223aa1 to your computer and use it in GitHub Desktop.
Save SidJain1412/a2d2d06ef7e45c3cde15556bc8223aa1 to your computer and use it in GitHub Desktop.
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