#| Always take the middle taxi in Racket/Gamble From https://www.janestreet.com/static/pdfs/real-numbers/RN_PDF_02.pdf """ In the busy city center of Metropolis, exactly half of the cars are yellow taxis. You are trying to catch a taxi with your friend, who is superstitious and will only ride in a taxi that is both directly behind another taxi and directly in front of another taxi. If a stoplight in Metropolis has six cars stopped at it, and each car is a taxi independently with probability 1/2, what is the probability that there are three yellow taxis in a row at the traffic light? """ Via Pascal Bercker: "Jane Street — Problem Of The Week #2 — Take the Taxi in the Middle" https://medium.com/@pbercker/jane-street-problem-of-the-week-2-take-the-taxi-in-the-middle-fb5cf988cb84 variable : cars (0 0 0 0 1 1): 1/64 (0.015625) (1 1 1 1 0 1): 1/64 (0.015625) (1 0 1 1 0 1): 1/64 (0.015625) (1 1 0 1 1 0): 1/64 (0.015625) (1 0 1 1 1 1): 1/64 (0.015625) ... (0 0 1 0 0 1): 1/64 (0.015625) (0 0 0 1 1 1): 1/64 (0.015625) (0 1 1 1 0 1): 1/64 (0.015625) (1 0 0 0 0 0): 1/64 (0.015625) (0 1 1 1 0 0): 1/64 (0.015625) variable : s 0: 11/16 (0.6875) 1: 3/16 (0.1875) 2: 5/64 (0.078125) 3: 1/32 (0.03125) 4: 1/64 (0.015625) mean: 1/2 (0.5) variable : p #f: 11/16 (0.6875) #t: 5/16 (0.3125) mean: 5/16 (0.3125) variable : p2 #f: 13/16 (0.8125) #t: 3/16 (0.1875) mean: 3/16 (0.1875) Cf my Picat PPL model ppl_always_take_the_middle_taxi.pi 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") ; (require "gamble_distributions.rkt") (define (model) (enumerate ; rejection-sampler ; importance-sampler ; mh-sampler (define n 6) (define taxip 1/2) (define cars (for/list ([i n]) (bernoulli taxip))) (define s (count-occurrences-sublist '(1 1 1) cars)) (define p (> s 0)) ; at least 1 occurrence of three taxis in a row (define p2 (= s 1)) ; exactly 1 occurrence of three taxis in a row (list cars s p p2) ) ) (show-marginals (model) (list "cars" "s" "p" "p2" ) #:num-samples 1000 #:truncate-output 5 ; #:skip-marginals? #t ; #:show-stats? #t ; #:credible-interval 0.84 ; #:hpd-interval (list 0.84) ; #:show-histogram? #t ; #:show-percentiles? #t ; #:burn 0 ; #:thin 0 )