rstat 0.0.4 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (24) hide show
  1. data/CHANGELOG.md +9 -0
  2. data/Gemfile +1 -1
  3. data/lib/rstat.rb +0 -1
  4. data/lib/rstat/core_ext/array/descriptive_statistics.rb +3 -0
  5. data/lib/rstat/core_ext/array/descriptive_statistics/dispersion.rb +3 -0
  6. data/lib/rstat/core_ext/array/{percentile.rb → descriptive_statistics/dispersion/percentile.rb} +1 -1
  7. data/lib/rstat/core_ext/array/{range.rb → descriptive_statistics/dispersion/range.rb} +0 -0
  8. data/lib/rstat/core_ext/array/{standard_deviation.rb → descriptive_statistics/dispersion/standard_deviation.rb} +0 -6
  9. data/lib/rstat/core_ext/array/descriptive_statistics/index_of_dispersion.rb +25 -0
  10. data/lib/rstat/core_ext/array/descriptive_statistics/location.rb +3 -0
  11. data/lib/rstat/core_ext/array/{mean.rb → descriptive_statistics/location/mean.rb} +2 -2
  12. data/lib/rstat/core_ext/array/{median.rb → descriptive_statistics/location/median.rb} +1 -1
  13. data/lib/rstat/core_ext/array/{mode.rb → descriptive_statistics/location/mode.rb} +0 -0
  14. data/lib/rstat/core_ext/array/descriptive_statistics/shape.rb +3 -0
  15. data/lib/rstat/core_ext/array/descriptive_statistics/shape/kurtosis.rb +5 -0
  16. data/lib/rstat/core_ext/array/descriptive_statistics/shape/moment.rb +23 -0
  17. data/lib/rstat/core_ext/array/descriptive_statistics/shape/skewness.rb +5 -0
  18. data/lib/rstat/core_ext/array/descriptive_statistics/shape/variance.rb +5 -0
  19. data/lib/rstat/core_ext/fixnum.rb +3 -0
  20. data/lib/rstat/core_ext/fixnum/binomial_coefficient.rb +5 -0
  21. data/lib/rstat/version.rb +1 -1
  22. data/spec/rstat/array_spec.rb +143 -67
  23. data/spec/rstat/fixnum_spec.rb +12 -0
  24. metadata +21 -8
@@ -1,3 +1,12 @@
1
+ ## v0.1.0
2
+
3
+ * Major reorganization! All files now in directories named after their corresponding discipline (as sort of defined by Wikipedia).
4
+ * Added skewness.
5
+ * Mean methods now now use the sum method.
6
+ * Minor reorganization of tests.
7
+ * Changed Gemfile source to https.
8
+ * Added index of dispersion.
9
+
1
10
  ## v0.0.4
2
11
 
3
12
  * Added percentile.
data/Gemfile CHANGED
@@ -1,4 +1,4 @@
1
- source "http://rubygems.org"
1
+ source "https://rubygems.org"
2
2
 
3
3
  # Specify your gem's dependencies in rstat.gemspec
4
4
  gemspec
@@ -2,5 +2,4 @@ require "rstat/version"
2
2
  require "rstat/core_ext"
3
3
 
4
4
  module Rstat
5
- # Your code goes here...
6
5
  end
@@ -0,0 +1,3 @@
1
+ Dir["#{File.dirname(__FILE__)}/descriptive_statistics/*.rb"].sort.each do |path|
2
+ require "rstat/core_ext/array/descriptive_statistics/#{File.basename(path, ".rb")}"
3
+ end
@@ -0,0 +1,3 @@
1
+ Dir["#{File.dirname(__FILE__)}/dispersion/*.rb"].sort.each do |path|
2
+ require "rstat/core_ext/array/descriptive_statistics/dispersion/#{File.basename(path, ".rb")}"
3
+ end
@@ -3,7 +3,7 @@ class Array
3
3
  if p < 0 || p > 100
4
4
  nil
