Skip to content

Instantly share code, notes, and snippets.

@jpoz
Created February 22, 2010 00:41
Show Gist options
  • Select an option

  • Save jpoz/310655 to your computer and use it in GitHub Desktop.

Select an option

Save jpoz/310655 to your computer and use it in GitHub Desktop.
#!/usr/bin/env clj
(ns ols)
(defn sum [x]
(reduce + 0 x))
(defn multsum [x y]
(loop [xdata x
ydata y
index 0
total 0]
(if (seq xdata)
(recur (rest xdata)
(rest ydata)
(inc index)
(+ total (* (first xdata) (first ydata))))
total)))
(defn olsbeta [x y]
(/ (- (* (count x) (multsum x y)) (* (sum x) (sum y)))
(- (* (count x) (multsum x x)) (* (sum x) (sum x)))
))
(defn yintercept [x y]
(- (* (/ 1 (count x)) (sum y)) (* (olsbeta x y) (* (/ 1 (count x)) (sum x)))))
;; test
(def x [1.47 1.50 1.52 1.55 1.57 1.60 1.63 1.65 1.68 1.70 1.73 1.75 1.78 1.80 1.83])
(def y [52.21 53.12 54.48 55.84 57.20 58.57 59.93 61.29 63.11 64.47 66.28 68.10 69.92 72.19 74.46])
(prn (apply str ["y = " (olsbeta x y) "*x + " (yintercept x y)]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment