Skip to content

Commit

Permalink
test: Test importance sampling with a model and constraints resulting…
Browse files Browse the repository at this point in the history
… in samples with weight 0

This crashed previously.
  • Loading branch information
Schaechtle committed Apr 10, 2024
1 parent d4c5152 commit 4d2538e
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions test/gen/inference/importance_test.cljc
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
(ns gen.inference.importance-test
(:require [clojure.test :refer [deftest is testing]]
[gen.choicemap :as choicemap :refer [choicemap get-value]]
[gen.distribution.commons-math :as dist]
[gen.dynamic :as dynamic :refer [gen]]
[gen.inference.importance :as importance]
[gen.trace :as trace]))

(def model-causing-rejection-sampling
(gen
[]
(if (dynamic/trace! :foo dist/bernoulli 0.5)
(dynamic/trace! :bar dist/bernoulli 1.0)
(dynamic/trace! :bar dist/bernoulli 0.0))))

(deftest rejection
(testing "Robustness in the presence of importance samples with weight log(0)."
(is {:foo true :bar true}
;; Needs a couple of samples to trigger previous bug here.
(-> (importance/resampling model-causing-rejection-sampling [] (choicemap {:bar true}) 10)
(:trace)
(trace/get-choices)
(get-value)))))

0 comments on commit 4d2538e

Please sign in to comment.