Class: Vanity::Experiment::Alternative

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

Overview

One of several alternatives in an A/B test (see AbTest#alternatives).

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(experiment, id, value) ⇒ Alternative

, participants, converted, conversions)



7
8
9
10
11
12
# File 'lib/vanity/experiment/alternative.rb', line 7

def initialize(experiment, id, value) #, participants, converted, conversions)
  @experiment = experiment
  @id = id
  @name = "option #{(@id + 65).chr}"
  @value = value
end

Instance Attribute Details

#differenceObject

Difference from least performing alternative. Populated by AbTest#score.



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

def difference
  @difference
end

#experimentObject (readonly)

Experiment this alternative belongs to.



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

def experiment
  @experiment
end

#idObject (readonly)

Alternative id, only unique for this experiment.



15
16
17
# File 'lib/vanity/experiment/alternative.rb', line 15

def id
  @id
end

#nameObject (readonly)

Alternative name (option A, option B, etc).



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

def name
  @name
end

#probabilityObject

Probability alternative is best. Populated by AbTest#score.



51
52
53
# File 'lib/vanity/experiment/alternative.rb', line 51

def probability
  @probability
end

#valueObject (readonly)

Alternative value.



21
22
23
# File 'lib/vanity/experiment/alternative.rb', line 21

def value
  @value
end

#z_scoreObject

Z-score for this alternative, related to 2nd-best performing alternative. Populated by AbTest#score if #score_method is :z_score.



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

def z_score
  @z_score
end

Instance Method Details

#<=>(other) ⇒ Object



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

def <=>(other)
  measure <=> other.measure
end

#==(other) ⇒ Object



72
73
74
# File 'lib/vanity/experiment/alternative.rb', line 72

def ==(other)
  other && id == other.id && experiment == other.experiment
end

#conversion_rateObject

Conversion rate calculated as converted/participants



58
59
60
# File 'lib/vanity/experiment/alternative.rb', line 58

def conversion_rate
  @conversion_rate ||= (participants > 0 ? converted.to_f/participants.to_f  : 0.0)
end

#conversionsObject

Number of conversions for this alternative (same participant may be counted more than once).



41
42
43
44
# File 'lib/vanity/experiment/alternative.rb', line 41

def conversions
  load_counts unless @conversions
  @conversions
end

#convertedObject

Number of participants who converted on this alternative (a participant is counted only once).



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

def converted
  load_counts unless @converted
  @converted
end

#inspectObject



80
81
82
# File 'lib/vanity/experiment/alternative.rb', line 80

def inspect
  "#{name}: #{value} #{converted}/#{participants}"
end

#load_countsObject



84
85
86
87
88
89
90
# File 'lib/vanity/experiment/alternative.rb', line 84

def load_counts
  if @experiment.playground.collecting?
    @participants, @converted, @conversions = @experiment.playground.connection.ab_counts(@experiment.id, id).values_at(:participants, :converted, :conversions)
  else
    @participants = @converted = @conversions = 0
  end
end

#measureObject

The measure we use to order (sort) alternatives and decide which one is better (by calculating z-score). Defaults to conversion rate.



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

def measure
  conversion_rate
end

#participantsObject

Number of participants who viewed this alternative.



27
28
29
30
# File 'lib/vanity/experiment/alternative.rb', line 27

def participants
  load_counts unless @participants
  @participants
end

#to_sObject



76
77
78
# File 'lib/vanity/experiment/alternative.rb', line 76

def to_s
  name
end