Class: LEWT::SimpleRegression

Inherits:
MetaMath
  • Object
show all
Defined in:
lib/extensions/metastat/metamath.rb

Overview

This class performas a simple regression on an x/y dataset

Instance Method Summary collapse

Methods inherited from MetaMath

#descriptive_stats, #mean, #median, #mode

Constructor Details

#initialize(xs, ys) ⇒ SimpleRegression

Returns a new instance of SimpleRegression.



82
83
84
85
# File 'lib/extensions/metastat/metamath.rb', line 82

def initialize (xs, ys)
  raise Exception "_x & _y datasets must be of equal length" if xs.length != ys.length
  @xs, @ys = xs, ys
end

Instance Method Details

#slopeObject



91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/extensions/metastat/metamath.rb', line 91

def slope
  x_mean = mean(@xs)
  y_mean = mean(@ys)
  
  numerator = (0...@xs.length).reduce(0) do |sum, i|
    sum + ((@xs[i] - x_mean) * (@ys[i] - y_mean))
  end
  
  denominator = @xs.reduce(0) do |sum, x|
    sum + ((x - x_mean) ** 2)
  end
  
  (numerator / denominator)
end

#y_interceptObject



87
88
89
# File 'lib/extensions/metastat/metamath.rb', line 87

def y_intercept
  mean(@ys) - (slope * mean(@xs))
end