/* Geometric counting cars in Picat. From Mathematica (GeometricDistribution) """ A person is standing by a road counting cars until he sees a red one, at which point he restarts the count. Simulate the counting process, assuming that 20% of the cars are red: RandomVariate(GeometricDistribution(0.2), 20) -> (10,0,2,0,2,10,8,4,0,0,15,4,4,1,6,1,6,1,0,0) Find the expected number of cars to come by before the count starts over: Mean(GeometricDistribution(0.2)) -> 4 Find the probability of counting 10 or more cars before a red one: NProbability(x >= 10, x -> GeometricDistribution(0.2)) -> 0.107374 """ Picat> X=geometric_dist_n(0.2,20) X = [4,4,1,3,1,11,0,2,0,1,7,1,1,11,0,2,1,10,24,2] Exact probabilities: Picat> X=geometric_dist_mean(0.2) X = 4.0 Picat> X=1-geometric_dist_cdf(0.2,9) X = 0.1073741824 Cf my Gamble model gamble_geometric_counting_cars.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 : v Probabilities (truncated): 0: 0.2007000000000000 1: 0.1583000000000000 2: 0.1271000000000000 3: 0.0992000000000000 ......... 44: 0.0001000000000000 42: 0.0001000000000000 41: 0.0001000000000000 35: 0.0001000000000000 mean = 4.0888 var : p Probabilities: false: 0.8852000000000000 true: 0.1148000000000000 mean = [false = 0.8852,true = 0.1148] */ go ?=> reset_store, run_model(10_000,$model,[show_probs_trunc,mean]), nl, % show_store_lengths,nl, % fail, nl. go => true. model() => V = geometric_dist(2/10), P = check(V >= 10), add("v",V), add("p",P).