module Nuggets::Array::MeanMixin
Public Instance Methods
Calculates the arithmetic mean of the values in array.
An optional block may be passed to provide a weight for each value. Defaults to 1. Returns nil
if the sum of all weights is zero (this includes array being empty).
# File lib/nuggets/array/mean_mixin.rb 70 def arithmetic_mean(&block) 71 generalized_mean(1, &block) 72 end
Calculates the generalized mean of the values in array for exponent
. Returns the geometric_mean
if exponent
is zero.
An optional block may be passed to provide a weight for each value. Defaults to 1. Returns nil
if the sum of all weights is zero (this includes array being empty).
# File lib/nuggets/array/mean_mixin.rb 42 def generalized_mean(exponent, &block) 43 return geometric_mean(&block) if exponent.zero? 44 45 total, weights = 0, 0 46 47 each { |x| 48 weight = block ? block[x] : 1 49 50 total += weight * x ** exponent 51 weights += weight 52 } 53 54 (total / weights.to_f) ** (1.0 / exponent) unless weights.zero? 55 end
Calculates the geometric mean of the values in array.
An optional block may be passed to provide a weight for each value. Defaults to 1. Returns nil
if the sum of all weights is zero (this includes array being empty).
# File lib/nuggets/array/mean_mixin.rb 121 def geometric_mean 122 total, weights = 1, 0 123 124 each { |x| 125 weight = block_given? ? yield(x) : 1 126 127 total *= x ** weight 128 weights += weight 129 } 130 131 total ** (1 / weights.to_f) unless weights.zero? 132 end
Calculates the harmonic mean of the values in array.
An optional block may be passed to provide a weight for each value. Defaults to 1. Returns nil
if the sum of all weights is zero (this includes array being empty).
# File lib/nuggets/array/mean_mixin.rb 105 def harmonic_mean(&block) 106 generalized_mean(-1, &block) 107 end
Expects array to be an array of arrays (“rows”) of numeric values; the first “row” may consist of strings (labels) instead. Returns an array of strings with the mean (according to method
, default mean
) of each “column”, prepended with the label, if present, and appended with the standard deviation, if available; all values are subject to precision
.
If array is a flat array of numeric values, it is treated as a single “column”.
Returns nil
if array is empty.
Examples (with standard deviation):
[[9.4, 34.75], [9.46, 34.68], [9.51, 34.61]].report_mean #=> ["9.4567 +/- 0.0450", "34.6800 +/- 0.0572"] [[9.4, 34.75], [9.46, 34.68], [9.51, 34.61]].report_mean(:harmonic) #=> ["9.4565 +/- 0.0450", "34.6799 +/- 0.0572"] [["a", "b"], [9.4, 34.75], [9.46, 34.68], [9.51, 34.61]].report_mean(nil, 2) #=> ["a 9.46 +/- 0.04", "b 34.68 +/- 0.06"] CSV.read('csv', headers: true, converters: :numeric).to_a.report_mean #=> ["a 9.4567 +/- 0.0450", "b 34.6800 +/- 0.0572"] [9.4, 9.46, 9.51].report_mean #=> ["9.4567 +/- 0.0450"] [34.75, 34.68, 34.61].report_mean #=> ["34.6800 +/- 0.0572"] [].report_mean #=> nil
# File lib/nuggets/array/mean_mixin.rb 174 def report_mean(method = nil, precision = 4) 175 return if empty? 176 177 return clone.replace(self.class.new.push(self).transpose). 178 report_mean(method, precision) unless first.is_a?(self.class) 179 180 met, sep = [method ||= :mean, 'mean'], ['', '_'] 181 lab, std = first.first.is_a?(::String), respond_to?(:std) 182 183 fmt = ["%-#{precision}s", "%.#{precision}f", "+/- %.#{precision}f"] 184 185 until respond_to?(method) || sep.empty? 186 method = met.join(sep.shift) 187 end 188 189 transpose.map! { |x| 190 i, a = [], [] 191 192 i << 0 and a << x.shift if lab 193 i << 1 and a << x.send(method) 194 i << 2 and a << x.std if std 195 196 fmt.values_at(*i).join(' ') % a 197 } 198 end
Calculates the root mean square (quadratic mean) of the values in array.
An optional block may be passed to provide a weight for each value. Defaults to 1. Returns nil
if the sum of all weights is zero (this includes array being empty).
# File lib/nuggets/array/mean_mixin.rb 88 def root_mean_square(&block) 89 generalized_mean(2, &block) 90 end