Class: LEWT::SimpleRegression
- Defined in:
- lib/extensions/metastat/metamath.rb
Overview
This class performas a simple regression on an x/y dataset
Instance Method Summary collapse
-
#initialize(xs, ys) ⇒ SimpleRegression
constructor
A new instance of SimpleRegression.
- #slope ⇒ Object
- #y_intercept ⇒ Object
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
#slope ⇒ Object
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_intercept ⇒ Object
87 88 89 |
# File 'lib/extensions/metastat/metamath.rb', line 87 def y_intercept mean(@ys) - (slope * mean(@xs)) end |