Skip to content

Instantly share code, notes, and snippets.

@ESeufert
Last active August 29, 2015 13:56
Show Gist options
  • Save ESeufert/9319661 to your computer and use it in GitHub Desktop.
Save ESeufert/9319661 to your computer and use it in GitHub Desktop.
Psuedo code for a Bayesian Bandit implementation. Bandit is a class.
epsilon = .1;
n_trials = 1000;
counter = 0;
function test() {
//pick a random number between 1 and 10, inclusive
random_number = rand(1, 10);
//if the random number is within the epsilon percentage
if (random_number <= bandit.epsilon * 10) {
//simply picks a bandit at random
showBandit( getRandomBandit() );
} else {
//picks the best performing bandit
showBandit( getBestPerformingBandit() );
}
//increment the counter
counter++;
//indicate somehow that the test has ended if n_trials has been reached
if (counter == n_trials) {
end;
}
}
function showBandit( bandit ) {
//runs the bandit and collects the success metric
//(converted / didn't convert, etc.)
outcome = run_trial( bandit );
//add the outcome to an array containing the trial results
bandit.trials.push( outcome );
//update the bandit's success metric as simply the sum of trials
//(since success = 1)
bandit.successes = sum( bandit.trials );
//the success rate, or probability for success,
//is updated with the new information from the most recent outcome
bandit.success_rate = bandit.successes / bandit.trials.length;
//update the posterior as a beta distribution
//assumes the first prior for each bandit is (1, 1)
a = 1 + bandit.successes - 1;
b = 1 + bandit.trials.length - bandit.successes - 1;
bandit.distribution = beta( a, b );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment