Class: Regression::Linear

Inherits:
Base
  • Object
show all
Defined in:
lib/linear-regression/linear.rb

Instance Method Summary collapse

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

#interceptObject

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

#slopeObject

y = kx + b



19
20
21
# File 'lib/linear-regression/linear.rb', line 19

def slope
  @slope ||= covariance(@xs, @ys) / variance(@xs)   
end

#trendObject



10
11
12
# File 'lib/linear-regression/linear.rb', line 10

def trend
  @xs.map{|x| predict x}
end