Skip to content

Instantly share code, notes, and snippets.

@clizzin
Created January 24, 2012 01:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save clizzin/1667186 to your computer and use it in GitHub Desktop.
Save clizzin/1667186 to your computer and use it in GitHub Desktop.
moment function for cascalog.math
;; As predicate macro. Doesn't work because requires two-step aggregation (first step to get the mean, second step to get the moment).
(def moment
(<- [!val !k :> !moment]
(c/avg !val :> !mean)
(- !val !mean :> !dev)
(expt !dev !k :> !pow-dev)
(c/sum !pow-dev :> !pow-dev-sum)
(c/count !count)
(div !pow-dev-sum !count :> !moment)))
;; As function with subquery to get the mean
(defn moment [vals k]
(let [mean (first (first (??<- [!mean]
(vals !val)
(c/avg !val :> !mean))))]
(<- [!moment]
(vals !val)
(- !val mean :> !dev)
(expt !dev 2 :> !pow-dev)
(c/sum !pow-dev :> !pow-dev-sum)
(c/count !count)
(div !pow-dev-sum !count :> !moment))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment