Skip to content

Instantly share code, notes, and snippets.

@aria42
Created September 19, 2010 19:01
Show Gist options
  • Save aria42/587021 to your computer and use it in GitHub Desktop.
Save aria42/587021 to your computer and use it in GitHub Desktop.
;; Probability Distribution
;; counter: counts of objects
;; lambda: smoothing constants
;; num-keys: number of possible keys, needed to normalize
(defrecord DirichletMultinomial [counter lambda num-keys])
(defn new-dirichlet [lambda num-keys]
(DirichletMultinomial. (Counter. {} 0) lambda num-keys))
(defn log-prob
"log prob. from a DirichletMultinomial"
[distr key]
{:post [(> % Double/NEGATIVE_INFINITY)]}
(let [{:keys [counter lambda num-keys]} distr]
(Math/log (/ (+ (get-count counter key) lambda)
(+ (:total counter) (* lambda num-keys))))))
(defn observe
"make an observation to a DirichletMultinomial"
[distr key weight]
(let [{:keys [counter,total,lambda,num-keys]} distr]
(DirichletMultinomial. (inc-count counter key weight) lambda num-keys)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment