Sha256: c302da12e9d49a72ea1a9c2ac171ccf05439e54c7a7a7d294875a06615f57339

Contents?: true

Size: 777 Bytes

Versions: 2

Compression:

Stored size: 777 Bytes

Contents

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

    n = x.length

    xy = x.zip(y).map { |a| a[0] * a[1] }

    xx = x.map { |a| a * a }

    sumx = x.sum

    sumy = y.sum

    sumxy = xy.sum

    sumxx = xx.sum

    slope = ((n * sumxy) - (sumx * sumy)) / ((n * sumxx) - (sumx ** 2))

    intercept = (sumy - (slope * sumx)) / n

    { :slope => slope, :intercept => intercept }
  end

  def self.simple_linear_regression_slope(x, y)
    if x.length != y.length
      return nil
    end

    Rstat.simple_linear_regression(x, y)[:slope]
  end

  def self.simple_linear_regression_intercept(x, y)
    if x.length != y.length
      return nil
    end

    Rstat.simple_linear_regression(x, y)[:intercept]
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rstat-0.1.2 lib/rstat/regression_analysis/linear_regression/simple_linear_regression.rb
rstat-0.1.1 lib/rstat/regression_analysis/linear_regression/simple_linear_regression.rb