Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
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
You can’t perform that action at this time.