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.



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

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.



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

def experiment_name
  @experiment_name
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#weightObject

Returns the value of attribute weight.



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

def weight
  @weight
end

Instance Method Details

#all_completed_countObject



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

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



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

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

#control?Boolean

Returns:

  • (Boolean)


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

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

#conversion_rate(goal = nil) ⇒ Object



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

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

#deleteObject



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

def delete
  Split.redis.del(key)
end

#experimentObject



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

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

#goalsObject



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

def goals
  self.experiment.goals
end

#increment_completion(goal = nil) ⇒ Object



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

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

#increment_participationObject



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

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

#p_winner(goal = nil) ⇒ Object



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

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



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

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

#participant_count=(count) ⇒ Object



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

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

#resetObject



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

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



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

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



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

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

#set_field(goal) ⇒ Object



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

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

#set_p_winner(prob, goal = nil) ⇒ Object



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

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



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

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

#to_sObject



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

def to_s
  name
end

#unfinished_countObject



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

def unfinished_count
  participant_count - all_completed_count
end

#validate!Object



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

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

#z_score(goal = nil) ⇒ Object



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

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