easystats 0.3.1 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
data/Guardfile CHANGED
@@ -1,4 +1,4 @@
1
- guard 'rspec', :version => 2 do
1
+ guard 'rspec', :version => 2, :rvm => %w[1.8.7 1.9.3].map { |v| v << "@easystats" } do
2
2
  watch(%r{^spec/.+_spec\.rb$})
3
3
  watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
4
4
  watch('spec/spec_helper.rb') { "spec" }
@@ -1,3 +1,3 @@
1
1
  module Easystats
2
- VERSION = "0.3.1"
2
+ VERSION = "0.3.2"
3
3
  end
data/lib/easystats.rb CHANGED
@@ -1,16 +1,16 @@
1
1
  class Array
2
2
  # take in an array of numbers and calculate the mean (average)
3
3
  def mean
4
- return unless self.any?
4
+ return if self.empty?
5
5
 
6
6
  self.sum / self.count.to_f
7
- end unless Array.method_defined? :mean
7
+ end unless method_defined? :mean
8
8
 
9
- alias_method :average, :mean unless Array.method_defined? :average
9
+ alias_method :average, :mean unless method_defined? :average
10
10
 
11
11
  # take in the array of numbers and calculate the median
12
12
  def median
13
- return unless self.any?
13
+ return if self.empty?
14
14
 
15
15
  data = self
16
16
 
@@ -30,11 +30,11 @@ class Array
30
30
  end
31
31
 
32
32
  median
33
- end unless Array.method_defined? :median
33
+ end unless method_defined? :median
34
34
 
35
35
  # take in an array of numbers and return the mode
36
36
  def mode
37
- return unless self.any?
37
+ return if self.empty?
38
38
 
39
39
  # Sort the array
40
40
  data = self.sort
@@ -92,34 +92,34 @@ class Array
92
92
  highest_value
93
93
  end
94
94
  end
95
- end unless Array.method_defined? :mode
95
+ end unless method_defined? :mode
96
96
 
97
97
  # take in an array of numbers and calculate the range
98
98
  def range
99
- return unless self.any?
99
+ return if self.empty?
100
100
 
101
101
  data = self.sort
102
102
  data.last - data.first
103
- end unless Array.method_defined? :range
103
+ end unless method_defined? :range
104
104
 
105
105
  # take in an array of numbers and calculate the standard deviation
106
106
  def standard_deviation
107
- return unless self.any?
107
+ return if self.empty?
108
108
  return 0 if self.one?
109
109
 
110
110
  Math::sqrt(self.sum_of_deviations_squared / (self.count-1))
111
- end unless Array.method_defined? :standard_deviation
111
+ end unless method_defined? :standard_deviation
112
112
 
113
113
  # take in an array of numbers and calculate the sum
114
114
  def sum
115
115
  self.reduce { |total, number| total + number }
116
- end unless Array.method_defined? :sum
116
+ end unless method_defined? :sum
117
117
 
118
118
  def variance
119
- return unless self.any?
119
+ return if self.empty?
120
120
 
121
121
  self.sum_of_deviations_squared / self.count.to_f
122
- end unless Array.method_defined? :variance
122
+ end unless method_defined? :variance
123
123
 
124
124
  protected
125
125
 
