accumulators 0.4.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -89,6 +89,7 @@ Available accumulators
89
89
 
90
90
  * Count
91
91
  * Sum
92
+ * MinMax
92
93
  * Mean
93
94
  * MeanVariance
94
95
 
@@ -97,7 +98,6 @@ TODO
97
98
 
98
99
  * Skew?
99
100
  * Weighted Means
100
- * min, max, and min-max
101
101
 
102
102
  Contributing to accumulators
103
103
  ----------------------------
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.4.0
1
+ 0.5.0
data/accumulators.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{accumulators}
8
- s.version = "0.4.0"
8
+ s.version = "0.5.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Gavin Heavyside"]
@@ -31,10 +31,12 @@ Gem::Specification.new do |s|
31
31
  "lib/accumulators/count.rb",
32
32
  "lib/accumulators/mean.rb",
33
33
  "lib/accumulators/mean_variance.rb",
34
+ "lib/accumulators/minmax.rb",
34
35
  "lib/accumulators/sum.rb",
35
36
  "spec/lib/accumulators/count_spec.rb",
36
37
  "spec/lib/accumulators/mean_spec.rb",
37
38
  "spec/lib/accumulators/mean_variance_spec.rb",
39
+ "spec/lib/accumulators/min_max_spec.rb",
38
40
  "spec/lib/accumulators/sum_spec.rb",
39
41
  "spec/spec_helper.rb"
40
42
  ]
data/lib/accumulators.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  module Accumulators
2
2
  autoload :Count, "accumulators/count"
3
3
  autoload :Sum, "accumulators/sum"
4
+ autoload :MinMax, "accumulators/minmax"
4
5
  autoload :Mean, "accumulators/mean"
5
6
  autoload :MeanVariance, "accumulators/mean_variance"
6
7
  end
@@ -0,0 +1,18 @@
1
+ module Accumulators
2
+ class MinMax
3
+ attr_reader :min
4
+ attr_reader :max
5
+
6
+ def add(rhs)
7
+ if rhs.is_a? Numeric
8
+ @min = [@min, rhs].min rescue rhs
9
+ @max = [@max, rhs].max rescue rhs
10
+ elsif rhs.is_a? self.class
11
+ @min = [@min, rhs.min].min rescue rhs.min
12
+ @max = [@max, rhs.max].max rescue rhs.max
13
+ else
14
+ raise ArgumentError.new("You may not add #{rhs.class} to #{self.class}")
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,77 @@
1
+ require 'spec_helper'
2
+
3
+ module Accumulators
4
+ describe MinMax do
5
+ let(:minmax){ MinMax.new }
6
+
7
+ context "Creation" do
8
+ it "can be created" do
9
+ lambda{ MinMax.new }.should_not raise_error
10
+ end
11
+
12
+ it "returns a min and max of nil before anything is added to it" do
13
+ minmax.min.should be_nil
14
+ minmax.max.should be_nil
15
+ end
16
+ end
17
+
18
+ context "adding numbers or distributions" do
19
+ it "allows integers to be added" do
20
+ lambda { minmax.add 5 }.should_not raise_error
21
+ end
22
+
23
+ it "allows floats to be added" do
24
+ lambda { minmax.add 3.4 }.should_not raise_error
25
+ end
26
+
27
+ it "allows other MinMax distributions to be added" do
28
+ lambda { minmax.add MinMax.new }.should_not raise_error
29
+ end
30
+
31
+ it "raises an ArgumentError if a String is added" do
32
+ lambda { minmax.add "1.5" }.should raise_error(
33
+ ArgumentError,
34
+ "You may not add String to Accumulators::MinMax")
35
+ end
36
+ end
37
+
38
+ context "correctness of int min/max" do
39
+ it "returns Integer -3, 76 when 3,23,-3,-2,75,76,1 are added" do
40
+ [3,23,-3,-2,75,76,1].each{|n| minmax.add n }
41
+ minmax.min.should be_a Integer
42
+ minmax.min.should == -3
43
+ minmax.max.should be_a Integer
44
+ minmax.max.should == 76
45
+ end
46
+ end
47
+
48
+ context "correctness of float min/max" do
49
+ it "returns Integer -3.2, 76.2 when 3.2,23.2,-3.2,-2.2,75.2,76.2,1.2 are added" do
50
+ [3.2,23.2,-3.2,-2.2,75.2,76.2,1.2].each{|n| minmax.add n }
51
+ minmax.min.should be_a Float
52
+ minmax.min.should == -3.2
53
+ minmax.max.should be_a Float
54
+ minmax.max.should == 76.2
55
+ end
56
+ end
57
+
58
+ context "Correctness of MinMax additions" do
59
+ it "combines two MinMaxes correctly" do
60
+ mm2 = MinMax.new
61
+ vals = []
62
+ 500.times do
63
+ vals << rand * 1000*1000
64
+ minmax.add vals.last
65
+ vals << rand(100*1000)
66
+ mm2.add vals.last
67
+ end
68
+
69
+ minmax.add(mm2)
70
+ minmax.min.class.should == vals.min.class
71
+ minmax.min.should == vals.min
72
+ minmax.max.class.should == vals.max.class
73
+ minmax.max.should == vals.max
74
+ end
75
+ end
76
+ end
77
+ end
data/spec/spec_helper.rb CHANGED
@@ -7,7 +7,7 @@ require 'accumulators'
7
7
  # in ./support/ and its subdirectories.
8
8
  Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
9
9
 
10
- EPSILON = 0.00001
10
+ EPSILON = 0.000001
11
11
 
12
12
  RSpec.configure do |config|
13
13
 
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: accumulators
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.4.0
5
+ version: 0.5.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - Gavin Heavyside
@@ -103,10 +103,12 @@ files:
103
103
  - lib/accumulators/count.rb
104
104
  - lib/accumulators/mean.rb
105
105
  - lib/accumulators/mean_variance.rb
106
+ - lib/accumulators/minmax.rb
106
107
  - lib/accumulators/sum.rb
107
108
  - spec/lib/accumulators/count_spec.rb
108
109
  - spec/lib/accumulators/mean_spec.rb
109
110
  - spec/lib/accumulators/mean_variance_spec.rb
111
+ - spec/lib/accumulators/min_max_spec.rb
110
112
  - spec/lib/accumulators/sum_spec.rb
111
113
  - spec/spec_helper.rb
112
114
  has_rdoc: true
@@ -123,7 +125,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
123
125
  requirements:
124
126
  - - ">="
125
127
  - !ruby/object:Gem::Version
126
- hash: -3603142917674474649
128
+ hash: -2545635480310131054
127
129
  segments:
128
130
  - 0
129
131
  version: "0"