/* Flipping coins until HTTTH or HTHTHT in Picat. From "Perplexing the Web, One Probability Puzzle at a Time" https://www.quantamagazine.org/perplexing-the-web-one-probability-puzzle-at-a-time-20240829/?mc_cid=94caee8978 (about Daniel Litt) """ Puzzle 2 You keep flipping a fair coins until you see five consecutive flips of the form HTTTH or HTHTH (where H means heads and T means tails). Then stop. Those last five flips are more likely to have been - HTTTH - HTHTH - equally likely """ The problem was originally stated in Daniel Litt's https://x.com/littmath/status/1786115416231641335 (May 2, 2024) """ Keep flipping a fair coin until you see five consecutive flips of the form HTTTH or HTHTH (where H means heads and T means tails), then stop. Your last 5 flips are more likely to have been: HTTTH 15.2% HTHTH 14.5% Equally likely 53.5% Don't know/see results 16.7% """ Here we code H -> 1, T -> 0 Cf my Gamble model gamble_flipping_coins_until_pattern.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. /* patterns = [[1,0,0,0,1],[1,0,1,0,1]] var : len Probabilities (truncated): 6: 0.0597000000000000 5: 0.0593000000000000 8: 0.0566000000000000 7: 0.0555000000000000 ......... 101: 0.0001000000000000 96: 0.0001000000000000 95: 0.0001000000000000 91: 0.0001000000000000 mean = 19.7193 var : pattern Probabilities: [1,0,0,0,1]: 0.5568000000000000 [1,0,1,0,1]: 0.4432000000000000 mean = [[1,0,0,0,1] = 0.5568,[1,0,1,0,1] = 0.4432] patterns = [[1,0,1],[1,1,1]] var : len Probabilities (truncated): 3: 0.2549000000000000 4: 0.1834000000000000 5: 0.0914000000000000 6: 0.0794000000000000 ......... 39: 0.0001000000000000 38: 0.0001000000000000 35: 0.0001000000000000 32: 0.0001000000000000 mean = 6.8642 var : pattern Probabilities: [1,0,1]: 0.5899000000000000 [1,1,1]: 0.4101000000000000 mean = [[1,0,1] = 0.5899,[1,1,1] = 0.4101] patterns = [[1,0,1,1],[1,1,1,0],[1,1],[0,0]] var : len Probabilities (truncated): 2: 0.5044000000000000 3: 0.2488000000000000 4: 0.1251000000000000 5: 0.0588000000000000 ......... 11: 0.0010000000000000 12: 0.0004000000000000 13: 0.0003000000000000 14: 0.0001000000000000 mean = 2.9917 var : pattern Probabilities: [0,0]: 0.5026000000000000 [1,1]: 0.3718000000000000 [1,0,1,1]: 0.1256000000000000 mean = [[0,0] = 0.5026,[1,1] = 0.3718,[1,0,1,1] = 0.1256] patterns = [[1,1],[1,0]] var : len Probabilities (truncated): 2: 0.5068000000000000 3: 0.2455000000000000 4: 0.1228000000000000 5: 0.0638000000000000 ......... 11: 0.0007000000000000 13: 0.0005000000000000 14: 0.0003000000000000 15: 0.0001000000000000 mean = 2.9916 var : pattern Probabilities: [1,1]: 0.5023000000000000 [1,0]: 0.4977000000000000 mean = [[1,1] = 0.5023,[1,0] = 0.4977] */ go ?=> PatternsList = [ [ [1,0,0,0,1], % HTTTH [1,0,1,0,1] % HTHTH ], [ [1,0,1], % HTH [1,1,1] % HHH ], [ [1,0,1,1], % HTHH [1,1,1,0], % HHHT This is not reached [1,1], % HH [0,0] % TT ], [ [1,1], % HH [1,0] % HT ] ], member(Patterns,PatternsList), println(patterns=Patterns), reset_store, run_model(10_000,$model(Patterns),[show_probs_trunc,mean]), nl, % show_store_lengths, fail, nl. go => true. model(Patterns) => % [Found,Pattern] = flip_until_pattern(Patterns,[]), [Found,Pattern] = draw_until_one_pattern([],Patterns,[0,1]), % add("found",Found), add("pattern",Pattern), add("len",Found.len).