/* Empty boxes in Picat. From https://medium.com/@moligninip/this-key-concept-will-simplify-all-your-probability-calculations-6e8d4b563b76 """ A total of k differently colored balls are randomly placed into n differently colored boxes. What is the expected number of empty boxes? """ Cf my Gamble model gamble_empty_boxes.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. /* [num_balls = 3,num_boxes = 4] var : num empty boxes Probabilities: 2: 0.5629000000000000 1: 0.3783000000000000 3: 0.0588000000000000 mean = 1.6805 [num_balls = 5,num_boxes = 3] var : num empty boxes Probabilities: 0: 0.6198000000000000 1: 0.3685000000000000 2: 0.0117000000000000 mean = 0.3919 */ go ?=> member([NumBalls,NumBoxes],[[3,4],[5,3]]), println([num_balls=NumBalls,num_boxes=NumBoxes]), reset_store, run_model(10_000,$model(NumBalls,NumBoxes),[show_probs_trunc,mean % , % show_simple_stats, % show_percentiles, % show_hpd_intervals,hpd_intervals=[0.84,0.9,0.94,0.99,0.99999] % show_histogram, % min_accepted_samples=1000,show_accepted_samples=true ]), nl, % show_store_lengths,nl, fail, nl. go => true. model(NumBalls,NumBoxes) => % Place the NumBalls balls into one of the NumBoxes boxes X = random_integer1_n(NumBoxes,NumBalls), Boxes = count_occurrences_list(1..NumBoxes,X), NumEmptyBoxes = count_occurrences(0,Boxes), add("num empty boxes",NumEmptyBoxes).