Skip to content

Instantly share code, notes, and snippets.

@drewr
Created November 20, 2011 03:01
Show Gist options
  • Save drewr/1379750 to your computer and use it in GitHub Desktop.
Save drewr/1379750 to your computer and use it in GitHub Desktop.
Linear regression
(defn reg [pairs]
(let [M (count pairs)
bldr (fn [acc [x y]]
(let [x (bigdec x)
y (bigdec y)]
(merge-with
+ acc
{:∑x x
:∑y y
:∑xy (* x y)
:∑x² (pow x 2)})))
s (reduce bldr {} pairs)
∑x² (:∑x² s) ∑xy (:∑xy s) ∑x (:∑x s) ∑y (:∑y s)
∑x∑y (* ∑x ∑y)
w1 (/ (- (* M ∑xy) ∑x∑y)
(- (* M ∑x²) (pow ∑x 2)))
w0 (- (/ ∑y M) (/ (* w1 ∑x) M))]
{:w1 w1 :w0 w0}))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment