Class: Split::Alternative

Inherits:
Object
  • Object
show all
Includes:
Zscore
Defined in:
lib/split/alternative.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Zscore

calculate

Constructor Details

#initialize(name, experiment_name) ⇒ Alternative

Returns a new instance of Alternative.


14
15
16
17
18
19
20
21
22
23
24
# File 'lib/split/alternative.rb', line 14

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
  p_winner = 0.0
end

Instance Attribute Details

#experiment_nameObject

Returns the value of attribute experiment_name


9
10
11
# File 'lib/split/alternative.rb', line 9

def experiment_name
  @experiment_name
end

#nameObject

Returns the value of attribute name


8
9
10
# File 'lib/split/alternative.rb', line 8

def name
  @name
end

#weightObject

Returns the value of attribute weight


10
11
12
# File 'lib/split/alternative.rb', line 10

def weight
  @weight
end

Instance Method Details

#all_completed_countObject


58
59
60
61
62
63
64
65
66
# File 'lib/split/alternative.rb', line 58

def all_completed_count
  if goals.empty?
    completed_count
  else
    goals.inject(completed_count) do |sum, g|
      sum + completed_count(g)
    end
  end
end

#completed_count(goal = nil) ⇒ Object


53
54
55
56
# File 'lib/split/alternative.rb', line 53

def completed_count(goal = nil)
  field = set_field(goal)
  Split.redis.hget(key, field).to_i
end

#control?Boolean

Returns:

  • (Boolean)

98
99
100
# File 'lib/split/alternative.rb', line 98

def control?
  experiment.control.name == self.name
end

#conversion_rate(goal = nil) ⇒ Object


102
103
104
105
# File 'lib/split/alternative.rb', line 102

def conversion_rate(goal = nil)
  return 0 if participant_count.zero?
  (completed_count(goal).to_f)/participant_count.to_f
end

#deleteObject


153
154
155
# File 'lib/split/alternative.rb', line 153

def delete
  Split.redis.del(key)
end

#experimentObject


107
108
109
# File 'lib/split/alternative.rb', line 107

def experiment
  Split::ExperimentCatalog.find(experiment_name)
end

#goalsObject


30
31
32
# File 'lib/split/alternative.rb', line 30

def goals
  self.experiment.goals
end

#increment_completion(goal = nil) ⇒ Object


93
94
95
96
# File 'lib/split/alternative.rb', line 93

def increment_completion(goal = nil)
  field = set_field(goal)
  Split.redis.hincrby(key, field, 1)
end

#increment_participationObject


89
90
91
# File 'lib/split/alternative.rb', line 89

def increment_participation
  Split.redis.hincrby key, 'participant_count', 1
end

#p_winner(goal = nil) ⇒ Object


34
35
36
37
38
# File 'lib/split/alternative.rb', line 34

def p_winner(goal = nil)
  field = set_prob_field(goal)
  @p_winner = Split.redis.hget(key, field).to_f
  return @p_winner
end

#participant_countObject


45
46
47
# File 'lib/split/alternative.rb', line 45

def participant_count
  Split.redis.hget(key, 'participant_count').to_i
end

#participant_count=(count) ⇒ Object


49
50
51
# File 'lib/split/alternative.rb', line 49

def participant_count=(count)
  Split.redis.hset(key, 'participant_count', count.to_i)
end

#resetObject


143
144
145
146
147
148
149
150
151
# File 'lib/split/alternative.rb', line 143

def reset
  Split.redis.hmset key, 'participant_count', 0, 'completed_count', 0
  unless goals.empty?
    goals.each do |g|
      field = "completed_count:#{g}"
      Split.redis.hset key, field, 0
    end
  end
end

#saveObject


131
132
133
134
135
# File 'lib/split/alternative.rb', line 131

def save
  Split.redis.hsetnx key, 'participant_count', 0
  Split.redis.hsetnx key, 'completed_count', 0
  Split.redis.hsetnx key, 'p_winner', p_winner
end

#set_completed_count(count, goal = nil) ⇒ Object


84
85
86
87
# File 'lib/split/alternative.rb', line 84

def set_completed_count (count, goal = nil)
  field = set_field(goal)
  Split.redis.hset(key, field, count.to_i)
end

#set_field(goal) ⇒ Object


72
73
74
75
76
# File 'lib/split/alternative.rb', line 72

def set_field(goal)
  field = "completed_count"
  field += ":" + goal unless goal.nil?
  return field
end

#set_p_winner(prob, goal = nil) ⇒ Object


40
41
42
43
# File 'lib/split/alternative.rb', line 40

def set_p_winner(prob, goal = nil)
  field = set_prob_field(goal)
  Split.redis.hset(key, field, prob.to_f)
end

#set_prob_field(goal) ⇒ Object


78
79
80
81
82
# File 'lib/split/alternative.rb', line 78

def set_prob_field(goal)
  field = "p_winner"
  field += ":" + goal unless goal.nil?
  return field
end

#to_sObject


26
27
28
# File 'lib/split/alternative.rb', line 26

def to_s
  name
end

#unfinished_countObject


68
69
70
# File 'lib/split/alternative.rb', line 68

def unfinished_count
  participant_count - all_completed_count
end

#validate!Object


137
138
139
140
141
# File 'lib/split/alternative.rb', line 137

def validate!
  unless String === @name || hash_with_correct_values?(@name)
    raise ArgumentError, 'Alternative must be a string'
  end
end

#z_score(goal = nil) ⇒ Object


111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/split/alternative.rb', line 111

def z_score(goal = nil)
  # p_a = Pa = proportion of users who converted within the experiment split (conversion rate)
  # p_c = Pc = proportion of users who converted within the control split (conversion rate)
  # n_a = Na = the number of impressions within the experiment split
  # n_c = Nc = the number of impressions within the control split

  control = experiment.control
  alternative = self

  return 'N/A' if control.name == alternative.name

  p_a = alternative.conversion_rate(goal)
  p_c = control.conversion_rate(goal)

  n_a = alternative.participant_count
  n_c = control.participant_count

  z_score = Split::Zscore.calculate(p_a, n_a, p_c, n_c)
end