/* Card problem in Picat. From https://math.stackexchange.com/questions/513250/conditional-probability-question-with-cards-where-the-other-side-color-is-to-be """ A box contains three cards. One card is red on both sides, one card is green on both sides, and one card is red on one side and green on the other. One card is selected from the box at random, and the color on one side is observed. If this side is green, what is the probability that the other side of the card is also green? ... the answer to this question is 2/3. """ Given that the color of card is green, the the probability of the different cards are: (all_green : 2/3 (0.6666666666666666)) (red_green : 1/3 (0.3333333333333333)) This is a port of my Racket/Gamble model gamble_card_problem.rkt This program was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ import ppl_distributions, ppl_utils. main => go. /* With 100_000 runs we might get the exact probabilities Var selected_card: Probabilities: all_green: 0.66666666666666663 (2 / 3) red_green: 0.33333333333333331 (1 / 3) */ go ?=> reset_store, run_model(10_000,$model,[show_probs_rat]), fail, nl. go => true. model() => SelectedCard = categorical([1,1,1].simplex,["all_red","all_green","red_green"]), Card = cond(SelectedCard=="all_red","red", cond(SelectedCard=="all_green","green", categorical([1/2,1/2],["red","green"]))), observe(Card == "green"), if observed_ok then add("selected_card",SelectedCard) end.