Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added linear regression #1

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ To implement

### Regressions ###

- Linear regression
- <del>Linear regression</del>
- Multiple linear regression
- Pearson's correlation
- Spearman correlation
Expand Down
1 change: 1 addition & 0 deletions lib/stats.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
require 'stats/distribution'
require 'stats/significance'
require 'stats/assumption'
require 'stats/regression'
26 changes: 26 additions & 0 deletions lib/stats/regression.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module Stats
module Regression
class << self

# Performs linear regression on the supplied data using least
# squares fit.
#
# Returns the results as Hash with keys 'gradient' and 'intercept' which
# map to the gradient and intercept respectively.
def linear_regression(x, y)
return nil if x.length < 2 || y.length < 2 || x.length != y.length
sum_x = x.reduce(&:+)
sum_y = y.reduce(&:+)
sum_xy = x.zip(y).map { |x1, y1| x1 * y1 }.reduce(&:+)
sum_x_squared = x.map { |x1| x1 * x1 }.reduce(&:+)

gradient = (sum_xy - (sum_x * sum_y).to_f / x.length).to_f / (sum_x_squared - sum_x * sum_x / x.length)
intercept = (sum_y - gradient * sum_x).to_f / x.length

['gradient' => gradient, 'intercept' => intercept]
end
end
end
end


36 changes: 36 additions & 0 deletions spec/regression_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
require 'spec_helper'

module Stats
describe Regression do
it "performs linear regression using least squares fit" do
x = []
y = [1, 2, 3, 4, 5]
Regression.linear_regression(x, y).should == nil

x = [1, 2, 3, 4, 5]
y = []
Regression.linear_regression(x, y).should == nil

x = [1]
y = [1]
Regression.linear_regression(x, y).should == nil

x = [1, 2, 3, 4, 5]
y = [1, 2, 3, 4, 5, 6]
Regression.linear_regression(x, y).should == nil

x = [2, 4, 6, 8]
y = [2, 5, 5, 8]
Regression.linear_regression(x, y).should == ['gradient' => 0.9, 'intercept' => 0.5]

x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
Regression.linear_regression(x, y).should == ['gradient' => 2, 'intercept' => 0]

x = [0, 1, 2, 3, 4]
y = [3, 6, 7, 8, 11]
Regression.linear_regression(x, y).should == ['gradient' => 1.8, 'intercept' => 3.4]
end
end
end