easystats 0.3.1 → 0.3.2
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/Guardfile +1 -1
- data/lib/easystats/version.rb +1 -1
- data/lib/easystats.rb +14 -14
- data/spec/lib/easystats_spec.rb +125 -69
- data/spec/spec_helper.rb +24 -2
- metadata +10 -10
data/Guardfile
CHANGED
data/lib/easystats/version.rb
CHANGED
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
|
4
|
+
return if self.empty?
|
5
5
|
|
6
6
|
self.sum / self.count.to_f
|
7
|
-
end unless
|
7
|
+
end unless method_defined? :mean
|
8
8
|
|
9
|
-
alias_method :average, :mean unless
|
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
|
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
|
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
|
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
|
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
|
99
|
+
return if self.empty?
|
100
100
|
|
101
101
|
data = self.sort
|
102
102
|
data.last - data.first
|
103
|
-
end unless
|
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
|
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
|
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
|
116
|
+
end unless method_defined? :sum
|
117
117
|
|
118
118
|
def variance
|
119
|
-
return
|
119
|
+
return if self.empty?
|
120
120
|
|
121
121
|
self.sum_of_deviations_squared / self.count.to_f
|
122
|
-
end unless
|
122
|
+
end unless method_defined? :variance
|
123
123
|
|
124
124
|
protected
|
125
125
|
|
data/spec/lib/easystats_spec.rb
CHANGED
@@ -1,74 +1,130 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Array do
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
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
|
-
|
2
|
-
|
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.
|
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-
|
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: &
|
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: *
|
25
|
+
version_requirements: *70193074432080
|
26
26
|
- !ruby/object:Gem::Dependency
|
27
27
|
name: rake
|
28
|
-
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: *
|
36
|
+
version_requirements: *70193074431400
|
37
37
|
- !ruby/object:Gem::Dependency
|
38
38
|
name: rspec
|
39
|
-
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: *
|
47
|
+
version_requirements: *70193074430520
|
48
48
|
- !ruby/object:Gem::Dependency
|
49
49
|
name: simplecov
|
50
|
-
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: *
|
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!
|