-
-
Save rougier/9b6d78bdfdf16fa7bb2a66f3a890b115 to your computer and use it in GitHub Desktop.
The beta-distribution based bayesian bandit algorith,.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from numpy import * | |
from scipy.stats import beta | |
class BetaBandit(object): | |
def __init__(self, num_options=2, prior=(1.0,1.0)): | |
self.trials = zeros(shape=(num_options,), dtype=int) | |
self.successes = zeros(shape=(num_options,), dtype=int) | |
self.num_options = num_options | |
self.prior = prior | |
def add_result(self, trial_id, success): | |
self.trials[trial_id] = self.trials[trial_id] + 1 | |
if (success): | |
self.successes[trial_id] = self.successes[trial_id] + 1 | |
def get_recommendation(self): | |
sampled_theta = [] | |
for i in range(self.num_options): | |
#Construct beta distribution for posterior | |
dist = beta(self.prior[0]+self.successes[i], | |
self.prior[1]+self.trials[i]-self.successes[i]) | |
#Draw sample from beta distribution | |
sampled_theta += [ dist.rvs() ] | |
# Return the index of the sample with the largest value | |
return sampled_theta.index( max(sampled_theta) ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment