module Nuggets::Array::CorrelationMixin

Public Class Methods

included(base) click to toggle source
# File lib/nuggets/array/correlation_mixin.rb, line 33
def self.included(base)
  base.send :include, Nuggets::Array::StandardDeviationMixin
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, line 45
def correlation_coefficient
  return 0.0 if empty?

  target = first.respond_to?(:to_ary) ? self :
    self.class.new(size) { |i| [i + 1, at(i)] }

  c = target.cov

  (sx = target.std { |x, _| x }).zero? ||
  (sy = target.std { |_, y| y }).zero? ?
    c < 0 ? -1.0 : 1.0 : c / (sx * sy)
end
Also aliased as: corr, pmcc
pmcc()