Class: Regression::Linear
Instance Method Summary collapse
-
#initialize(xs, ys) ⇒ Linear
constructor
A new instance of Linear.
-
#intercept ⇒ Object
y = kx + b.
- #predict(x) ⇒ Object
-
#slope ⇒ Object
y = kx + b.
- #trend ⇒ Object
Methods inherited from Base
#covariance, #covariance2, #mean, #standard_deviation, #variance
Constructor Details
#initialize(xs, ys) ⇒ Linear
Returns a new instance of Linear.
3 4 5 6 7 8 |
# File 'lib/linear-regression/linear.rb', line 3 def initialize(xs, ys) abort "Length xs and ys must be equal" unless xs.length == ys.length @xs = xs @ys = ys end |
Instance Method Details
#intercept ⇒ Object
y = kx + b
24 25 26 |
# File 'lib/linear-regression/linear.rb', line 24 def intercept @intercept ||= mean(@ys) - slope * mean(@xs) end |
#predict(x) ⇒ Object
14 15 16 |
# File 'lib/linear-regression/linear.rb', line 14 def predict(x) y = slope * x + intercept end |
#slope ⇒ Object
y = kx + b
19 20 21 |
# File 'lib/linear-regression/linear.rb', line 19 def slope @slope ||= covariance(@xs, @ys) / variance(@xs) end |
#trend ⇒ Object
10 11 12 |
# File 'lib/linear-regression/linear.rb', line 10 def trend @xs.map{|x| predict x} end |