edhd 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Kyle Maxwell
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,40 @@
1
+ = edhd
2
+
3
+ Experiment A/B framework for Ruby. Thread-safety is left as
4
+ an exercise to the user. I use globals. Shoot me. Supports
5
+ nested experiments.
6
+
7
+ == Overview
8
+
9
+ Experiments are defined as follows (look at the specs for more details):
10
+
11
+ * Exactly one of the hypotheses blocks in an experiment will run.
12
+ * A hypothesis will be chosen with probability proportional to its weight.
13
+ * You can pass in a seed for the random number generator to ensure that, for example, a given user always sees the same hypothesis.
14
+ * An experiment block will log which hypothesis actually runs by calling $observer.hypothesized(experiment_name, hypothesis_name, seed)
15
+ * Calling observation(name, value, seed) will log to $observer.observed(name, value, seed).
16
+ * $observer = IOObserver(STDOUT) will give you a sample observer, but you'll probably want to log to a database or something. Exercise left to the user.
17
+ * Statistical analysis is an exercise left to the user.
18
+
19
+ # Method signatures
20
+ # def experiment(name = "default", seed = rand, &block)
21
+ # def hypothesis(name, weight = 1, &block)
22
+ # def observation(name, value, seed)
23
+
24
+ # Example
25
+ experiment "hello world", current_user.login do
26
+ hypothesis "a1", 3 do
27
+ @a = 1
28
+ end
29
+
30
+ hypothesis "a2" do
31
+ @a = 2
32
+ end
33
+ end
34
+
35
+ observation "value of a", @a, current_user.login
36
+
37
+
38
+ == Copyright
39
+
40
+ Copyright (c) 2009 Kyle Maxwell. MIT Licensed
@@ -0,0 +1,45 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "edhd"
8
+ gem.summary = "Simple AB testing"
9
+ gem.description = "Simple AB testing"
10
+ gem.email = "kyle@kylemaxwell.com"
11
+ gem.homepage = "http://github.com/fizx/edhd"
12
+ gem.authors = ["Kyle Maxwell"]
13
+ gem.add_development_dependency "rspec", ">= 1.2.9"
14
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
+ end
16
+ Jeweler::GemcutterTasks.new
17
+ rescue LoadError
18
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
19
+ end
20
+
21
+ require 'spec/rake/spectask'
22
+ Spec::Rake::SpecTask.new(:spec) do |spec|
23
+ spec.libs << 'lib' << 'spec'
24
+ spec.spec_files = FileList['spec/**/*_spec.rb']
25
+ end
26
+
27
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
28
+ spec.libs << 'lib' << 'spec'
29
+ spec.pattern = 'spec/**/*_spec.rb'
30
+ spec.rcov = true
31
+ end
32
+
33
+ task :spec => :check_dependencies
34
+
35
+ task :default => :spec
36
+
37
+ require 'rake/rdoctask'
38
+ Rake::RDocTask.new do |rdoc|
39
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
40
+
41
+ rdoc.rdoc_dir = 'rdoc'
42
+ rdoc.title = "edhd #{version}"
43
+ rdoc.rdoc_files.include('README*')
44
+ rdoc.rdoc_files.include('lib/**/*.rb')
45
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,24 @@
1
+ require File.dirname(__FILE__) + "/io_observer"
2
+ module Kernel
3
+ $observer = IOObserver.new(STDOUT)
4
+
5
+ def experiment(name = "default", seed = rand)
6
+ $hypothesis_sets ||= []
7
+ $hypothesis_sets << []
8
+ yield
9
+ hypothesis_set = $hypothesis_sets.pop
10
+ hypothesis_name, block = hypothesis_set[seed.to_s.hash % hypothesis_set.length]
11
+ block.call
12
+ $observer.hypothesized(name, hypothesis_name, seed)
13
+ end
14
+
15
+ def hypothesis(name, weight = 1, &block)
16
+ weight.times do
17
+ $hypothesis_sets.last << [name, block]
18
+ end
19
+ end
20
+
21
+ def observation(name, value, seed)
22
+ $observer.observed(name, value, seed)
23
+ end
24
+ end
@@ -0,0 +1,13 @@
1
+ class IOObserver
2
+ def initialize(io)
3
+ @io = io
4
+ end
5
+
6
+ def hypothesized(experiment, hypothesis, seed)
7
+ @io.puts "#{Time.now.to_i}\thypothesis\t#{experiment}\t#{hypothesis}\t#{seed}"
8
+ end
9
+
10
+ def observed(name, value, seed)
11
+ @io.puts "#{Time.now.to_i}\tobservation\t#{name}\t#{value}\t#{seed}"
12
+ end
13
+ end
@@ -0,0 +1,160 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ class QuietObserver
4
+ def method_missing(*args);end
5
+ end
6
+
7
+ describe "Edhd" do
8
+ before do
9
+ @a = 0
10
+ @b = 0
11
+ $observer = QuietObserver.new
12
+ end
13
+
14
+ it "should define #experiment" do
15
+ Kernel.should respond_to(:experiment)
16
+ end
17
+
18
+ it "should define #hypothesis" do
19
+ Kernel.should respond_to(:hypothesis)
20
+ end
21
+
22
+ it "should define #observation" do
23
+ Kernel.should respond_to(:observation)
24
+ end
25
+
26
+ context "one hypothesis" do
27
+ it "should execute the hypothesis" do
28
+ experiment do
29
+ hypothesis "a" do
30
+ @a = 1
31
+ end
32
+ end
33
+ @a.should == 1
34
+ end
35
+ end
36
+
37
+ context "multiple hypotheses" do
38
+ it "should execute only one" do
39
+ experiment do
40
+ hypothesis "a" do
41
+ @a += 1
42
+ end
43
+ hypothesis "b" do
44
+ @a += 1
45
+ end
46
+ end
47
+ @a.should == 1
48
+ end
49
+
50
+ it "should execute randomly if no seed provided" do
51
+ 100.times do
52
+ experiment do
53
+ hypothesis "a" do
54
+ @a += 1
55
+ end
56
+ hypothesis "b" do
57
+ @b += 1
58
+ end
59
+ end
60
+ end
61
+ @a.should > 0
62
+ @b.should > 0
63
+ (@a + @b).should == 100
64
+ end
65
+
66
+ it "should execute deterministically if a seed is provided" do
67
+ 100.times do
68
+ experiment "seeded experiment", "ADSF" do
69
+ hypothesis "a" do
70
+ @a += 1
71
+ end
72
+ hypothesis "b" do
73
+ @b += 1
74
+ end
75
+ end
76
+ end
77
+ @a.should == 0
78
+ @b.should == 100
79
+ end
80
+
81
+ it "should call hypotheses more often if weighted higher" do
82
+ 5.times do
83
+ @a = 0
84
+ @b = 0
85
+ 100.times do
86
+ experiment do
87
+ hypothesis "a", 3 do
88
+ @a += 1
89
+ end
90
+ hypothesis "b" do
91
+ @b += 1
92
+ end
93
+ end
94
+ end
95
+ @a.should > @b
96
+ end
97
+ end
98
+ end
99
+
100
+ context "with an observer" do
101
+ class TestObserver
102
+ def initialize(events = [])
103
+ @events = events
104
+ end
105
+ def hypothesized(*args)
106
+ @events << [:hypothesis] + args
107
+ end
108
+ def observed(*args)
109
+ @events << [:observation] + args
110
+ end
111
+ end
112
+
113
+ before do
114
+ $observer = TestObserver.new(@events = [])
115
+ end
116
+
117
+ it "should record the hypothesis chosen" do
118
+ experiment "name", "seed" do
119
+ hypothesis "a", 3 do
120
+ @a += 1
121
+ end
122
+ hypothesis "b" do
123
+ @b += 1
124
+ end
125
+ end
126
+ @events.length.should == 1
127
+ type, exp, hypo, seed = @events.first
128
+ type.should == :hypothesis
129
+ exp.should == "name"
130
+ hypo.should == "a"
131
+ seed.should == "seed"
132
+ end
133
+
134
+ it "should record observations" do
135
+ experiment "name", "seed" do
136
+ hypothesis "a", 3 do
137
+ @a += 1
138
+ end
139
+ hypothesis "b" do
140
+ @b += 1
141
+ end
142
+ end
143
+ observation "value of a", @a, "seed"
144
+
145
+ @events.length.should == 2
146
+ type, exp, hypo, seed = @events.first
147
+ type.should == :hypothesis
148
+ exp.should == "name"
149
+ hypo.should == "a"
150
+ seed.should == "seed"
151
+
152
+ type, name, value, seed = @events.last
153
+ type.should == :observation
154
+ name.should == "value of a"
155
+ value.should == 1
156
+ seed.should == "seed"
157
+ end
158
+
159
+ end
160
+ end
@@ -0,0 +1,18 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "IOObserver" do
4
+
5
+ before do
6
+ @observer = IOObserver.new(StringIO.new(@string = ""))
7
+ end
8
+
9
+ it "should log to the io stream" do
10
+ @observer.hypothesized "experiment", "hypothesis", "seed"
11
+ @string.length.should > 0
12
+ end
13
+
14
+ it "should log to the io stream" do
15
+ @observer.observed "event", "value", "seed"
16
+ @string.length.should > 0
17
+ end
18
+ end
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,9 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'edhd'
4
+ require 'spec'
5
+ require 'spec/autorun'
6
+
7
+ Spec::Runner.configure do |config|
8
+
9
+ end
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: edhd
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Kyle Maxwell
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-10-30 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rspec
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 1.2.9
24
+ version:
25
+ description: Simple AB testing
26
+ email: kyle@kylemaxwell.com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - LICENSE
33
+ - README.rdoc
34
+ files:
35
+ - .document
36
+ - .gitignore
37
+ - LICENSE
38
+ - README.rdoc
39
+ - Rakefile
40
+ - VERSION
41
+ - lib/edhd.rb
42
+ - lib/io_observer.rb
43
+ - spec/edhd_spec.rb
44
+ - spec/io_observer_spec.rb
45
+ - spec/spec.opts
46
+ - spec/spec_helper.rb
47
+ has_rdoc: true
48
+ homepage: http://github.com/fizx/edhd
49
+ licenses: []
50
+
51
+ post_install_message:
52
+ rdoc_options:
53
+ - --charset=UTF-8
54
+ require_paths:
55
+ - lib
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: "0"
61
+ version:
62
+ required_rubygems_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: "0"
67
+ version:
68
+ requirements: []
69
+
70
+ rubyforge_project:
71
+ rubygems_version: 1.3.5
72
+ signing_key:
73
+ specification_version: 3
74
+ summary: Simple AB testing
75
+ test_files:
76
+ - spec/edhd_spec.rb
77
+ - spec/io_observer_spec.rb
78
+ - spec/spec_helper.rb