Skip to content

Instantly share code, notes, and snippets.

@chrisranderson
Last active February 18, 2019 12:39
Show Gist options
  • Select an option

  • Save chrisranderson/6eef81fecac7d55339af23cb812c1141 to your computer and use it in GitHub Desktop.

Select an option

Save chrisranderson/6eef81fecac7d55339af23cb812c1141 to your computer and use it in GitHub Desktop.
Church solution to the Monty Hall problem
; The Monty Hall problem: can be executed online here: https://probmods.org/play-space.html
(define door-options (list 0 1 2))
(define (random-list-index l)
(sample-discrete (make-list (length l) (div 1 (length l)))))
(define (filter-out a-list value)
(filter (lambda (x) (if (equal? x value) #f #t)) a-list))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define (pick-door)
(random-list-index door-options))
(define (gen-doors car-door)
(let [(doors (list 'goat 'goat 'goat))]
(update-list doors car-door 'car)))
(define (show-door first-choice car-door)
(if (= first-choice car-door)
(random-list-index (filter-out door-options car-door))
(first (filter-out (filter-out door-options first-choice) car-door))))
(define samples
(mh-query
10000 1
(define car-door (pick-door))
(define doors (gen-doors car-door))
(define first-choice (pick-door))
(define shown-door (show-door first-choice car-door))
(define switch-doors (flip .5))
(define second-choice
(if switch-doors
(first (filter-out (filter-out door-options first-choice) shown-door))
first-choice))
; What is the probability that I picked the car door...
(equal? second-choice car-door)
; given that I switched?
switch-doors))
(hist samples "Chance of winning if you switch")
@chrisranderson
Copy link
Copy Markdown
Author

You can run the code here: https://probmods.org/play-space.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment