Why metro polis version and monte carlo version emits different results?
(defn sum-of-flips-infer []
(det-cp :sum-of-flips-infer
(let [a (gv (flip-cp :a 0.5))
b (gv (flip-cp :b 0.5))
c (gv (flip-cp :c 0.5))
v (fn [x] (if x 1 0))
s (+ (v a) (v b) (v c))]
(if (= s 2)
(v a)
(trace-failure)))))
(defn run-sample [model]
(density
(take 7500 (drop 500 (metropolis-hastings-sampling model)))))
(defn run-sample-monte [model]
(density
(take 7500 (drop 500 (monte-carlo-sampling model)))))
run-sample-monte emits correct result, however run-sample does not. Am I doing wrong or something is wrong in the code?
Your code is correct, but you hit a limitation of the current implementation that the proposal distribution changes single choice points only. In your example, any trace without exactly two heads will fail, and so no proposal that changes a single coin flip can move the chain away from the current state. Here, you would need blocked proposals which change two or more coin flips simultaneously. This is currently not supported. Choosing a good proposal distribution is a general problem in MCMC, especially if the likelihood function is deterministic (as in your example) or nearly so. Any implementation based on Gibbs sampling would also not work in this case for the same reason. The current API does give you some control about proposal distributions by allowing you to define custom choice points, see def-prop-cp. As a possible workaround in your example you could use this to define a choice point that draws three coins at once and implement a suitable, blocked proposal for it.
Thank you.