utilities 0.0.23 → 1.0.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.
@@ -28,7 +28,7 @@ module Utilities
28
28
  module Statistics
29
29
  # Add each object of the array to each other in order to get the sum, as long as all objects respond to + operator
30
30
  def sum
31
- flatten.compact.inject( :+ )
31
+ empty? ? 0 : flatten.compact.inject( :+ )
32
32
  end
33
33
 
34
34
  # Calculate squares of each item
@@ -51,7 +51,7 @@ module Utilities
51
51
  # Calculate the mean of the array, as long as all objects respond to / operator
52
52
  def mean
53
53
  a = flatten.compact.to_stat
54
- (a.size > 0) ? a.sum.to_f / a.size : 0.0
54
+ (a.size > 0) ? a.sum.to_f / a.size : nil
55
55
  end
56
56
  alias_method :average, :mean
57
57
 
@@ -62,6 +62,7 @@ module Utilities
62
62
 
63
63
  # Return the variance of self
64
64
  def variance( population = false )
65
+ return nil if empty?
65
66
  m = mean.to_f
66
67
  collect{|v| (v - m).square }.to_stats.sum / (size - (population ? 0 : 1))
67
68
  end
@@ -70,6 +71,7 @@ module Utilities
70
71
  # If population is set to true, then we consider the dataset as the complete population
71
72
  # Else, we consider the dataset as a sample, so we use the sample standard deviation (size - 1)
72
73
  def standard_deviation( population = false )
74
+ return nil if empty?
73
75
  size > 1 ? Math.sqrt( variance( population ) ) : 0.0
74
76
  end
75
77
  alias_method :std_dev, :standard_deviation
@@ -1,7 +1,7 @@
1
1
  module Utilities
2
- MAJOR = 0
2
+ MAJOR = 1
3
3
  MINOR = 0
4
- BUILD = 23
4
+ BUILD = 0
5
5
 
6
6
  VERSION = "#{MAJOR}.#{MINOR}.#{BUILD}"
7
7
  end
@@ -173,8 +173,109 @@ describe String do
173
173
  end
174
174
 
175
175
  describe Utilities::Statistics do
176
+ before(:all) do
177
+ class Array
178
+ include Utilities::Statistics
179
+ end
180
+ end
181
+
182
+ it "#first should returns the first element" do
183
+ [].first.should == nil
184
+ [4,3,5,1,2].first.should == 4
185
+ end
186
+
187
+ it "#last should returns the last element" do
188
+ [].last.should == nil
189
+ [4,3,5,1,2].last.should == 2
190
+ end
191
+
192
+ it "#size should returns the last element" do
193
+ [].size.should == 0
194
+ [4,3,5,1,2].size.should == 5
195
+ end
196
+
197
+ it "#sum should returns the last element" do
198
+ [].sum.should == 0
199
+ [4,3,5,1,2].sum.should == 15
200
+ end
201
+
202
+ it "#squares should returns the square of all elements" do
203
+ [].squares.should == []
204
+ [1,2,3,4,5].squares.should == [1, 4, 9, 16, 25]
205
+ end
206
+
207
+ it "#sqrts should returns the square root of all elements" do
208
+ [].sqrts.should == []
209
+ [1,2,3,4,5].sqrts.should == [1.0, 1.4142135623730951, 1.7320508075688772, 2.0, 2.23606797749979]
210
+ end
211
+
212
+ it "#min should return the smallest element" do
213
+ [].min.should == nil
214
+ [1,2,3,4,5].min.should == 1
215
+ end
216
+
217
+ it "#max should returns the biggest element" do
218
+ [].max.should == nil
219
+ [1,2,3,4,5].max.should == 5
220
+ end
221
+
222
+ it "#mean should returns the mean of all elements" do
223
+ [].mean.should == nil
224
+ [1,2,3,4,5].mean.should == 3
225
+ end
226
+
227
+ it "#frequences should returns the mean of all elements" do
228
+ [].frequences.should == {}
229
+ [1,2,3,4,5].frequences.should == {1=>1, 2=>1, 3=>1, 4=>1, 5=>1}
230
+ end
231
+
232
+ it "#variance should returns the mean of all elements" do
233
+ [].variance.should == nil
234
+ [1,2,3,4,5].variance.should == 2.5
235
+ end
236
+
237
+ it "#standard_deviation should returns the mean of all elements" do
238
+ [].standard_deviation.should == nil
239
+ [4387].standard_deviation.should == 0
240
+ [1,2,3,4,5].standard_deviation.should == 1.5811388300841898
241
+ end
242
+
243
+ it "#median should returns the median of all elements" do
244
+ [].median.should == nil
245
+ [1,2,3,4,5].median.should == 3
246
+ end
247
+
248
+ it "#modes should returns all modes" do
249
+ [].modes.should == {}
250
+ [1,2,3,4,5].modes.should == {1=>1, 2=>1, 3=>1, 4=>1, 5=>1}
251
+ end
252
+
176
253
  it "#statistics should returns a hash with all statistics included" do
177
- [1,2,3,4,5].to_stats.statistics.should == {
254
+ [].statistics.should == {
255
+ :first=>nil,
256
+ :last=>nil,
257
+ :size=>0,
258
+ :sum=>0,
259
+ :squares=>[],
260
+ :sqrts=>[],
261
+ :min=>nil,
262
+ :max=>nil,
263
+ :mean=>nil,
264
+ :frequences=>{},
265
+ :variance=>nil,
266
+ :standard_deviation=>nil,
267
+ :population_variance=>nil,
268
+ :population_standard_deviation=>nil,
269
+ :modes=>{},
270
+ :ranks=>[],
271
+ :median=>nil,
272
+ :midrange=>nil,
273
+ :statistical_range=>nil,
274
+ :quartiles=>[nil, nil, nil],
275
+ :interquartile_range=>nil
276
+ }
277
+
278
+ [1,2,3,4,5].statistics.should == {
178
279
  :first=>1,
179
280
  :last=>5,
180
281
  :size=>5,
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: utilities
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.23
4
+ version: 1.0.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -11,7 +11,7 @@ authors:
11
11
  autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
- date: 2012-03-01 00:00:00.000000000 Z
14
+ date: 2012-09-22 00:00:00.000000000 Z
15
15
  dependencies: []
16
16
  description: Few utilities include in all my projects, including a module for statistics,
17
17
  some to_date and to_time functions as well as intersection method for Range object.
@@ -60,7 +60,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
60
60
  version: '0'
61
61
  requirements: []
62
62
  rubyforge_project:
63
- rubygems_version: 1.8.15
63
+ rubygems_version: 1.8.24
64
64
  signing_key:
65
65
  specification_version: 3
66
66
  summary: Few utilities include in all my projects