Skip to content

Commit

Permalink
Merge pull request #616 from splitrb/rubystats-for-beta-distribution
Browse files Browse the repository at this point in the history
Replace usage of SimpleRandom with RubyStats
  • Loading branch information
andrehjr authored May 7, 2020
2 parents f48c196 + 036374e commit d3ef9d7
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 8 deletions.
4 changes: 2 additions & 2 deletions lib/split/algorithms/whiplash.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true
# A multi-armed bandit implementation inspired by
# @aaronsw and victorykit/whiplash
require 'simple-random'
require 'rubystats'

module Split
module Algorithms
Expand All @@ -16,7 +16,7 @@ def choose_alternative(experiment)
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)
Rubystats::BetaDistribution.new(a+fairness_constant, b+fairness_constant).rng
end

def best_guess(alternatives)
Expand Down
9 changes: 4 additions & 5 deletions lib/split/experiment.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# frozen_string_literal: true

require 'rubystats'

module Split
class Experiment
attr_accessor :name
Expand Down Expand Up @@ -354,17 +357,13 @@ def find_simulated_winner(simulated_cr_hash)
end

def calc_simulated_conversion_rates(beta_params)
# initialize a random variable (from which to simulate conversion rates ~beta-distributed)
rand = SimpleRandom.new
rand.set_seed

simulated_cr_hash = {}

# create a hash which has the conversion rate pulled from each alternative's beta distribution
beta_params.each do |alternative, params|
alpha = params[0]
beta = params[1]
simulated_conversion_rate = rand.beta(alpha, beta)
simulated_conversion_rate = Rubystats::BetaDistribution.new(alpha, beta).rng
simulated_cr_hash[alternative] = simulated_conversion_rate
end

Expand Down
2 changes: 1 addition & 1 deletion split.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Gem::Specification.new do |s|

s.add_dependency 'redis', '>= 2.1'
s.add_dependency 'sinatra', '>= 1.2.6'
s.add_dependency 'simple-random', '>= 0.9.3'
s.add_dependency 'rubystats', '>= 0.3.0'

s.add_development_dependency 'bundler', '>= 1.17'
s.add_development_dependency 'simplecov', '~> 0.15'
Expand Down

0 comments on commit d3ef9d7

Please sign in to comment.