-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from Springest/weighted_scenarios
Weighted scenarios.
- Loading branch information
Showing
10 changed files
with
165 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,8 +6,8 @@ require 'ab_panel/version' | |
Gem::Specification.new do |spec| | ||
spec.name = "ab_panel" | ||
spec.version = AbPanel::VERSION | ||
spec.authors = ["Wouter de Vos", "Mark Mulder"] | ||
spec.email = ["[email protected]", "[email protected]"] | ||
spec.authors = ["Wouter de Vos", "Mark Mulder", "Peter de Ruijter"] | ||
spec.email = ["[email protected]", "[email protected]", "[email protected]"] | ||
spec.description = %q{Run A/B test experiments on your Rails 3+ site using Mixpanel as a backend.} | ||
spec.summary = %q{Run A/B test experiments on your Rails 3+ site using Mixpanel as a backend.} | ||
spec.homepage = "https://github.com/Springest/ab_panel" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
module AbPanel | ||
VERSION = "0.2.0" | ||
VERSION = "0.3.0" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
class Array | ||
def weighted_sample(weights=nil) | ||
weights = Array.new(length, 1.0) if weights.nil? || weights.sum == 0 | ||
total = weights.sum | ||
|
||
# The total sum of weights is multiplied by a random number | ||
trigger = Kernel::rand * total | ||
|
||
subtotal = 0 | ||
result = nil | ||
|
||
# The subtotal is checked agains the trigger. The higher the sum, the higher | ||
# the probability of triggering a result. | ||
weights.each_with_index do |weight, index| | ||
subtotal += weight | ||
|
||
if subtotal > trigger | ||
result = self[index] | ||
break | ||
end | ||
end | ||
# Returns self.last from current array if result is nil | ||
result || last | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
require 'spec_helper' | ||
|
||
describe AbPanel::Config do | ||
let(:config) { AbPanel::Config.new } | ||
before do | ||
AbPanel::Config.any_instance.stub(:settings) { { exp1: { scenario1: 25, scenario2: 75 } } } | ||
end | ||
|
||
describe '.experiments' do | ||
subject { config.experiments } | ||
it { should =~ [:exp1] } | ||
end | ||
|
||
describe '.weights' do | ||
subject { config.weights('exp1') } | ||
|
||
it { should =~ [75.0, 25.0] } | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
require 'spec_helper' | ||
|
||
describe Array do | ||
describe '.weighted_sample' do | ||
before do | ||
Kernel.stub(:rand) { 0.5 } | ||
end | ||
|
||
context "Stub test" do | ||
subject { Kernel.rand } | ||
it { should eq 0.5 } | ||
end | ||
|
||
let(:array) { [1, 2, 3, 4] } | ||
subject { array.weighted_sample } | ||
|
||
it { should eq 3 } | ||
|
||
context "different random" do | ||
before do | ||
Kernel.stub(:rand) { 0 } | ||
end | ||
|
||
it { should eq 1 } | ||
end | ||
|
||
context "different random" do | ||
before do | ||
Kernel.stub(:rand) { 1 } | ||
end | ||
|
||
it { should eq 4 } | ||
end | ||
|
||
context "with weights" do | ||
subject { array.weighted_sample([1, 0, 0, 0]) } | ||
it { should eq 1 } | ||
end | ||
|
||
context "all the same weights" do | ||
before { Kernel.stub(:rand) { 1 } } | ||
subject { array.weighted_sample([0, 0, 0, 0]) } | ||
it { should eq 4 } | ||
context "random 0" do | ||
before { Kernel.stub(:rand) { 0 } } | ||
it { should eq 1 } | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
experiment1: | ||
- scenario1 | ||
- scenario2 | ||
- scenario3 | ||
scenario1: 25 | ||
scenario2: 25 | ||
scenario3: 25 | ||
original: 25 | ||
|
||
experiment2: | ||
- scenario4 | ||
- scenario5 | ||
scenario4: 33.4 | ||
scenario5: 33.3 | ||
original: 33.3 | ||
|