Class: FieldTest::Experiment
- Inherits:
-
Object
- Object
- FieldTest::Experiment
- Defined in:
- lib/field_test/experiment.rb
Instance Attribute Summary collapse
-
#ended_at ⇒ Object
readonly
Returns the value of attribute ended_at.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#started_at ⇒ Object
readonly
Returns the value of attribute started_at.
-
#variants ⇒ Object
readonly
Returns the value of attribute variants.
-
#winner ⇒ Object
readonly
Returns the value of attribute winner.
Class Method Summary collapse
Instance Method Summary collapse
- #active? ⇒ Boolean
- #convert(participants) ⇒ Object
-
#initialize(attributes) ⇒ Experiment
constructor
A new instance of Experiment.
- #memberships ⇒ Object
- #results ⇒ Object
- #variant(participants, options = {}) ⇒ Object
Constructor Details
#initialize(attributes) ⇒ Experiment
Returns a new instance of Experiment.
5 6 7 8 9 10 11 12 13 |
# File 'lib/field_test/experiment.rb', line 5 def initialize(attributes) attributes = attributes.symbolize_keys @id = attributes[:id] @name = attributes[:name] || @id.to_s.titleize @variants = attributes[:variants] @winner = attributes[:winner] @started_at = Time.zone.parse(attributes[:started_at].to_s) if attributes[:started_at] @ended_at = Time.zone.parse(attributes[:ended_at].to_s) if attributes[:ended_at] end |
Instance Attribute Details
#ended_at ⇒ Object (readonly)
Returns the value of attribute ended_at.
3 4 5 |
# File 'lib/field_test/experiment.rb', line 3 def ended_at @ended_at end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
3 4 5 |
# File 'lib/field_test/experiment.rb', line 3 def id @id end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
3 4 5 |
# File 'lib/field_test/experiment.rb', line 3 def name @name end |
#started_at ⇒ Object (readonly)
Returns the value of attribute started_at.
3 4 5 |
# File 'lib/field_test/experiment.rb', line 3 def started_at @started_at end |
#variants ⇒ Object (readonly)
Returns the value of attribute variants.
3 4 5 |
# File 'lib/field_test/experiment.rb', line 3 def variants @variants end |
#winner ⇒ Object (readonly)
Returns the value of attribute winner.
3 4 5 |
# File 'lib/field_test/experiment.rb', line 3 def winner @winner end |
Class Method Details
.all ⇒ Object
112 113 114 115 116 |
# File 'lib/field_test/experiment.rb', line 112 def self.all FieldTest.config["experiments"].map do |id, settings| FieldTest::Experiment.new(settings.merge(id: id.to_s)) end end |
.find(id) ⇒ Object
105 106 107 108 109 110 |
# File 'lib/field_test/experiment.rb', line 105 def self.find(id) experiment = all.index_by(&:id)[id.to_s] raise FieldTest::ExperimentNotFound unless experiment experiment end |
Instance Method Details
#active? ⇒ Boolean
101 102 103 |
# File 'lib/field_test/experiment.rb', line 101 def active? !winner end |
#convert(participants) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/field_test/experiment.rb', line 42 def convert(participants) participants = FieldTest::Participant.standardize(participants) membership = membership_for(participants) if membership membership.converted = true membership.save! if membership.changed? true else false end end |
#memberships ⇒ Object
55 56 57 |
# File 'lib/field_test/experiment.rb', line 55 def memberships FieldTest::Membership.where(experiment: id) end |
#results ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/field_test/experiment.rb', line 59 def results data = memberships.group(:variant).group(:converted) data = data.where("created_at >= ?", started_at) if started_at data = data.where("created_at <= ?", ended_at) if ended_at data = data.count results = {} variants.each do |variant| converted = data[[variant, true]].to_i participated = converted + data[[variant, false]].to_i results[variant] = { participated: participated, converted: converted, conversion_rate: participated > 0 ? converted.to_f / participated : nil } end case variants.size when 1 results[variants[0]][:prob_winning] = 1 when 2, 3 variants.size.times do |i| c = results.values[i] b = results.values[(i + 1) % variants.size] a = results.values[(i + 2) % variants.size] alpha_a = 1 + a[:converted] beta_a = 1 + a[:participated] - a[:converted] alpha_b = 1 + b[:converted] beta_b = 1 + b[:participated] - b[:converted] alpha_c = 1 + c[:converted] beta_c = 1 + c[:participated] - c[:converted] results[variants[i]][:prob_winning] = if variants.size == 2 prob_b_beats_a(alpha_b, beta_b, alpha_c, beta_c) else prob_c_beats_a_and_b(alpha_a, beta_a, alpha_b, beta_b, alpha_c, beta_c) end end end results end |
#variant(participants, options = {}) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/field_test/experiment.rb', line 15 def variant(participants, = {}) return winner if winner return variants.first if [:exclude] participants = FieldTest::Participant.standardize(participants) membership = membership_for(participants) || FieldTest::Membership.new(experiment: id) if [:variant] && variants.include?([:variant]) membership.variant = [:variant] else membership.variant ||= variants.sample end # upgrade to preferred participant membership.participant = participants.first if membership.changed? begin membership.save! rescue ActiveRecord::RecordNotUnique membership = memberships.find_by(participant: participants.first) end end membership.try(:variant) || variants.first end |