Skip to content

Instantly share code, notes, and snippets.

@alexpw
Created May 16, 2012 22:00
Show Gist options
  • Save alexpw/2714318 to your computer and use it in GitHub Desktop.
Save alexpw/2714318 to your computer and use it in GitHub Desktop.
Cascalog - std-dev using defaggregateop
(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))
@alexpw
Copy link
Author

alexpw commented May 17, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment