Skip to content

Instantly share code, notes, and snippets.

@andriytyurnikov
Last active April 20, 2021 14:48
Show Gist options
  • Save andriytyurnikov/d140fdcce611d2fa88be2a1d9d5a70c9 to your computer and use it in GitHub Desktop.
Save andriytyurnikov/d140fdcce611d2fa88be2a1d9d5a70c9 to your computer and use it in GitHub Desktop.
A/B test simulation
def simulate_a_b
sizes = [1_000, 10_000, 100_000]
funded_distribution = [500, 500, 500, 500, 600, 700, 800, 1000, 1500, 2000]
convertion_rate = 0.03
results = {}
results = {
a: {visits: 0, funded: 0, converted: 0},
b: {visits: 0, funded: 0, converted: 0},
}
simulate = ->(i) {
funded = funded_distribution.sample;
converted = rand > (1 - convertion_rate);
consumer = (i.even?) ? (:a) : (:b);
results[consumer][:visits] = results[consumer][:visits] + 1;
results[consumer][:funded] = results[consumer][:funded] + (converted ? funded : 0);
results[consumer][:converted] = results[consumer][:converted] + (converted ? 1 : 0) ;
}
puts 'For A = B (no difference)'
1_000_000.times do |i|
simulate.call(i)
if [1_000, 10_000, 100_000, 1_000_000].include? (i+1)
puts "[Total visits (A+B) #{i+1}]"
puts "visits A/B: #{results[:a][:visits].to_f/results[:b][:visits]}, converted customers A/B: #{results[:a][:converted].to_f/results[:b][:converted]}, $ funded A/B: #{results[:a][:funded].to_f/results[:b][:funded]}"
end
end
end
simulate_a_b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment