Class: Split::Alternative

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

Instance Attribute Summary collapse

Class Method 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

Class Method Details

.hash_with_correct_values?(name) ⇒ Boolean

Returns:

  • (Boolean)


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

def self.hash_with_correct_values?(name)
  Hash === name && String === name.keys.first && Float(name.values.first) rescue false
end

.valid?(name) ⇒ Boolean

Returns:

  • (Boolean)


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

def self.valid?(name)
  String === name || hash_with_correct_values?(name)
end

Instance Method Details

#completed_countObject



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

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

#completed_count=(count) ⇒ Object



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

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

#control?Boolean

Returns:

  • (Boolean)


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

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

#conversion_rateObject



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

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

#deleteObject



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

def delete
  Split.redis.del(key)
end

#experimentObject



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

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

#increment_completionObject



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

def increment_completion
  Split.redis.hincrby key, 'completed_count', 1
end

#increment_participationObject



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

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

#participant_countObject



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

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

#participant_count=(count) ⇒ Object



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

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

#resetObject



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

def reset
  Split.redis.hmset key, 'participant_count', 0, 'completed_count', 0
end

#saveObject



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

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

#to_sObject



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

def to_s
  name
end

#z_scoreObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/split/alternative.rb', line 59

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