#| Continuous weight example in Racket Gamble. From Church """ (define observed-data '(h h h h h)) (define num-flips (length observed-data)) (define num-samples 1000) (define prior-samples (repeat num-samples (lambda () (uniform 0 1)))) (define samples (mh-query num-samples 10 (define coin-weight (uniform 0 1)) (define make-coin (lambda (weight) (lambda () (if (flip weight) 'h 't)))) (define coin (make-coin coin-weight)) coin-weight (equal? observed-data (repeat num-flips coin)))) ;;; (density prior-samples "Coin weight, prior to observing data" #t ) ;;; (density samples "Coin weight, conditioned on observed data" #t) """ mh-sampler: (0.9441075686771364 : 0.08399999999999999) (0.8532642907181226 : 0.07999999999999999) (0.9920826638939778 : 0.07999999999999999) (0.9617464498251821 : 0.06399999999999999) (0.4520144106398797 : 0.04599999999999999) (0.9154353687564277 : 0.04599999999999999) (0.8713658040492068 : 0.041999999999999996) (0.8655299961171671 : 0.03899999999999999) (0.7744349464966145 : 0.03699999999999999) (0.9744554191564041 : 0.031999999999999994) (0.7528056978675502 : 0.029999999999999992) (0.9998619311887962 : 0.027999999999999994) (0.8457160244949473 : 0.025999999999999992) (0.6308004924111308 : 0.024999999999999994) (0.9902605467881528 : 0.022999999999999996) (0.6562640821800868 : 0.021999999999999995) (0.8908171894238274 : 0.021999999999999995) (0.7843242690757495 : 0.020999999999999998) (0.7288367677475438 : 0.019999999999999997) (0.7568423874730283 : 0.019999999999999997) (0.7753549761310767 : 0.018999999999999996) (0.8179195230191716 : 0.018999999999999996) (0.841599023447511 : 0.018999999999999996) (0.8768413181842758 : 0.018999999999999996) (0.9383813725279937 : 0.018999999999999996) (0.9288326274131394 : 0.017999999999999995) (0.943367338790653 : 0.014999999999999996) (0.6882771118920389 : 0.010999999999999998) (0.6860467010870842 : 0.009999999999999998) (0.7476971634945391 : 0.008999999999999998) (0.9461499528957509 : 0.008999999999999998) (0.9568032124114848 : 0.006999999999999998) (0.7604935437400493 : 0.004999999999999999) (0.910236941261516 : 0.004999999999999999) (0.9797453686564781 : 0.004999999999999999) (0.46703174597178665 : 0.003999999999999999) (0.9374416663748834 : 0.003999999999999999) (0.5830570508902583 : 0.002999999999999999) (0.8450778047033082 : 0.002999999999999999) (0.8734950096548914 : 0.002999999999999999) (0.727363054708465 : 0.0019999999999999996) (0.8745923435583729 : 0.0019999999999999996) (0.6210103410226645 : 0.0009999999999999998) (0.8913221762969654 : 0.0009999999999999998) (0.9090040312318222 : 0.0009999999999999998) (mean: 0.8523075591565509) Min: 0.3192727012580079 Mean: 0.8507832650018279 Max: 0.9980944727090305 Variance: 0.013928170628136036 Stddev: 0.11801767082999069 Credible interval (0.84): 0.7632032539104757..0.9980944727090305 This program was created by Hakan Kjellerstrand, hakank@gmail.com See also my Racket page: http://www.hakank.org/racket/ |# #lang gamble ;;; (require gamble/viz) (require racket) (require "gamble_utils.rkt") (define observed-data '(h h h h h)) (define num-flips (length observed-data)) (define num-samples 1000) (define prior-samples (repeat (lambda () (uniform 0 1)) num-samples)) (define samples (; rejection-sampler ; importance-sampler mh-sampler (define coin-weight (uniform 0 1)) (define make-coin (lambda (weight) (lambda () (if (flip weight) 'h 't)))) (define coin (make-coin coin-weight)) (observe/fail (equal? observed-data (repeat coin num-flips ))) coin-weight ) ) ;; (sampler->discrete-dist samples 1000) ;; (show-freq (repeat samples 1000)) ;; (sampler->mean+variance samples 1000) (show-model samples)