Created
September 19, 2010 19:01
-
-
Save aria42/587021 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; 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