Created
January 5, 2016 12:41
-
-
Save domgetter/04a37130d0b36ddce498 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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