/* Number of double heads in Picat. https://brainstellar.com/puzzles/probability/118 """ A coin is tossed 10 times and the output written as a string. What is the expected number of HH? Note that in HHH, number of HH = 2. (eg: expected number of HH in 2 tosses is 0.25, 3 tosses is 0.5) Solution: E[n] = (n-1)/4 ... E[10]=2.5. """ Cf my Gamble model gamble_number_of_double_heads.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. main => go. /* n = 10 var : double heads Probabilities: 2: 0.2333000000000000 1: 0.2279500000000000 3: 0.1854000000000000 0: 0.1402500000000000 4: 0.1136000000000000 5: 0.0626500000000000 6: 0.0233500000000000 7: 0.0101000000000000 8: 0.0023500000000000 9: 0.0010500000000000 mean = 2.25745 n = 20 var : double heads Probabilities: 4: 0.1629500000000000 5: 0.1574000000000000 3: 0.1466000000000000 6: 0.1284000000000000 2: 0.1068000000000000 7: 0.0923500000000000 8: 0.0589500000000000 1: 0.0584500000000000 9: 0.0369000000000000 10: 0.0204000000000000 0: 0.0153000000000000 11: 0.0096500000000000 12: 0.0039000000000000 13: 0.0015500000000000 14: 0.0003000000000000 17: 0.0000500000000000 16: 0.0000500000000000 mean = 4.75415 */ go ?=> member(N,[10,20]), println(n=N), reset_store, run_model(20_000,$model(N),[show_probs,mean]), nl, % show_store_lengths,nl, fail, nl. go => true. model(N) => Tosses = bern_n(1/2,N), DoubleHeads = [cond(Chunk == [1,1],1,0) : Chunk in sublists(Tosses,2)].sum, add("double heads",DoubleHeads).