module Nuggets::Array::VarianceMixin
Public Instance Methods
covariance → aFloat
click to toggle source
Calculates the covariance of the {x,y}
pairs in array. If array only contains values instead of pairs, y
will be the value and x
will be each value's position (rank) in array.
# File lib/nuggets/array/variance_mixin.rb 60 def covariance 61 sx, sy, sp = 0.0, 0.0, 0.0 62 63 return sx if empty? 64 65 target = first.respond_to?(:to_ary) ? self : 66 self.class.new(size) { |i| [i + 1, at(i)] } 67 68 target.each { |x, y| 69 sx += x 70 sy += y 71 sp += x * y 72 } 73 74 (sp - sx * sy / size) / size 75 end
Also aliased as: cov
variance → aFloat
click to toggle source
Calculates the variance of the values in array.
# File lib/nuggets/array/variance_mixin.rb 36 def variance 37 sx, sq = 0.0, 0.0 38 39 return sx if empty? 40 41 each { |x| 42 x = yield x if block_given? 43 44 sx += x 45 sq += x ** 2 46 } 47 48 (sq - sx ** 2 / size) / size 49 end
Also aliased as: var