Skip to content

Instantly share code, notes, and snippets.

@camilleroux
Created November 27, 2010 17:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save camilleroux/718108 to your computer and use it in GitHub Desktop.
Save camilleroux/718108 to your computer and use it in GitHub Desktop.
require 'matrix'
require 'benchmark'
def regress x, y, degree
x_data = x.map { |xi| (0..degree).map { |pow| (xi**pow).to_f } }
mx = Matrix[*x_data]
my = Matrix.column_vector(y)
((mx.t * mx).inv * mx.t * my).transpose.to_a[0]
end
n= 1000
x = []
y = []
1.upto(n) do |i|
x << i
y << rand(10)
end
Benchmark.bm do |b|
b.report("degree 6") { regress(x,y,6) }
b.report("degree 8") { regress(x,y,8) }
b.report("degree 10") { regress(x,y,10) }
b.report("degree 12") { regress(x,y,10) }
b.report("degree 15") { regress(x,y,10) }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment