Skip to content

Commit

Permalink
fix: prevent overflow in gaussian log-likelihood (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
sritchie committed Dec 12, 2023
1 parent eefa9af commit 0b015ab
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
9 changes: 4 additions & 5 deletions src/gen/distribution/math/log_likelihood.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -160,12 +160,11 @@
\\end{aligned}
$$"
[mu sigma v]
(let [v-mu (- v mu)
v-mu-sq (* v-mu v-mu)
variance (* sigma sigma)]
(let [v-mu:sigma (/ (- v mu) sigma)]
(* -0.5 (+ log-2pi
(Math/log variance)
(/ v-mu-sq variance)))))
(* 2 (Math/log sigma))
(* v-mu:sigma
v-mu:sigma)))))

(defn uniform
"Returns the log-likelihood of the continuous [uniform
Expand Down
5 changes: 3 additions & 2 deletions test/gen/distribution_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -174,15 +174,16 @@
(dist/logpdf (->normal 0.0 sigma) (- v)))
"Normal is symmetric about the mean")

(with-comparator (within 1e-12)
(with-comparator (within 1e-10)
(is (ish? (dist/logpdf (->normal mu sigma) v)
(dist/logpdf (->normal (+ mu shift) sigma) (+ v shift)))
"shifting by the mean is a symmetry")))

(testing "spot checks"
(is (= -1.0439385332046727 (dist/logpdf (->normal 0 1) 0.5)))
(is (= -1.643335713764618 (dist/logpdf (->normal 0 2) 0.5)))
(is (= -1.612085713764618 (dist/logpdf (->normal 0 2) 0)))))
(is (= -1.612085713764618 (dist/logpdf (->normal 0 2) 0)))
(is (= -8.996964098844152E20 (dist/logpdf (->normal 4241959036 0.1) 33978)))))

(defn uniform-tests [->uniform]
(checking "(log of the) Beta function is symmetrical"
Expand Down

0 comments on commit 0b015ab

Please sign in to comment.