Skip to content

Instantly share code, notes, and snippets.

@logicmason
Last active August 29, 2015 14:19
Show Gist options
  • Save logicmason/d010683e7c6fc3420819 to your computer and use it in GitHub Desktop.
Save logicmason/d010683e7c6fc3420819 to your computer and use it in GitHub Desktop.
// Mini-bandit
const explorationRatio = 0.1;
// example levers array
/* [{name: 'control', trials: 412, conversions: 113, revenue: 22260},
{name: 'noCarousel', trials: 723, conversions: 298, revenue: 45390},
{name: 'crazyCarousel', trials: 19, conversions: 11, revenue: 6600}
]
*/
var choose = function(levers) {
if (Math.random() < explorationRatio) {
// exploration!
// choose a random lever
}
else {
// exploitation!
levers.forEach((x) => {
// calculate expected reward
// this is trials / conversions or even trials / revenue
bestLever = getLeverWithHighestReward(levers);
assignBucket(bestLever); // store choice in cookie
bestLever.trials++; // store test data in redis or something
}
}
}
var reward(lever, amount) {
lever.conversions++;
lever.revenue += amount;
save(lever); // api call
};
// In app
var bucket = mycookie.currentLever;
if (bucket.name === 'noCarousel') return;
else if (bucket.name === 'crazyCarousel') launchCrazyCarousel();
else launchCarousel();
//upon conversion
reward(bucket, amount);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment