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
# 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
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



45
46
47
48
49
50
51
52
53
# File 'lib/split/alternative.rb', line 45

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



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

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

#control?Boolean

Returns:

  • (Boolean)


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

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

#conversion_rate(goal = nil) ⇒ Object



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

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



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

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

#goalsObject



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

def goals
  self.experiment.goals
end

#increment_completion(goal = nil) ⇒ Object



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

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

#increment_participationObject



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

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

#participant_countObject



32
33
34
# File 'lib/split/alternative.rb', line 32

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

#participant_count=(count) ⇒ Object



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

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



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

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

#set_field(goal) ⇒ Object



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

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

#to_sObject



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

def to_s
  name
end

#unfinished_countObject



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

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



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 92

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