probmods / webchurch

A Church to Javascript compiler (DEPRECATED)
Other
140 stars 15 forks source link

(flip 0) is not always false inside MH conditioning #54

Closed longouyang closed 10 years ago

longouyang commented 10 years ago

In the code below, we randomly sample a letter from the set (a b c d e) and then condition (in a baroque fashion) on it being equal to a. In particular, we condition on a flip whose weight is 1 for a and 0 otherwise.

(define samps
  (mh-query
   10000 10
   (define x (uniform-draw '(a b c d e)))
   x
   (flip (if (equal? x 'a) 1 0))))

(hist samps)

If you run this code a few times, you'll find that sometimes you'll get samples that are not a. What is going on here?

ngoodman commented 10 years ago

Seems to have to do with init: the non 'a samples are always at the beginning.

longouyang commented 10 years ago

I think I figured it out. Currently, we use "lessdumb" initialization, which interleaves rejection with enumeration.

The enumeration was messing up because the nextVal function for flips (inside probjs) isn't quite correct for cases where the weight is 0 or 1.

I'll land a fix for this soon in probjs.

longouyang commented 10 years ago

Note to self: a similar affliction affects nextVal for multinomials.

(define samps 
  (mh-query
   1 1
   (define x (uniform-draw '(a b c d e f)))
   x
   (multinomial '(#t #f) 
                (if (equal? x 'a)
                    '(1 0)
                    '(0 1)))))

(first samps)
longouyang commented 10 years ago

Fixed in db61bc5fb5e753f4626b526b06c386a623c96fb6.