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.

Revisions

  1. logicmason revised this gist Apr 24, 2015. 1 changed file with 4 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion Mini-bandit.js
    Original file line number Diff line number Diff line change
    @@ -35,4 +35,7 @@ var reward(lever, amount) {
    var bucket = mycookie.currentLever;
    if (bucket.name === 'noCarousel') return;
    else if (bucket.name === 'crazyCarousel') launchCrazyCarousel();
    else launchCarousel();
    else launchCarousel();

    //upon conversion
    reward(bucket, amount);
  2. logicmason revised this gist Apr 24, 2015. 1 changed file with 7 additions and 1 deletion.
    8 changes: 7 additions & 1 deletion Mini-bandit.js
    Original file line number Diff line number Diff line change
    @@ -29,4 +29,10 @@ 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();
  3. logicmason revised this gist Apr 24, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Mini-bandit.js
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    Bandit
    // Mini-bandit
    const explorationRatio = 0.1;

    // example levers array
  4. logicmason created this gist Apr 24, 2015.
    32 changes: 32 additions & 0 deletions Mini-bandit.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    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
    };