module Nuggets::Array::CorrelationMixin

Public Class Methods

included(base) click to toggle source
   # File lib/nuggets/array/correlation_mixin.rb
33 def self.included(base)
34   base.send :include, Nuggets::Array::StandardDeviationMixin
35 end

Public Instance Methods

corr()
correlation_coefficient → anArray click to toggle source

Calculates the Pearson product-moment correlation coefficient for 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/correlation_mixin.rb
45 def correlation_coefficient
46   return 0.0 if empty?
47 
48   target = first.respond_to?(:to_ary) ? self :
49     self.class.new(size) { |i| [i + 1, at(i)] }
50 
51   c = target.cov
52 
53   (sx = target.std { |x, _| x }).zero? ||
54   (sy = target.std { |_, y| y }).zero? ?
55     c < 0 ? -1.0 : 1.0 : c / (sx * sy)
56 end
Also aliased as: corr, pmcc
pmcc()