Skip to content

Instantly share code, notes, and snippets.

@rikkipitt
Forked from rweald/simple-linear-regression.rb
Last active April 20, 2018 18:47
Show Gist options
  • Save rikkipitt/7005285 to your computer and use it in GitHub Desktop.
Save rikkipitt/7005285 to your computer and use it in GitHub Desktop.
Simple Linear Regression in Ruby
class SimpleLinearRegression
def initialize(xs, ys)
@xs, @ys = xs, ys
if @xs.length != @ys.length
raise "Unbalanced data. xs need to be same length as ys"
end
end
def y_intercept
mean(@ys) - (slope * mean(@xs))
end
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
def regression_line
x_min = @xs.min
x_max = @xs.max
[[x_min, (slope * x_min + y_intercept)], [x_max, (slope * x_max + y_intercept)]]
end
def mean(values)
total = values.reduce(0) { |sum, x| x + sum }
Float(total) / Float(values.length)
end
end
@rikkipitt
Copy link
Author

Added the regression_line method to return an array of 2 points. Useful for adding regression lines in Highcharts.js

Usage:

linear_model = SimpleLinearRegression.new(xs, ys)
@regression_line = linear_model.regression_line

Where, xs and xy are arrays of all the x and y values respectively.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment