/* Size of material in Picat. https://pedrozudo.github.io/docs/20_thesis/thesis_final_online_compressed.pdf page 100 """ Example 6.11. We model the size of a ball as a mixture of different beta distributions, depending on whether the ball is made out of wood or metal (Line 1)4. We would now like to know the probability of the ball being made out of wood given that we have a measurement of the size of the ball. In order to condition on a continuous random variable we introduce the observation/2 predicate, which has an analogous functionality as the evidence predicates for Boolean random variables. 1 3/10::material(wood);7/10::material(metal). 2 3 size~beta(2,3):-material(metal). 4 size~beta(4,2):-material(wood). 5 6 observation(size,4/10). 7 query(material(wood)). """ Cf my Gamble model gamble_size_of_material.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 : material Probabilities: metal: 0.8610000000000000 wood: 0.1390000000000000 mean = [metal = 0.861,wood = 0.139] HPD intervals: show_hpd_intervals: data is not numeric var : size Probabilities (truncated): 0.400997296297817: 0.0010000000000000 0.40099693633027: 0.0010000000000000 0.400996650951801: 0.0010000000000000 0.400995567975159: 0.0010000000000000 ......... 0.399002571247472: 0.0010000000000000 0.399001677083766: 0.0010000000000000 0.399001473749497: 0.0010000000000000 0.39900105434383: 0.0010000000000000 mean = 0.399999 HPD intervals: HPD interval (0.9): 0.39922140219196..0.40099729629782 HPD interval (0.95): 0.39900105434383..0.40089271816347 HPD interval (0.99): 0.39900105434383..0.40097725278882 */ go ?=> reset_store, run_model(10_000,$model,[show_probs_trunc,mean, % show_percentiles, show_hpd_intervals,hpd_intervals=[0.9,0.95,0.99], % show_histogram, min_accepted_samples=1000,show_accepted_samples=true ]), nl, % show_store_lengths,nl, % fail, nl. go => true. model() => MaterialType = [wood,metal], Material = categorical([3/10,7/10],MaterialType), Size = cond(Material == metal, beta_dist(2,3), beta_dist(4,2)), observe(abs(Size-(4/10)) < 0.001), if observed_ok then add("material",Material), add("size",Size) end.