Sha256: 7ce589dc0365604cc1d85ed80af984a3bcd54baa0162b52ac3d7f25e375d3679

Contents?: true

Size: 460 Bytes

Versions: 2

Compression:

Stored size: 460 Bytes

Contents

module Rstat
  def self.sample_correlation_coefficient(x, y)
    if x.length != y.length
      return nil
    end

    xm = x.mean
    ym = y.mean

    top = x.zip(y).map { |a, b| (a - xm) * (b - ym) }.inject(:+)

    bottom = Math.sqrt(x.map { |a| (a - xm) ** 2 }.inject(:+)) * Math.sqrt(y.map { |b| (b - ym) ** 2 }.inject(:+))

    top / bottom
  end

  def self.pearsons_correlation_coefficient(x, y)
    Rstat.sample_correlation_coefficient x, y
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rstat-0.1.2 lib/rstat/regression_analysis/sample_correlation_coefficient.rb
rstat-0.1.1 lib/rstat/regression_analysis/sample_correlation_coefficient.rb