Skip to content

Instantly share code, notes, and snippets.

@w01fe
Created September 28, 2012 23:25
Show Gist options
  • Save w01fe/3802579 to your computer and use it in GitHub Desktop.
Save w01fe/3802579 to your computer and use it in GitHub Desktop.
Univariate stats example: as 'let'
;; Take a map {:xs xs} and return a map
;; of simple univariate statistics of xs
(defn stats [{:keys [xs]}]
(let [n (count xs)
m (/ (sum identity xs) n)
m2 (/ (sum #(* % %) xs) n)
v (- m2 (* m m))]
{:n n ; count
:m m ; mean
:m2 m2 ; mean square
:v v ; variance
}))
(stats {:xs [1 2 3 6]})
; ==> {:n 4
; :m 3
; :m2 12.5
; :v 3.5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment