accumulators 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ group :development do
9
+ gem "rspec", "~> 2.1.0"
10
+ gem "cucumber", ">= 0"
11
+ gem "bundler", "~> 1.0.0"
12
+ gem "jeweler", "~> 1.5.1"
13
+ gem "rcov", ">= 0"
14
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,41 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ builder (2.1.2)
5
+ cucumber (0.9.4)
6
+ builder (~> 2.1.2)
7
+ diff-lcs (~> 1.1.2)
8
+ gherkin (~> 2.2.9)
9
+ json (~> 1.4.6)
10
+ term-ansicolor (~> 1.0.5)
11
+ diff-lcs (1.1.2)
12
+ gherkin (2.2.9)
13
+ json (~> 1.4.6)
14
+ term-ansicolor (~> 1.0.5)
15
+ git (1.2.5)
16
+ jeweler (1.5.1)
17
+ bundler (~> 1.0.0)
18
+ git (>= 1.2.5)
19
+ rake
20
+ json (1.4.6)
21
+ rake (0.8.7)
22
+ rcov (0.9.9)
23
+ rspec (2.1.0)
24
+ rspec-core (~> 2.1.0)
25
+ rspec-expectations (~> 2.1.0)
26
+ rspec-mocks (~> 2.1.0)
27
+ rspec-core (2.1.0)
28
+ rspec-expectations (2.1.0)
29
+ diff-lcs (~> 1.1.2)
30
+ rspec-mocks (2.1.0)
31
+ term-ansicolor (1.0.5)
32
+
33
+ PLATFORMS
34
+ ruby
35
+
36
+ DEPENDENCIES
37
+ bundler (~> 1.0.0)
38
+ cucumber
39
+ jeweler (~> 1.5.1)
40
+ rcov
41
+ rspec (~> 2.1.0)
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Gavin Heavyside
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.
data/README.rdoc ADDED
@@ -0,0 +1,27 @@
1
+ = accumulators
2
+
3
+ Statistical accumulators for Ruby
4
+
5
+ == TODO ==
6
+
7
+ * Running Mean
8
+ * Running Variance/Std Deviation (sample and population)
9
+ * Combination of mean and mean-variance
10
+ * Skew?
11
+ * Weighted Means
12
+
13
+ == Contributing to accumulators
14
+
15
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
16
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
17
+ * Fork the project
18
+ * Start a feature/bugfix branch
19
+ * Commit and push until you are happy with your contribution
20
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
21
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
22
+
23
+ == Copyright
24
+
25
+ Copyright (c) 2010 Gavin Heavyside. See LICENSE.txt for
26
+ further details.
27
+
data/Rakefile ADDED
@@ -0,0 +1,53 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'rake'
11
+
12
+ require 'jeweler'
13
+ Jeweler::Tasks.new do |gem|
14
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
15
+ gem.name = "accumulators"
16
+ gem.homepage = "http://github.com/hgavin/accumulators"
17
+ gem.license = "MIT"
18
+ gem.summary = %Q{Statiscal accumulators for Ruby}
19
+ gem.description = %Q{Statistical accumulators for Ruby}
20
+ gem.email = "gavin@heavyside.co.uk"
21
+ gem.authors = ["Gavin Heavyside"]
22
+ # Include your dependencies below. Runtime dependencies are required when using your gem,
23
+ # and development dependencies are only needed for development (ie running rake tasks, tests, etc)
24
+ # gem.add_runtime_dependency 'jabber4r', '> 0.1'
25
+ # gem.add_development_dependency 'rspec', '> 1.2.3'
26
+ end
27
+ Jeweler::RubygemsDotOrgTasks.new
28
+
29
+ require 'rspec/core'
30
+ require 'rspec/core/rake_task'
31
+ RSpec::Core::RakeTask.new(:spec) do |spec|
32
+ spec.pattern = FileList['spec/**/*_spec.rb']
33
+ end
34
+
35
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
36
+ spec.pattern = 'spec/**/*_spec.rb'
37
+ spec.rcov = true
38
+ end
39
+
40
+ require 'cucumber/rake/task'
41
+ Cucumber::Rake::Task.new(:features)
42
+
43
+ task :default => :spec
44
+
45
+ require 'rake/rdoctask'
46
+ Rake::RDocTask.new do |rdoc|
47
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
48
+
49
+ rdoc.rdoc_dir = 'rdoc'
50
+ rdoc.title = "accumulators #{version}"
51
+ rdoc.rdoc_files.include('README*')
52
+ rdoc.rdoc_files.include('lib/**/*.rb')
53
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1 @@
1
+ Autotest.add_discovery { "rspec2" }
@@ -0,0 +1,9 @@
1
+ Feature: something something
2
+ In order to something something
3
+ A user something something
4
+ something something something
5
+
6
+ Scenario: something something
7
+ Given inspiration
8
+ When I create a sweet new gem
9
+ Then everyone should see how awesome I am
File without changes
@@ -0,0 +1,13 @@
1
+ require 'bundler'
2
+ begin
3
+ Bundler.setup(:default, :development)
4
+ rescue Bundler::BundlerError => e
5
+ $stderr.puts e.message
6
+ $stderr.puts "Run `bundle install` to install missing gems"
7
+ exit e.status_code
8
+ end
9
+
10
+ $LOAD_PATH.unshift(File.dirname(__FILE__) + '/../../lib')
11
+ require 'accumulators'
12
+
13
+ require 'rspec/expectations'
@@ -0,0 +1,18 @@
1
+ module Accumulators
2
+ class Count
3
+ attr_reader :count
4
+
5
+ def initialize
6
+ @count = 0
7
+ end
8
+
9
+ def add(rhs)
10
+ if rhs.is_a? Accumulators::Count
11
+ @count += rhs.count
12
+ else
13
+ @count += 1
14
+ end
15
+ end
16
+ end
17
+ end
18
+
@@ -0,0 +1,27 @@
1
+
2
+ module Accumulators
3
+ class Mean
4
+ attr_reader :count, :mean
5
+
6
+ def initialize
7
+ @count = 0
8
+ @mean = 0.0
9
+ end
10
+
11
+ def add(rhs)
12
+ if rhs.is_a? Numeric
13
+ value_to_add = rhs.to_f
14
+ @mean = @mean + (value_to_add - @mean)/(@count + 1)
15
+ @count += 1
16
+ elsif rhs.is_a? Accumulators::Mean
17
+ sum = @mean * @count
18
+ rhs_sum = rhs.mean * rhs.count
19
+ @count = @count + rhs.count
20
+ @mean = (sum + rhs_sum) / @count
21
+ else
22
+ raise ArgumentError.new("Can only add numbers to a Mean Accumulator")
23
+ end
24
+ end
25
+ end
26
+ end
27
+
@@ -0,0 +1,3 @@
1
+ module Accumulators
2
+ end
3
+
@@ -0,0 +1,65 @@
1
+ require 'spec_helper'
2
+
3
+ require 'accumulators/count'
4
+
5
+
6
+ describe "Accumulators" do
7
+ describe "Count" do
8
+ before :each do
9
+ @count = Accumulators::Count.new
10
+ end
11
+
12
+ context "Creation" do
13
+ it "can be created" do
14
+ lambda{ Accumulators::Count.new }.should_not raise_error
15
+ end
16
+
17
+ it "returns count of 0 before anything is added to it" do
18
+ Accumulators::Count.new.count.should == 0
19
+ end
20
+ end
21
+
22
+ context "adding numbers or distributions" do
23
+ it "allows integers to be added" do
24
+ lambda{@count.add 5}.should_not raise_error
25
+ end
26
+
27
+ it "allows floats to be added" do
28
+ lambda{@count.add 3.4}.should_not raise_error
29
+ end
30
+
31
+ it "allows other Count distributions to be added" do
32
+ c2 = Accumulators::Count.new
33
+ lambda{@count.add c2}.should_not raise_error
34
+ end
35
+
36
+ it "allows strings to be added" do
37
+ lambda{@count.add "1.5"}.should_not raise_error
38
+ end
39
+ end
40
+
41
+ context "correctness of item additions" do
42
+ it "should return a count of 1 when a single item is added" do
43
+ @count.add 5
44
+ @count.count.should == 1
45
+ end
46
+
47
+ it "should return the count of how many items have been added" do
48
+ 1.upto(1000) do |i|
49
+ @count.add i
50
+ @count.count.should == i
51
+ end
52
+ end
53
+ end
54
+
55
+ context "correctness of accumulator additions" do
56
+ it "should combine two Count accumulators correctly" do
57
+ c2 = Accumulators::Count.new
58
+ 10.times{ c2.add 1; @count.add 1 }
59
+ @count.add(c2)
60
+ @count.count.should == 20
61
+ end
62
+ end
63
+ end
64
+ end
65
+
@@ -0,0 +1,102 @@
1
+ require 'spec_helper'
2
+
3
+ require 'accumulators/mean'
4
+
5
+ EPSILON = 0.00001
6
+
7
+ describe "Accumulators" do
8
+ describe "Mean" do
9
+ before :each do
10
+ @mean = Accumulators::Mean.new
11
+ end
12
+
13
+ context "Creation" do
14
+ it "can be created" do
15
+ lambda{ Accumulators::Mean.new}.should_not raise_error
16
+ end
17
+
18
+ it "returns count and mean of 0 before anything is added to it" do
19
+ mean = Accumulators::Mean.new
20
+ mean.count.should == 0
21
+ mean.mean.should be_within(EPSILON).of(0.0)
22
+ end
23
+
24
+ end
25
+
26
+ context "adding numbers or distributions" do
27
+ it "allows integers to be added" do
28
+ lambda{@mean.add 5}.should_not raise_error
29
+ end
30
+
31
+ it "allows floats to be added" do
32
+ lambda{@mean.add 3.4}.should_not raise_error
33
+ end
34
+
35
+ it "allows other Mean distributions to be added" do
36
+ mean2 = Accumulators::Mean.new
37
+ lambda{@mean.add mean2}.should_not raise_error
38
+ end
39
+
40
+ it "raises an ArgumentError if a string is added" do
41
+ lambda{@mean.add "1.5"}.should raise_error(ArgumentError)
42
+ end
43
+ end
44
+
45
+ context "correctness of int additions" do
46
+ it "should return count of 1 and mean of 5 when 5 is added" do
47
+ @mean.add(5)
48
+ @mean.count.should == 1
49
+ @mean.mean.should be_within(EPSILON).of(5)
50
+ end
51
+
52
+ it "should calculate the mean correctly for a set of integers" do
53
+ 1.upto(10) {|i| @mean.add i}
54
+ @mean.count.should == 10
55
+ @mean.mean.should be_within(EPSILON).of((1..10).reduce(:+).to_f/10)
56
+ end
57
+
58
+ it "should calculate the mean correctly for a random set of 1000 integers" do
59
+ vals = []
60
+ 1000.times do
61
+ vals << rand(100000)
62
+ @mean.add(vals.last)
63
+ end
64
+
65
+ @mean.mean.should be_within(EPSILON).of(vals.reduce(:+).to_f/vals.size)
66
+ end
67
+ end
68
+
69
+ context "correctness of float additions" do
70
+ it "should calculate the mean correctly for a set of floats" do
71
+ 1.upto(10) {|i| @mean.add i+0.1}
72
+ @mean.count.should == 10
73
+ @mean.mean.should be_within(EPSILON).of((1..10).map{|i| i+0.1}.reduce(:+)/10)
74
+ end
75
+
76
+ it "should calculate the mean correctly for a random set of 1000 floats" do
77
+ vals = []
78
+ 1000.times do
79
+ vals << rand * 1000000
80
+ @mean.add vals.last
81
+ end
82
+ @mean.mean.should be_within(EPSILON).of(vals.reduce(:+)/vals.size)
83
+ end
84
+ end
85
+
86
+ context "correctness of accumulator additions" do
87
+ it "should combine two means correctly" do
88
+ m2 = Accumulators::Mean.new
89
+ vals = []
90
+ 500.times do
91
+ vals << rand * 1000000
92
+ @mean.add vals.last
93
+ vals << rand * 100000
94
+ m2.add vals.last
95
+ end
96
+ @mean.add(m2)
97
+ @mean.count.should == 1000
98
+ @mean.mean.should be_within(EPSILON).of(vals.reduce(:+)/vals.size)
99
+ end
100
+ end
101
+ end
102
+ end
@@ -0,0 +1,12 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rspec'
4
+ require 'accumulators'
5
+
6
+ # Requires supporting files with custom matchers and macros, etc,
7
+ # in ./support/ and its subdirectories.
8
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
9
+
10
+ RSpec.configure do |config|
11
+
12
+ end
metadata ADDED
@@ -0,0 +1,155 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: accumulators
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Gavin Heavyside
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-11-27 00:00:00 +00:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rspec
22
+ requirement: &id001 !ruby/object:Gem::Requirement
23
+ none: false
24
+ requirements:
25
+ - - ~>
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 2
29
+ - 1
30
+ - 0
31
+ version: 2.1.0
32
+ type: :development
33
+ prerelease: false
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: cucumber
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ segments:
43
+ - 0
44
+ version: "0"
45
+ type: :development
46
+ prerelease: false
47
+ version_requirements: *id002
48
+ - !ruby/object:Gem::Dependency
49
+ name: bundler
50
+ requirement: &id003 !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ~>
54
+ - !ruby/object:Gem::Version
55
+ segments:
56
+ - 1
57
+ - 0
58
+ - 0
59
+ version: 1.0.0
60
+ type: :development
61
+ prerelease: false
62
+ version_requirements: *id003
63
+ - !ruby/object:Gem::Dependency
64
+ name: jeweler
65
+ requirement: &id004 !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ~>
69
+ - !ruby/object:Gem::Version
70
+ segments:
71
+ - 1
72
+ - 5
73
+ - 1
74
+ version: 1.5.1
75
+ type: :development
76
+ prerelease: false
77
+ version_requirements: *id004
78
+ - !ruby/object:Gem::Dependency
79
+ name: rcov
80
+ requirement: &id005 !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ segments:
86
+ - 0
87
+ version: "0"
88
+ type: :development
89
+ prerelease: false
90
+ version_requirements: *id005
91
+ description: Statistical accumulators for Ruby
92
+ email: gavin@heavyside.co.uk
93
+ executables: []
94
+
95
+ extensions: []
96
+
97
+ extra_rdoc_files:
98
+ - LICENSE.txt
99
+ - README.rdoc
100
+ files:
101
+ - .document
102
+ - .rspec
103
+ - Gemfile
104
+ - Gemfile.lock
105
+ - LICENSE.txt
106
+ - README.rdoc
107
+ - Rakefile
108
+ - VERSION
109
+ - autotest/discover.rb
110
+ - features/accumulators.feature
111
+ - features/step_definitions/accumulators_steps.rb
112
+ - features/support/env.rb
113
+ - lib/accumulators.rb
114
+ - lib/accumulators/count.rb
115
+ - lib/accumulators/mean.rb
116
+ - spec/accumulators/count_spec.rb
117
+ - spec/accumulators/mean_spec.rb
118
+ - spec/spec_helper.rb
119
+ has_rdoc: true
120
+ homepage: http://github.com/hgavin/accumulators
121
+ licenses:
122
+ - MIT
123
+ post_install_message:
124
+ rdoc_options: []
125
+
126
+ require_paths:
127
+ - lib
128
+ required_ruby_version: !ruby/object:Gem::Requirement
129
+ none: false
130
+ requirements:
131
+ - - ">="
132
+ - !ruby/object:Gem::Version
133
+ hash: -2246303287397258855
134
+ segments:
135
+ - 0
136
+ version: "0"
137
+ required_rubygems_version: !ruby/object:Gem::Requirement
138
+ none: false
139
+ requirements:
140
+ - - ">="
141
+ - !ruby/object:Gem::Version
142
+ segments:
143
+ - 0
144
+ version: "0"
145
+ requirements: []
146
+
147
+ rubyforge_project:
148
+ rubygems_version: 1.3.7
149
+ signing_key:
150
+ specification_version: 3
151
+ summary: Statiscal accumulators for Ruby
152
+ test_files:
153
+ - spec/accumulators/count_spec.rb
154
+ - spec/accumulators/mean_spec.rb
155
+ - spec/spec_helper.rb