From 036374ee5f41572238c1ae010e66f66ef67c50a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Luis=20Leal=20Cardoso=20Junior?= Date: Thu, 7 May 2020 18:59:11 -0300 Subject: [PATCH] Replace usage of SimpleRandom with RubyStats --- lib/split/algorithms/whiplash.rb | 4 ++-- lib/split/experiment.rb | 9 ++++----- split.gemspec | 2 +- 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/lib/split/algorithms/whiplash.rb b/lib/split/algorithms/whiplash.rb index b9d844f6..5373cada 100644 --- a/lib/split/algorithms/whiplash.rb +++ b/lib/split/algorithms/whiplash.rb @@ -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 @@ -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) diff --git a/lib/split/experiment.rb b/lib/split/experiment.rb index 36a7a26f..c167538f 100644 --- a/lib/split/experiment.rb +++ b/lib/split/experiment.rb @@ -1,4 +1,7 @@ # frozen_string_literal: true + +require 'rubystats' + module Split class Experiment attr_accessor :name @@ -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 diff --git a/split.gemspec b/split.gemspec index d8f969c4..f16e7df8 100644 --- a/split.gemspec +++ b/split.gemspec @@ -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'