Sha256: cd71953f5adafa847e90c7004e7824816835ff4d671df729431c14a245e2d23f

Contents?: true

Size: 1.02 KB

Versions: 24

Compression:

Stored size: 1.02 KB

Contents

# frozen_string_literal: true
# A multi-armed bandit implementation inspired by
# @aaronsw and victorykit/whiplash
require 'simple-random'

module Split
  module Algorithms
    module Whiplash
      class << self
        def choose_alternative(experiment)
          experiment[best_guess(experiment.alternatives)]
        end

        private

        def arm_guess(participants, completions)
          a = [participants, 0].max
          b = [participants-completions, 0].max
          s = SimpleRandom.new; s.set_seed; s.beta(a+fairness_constant, b+fairness_constant)
        end

        def best_guess(alternatives)
          guesses = {}
          alternatives.each do |alternative|
            guesses[alternative.name] = arm_guess(alternative.participant_count, alternative.all_completed_count)
          end
          gmax = guesses.values.max
          best = guesses.keys.select { |name| guesses[name] ==  gmax }
          best.sample
        end

        def fairness_constant
          7
        end
      end
    end
  end
end

Version data entries

24 entries across 24 versions & 2 rubygems

Version Path
ab-split-1.0.2 lib/split/algorithms/whiplash.rb
ab-split-1.0.1 lib/split/algorithms/whiplash.rb
ab-split-1.0.0 lib/split/algorithms/whiplash.rb
split-3.4.1 lib/split/algorithms/whiplash.rb
split-3.4.0 lib/split/algorithms/whiplash.rb
split-3.3.2 lib/split/algorithms/whiplash.rb
split-3.3.1 lib/split/algorithms/whiplash.rb
split-3.3.0 lib/split/algorithms/whiplash.rb
split-3.2.0 lib/split/algorithms/whiplash.rb
split-3.1.1 lib/split/algorithms/whiplash.rb
split-3.1.0 lib/split/algorithms/whiplash.rb
split-3.0.0 lib/split/algorithms/whiplash.rb
split-2.2.0 lib/split/algorithms/whiplash.rb
split-2.1.0 lib/split/algorithms/whiplash.rb
split-2.0.0 lib/split/algorithms/whiplash.rb
split-1.7.0 lib/split/algorithms/whiplash.rb
split-1.6.0 lib/split/algorithms/whiplash.rb
split-1.5.0 lib/split/algorithms/whiplash.rb
split-1.4.5 lib/split/algorithms/whiplash.rb
split-1.4.4 lib/split/algorithms/whiplash.rb