@@ -1,74 +1,130 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Array do
4
- describe "#mean" do
5
- it { [].mean.should be_nil }
6
- it { [1].mean.should == 1 }
7
- it { [1,2].mean.should == 1.5 }
8
- it { [1,2,3].mean.should == 2 }
9
- it { [1,2,3,4].mean.should == 2.5 }
10
- end
11
-
12
- describe "#average" do
13
- it { [1,2,3,4].average.should == 2.5 }
14
- end
15
-
16
- describe "#median" do
17
- it { [].median.should be_nil }
18
- it { [1].median.should == 1 }
19
- it { [1,2].median.should == 1.5 }
20
- it { [1,2,3].median.should == 2 }
21
- it { [1,2,3,4].median.should == 2.5 }
22
- it { [1,2,2,4].median.should == 2 }
23
- it { [1,3,3,4].median.should == 3 }
24
- end
25
-
26
- describe "#mode" do
27
- it { [].mode.should be_nil }
28
- it { [1].mode.should == 1 }
29
- it { [1,2].mode.should be_nil }
30
- it { [1,2,3].mode.should be_nil }
31
- it { [1,2,3,4].mode.should be_nil }
32
- it { [1,2,2,4].mode.should == 2 }
33
- it { [1,3,3,4].mode.should == 3 }
34
- end
35
-
36
- describe "#range" do
37
- it { [].range.should be_nil }
38
- it { [1].range.should == 0 }
39
- it { [1,2].range.should == 1 }
40
- it { [1,2,3].range.should == 2 }
41
- it { [1,2,3,4].range.should == 3 }
42
- it { [1,2,2,4].range.should == 3 }
43
- it { [1,3,3,4].range.should == 3 }
44
- end
45
-
46
- describe "#standard_deviation" do
47
- let(:delta) { 0.00000000000001 }
48
-
49
- it { [].standard_deviation.should be_nil }
50
- it { [1].standard_deviation.should == 0 }
51
- it { [1,2].standard_deviation.should be_within(delta).of(0.707106781186548) }
52
- it { [1,2,3].standard_deviation.should be_within(delta).of(1) }
53
- it { [1,2,3,4].standard_deviation.should be_within(delta).of(1.29099444873581) }
54
- end
55
-
56
- describe "#sum" do
57
- it { [].sum.should be_nil }
58
- it { [1].sum.should == 1 }
59
- it { [1,2].sum.should == 3 }
60
- it { [1,2,3].sum.should == 6 }
61
- it { [1,2,3,4].sum.should == 10 }
62
- it { [1,2,2,4].sum.should == 9 }
63
- it { [1,3,3,4].sum.should == 11 }
64
- end
65
-
66
- describe "#variance" do
67
- it { [].variance.should be_nil }
68
- it { [1].variance.should == 0.0 }
69
- it { [1,2].variance.should == 0.25 }
70
- it { [1,2,3].variance.should == 2.0/3.0 }
71
- it { [1,2,3,4].variance.should == 1.25 }
72
- end
4
+ DELTA = 0.00000000000001
5
+
6
+ it { should respond_to :average }
7
+
8
+ build_stats_spec [], {
9
+ :mean => nil,
10
+ :median => nil,
11
+ :mode => nil,
12
+ :range => nil,
13
+ :standard_deviation => nil,
14
+ :sum => nil,
15
+ :variance => nil
16
+ }
17
+
18
+ build_stats_spec [0], {
19
+ :mean => 0,
20
+ :median => 0,
21
+ :mode => 0,
22
+ :range => 0,
23
+ :standard_deviation => 0,
24
+ :sum => 0,
25
+ :variance => 0.0
26
+ }
27
+
28
+ build_stats_spec [1], {
29
+ :mean => 1,
30
+ :median => 1,
31
+ :mode => 1,
32
+ :range => 0,
33
+ :standard_deviation => 0,
34
+ :sum => 1,
35
+ :variance => 0.0
36
+ }
37
+
38
+ build_stats_spec [1,2], {
39
+ :mean => 1.5,
40
+ :median => 1.5,
41
+ :mode => nil,
42
+ :range => 1,
43
+ :standard_deviation => 0.707106781186548,
44
+ :sum => 3,
45
+ :variance => 0.25
46
+ }
47
+
48
+ build_stats_spec [1,2,3], {
49
+ :mean => 2,
50
+ :median => 2,
51
+ :mode => nil,
52
+ :range => 2,
53
+ :standard_deviation => 1,
54
+ :sum => 6,
55
+ :variance => 2.0 / 3.0
56
+ }
57
+
58
+ build_stats_spec [1,2,3,4], {
59
+ :mean => 2.5,
60
+ :median => 2.5,
61
+ :mode => nil,
62
+ :range => 3,
63
+ :standard_deviation => 1.29099444873581,
64
+ :sum => 10,
65
+ :variance => 1.25
66
+ }
67
+
68
+
69
+ build_stats_spec [1,2,2,4], {
70
+ :mean => 2.25,
71
+ :median => 2,
72
+ :mode => 2,
73
+ :range => 3,
74
+ :standard_deviation => 1.2583057392117916,
75
+ :sum => 9,
76
+ :variance => 1.1875
77
+ }
78
+
79
+
80
+ build_stats_spec [1,3,3,4], {
81
+ :mean => 2.75,
82
+ :median => 3,
83
+ :mode => 3,
84
+ :range => 3,
85
+ :standard_deviation => 1.2583057392117916,
86
+ :sum => 11,
87
+ :variance => 1.1875
88
+ }
89
+
90
+ build_stats_spec (0..100).to_a, {
91
+ :mean => 50,
92
+ :median => 50,
93
+ :mode => nil,
94
+ :range => 100,
95
+ :standard_deviation => 29.300170647967224,
96
+ :sum => 5050,
97
+ :variance => 850.0
98
+ }
99
+
100
+ build_stats_spec (1..100).to_a, {
101
+ :mean => 50.5,
102
+ :median => 50.5,
103
+ :mode => nil,
104
+ :range => 99,
105
+ :standard_deviation => 29.011491975882016,
106
+ :sum => 5050,
107
+ :variance => 833.25
108
+ }
109
+
110
+ build_stats_spec [-1,0,1], {
111
+ :mean => 0,
112
+ :median => 0,
113
+ :mode => nil,
114
+ :range => 2,
115
+ :standard_deviation => 1,
116
+ :sum => 0,
117
+ :variance => 0.6666666666666666
118
+ }
119
+
120
+ build_stats_spec [1,2.5], {
121
+ :mean => 1.75,
122
+ :median => 1.75,
123
+ :mode => nil,
124
+ :range => 1.5,
125
+ :standard_deviation => 1.0606601717798212,
126
+ :sum => 3.5,
127
+ :variance =>0.5625
128
+ }
73
129
  end
