Skip to content

Instantly share code, notes, and snippets.

@jasondew
Created July 22, 2010 14:25
Show Gist options
  • Save jasondew/486030 to your computer and use it in GitHub Desktop.
Save jasondew/486030 to your computer and use it in GitHub Desktop.
x_bar = xs.sum / xs.length.to_f
y_bar = ys.sum / ys.length.to_f
normalized_xs = xs.map {|x| x - x_bar }
normalized_ys = ys.map {|y| y - y_bar }
beta_hat = normalized_xs.zip(normalized_ys).map {|x, y| x*y }.sum / normalized_xs.inject(0) {|sum, x| sum + x*x }
alpha_hat = y_bar - beta_hat * x_bar
[[0, alpha_hat], [xs.last, (alpha_hat + xs.last * beta_hat)]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment