/* Streaks in Picat. Here's an exploration of streaks. From Grinstead, Peterson, Snell: "Probability Tales", page2ff """ It can be shown that in a sequence of 200 independent trials, where the probability of a success on a given trial is .85, the average length of the longest run of successes is about 24.0. (We will discuss this calculation a little later in this chapter.) Since many players shoot 200 or more free throws in a given season, it is not surprising that this player has a success run of 20. We will say more below about the length of the longest run in this model. """ This model checks for min, mean, and max value of the lengths of the runs on a binary list generated by (bernoulli 0.85). This verifies that the average max run length is (about) 24. Cf - ppl_runs.pi - my Gamble model gamble_streaks.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. /* [n = 200,p = 0.85,r = 20] var : min run Probabilities: 1: 1.0000000000000000 mean = 1.0 HPD intervals: HPD interval (0.5): 1.00000000000000..1.00000000000000 HPD interval (0.84): 1.00000000000000..1.00000000000000 HPD interval (0.9): 1.00000000000000..1.00000000000000 HPD interval (0.95): 1.00000000000000..1.00000000000000 HPD interval (0.99): 1.00000000000000..1.00000000000000 var : mean run Probabilities (truncated): 3.92156862745098: 0.0772000000000000 4.081632653061225: 0.0734000000000000 3.773584905660377: 0.0723000000000000 4.25531914893617: 0.0672000000000000 ......... 2.409638554216868: 0.0002000000000000 7.142857142857143: 0.0001000000000000 6.666666666666667: 0.0001000000000000 2.631578947368421: 0.0001000000000000 mean = 3.95942 HPD intervals: HPD interval (0.5): 3.38983050847458..4.08163265306122 HPD interval (0.84): 3.07692307692308..4.65116279069767 HPD interval (0.9): 2.98507462686567..4.87804878048780 HPD interval (0.95): 2.81690140845070..5.12820512820513 HPD interval (0.99): 2.66666666666667..5.88235294117647 var : max run Probabilities (truncated): 21: 0.0679000000000000 20: 0.0674000000000000 22: 0.0668000000000000 19: 0.0661000000000000 ......... 65: 0.0001000000000000 63: 0.0001000000000000 60: 0.0001000000000000 57: 0.0001000000000000 mean = 23.9604 HPD intervals: HPD interval (0.5): 17.00000000000000..24.00000000000000 HPD interval (0.84): 14.00000000000000..31.00000000000000 HPD interval (0.9): 14.00000000000000..34.00000000000000 HPD interval (0.95): 12.00000000000000..37.00000000000000 HPD interval (0.99): 10.00000000000000..46.00000000000000 var : runs len Probabilities (truncated): 51: 0.0772000000000000 49: 0.0734000000000000 53: 0.0723000000000000 47: 0.0672000000000000 ......... 27: 0.0002000000000000 76: 0.0001000000000000 30: 0.0001000000000000 28: 0.0001000000000000 mean = 51.7517 HPD intervals: HPD interval (0.5): 45.00000000000000..55.00000000000000 HPD interval (0.84): 39.00000000000000..61.00000000000000 HPD interval (0.9): 39.00000000000000..64.00000000000000 HPD interval (0.95): 37.00000000000000..67.00000000000000 HPD interval (0.99): 31.00000000000000..71.00000000000000 var : num01 Probabilities (truncated): 25: 0.1045000000000000 24: 0.0981000000000000 26: 0.0966000000000000 27: 0.0913000000000000 ......... 39: 0.0005000000000000 13: 0.0003000000000000 41: 0.0002000000000000 40: 0.0002000000000000 mean = 25.3786 HPD intervals: HPD interval (0.5): 21.00000000000000..26.00000000000000 HPD interval (0.84): 19.00000000000000..30.00000000000000 HPD interval (0.9): 18.00000000000000..31.00000000000000 HPD interval (0.95): 17.00000000000000..32.00000000000000 HPD interval (0.99): 15.00000000000000..35.00000000000000 var : num10 Probabilities (truncated): 25: 0.1003000000000000 26: 0.0997000000000000 24: 0.0981000000000000 27: 0.0898000000000000 ......... 39: 0.0006000000000000 41: 0.0002000000000000 40: 0.0002000000000000 13: 0.0002000000000000 mean = 25.3731 HPD intervals: HPD interval (0.5): 21.00000000000000..26.00000000000000 HPD interval (0.84): 19.00000000000000..30.00000000000000 HPD interval (0.9): 18.00000000000000..31.00000000000000 HPD interval (0.95): 17.00000000000000..32.00000000000000 HPD interval (0.99): 15.00000000000000..35.00000000000000 var : comp Probabilities: 10=01: 0.7437000000000000 01>10: 0.1309000000000000 10>01: 0.1254000000000000 mean = [10=01 = 0.7437,01>10 = 0.1309,10>01 = 0.1254] var : prob >= r Probabilities: true: 0.7091000000000000 false: 0.2909000000000000 mean = 0.7091 theoretical_prob = 0.709091 */ go ?=> N = 200, P = 85/100, R = 20, println([n=N,p=P,r=R]), reset_store, run_model(1000,$model(N,P,R),[show_probs_trunc,mean, % show_percentiles, show_hpd_intervals,hpd_intervals=[0.5,0.84,0.9,0.95,0.99] % show_histogram, % min_accepted_samples=1000,show_accepted_samples=true ]), nl, println(theoretical_prob=probability_of_run_size(N,P,R)), % show_store_lengths,nl, % fail, nl. go => true. model(N,P,R) => % nl, Throws = bern_n(P,N), Runs = get_runs(Throws), % The number of the runs RunsLen = Runs.len, % The lengths of the runs RunsLens = get_runs_lens(Throws), MinRun = RunsLens.min, MeanRun = RunsLens.mean, MaxRun = RunsLens.max, % Comparison of positive (0->1), negative (1->0), no change (x->x) Num01 = count_occurrences_sublist([0,1],Throws), Num10 = count_occurrences_sublist([1,0],Throws), Comp = cases([[Num01>Num10,"01>10"], [Num10>Num01,"10>01"], [true,"10=01"]]), % What is the probability of having at least a run of R=20? ProbR = check(MaxRun >= R), add("min run",MinRun), add("mean run",MeanRun), add("max run",MaxRun), add("runs len",RunsLen), add("num01",Num01), add("num10",Num10), add("comp",Comp), add("prob >= r",ProbR).