74
130
 
data/spec/spec_helper.rb CHANGED
@@ -1,5 +1,27 @@
1
- require 'simplecov'
2
- SimpleCov.start
1
+ puts RUBY_DESCRIPTION
2
+
3
+ if RUBY_VERSION.to_f >= 1.9
4
+ require 'simplecov'
5
+ SimpleCov.start
6
+ end
3
7
 
4
8
  require 'easystats'
5
9
 
10
+ def build_stats_spec(array, expectations)
11
+ expectations.each do |method, expectation|
12
+ describe "##{method}" do
13
+ context "when #{array}" do
14
+ if expectation && method == :standard_deviation
15
+ it "should be within #{DELTA} of #{expectation.inspect}" do
16
+ array.send(method).should be_within(DELTA).of(expectation)
17
+ end
18
+ else
19
+ it "should be #{expectation.inspect}" do
20
+ array.send(method).should == expectation
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
27
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: easystats
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.3.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,11 +10,11 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2012-04-12 00:00:00.000000000 Z
13
+ date: 2012-04-17 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: guard-rspec
17
- requirement: &70327162346300 !ruby/object:Gem::Requirement
17
+ requirement: &70193074432080 !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ! '>='
@@ -22,10 +22,10 @@ dependencies:
22
22
  version: '0'
23
23
  type: :development
24
24
  prerelease: false
25
- version_requirements: *70327162346300
25
+ version_requirements: *70193074432080
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: rake
28
- requirement: &70327162345840 !ruby/object:Gem::Requirement
28
+ requirement: &70193074431400 !ruby/object:Gem::Requirement
29
29
  none: false
30
30
  requirements:
31
31
  - - ! '>='
@@ -33,10 +33,10 @@ dependencies:
33
33
  version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
- version_requirements: *70327162345840
36
+ version_requirements: *70193074431400
37
37
  - !ruby/object:Gem::Dependency
38
38
  name: rspec
39
- requirement: &70327162345260 !ruby/object:Gem::Requirement
39
+ requirement: &70193074430520 !ruby/object:Gem::Requirement
40
40
  none: false
41
41
  requirements:
42
42
  - - ! '>='
@@ -44,10 +44,10 @@ dependencies:
44
44
  version: '0'
45
45
  type: :development
46
46
  prerelease: false
47
- version_requirements: *70327162345260
47
+ version_requirements: *70193074430520
48
48
  - !ruby/object:Gem::Dependency
49
49
  name: simplecov
50
- requirement: &70327162344620 !ruby/object:Gem::Requirement
50
+ requirement: &70193074429080 !ruby/object:Gem::Requirement
51
51
  none: false
52
52
  requirements:
53
53
  - - ! '>='
@@ -55,7 +55,7 @@ dependencies:
55
55
  version: '0'
56
56
  type: :development
57
57
  prerelease: false
58
- version_requirements: *70327162344620
58
+ version_requirements: *70193074429080
59
59
  description: This gem contains statistics functions that are very easy to use. Much
60
60
  easier and much more complete than many of the other statistical gems available
61
61
  out there. If you need a feature added, send me a message on Github!