Created
May 16, 2012 22:00
-
-
Save alexpw/2714318 to your computer and use it in GitHub Desktop.
Cascalog - std-dev using defaggregateop
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
(defaggregateop variance | |
"An implementation of the Welford/Knuth online algorithm: | |
http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#On-line_algorithm" | |
([] [0 0 0]) | |
([[m2 mean n] val] | |
(let [n (inc n) | |
delta (- val mean) | |
mean (+ mean (div delta n)) | |
m2 (+ m2 (* delta delta))] | |
[m2 mean n])) | |
([[m2 _ n]] [(div m2 (max 1 (- n 1)))])) | |
(defn sqrt [x] (Math/sqrt x)) | |
;; Example of using it: | |
(?<- (stdout) [?key ?std-dev] (source ?key ?val) (variance ?val :> ?var) (sqrt ?var :> ?std-dev)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Prefer this version here: https://github.com/nathanmarz/cascalog-contrib/blob/master/cascalog.math/src/cascalog/math/stats.clj