5
5
  else
6
- self.sort[((p.to_f/100.0) * self.length.to_f) - 0.5]
6
+ self.sort[((p.to_f / 100.0) * self.length.to_f) - 0.5]
7
7
  end
8
8
  end
9
9
 
@@ -1,10 +1,4 @@
1
1
  class Array
2
- def variance
3
- mean = self.mean
4
-
5
- (1.0 / self.length) * self.map{|x| (x - mean) ** 2}.inject(:+)
6
- end
7
-
8
2
  def standard_deviation
9
3
  Math.sqrt(self.variance)
10
4
  end
@@ -0,0 +1,25 @@
1
+ class Array
2
+ def index_of_dispersion
3
+ self.variance / self.mean
4
+ end
5
+
6
+ def dispersion_index
7
+ self.index_of_dispersion
8
+ end
9
+
10
+ def coefficient_of_dispersion
11
+ self.index_of_dispersion
12
+ end
13
+
14
+ def variance_to_mean_ratio
15
+ self.index_of_dispersion
16
+ end
17
+
18
+ def vmr
19
+ self.index_of_dispersion
20
+ end
21
+
22
+ def fano_factor
23
+ self.index_of_dispersion
24
+ end
25
+ end
@@ -0,0 +1,3 @@
1
+ Dir["#{File.dirname(__FILE__)}/location/*.rb"].sort.each do |path|
2
+ require "rstat/core_ext/array/descriptive_statistics/location/#{File.basename(path, ".rb")}"
3
+ end
@@ -12,7 +12,7 @@ class Array
12
12
  end
13
13
 
14
14
  def harmonic_mean
15
- self.length / self.map{ |x| 1.0 / x }.inject{ |sum, x| sum + x }.to_f
15
+ self.length / self.map{ |x| 1.0 / x }.sum.to_f
16
16
  end
17
17
 
18
18
  def quadratic_mean
@@ -20,6 +20,6 @@ class Array
20
20
  end
21
21
 
22
22
  def power_mean(p = 1)
23
- ((1.0 / self.length) * self.map{ |x| x ** p }.inject{ |sum, x| sum + x }.to_f) ** (1.0 / p)
23
+ ((1.0 / self.length) * self.map{ |x| x ** p }.sum.to_f) ** (1.0 / p)
24
24
  end
25
25
  end
@@ -1,6 +1,6 @@
1
1
  class Array
2
2
  def median
3
- unless self.length == 0
3
+ if self.length != 0
4
4
  copy = self.sort
5
5
 
6
6
  if copy.length % 2 == 0
@@ -0,0 +1,3 @@
1
+ Dir["#{File.dirname(__FILE__)}/shape/*.rb"].sort.each do |path|
2
+ require "rstat/core_ext/array/descriptive_statistics/shape/#{File.basename(path, ".rb")}"
3
+ end
@@ -0,0 +1,5 @@
1
+ class Array
2
+ def kurtosis
3
+ (self.central_moment(4) / self.variance ** 2) - 3
4
+ end
5
+ end
@@ -0,0 +1,23 @@
1
+ class Array
2
+ def central_moment k
3
+ if k < 0
4
+ nil
5
+ else
6
+ mean = self.mean
7
+
8
+ (1.0 / self.length) * self.map{ |x| (x - mean) ** k }.sum
9
+ end
10
+ end
11
+
12
+ def raw_moment k
13
+ if k < 0
14
+ nil
15
+ else
16
+ (1.0 / self.length) * self.map{ |x| x ** k }.sum
17
+ end
18
+ end
19
+
20
+ # def l_moment k
21
+ #
22
+ # end
23
+ end
@@ -0,0 +1,5 @@
1
+ class Array
2
+ def skewness
3
+ self.central_moment(3) / (self.variance ** 1.5)
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ class Array
2
+ def variance
3
+ self.central_moment 2
4
+ end
5
+ end
@@ -0,0 +1,3 @@
1
+ Dir["#{File.dirname(__FILE__)}/fixnum/*.rb"].sort.each do |path|
2
+ require "rstat/core_ext/fixnum/#{File.basename(path, ".rb")}"
3
+ end
@@ -0,0 +1,5 @@
1
+ class Fixnum
2
+ def binomial_coefficient bottom
3
+ (self - bottom + 1 .. self).inject(1, &:*) / (2 .. bottom).inject(1, &:*)
4
+ end
5
+ end
@@ -1,3 +1,3 @@
1
1
  module Rstat
2
- VERSION = "0.0.4"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -14,114 +14,190 @@ describe Rstat do
14
14
  end
15
15
  end
16
16
 
17
- describe ".range" do
18
- it "finds the range of an array" do
19
- [0, 34, 656, 400, 1000].range.should be(1000)
17
+ context "Location" do
18
+ describe ".mean" do
19
+ it "calculates the mean of an array" do
20
+ [1, 2, 3, 4, 5].mean.should be_within(0.0001).of(3.0000)
21
+ end
20
22
  end
21
- end
22
23
 
23
- describe ".mean" do
24
- it "calculates the mean of an array" do
25
- [1, 2, 3, 4, 5].mean.should be_within(0.0001).of(3.0000)
24
+ describe ".geometric_mean" do
25
+ it "calculates the geometric mean of an array" do
26
+ [1, 2, 3, 4, 5].geometric_mean.should be_within(0.0001).of(2.6052)
27
+ end
28
+
29
+ it "calculates the geometric mean of an array with a zero element" do
30
+ [0, 2, 3, 4, 5].geometric_mean.should eql(0.0)
26
31
  end
27
- end
28
32
 
29
- describe ".geometric_mean" do
30
- it "calculates the geometric mean of an array" do
31
- [1, 2, 3, 4, 5].geometric_mean.should be_within(0.0001).of(2.6052)
33
+ it "calculates the geometric mean of an array with a negative element" do
34
+ [-1, 2, 3, 4, 5].geometric_mean.real?.should be_false
35
+ end
32
36
  end
33
37
 
34
- it "calculates the geometric mean of an array with a zero element" do
35
- [0, 2, 3, 4, 5].geometric_mean.should eql(0.0)
38
+ describe ".harmonic mean" do
39
+ it "calculates the harmonic mean of an array" do
40
+ [1, 2, 3, 4, 5].harmonic_mean.should be_within(0.0001).of(2.1897)
41
+ end
36
42
  end
37
43
 
38
- it "calculates the geometric mean of an array with a negative element" do
39
- [-1, 2, 3, 4, 5].geometric_mean.real?.should be_false
44
+ describe ".power_mean" do
45
+ it "power_mean(2) should equal quadratic_mean" do
46
+ p = [1, 2, 3, 4, 5].power_mean(2)
47
+ q = [1, 2, 3, 4, 5].quadratic_mean
48
+
49
+ p.should eql(q)
50
+ end
40
51
  end
41
- end
42
52
 
43
- describe ".harmonic mean" do
44
- it "calculates the harmonic mean of an array" do
45
- [1, 2, 3, 4, 5].harmonic_mean.should be_within(0.0001).of(2.1897)
53
+ it "AM > GM > HM" do
54
+ a = [1, 2, 3, 4, 5]
55
+ am = a.arithmetric_mean
56
+ gm = a.geometric_mean
57
+ hm = a.harmonic_mean
58
+
59
+ am.should be > gm
60
+ gm.should be > hm
46
61
  end
47
- end
48
62
 
49
- describe ".power_mean" do
50
- it "power_mean(2) should equal quadratic_mean" do
51
- p = [1, 2, 3, 4, 5].power_mean(2)
52
- q = [1, 2, 3, 4, 5].quadratic_mean
63
+ describe ".median" do
64
+ it "calculates the median of an array with an odd number of elements" do
65
+ [2, 3, 5, 1, 4].median.should eql(3)
66
+ end
53
67
 
54
- p.should eql(q)
68
+ it "calculates the median of an array with an even number of elements" do
69
+ [2, 3, 5, 1, 4, 6].median.should eql(3.5)
70
+ end
55
71
  end
56
- end
57
72
 
58
- it "AM > GM > HM" do
59
- a = [1, 2, 3, 4, 5]
60
- am = a.arithmetric_mean
61
- gm = a.geometric_mean
62
- hm = a.harmonic_mean
73
+ describe ".mode" do
74
+ it "calculates the mode of an empty array" do
75
+ [].mode.should eql([])
76
+ end
63
77
 
64
- am.should be > gm
65
- gm.should be > hm
66
- end
78
+ it "calculates the mode of an array with one element" do
79
+ [100].mode.should eql([100])
80
+ end
67
81
 
68
- describe ".median" do
69
- it "calculates the median of an array with an odd number of elements" do
70
- [2, 3, 5, 1, 4].median.should eql(3)
71
- end
82
+ it "calculates the mode of an unimodal array" do
83
+ [1, 1, 1, 2, 3, 4, 5, 5, 6].mode.should eql([1])
84
+ end
72
85
 
73
- it "calculates the median of an array with an even number of elements" do
74
- [2, 3, 5, 1, 4, 6].median.should eql(3.5)
86
+ it "calculates the mode of a bimodal array" do
87
+ [44, 45, 46, 44, 46, 50].mode.should eql([44, 46])
88
+ end
75
89
  end
76
90
  end
77
91
 
78
- describe ".mode" do
79
- it "calculates the mode of an empty array" do
80
- [].mode.should eql([])
92
+ context "Dispersion" do
93
+ describe ".range" do
94
+ it "finds the range of an array" do
95
+ [0, 34, 656, 400, 1000].range.should be(1000)
96
+ end
81
97
  end
82
98
 
83
- it "calculates the mode of an array with one element" do
84
- [100].mode.should eql([100])
99
+ describe ".standard_deviation" do
100
+ it "calculates the standard deviation of an array" do
101
+ [1, 2, 3, 4, 5, 6].standard_deviation.should be_within(0.00001).of(1.70783)
102
+ end
85
103
  end
86
104
 
87
- it "calculates the mode of an unimodal array" do
88
- [1, 1, 1, 2, 3, 4, 5, 5, 6].mode.should eql([1])
105
+ describe ".coefficient_of_variation" do
106
+ it "calculates the coefficient of variation of an array" do
107
+ [1, 2, 3, 4, 5, 6].coefficient_of_variation.should be_within(0.00001).of(0.48795)
108
+ end
89
109
  end
90
110
 
91
- it "calculates the mode of a bimodal array" do
92
- [44, 45, 46, 44, 46, 50].mode.should eql([44, 46])
111
+ describe ".percentile" do
112
+ it "finds the 30th percentile of an array" do
113
+ [1, 2, 3, 4, 5, 6, 7, 8, 9, 10].percentile(30).should be(3)
114
+ end
115
+
116
+ it "finds the 50th percentile of an array" do
117
+ [1, 2, 3, 4, 5].percentile(50).should be(3)
118
+ end
119
+
120
+ it "finds the 10th percentile of an array" do
121
+ [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 15, 16, 17].percentile(10).should be(2)
122
+ end
93
123
  end
94
- end
95
124
 
96
- describe ".standard_deviation" do
97
- it "calculates the variance of an array" do
98
- [1, 2, 3, 4, 5, 6].variance.should be_within(0.00001).of(2.91667)
125
+ describe ".interquartile_range" do
126
+ it "finds the interquartile range of an array" do
127
+ [1, 2, 3, 4, 5, 6, 7, 8, 9, 10].interquartile_range.should be(5)
128
+ end
99
129
  end
130
+ end
131
+
132
+ context "Shape" do
133
+ describe ".central_moment" do
134
+ it "calculates the 0th central moment of an array" do
135
+ [2, 43, 5, 65, 56, 7, 85, 3, -1, 199].central_moment(0).should be_within(0.00001).of(1.0)
136
+ end
137
+
138
+ it "calculates the 1st central moment of an array" do
139
+ [2, 43, 5, 65, 56, 7, 85, 3, -1, 199].central_moment(1).should be_within(0.00001).of(0.0)
140
+ end
100
141
 
101
- it "calculates the standard deviation of an array" do
102
- [1, 2, 3, 4, 5, 6].standard_deviation.should be_within(0.00001).of(1.70783)
142
+ it "calculates the 2nd central moment of an array" do
143
+ [2, 43, 5, 65, 56, 7, 85, 3, -1, 199].central_moment(2).should be_within(0.00001).of(3459.44000)
144
+ end
145
+
146
+ it "calculates the 3rd central moment of an array" do
147
+ [2, 43, 5, 65, 56, 7, 85, 3, -1, 199].central_moment(3).should be_within(0.00001).of(321046.00800)
148
+ end
149
+
150
+ it "calculates the 4th central moment of an array" do
151
+ [2, 43, 5, 65, 56, 7, 85, 3, -1, 199].central_moment(4).should be_within(0.00001).of(56245095.69920)
152
+ end
103
153
  end
104
154
 
105
- it "calculates the coefficient of variation of an array" do
106
- [1, 2, 3, 4, 5, 6].coefficient_of_variation.should be_within(0.00001).of(0.48795)
155
+ describe ".raw_moment" do
156
+ it "calculates the 0th central moment of an array" do
157
+ [2, 43, 5, 65, 56, 7, 85, 3, -1, 199].raw_moment(0).should be_within(0.00001).of(1.0)
158
+ end
159
+
160
+ it "calculates the 1st central moment of an array" do
161
+ [2, 43, 5, 65, 56, 7, 85, 3, -1, 199].raw_moment(1).should be_within(0.00001).of([2, 43, 5, 65, 56, 7, 85, 3, -1, 199].mean)
162
+ end
163
+
164
+ it "calculates the 2nd central moment of an array" do
165
+ [2, 43, 5, 65, 56, 7, 85, 3, -1, 199].raw_moment(2).should be_within(0.00001).of(5612.40000)
166
+ end
167
+
168
+ it "calculates the 3rd central moment of an array" do
169
+ [2, 43, 5, 65, 56, 7, 85, 3, -1, 199].raw_moment(3).should be_within(0.00001).of(902497.40000)
170
+ end
171
+
172
+ it "calculates the 4th central moment of an array" do
173
+ [2, 43, 5, 65, 56, 7, 85, 3, -1, 199].raw_moment(4).should be_within(0.00001).of(165154687.20000)
174
+ end
107
175
  end
108
- end
109
176
 
110
- describe ".percentile" do
111
- it "finds the 30th percentile of an array" do
112
- [1, 2, 3, 4, 5, 6, 7, 8, 9, 10].percentile(30).should be(3)
177
+ describe ".variance" do
178
+ it "calculates the variance of an array" do
179
+ [1, 2, 3, 4, 5, 6].variance.should be_within(0.00001).of(2.91667)
180
+ end
113
181
  end
114
182
 
115
- it "finds the 50th percentile of an array" do
116
- [1, 2, 3, 4, 5].percentile(50).should be(3)
183
+ describe ".skewness" do
184
+ it "finds the skewness of an array" do
185
+ [2, 43, 5, 65, 56, 7, 85, 3, -1, 199].skewness.should be_within(0.00001).of(1.57782)
186
+ end
117
187
  end
118
188
 
119
- it "finds the 10th percentile of an array" do
120
- [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 15, 16, 17].percentile(10).should be(2)
189
+ describe ".kurtosis" do
190
+ it "finds the kurtosis of an array" do
191
+ [2, 43, 5, 65, 56, 7, 85, 3, -1, 199].kurtosis.should be_within(0.00001).of(1.69973)
192
+ end
121
193
  end
194
+ end
122
195
 
123
- it "finds the interquartile range of an array" do
124
- [1, 2, 3, 4, 5, 6, 7, 8, 9, 10].interquartile_range.should be(5)
196
+ context "Count Data" do
197
+ describe ".index_of_dispersion" do
198
+ it "finds the index of dispersion of an array" do
199
+ [1, 2, 3, 4, 5, 6].index_of_dispersion.should be_within(0.00001).of(0.83333)
200
+ end
125
201
  end
126
202
  end
127
203
  end
@@ -0,0 +1,12 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe Rstat do
5
+ describe ".binomial_coefficient" do
6
+ it "computes the binomial coeffecient of itself and another number" do
7
+ 10.binomial_coefficient(4).should eq(210)
8
+
9
+ 50.binomial_coefficient(34).should eq(4_923_689_695_575)
10
+ end
11
+ end
12
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rstat
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-05-07 00:00:00.000000000 Z
12
+ date: 2012-05-16 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
@@ -43,17 +43,29 @@ files:
43
43
  - lib/rstat.rb
44
44
  - lib/rstat/core_ext.rb
45
45
  - lib/rstat/core_ext/array.rb
46
- - lib/rstat/core_ext/array/mean.rb
47
- - lib/rstat/core_ext/array/median.rb
48
- - lib/rstat/core_ext/array/mode.rb
49
- - lib/rstat/core_ext/array/percentile.rb
46
+ - lib/rstat/core_ext/array/descriptive_statistics.rb
47
+ - lib/rstat/core_ext/array/descriptive_statistics/dispersion.rb
48
+ - lib/rstat/core_ext/array/descriptive_statistics/dispersion/percentile.rb
49
+ - lib/rstat/core_ext/array/descriptive_statistics/dispersion/range.rb
50
+ - lib/rstat/core_ext/array/descriptive_statistics/dispersion/standard_deviation.rb
51
+ - lib/rstat/core_ext/array/descriptive_statistics/index_of_dispersion.rb
52
+ - lib/rstat/core_ext/array/descriptive_statistics/location.rb
53
+ - lib/rstat/core_ext/array/descriptive_statistics/location/mean.rb
54
+ - lib/rstat/core_ext/array/descriptive_statistics/location/median.rb
55
+ - lib/rstat/core_ext/array/descriptive_statistics/location/mode.rb
56
+ - lib/rstat/core_ext/array/descriptive_statistics/shape.rb
57
+ - lib/rstat/core_ext/array/descriptive_statistics/shape/kurtosis.rb
58
+ - lib/rstat/core_ext/array/descriptive_statistics/shape/moment.rb
59
+ - lib/rstat/core_ext/array/descriptive_statistics/shape/skewness.rb
60
+ - lib/rstat/core_ext/array/descriptive_statistics/shape/variance.rb
50
61
  - lib/rstat/core_ext/array/product.rb
51
- - lib/rstat/core_ext/array/range.rb
52
- - lib/rstat/core_ext/array/standard_deviation.rb
53
62
  - lib/rstat/core_ext/array/sum.rb
63
+ - lib/rstat/core_ext/fixnum.rb
64
+ - lib/rstat/core_ext/fixnum/binomial_coefficient.rb
54
65
  - lib/rstat/version.rb
55
66
  - rstat.gemspec
56
67
  - spec/rstat/array_spec.rb
68
+ - spec/rstat/fixnum_spec.rb
57
69
  - spec/spec_helper.rb
58
70
  homepage: http://seaneshbaugh.com/
59
71
  licenses: []
@@ -81,4 +93,5 @@ specification_version: 3
81
93
  summary: A Simple statistics gem.
82
94
  test_files:
83
95
  - spec/rstat/array_spec.rb
96
+ - spec/rstat/fixnum_spec.rb
84
97
  - spec/spec_helper.rb