Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Add support for control_value and related

  • Loading branch information...
commit c14dbc5db0f1dceb3645ee2c64444b87a8869e4f 1 parent 7194b70
Ariel Salomon authored sideshowbandana committed

Showing 1 changed file with 38 additions and 1 deletion. Show diff stats Hide diff stats

  1. +38 1 lib/vanity/experiment/ab_test.rb
39 lib/vanity/experiment/ab_test.rb
@@ -145,6 +145,10 @@ def metrics(*args)
145 145 # puts "#{alts.count} alternatives, with the colors: #{alts.map(&:value).join(", ")}"
146 146 def alternatives(*args)
147 147 @alternatives = args.empty? ? [true, false] : args.clone
  148 + if @control
  149 + @alternatives.delete(@control)
  150 + @alternatives.push(@control) # move to end
  151 + end
148 152 class << self
149 153 define_method :alternatives, instance_method(:_alternatives)
150 154 end
@@ -291,6 +295,24 @@ def chosen?
291 295 end
292 296 end
293 297
  298 + def test_percent(pct)
  299 + raise RuntimeError, "Test percent must be an integer" unless pct.kind_of?(Integer)
  300 + @test_pct = pct
  301 + end
  302 +
  303 + def control_percent(pct)
  304 + raise RuntimeError, "Control percent must be an integer" unless pct.kind_of?(Integer)
  305 + test_percent(100-pct)
  306 + end
  307 +
  308 + def control_value(value)
  309 + @control = value
  310 + if @alternatives
  311 + @alternatives.delete(@control)
  312 + @alternatives.push(@control) # move to end
  313 + end
  314 + end
  315 +
294 316
295 317 # -- Reporting --
296 318
@@ -490,9 +512,24 @@ def fake(values)
490 512 # identity, and randomly distributed alternatives for each identity (in the
491 513 # same experiment).
492 514 def alternative_for(identity)
493   - Digest::MD5.hexdigest("#{name}/#{identity}").to_i(17) % @alternatives.size
  515 + id_hash = Digest::MD5.hexdigest("#{name}/#{identity}").to_i(16)
  516 + return hash_to_alternative(id_hash)
494 517 end
495 518
  519 + def hash_to_alternative(id_hash)
  520 + alternatives_count = @alternatives.size
  521 + if @test_pct
  522 + if (id_hash % 100 >= @test_pct)
  523 + return alternatives_count-1 #@control
  524 + else
  525 + id_hash = id_hash / 100
  526 + alternatives_count -= 1
  527 + end
  528 + end
  529 + return id_hash % alternatives_count
  530 + end
  531 +
  532 +
496 533 begin
497 534 a = 50.0
498 535 # Returns array of [z-score, percentage]

0 comments on commit c14dbc5

Please sign in to comment.
Something went wrong with that request. Please try again.