/* Urns and balls in Picat. Assignment 1: https://edu.swi-prolog.org/mod/assign/view.php?id=242 """ http://cplint.eu/p/urns_and_balls.swinb Urns and balls Suppose you have two urns: urn1 contains 40 blue balls and 20 red balls and urn2 contains 25 blue balls and 30 red balls. You throw an unbiased coin and, if it turns out head, you draw a ball from the first urn, it it turns out tails you draw a ball from the second urn. Write a program modeling this process and a query for answering the question "What is the probability of drawing a blue ball?" """ The exact answer of drawing a blue call is 0.5*40/60 + 0.5*25/55 = 0.56060606060606060606 Cf my Gamble model gamble_urns_and_balls.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. import util. % import ordset. main => go. /* var : draw Probabilities: blue: 0.5597000000000000 red: 0.4403000000000000 mean = [blue = 0.5597,red = 0.4403] var : coin == tail Probabilities: tail == tail: 0.5092000000000000 head == tail: 0.4908000000000000 mean = [tail == tail = 0.5092,head == tail = 0.4908] var : coin == head Probabilities: tail == head: 0.5092000000000000 head == head: 0.4908000000000000 mean = [tail == head = 0.5092,head == head = 0.4908] var : draw == red Probabilities: blue == red: 0.5597000000000000 red == red: 0.4403000000000000 mean = [blue == red = 0.5597,red == red = 0.4403] var : draw == blue Probabilities: blue == blue: 0.5597000000000000 red == blue: 0.4403000000000000 mean = [blue == blue = 0.5597,red == blue = 0.4403] */ go ?=> reset_store, run_model(10_000,$model,[show_probs_trunc,mean % , % show_percentiles,show_histogram, % show_hpd_intervals,hpd_intervals=[0.94], % min_accepted_samples=1000,show_accepted_samples=true ]), nl, % show_store_lengths,nl, % fail, nl. go => true. model() => Coin = categorical([1/2,1/2],[tail,head]), Colors = [blue,red], Draw = cond(Coin == head, categorical([40,20],Colors), categorical([25,30],Colors)), add("draw",Draw), add("coin == tail",(Coin == tail)), add("coin == head",(Coin == head)), add("draw == red",(Draw == red)), add("draw == blue",(Draw == blue)).