utilities 0.0.5 → 0.0.6
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/lib/utilities.rb +102 -18
- data/lib/version.rb +1 -1
- metadata +4 -4
data/lib/utilities.rb
CHANGED
@@ -56,6 +56,16 @@ class Numeric
|
|
56
56
|
self * Math::PI / 180
|
57
57
|
end
|
58
58
|
|
59
|
+
# Return the square of self
|
60
|
+
def square
|
61
|
+
self * self
|
62
|
+
end
|
63
|
+
|
64
|
+
# Return the square root of self
|
65
|
+
def sqrt
|
66
|
+
Math.sqrt(self)
|
67
|
+
end
|
68
|
+
|
59
69
|
# Calculate the rank of self based on provided min and max
|
60
70
|
def rank min, max
|
61
71
|
s, min, max = self.to_f, min.to_f, max.to_f
|
@@ -71,6 +81,7 @@ class Numeric
|
|
71
81
|
def percentage_of n, t = 100
|
72
82
|
n == 0 ? 0.0 : self / n.to_f * t
|
73
83
|
end
|
84
|
+
alias_method :percent_of, :percentage_of
|
74
85
|
end
|
75
86
|
|
76
87
|
module Utilities
|
@@ -80,6 +91,16 @@ module Utilities
|
|
80
91
|
inject( :+ )
|
81
92
|
end
|
82
93
|
|
94
|
+
# Calculate squares of each item
|
95
|
+
def squares
|
96
|
+
map{ |i| i**2 }
|
97
|
+
end
|
98
|
+
|
99
|
+
# Calculate square roots of each item
|
100
|
+
def sqrts
|
101
|
+
map{ |i| i.sqrt }
|
102
|
+
end
|
103
|
+
|
83
104
|
# Calculate the mean of the array, as long as all objects respond to / operator
|
84
105
|
def mean
|
85
106
|
(size > 0) ? sum.to_f / size : 0.0
|
@@ -91,24 +112,56 @@ module Utilities
|
|
91
112
|
inject(Hash.new(0)) { |h, v| h[v] += 1; h }
|
92
113
|
end
|
93
114
|
|
94
|
-
# Return the variance of
|
115
|
+
# Return the variance of self
|
95
116
|
def variance
|
96
117
|
m = mean
|
97
118
|
inject(0) { |v, x| v += (x - m) ** 2 }
|
98
119
|
end
|
99
120
|
|
100
|
-
# Return the standard deviation of
|
101
|
-
|
102
|
-
|
121
|
+
# Return the (sample|population) standard deviation of self
|
122
|
+
# If population is set to true, then we consider the dataset as the complete population
|
123
|
+
# Else, we consider the dataset as a sample, so we use the sample standard deviation (size - 1)
|
124
|
+
def standard_deviation( population = false )
|
125
|
+
size > 1 ? Math.sqrt( variance / ( size - ( population ? 0 : 1 ) ) ) : 0.0
|
103
126
|
end
|
104
127
|
alias_method :std_dev, :standard_deviation
|
105
128
|
|
106
|
-
# Return the median of
|
107
|
-
def median(already_sorted=false)
|
129
|
+
# Return the median of sorted self
|
130
|
+
def median( already_sorted = false )
|
108
131
|
return nil if empty?
|
109
|
-
a =
|
132
|
+
a = sort_and_extend( already_sorted )
|
110
133
|
m_pos = size / 2
|
111
|
-
size % 2 == 1 ? a[m_pos] : a[m_pos-1
|
134
|
+
size % 2 == 1 ? a[m_pos] : (a[m_pos-1] + a[m_pos]).to_f / 2
|
135
|
+
end
|
136
|
+
alias_method :second_quartile, :median
|
137
|
+
|
138
|
+
# Return the first quartile of self
|
139
|
+
def first_quartile( already_sorted = false )
|
140
|
+
return nil if empty?
|
141
|
+
a = already_sorted ? self : sort
|
142
|
+
a[0..((size / 2) - 1)].extend(Utilities::Statistics).median( true )
|
143
|
+
end
|
144
|
+
alias_method :lower_quartile, :first_quartile
|
145
|
+
|
146
|
+
# Return the last quartile of self
|
147
|
+
def last_quartile( already_sorted = false )
|
148
|
+
return nil if empty?
|
149
|
+
a = already_sorted ? self : sort
|
150
|
+
a[((size / 2) + 1)..-1].extend(Utilities::Statistics).median( true )
|
151
|
+
end
|
152
|
+
alias_method :upper_quartile, :last_quartile
|
153
|
+
|
154
|
+
# Return an array containing the first, the second and the last quartile of self
|
155
|
+
def quartiles( already_sorted = false )
|
156
|
+
a = sort_and_extend( already_sorted )
|
157
|
+
[a.first_quartile( true ), a.median( true ), a.last_quartile( true )]
|
158
|
+
end
|
159
|
+
|
160
|
+
# Calculate the interquartile range of self
|
161
|
+
def interquartile_range( already_sorted = false )
|
162
|
+
return nil if empty?
|
163
|
+
a = sort_and_extend( already_sorted )
|
164
|
+
a.last_quartile - a.first_quartile
|
112
165
|
end
|
113
166
|
|
114
167
|
# Return an array of modes with their corresponding occurences
|
@@ -118,19 +171,50 @@ module Utilities
|
|
118
171
|
freq.select { |k, f| f == max }
|
119
172
|
end
|
120
173
|
|
121
|
-
# Return
|
122
|
-
def
|
174
|
+
# Return the midrange of sorted self
|
175
|
+
def midrange( already_sorted = false )
|
176
|
+
return nil if empty?
|
177
|
+
a = sort_and_extend( already_sorted )
|
178
|
+
(a.first + a.last) / 2.0
|
179
|
+
end
|
180
|
+
|
181
|
+
# Return the statistical range of sorted self
|
182
|
+
def statistical_range( already_sorted = false )
|
183
|
+
return nil if empty?
|
184
|
+
a = sort_and_extend( already_sorted )
|
185
|
+
(a.last - a.first)
|
186
|
+
end
|
187
|
+
|
188
|
+
# Return all statistics from self in a simple hash
|
189
|
+
def statistics( already_sorted = false )
|
190
|
+
sorted = sort_and_extend( already_sorted )
|
191
|
+
|
123
192
|
{
|
124
|
-
:
|
125
|
-
:
|
126
|
-
:
|
127
|
-
:
|
128
|
-
:
|
129
|
-
:
|
130
|
-
:
|
131
|
-
:
|
193
|
+
:first => self.first,
|
194
|
+
:last => self.last,
|
195
|
+
:size => self.size,
|
196
|
+
:sum => self.sum,
|
197
|
+
:min => self.min,
|
198
|
+
:max => self.max,
|
199
|
+
:mean => self.mean,
|
200
|
+
:frequences => self.frequences,
|
201
|
+
:variance => self.variance,
|
202
|
+
:standard_deviation => self.standard_deviation,
|
203
|
+
:modes => self.modes,
|
204
|
+
|
205
|
+
# Need to be sorted...
|
206
|
+
:median => sorted.median( true ),
|
207
|
+
:midrange => sorted.midrange( true ),
|
208
|
+
:statistical_range => sorted.statistical_range( true ),
|
209
|
+
:quartiles => sorted.quartiles( true ),
|
210
|
+
:interquartile_range => sorted.interquartile_range( true )
|
132
211
|
}
|
133
212
|
end
|
134
213
|
alias_method :stats, :statistics
|
214
|
+
|
215
|
+
protected
|
216
|
+
def sort_and_extend( already_sorted = false )
|
217
|
+
already_sorted ? self : sort.extend(Utilities::Statistics)
|
218
|
+
end
|
135
219
|
end
|
136
220
|
end
|
data/lib/version.rb
CHANGED
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: utilities
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 19
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 6
|
10
|
+
version: 0.0.6
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Christian Blais
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-02-
|
18
|
+
date: 2011-02-18 00:00:00 -05:00
|
19
19
|
default_executable:
|
20
20
|
dependencies: []
|
21
21
|
|