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)


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

def initialize(experiment, id, value) # , participants, converted, conversions)
  @experiment = experiment
  @id = id
  @name = I18n.t('vanity.option_number', char: (@id + 65).chr.upcase)
  @value = value
end

Instance Attribute Details

#differenceObject

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


53
54
55
# File 'lib/vanity/experiment/alternative.rb', line 53

def difference
  @difference
end

#experimentObject (readonly)

Experiment this alternative belongs to.


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

def experiment
  @experiment
end

#idObject (readonly)

Alternative id, only unique for this experiment.


13
14
15
# File 'lib/vanity/experiment/alternative.rb', line 13

def id
  @id
end

#nameObject (readonly)

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


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

def name
  @name
end

#probabilityObject

Probability alternative is best. Populated by AbTest#score.


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

def probability
  @probability
end

#valueObject (readonly)

Alternative value.


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

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.


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

def z_score
  @z_score
end

Instance Method Details

#<=>(other) ⇒ Object


66
67
68
# File 'lib/vanity/experiment/alternative.rb', line 66

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

#==(other) ⇒ Object


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

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

#conversion_rateObject

Conversion rate calculated as converted/participants


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

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

#conversionsObject

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


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

def conversions
  load_counts unless defined?(@conversions)
  @conversions
end

#convertedObject

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


32
33
34
35
# File 'lib/vanity/experiment/alternative.rb', line 32

def converted
  load_counts unless defined?(@converted)
  @converted
end

#default?Boolean

Returns:

  • (Boolean)

90
91
92
# File 'lib/vanity/experiment/alternative.rb', line 90

def default?
  @experiment.default == self
end

#inspectObject


78
79
80
# File 'lib/vanity/experiment/alternative.rb', line 78

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

#load_countsObject


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

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.


62
63
64
# File 'lib/vanity/experiment/alternative.rb', line 62

def measure
  conversion_rate
end

#participantsObject

Number of participants who viewed this alternative.


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

def participants
  load_counts unless defined?(@participants)
  @participants
end

#to_sObject


74
75
76
# File 'lib/vanity/experiment/alternative.rb', line 74

def to_s
  name
end