Skip to content

Instantly share code, notes, and snippets.

@domgetter
Created January 5, 2016 12:41
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 domgetter/04a37130d0b36ddce498 to your computer and use it in GitHub Desktop.
Save domgetter/04a37130d0b36ddce498 to your computer and use it in GitHub Desktop.
(defn rolling-variance
([] [0.0 0.0 0])
([[m v c] val]
(let [mean (+ m (/ (- val m) (inc c)))
var (+ v (* (- val m) (- val mean)))]
[mean var (inc c)])))
(defn standard-deviation [coll]
(let [[_ variance count] (reduce rolling-variance (rolling-variance) coll)]
(Math/sqrt (/ variance count))))
(standard-deviation [2 4 4 4 5 5 7 9]) ;=> 2-ish
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment