utilities 0.0.16 → 0.0.17

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -77,33 +77,58 @@ Usage
77
77
 
78
78
  48.percentage_of(50) #=> 96
79
79
 
80
- ##### Utilities
80
+ #### Utilities
81
81
  * Statistics
82
82
 
83
83
  This module is intended to provide basic statistic functionnalities to numeric arrays. You may either
84
84
  call [].to_stats or extend an existing array with Utilities::Statistics module.
85
85
 
86
- ** sum
86
+ * sum
87
87
 
88
- [1,2,3].sum #=> 6
88
+ [1,2,3].sum #=> 6
89
89
 
90
- ** squares
90
+ * squares
91
91
 
92
- [1,2,3].squares #=> [1,4,9]
92
+ [1,2,3].squares #=> [1,4,9]
93
93
 
94
- ** sqrts
94
+ * sqrts
95
95
 
96
- [9,16,25].sqrts #=> [3,4,5]
96
+ [9,16,25].sqrts #=> [3,4,5]
97
97
 
98
- ** mean
98
+ * mean
99
99
 
100
- [1,2,3,4,5].mean #=> 3
100
+ [1,2,3,4,5].mean #=> 3
101
101
 
102
- ** frequences
102
+ * frequences
103
103
 
104
- [1,1,2,3,3,3,4].frequences #=> {1=>2, 2=>1, 3=>3, 4=>1}
104
+ [1,1,2,3,3,3,4].frequences #=> {1=>2, 2=>1, 3=>3, 4=>1}
105
105
 
106
- ** modes
106
+ * modes
107
107
 
108
- [1,2,3,3,4,4,4,5].modes #=> {4=>3}
108
+ [1,2,3,3,4,4,4,5].modes #=> {4=>3}
109
+
110
+ * statistics
111
+ [1,2,3,4,5].statistics #=> {
112
+ :first=>1,
113
+ :last=>5,
114
+ :size=>5,
115
+ :sum=>15,
116
+ :squares=>[1, 4, 9, 16, 25],
117
+ :sqrts=>[1.0, 1.4142135623730951, 1.7320508075688772, 2.0, 2.23606797749979],
118
+ :min=>1,population sample variance
119
+ :max=>5,
120
+ :mean=>3.0,
121
+ :frequences=>{1=>1, 2=>1, 3=>1, 4=>1, 5=>1},
122
+ :variance=>2.5,
123
+ :standard_deviation=>1.5811388300841898,
124
+ :population_variance=>2.0,
125
+ :population_standard_deviation=>1.4142135623730951,
126
+ :modes=>{1=>1, 2=>1, 3=>1, 4=>1, 5=>1},
127
+ :ranks=>[0.0, 1.0, 2.0, 3.0, 4.0],
128
+ :median=>3,
129
+ :midrange=>3.0,
130
+ :statistical_range=>4,
131
+ :quartiles=>[1.5, 3, 4.5],
132
+ :interquartile_range=>3.0
133
+ }
109
134
 
@@ -15,4 +15,9 @@ class Hash
15
15
  def symbolize_keys
16
16
  collect_keys{ |k| k.to_sym }
17
17
  end
18
+
19
+ # Returns a new hash where all keys have been stringified
20
+ def stringify_keys
21
+ collect_keys{ |k| k.to_s }
22
+ end
18
23
  end
@@ -61,16 +61,16 @@ module Utilities
61
61
  end
62
62
 
63
63
  # Return the variance of self
64
- def variance
65
- m = mean
66
- inject(0) { |v, x| v += (x - m) ** 2 }
64
+ def variance( population = false )
65
+ m = mean.to_f
66
+ collect{|v| (v - mean).square }.to_stats.sum / (size - (population ? 0 : 1))
67
67
  end
68
68
 
69
69
  # Return the (sample|population) standard deviation of self
70
70
  # If population is set to true, then we consider the dataset as the complete population
71
71
  # Else, we consider the dataset as a sample, so we use the sample standard deviation (size - 1)
72
72
  def standard_deviation( population = false )
73
- size > 1 ? Math.sqrt( variance / ( size - ( population ? 0 : 1 ) ) ) : 0.0
73
+ size > 1 ? Math.sqrt( variance( population ) ) : 0.0
74
74
  end
75
75
  alias_method :std_dev, :standard_deviation
76
76
 
@@ -142,12 +142,16 @@ module Utilities
142
142
  :last => self.last,
143
143
  :size => self.size,
144
144
  :sum => self.sum,
145
+ :squares => self.squares,
146
+ :sqrts => self.sqrts,
145
147
  :min => self.min,
146
148
  :max => self.max,
147
149
  :mean => self.mean,
148
150
  :frequences => self.frequences,
149
151
  :variance => self.variance,
150
152
  :standard_deviation => self.standard_deviation,
153
+ :population_variance => self.variance(true),
154
+ :population_standard_deviation => self.standard_deviation(true),
151
155
  :modes => self.modes,
152
156
 
153
157
  # Need to be sorted...
data/lib/version.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  class Utilities
2
2
  MAJOR = 0
3
3
  MINOR = 0
4
- BUILD = 16
4
+ BUILD = 17
5
5
 
6
6
  VERSION = "#{MAJOR}.#{MINOR}.#{BUILD}"
7
7
  end
@@ -48,6 +48,10 @@ describe Hash do
48
48
  it "#symbolize_keys should returns a new hash where all keys have been symbolized" do
49
49
  {"a"=>1, "b"=>2, "c"=>3}.symbolize_keys.should == {:a=>1, :b=>2, :c=>3}
50
50
  end
51
+
52
+ it "#stringify_keys should returns a new hash where all keys have been stringified" do
53
+ {:a=>1, :b=>2, :c=>3}.stringify_keys.should == {"a"=>1, "b"=>2, "c"=>3}
54
+ end
51
55
  end
52
56
 
53
57
  describe Kernel do
@@ -125,3 +129,31 @@ describe Range do
125
129
  (1...5).overlap?(5..10).should == false
126
130
  end
127
131
  end
132
+
133
+ describe Utilities::Statistics do
134
+ it "#statistics should returns a hash with all statistics included" do
135
+ [1,2,3,4,5].to_stats.statistics.should == {
136
+ :first=>1,
137
+ :last=>5,
138
+ :size=>5,
139
+ :sum=>15,
140
+ :squares=>[1, 4, 9, 16, 25],
141
+ :sqrts=>[1.0, 1.4142135623730951, 1.7320508075688772, 2.0, 2.23606797749979],
142
+ :min=>1,population sample variance
143
+ :max=>5,
144
+ :mean=>3.0,
145
+ :frequences=>{1=>1, 2=>1, 3=>1, 4=>1, 5=>1},
146
+ :variance=>2.5,
147
+ :standard_deviation=>1.5811388300841898,
148
+ :population_variance=>2.0,
149
+ :population_standard_deviation=>1.4142135623730951,
150
+ :modes=>{1=>1, 2=>1, 3=>1, 4=>1, 5=>1},
151
+ :ranks=>[0.0, 1.0, 2.0, 3.0, 4.0],
152
+ :median=>3,
153
+ :midrange=>3.0,
154
+ :statistical_range=>4,
155
+ :quartiles=>[1.5, 3, 4.5],
156
+ :interquartile_range=>3.0
157
+ }
158
+ end
159
+ end
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.16
4
+ version: 0.0.17
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: