Class: Split::Alternative
- Inherits:
-
Object
- Object
- Split::Alternative
- Defined in:
- lib/split/alternative.rb
Instance Attribute Summary collapse
-
#experiment_name ⇒ Object
Returns the value of attribute experiment_name.
-
#name ⇒ Object
Returns the value of attribute name.
-
#weight ⇒ Object
Returns the value of attribute weight.
Class Method Summary collapse
Instance Method Summary collapse
- #completed_count ⇒ Object
- #completed_count=(count) ⇒ Object
- #control? ⇒ Boolean
- #conversion_rate ⇒ Object
- #delete ⇒ Object
- #experiment ⇒ Object
- #increment_completion ⇒ Object
- #increment_participation ⇒ Object
-
#initialize(name, experiment_name) ⇒ Alternative
constructor
A new instance of Alternative.
- #participant_count ⇒ Object
- #participant_count=(count) ⇒ Object
- #reset ⇒ Object
- #save ⇒ Object
- #to_s ⇒ Object
- #unfinished_count ⇒ Object
- #z_score ⇒ Object
Constructor Details
#initialize(name, experiment_name) ⇒ Alternative
Returns a new instance of Alternative.
7 8 9 10 11 12 13 14 15 16 |
# File 'lib/split/alternative.rb', line 7 def initialize(name, experiment_name) @experiment_name = experiment_name if Hash === name @name = name.keys.first @weight = name.values.first else @name = name @weight = 1 end end |
Instance Attribute Details
#experiment_name ⇒ Object
Returns the value of attribute experiment_name.
4 5 6 |
# File 'lib/split/alternative.rb', line 4 def experiment_name @experiment_name end |
#name ⇒ Object
Returns the value of attribute name.
3 4 5 |
# File 'lib/split/alternative.rb', line 3 def name @name end |
#weight ⇒ Object
Returns the value of attribute weight.
5 6 7 |
# File 'lib/split/alternative.rb', line 5 def weight @weight end |
Class Method Details
.hash_with_correct_values?(name) ⇒ Boolean
109 110 111 |
# File 'lib/split/alternative.rb', line 109 def self.hash_with_correct_values?(name) Hash === name && String === name.keys.first && Float(name.values.first) rescue false end |
.valid?(name) ⇒ Boolean
105 106 107 |
# File 'lib/split/alternative.rb', line 105 def self.valid?(name) String === name || hash_with_correct_values?(name) end |
Instance Method Details
#completed_count ⇒ Object
31 32 33 |
# File 'lib/split/alternative.rb', line 31 def completed_count @completed_count ||= Split.redis.hget(key, 'completed_count').to_i end |
#completed_count=(count) ⇒ Object
39 40 41 42 |
# File 'lib/split/alternative.rb', line 39 def completed_count=(count) @completed_count = count Split.redis.hset(key, 'completed_count', count.to_i) end |
#control? ⇒ Boolean
52 53 54 |
# File 'lib/split/alternative.rb', line 52 def control? experiment.control.name == self.name end |
#conversion_rate ⇒ Object
56 57 58 59 |
# File 'lib/split/alternative.rb', line 56 def conversion_rate return 0 if participant_count.zero? (completed_count.to_f/participant_count.to_f) end |
#delete ⇒ Object
101 102 103 |
# File 'lib/split/alternative.rb', line 101 def delete Split.redis.del(key) end |
#experiment ⇒ Object
61 62 63 |
# File 'lib/split/alternative.rb', line 61 def experiment Split::Experiment.find(experiment_name) end |
#increment_completion ⇒ Object
48 49 50 |
# File 'lib/split/alternative.rb', line 48 def increment_completion @completed_count = Split.redis.hincrby key, 'completed_count', 1 end |
#increment_participation ⇒ Object
44 45 46 |
# File 'lib/split/alternative.rb', line 44 def increment_participation @participant_count = Split.redis.hincrby key, 'participant_count', 1 end |
#participant_count ⇒ Object
22 23 24 |
# File 'lib/split/alternative.rb', line 22 def participant_count @participant_count ||= Split.redis.hget(key, 'participant_count').to_i end |
#participant_count=(count) ⇒ Object
26 27 28 29 |
# File 'lib/split/alternative.rb', line 26 def participant_count=(count) @participant_count = count Split.redis.hset(key, 'participant_count', count.to_i) end |
#reset ⇒ Object
95 96 97 98 99 |
# File 'lib/split/alternative.rb', line 95 def reset @participant_count = nil @completed_count = nil Split.redis.hmset key, 'participant_count', 0, 'completed_count', 0 end |
#save ⇒ Object
90 91 92 93 |
# File 'lib/split/alternative.rb', line 90 def save Split.redis.hsetnx key, 'participant_count', 0 Split.redis.hsetnx key, 'completed_count', 0 end |
#to_s ⇒ Object
18 19 20 |
# File 'lib/split/alternative.rb', line 18 def to_s name end |
#unfinished_count ⇒ Object
35 36 37 |
# File 'lib/split/alternative.rb', line 35 def unfinished_count participant_count - completed_count end |
#z_score ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/split/alternative.rb', line 65 def z_score # CTR_E = the CTR within the experiment split # CTR_C = the CTR within the control split # E = the number of impressions within the experiment split # C = the number of impressions within the control split control = experiment.control alternative = self return 'N/A' if control.name == alternative.name ctr_e = alternative.conversion_rate ctr_c = control.conversion_rate e = alternative.participant_count c = control.participant_count return 0 if ctr_c.zero? standard_deviation = ((ctr_e / ctr_c**3) * ((e*ctr_e)+(c*ctr_c)-(ctr_c*ctr_e)*(c+e))/(c*e)) ** 0.5 z_score = ((ctr_e / ctr_c) - 1) / standard_deviation end |