Class: Split::Alternative

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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_nameObject

Returns the value of attribute experiment_name.



4
5
6
# File 'lib/split/alternative.rb', line 4

def experiment_name
  @experiment_name
end

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/split/alternative.rb', line 3

def name
  @name
end

#weightObject

Returns the value of attribute weight.



5
6
7
# File 'lib/split/alternative.rb', line 5

def weight
  @weight
end

Instance Method Details

#all_completed_countObject



39
40
41
42
43
44
45
46
47
# File 'lib/split/alternative.rb', line 39

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



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

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

#control?Boolean

Returns:

  • (Boolean)


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

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

#conversion_rate(goal = nil) ⇒ Object



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

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

#deleteObject



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

def delete
  Split.redis.del(key)
end

#experimentObject



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

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

#goalsObject



22
23
24
# File 'lib/split/alternative.rb', line 22

def goals
  self.experiment.goals
end

#increment_completion(goal = nil) ⇒ Object



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

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

#increment_participationObject



64
65
66
# File 'lib/split/alternative.rb', line 64

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

#participant_countObject



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

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

#participant_count=(count) ⇒ Object



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

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

#resetObject



123
124
125
126
127
128
129
130
131
# File 'lib/split/alternative.rb', line 123

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



112
113
114
115
# File 'lib/split/alternative.rb', line 112

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

#set_completed_count(count, goal = nil) ⇒ Object



59
60
61
62
# File 'lib/split/alternative.rb', line 59

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

#set_field(goal) ⇒ Object



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

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

#to_sObject



18
19
20
# File 'lib/split/alternative.rb', line 18

def to_s
  name
end

#unfinished_countObject



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

def unfinished_count
  participant_count - all_completed_count
end

#validate!Object



117
118
119
120
121
# File 'lib/split/alternative.rb', line 117

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

#z_score(goal = nil) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/split/alternative.rb', line 86

def z_score(goal = nil)
  # 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(goal)
  ctr_c = control.conversion_rate(goal)


  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