/* Probability of index of the smallest value in a list in Picat. What is the probability of the index of the smallest value in a list. There are two cases, dependending how we handle ties: * if a tie: pick the smallest index with the smallest value * if a tie: pick any of the indices for the smallest value Cf my Gamble model gamble_index_of_smallest_value.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. /* * The first approach (pick the first value has a bias towards the first indices. var : amin Probabilities: 1: 0.3119000000000000 2: 0.2367000000000000 3: 0.1907000000000000 4: 0.1439000000000000 5: 0.1168000000000000 mean = 2.517 var : amax Probabilities: 1: 0.3045000000000000 2: 0.2393000000000000 3: 0.1891000000000000 4: 0.1464000000000000 5: 0.1207000000000000 mean = 2.5395 * By randomly picking indices with ties for the smallest value theres no bias var : amin2 Probabilities: 5: 0.2074000000000000 1: 0.2002000000000000 3: 0.1990000000000000 2: 0.1978000000000000 4: 0.1956000000000000 mean = 3.0122 var : amax2 Probabilities: 5: 0.2074000000000000 3: 0.2055000000000000 2: 0.1976000000000000 4: 0.1954000000000000 1: 0.1941000000000000 mean = 3.0244 This difference is - of course - only relevant when there are duplicate values in the list. */ go ?=> reset_store, run_model(10_000,$model,[show_probs_trunc,mean % , % show_percentiles,show_histogram, % show_hpd_intervals,hpd_intervals=[0.94], % min_accepted_samples=1000,show_accepted_samples=true ]), nl, % show_store_lengths,nl, % fail, nl. go => true. model() => N = 5, Q = random_integer_n(N,N), % Ties are handled by picking the first AMin = argmin(Q).first, AMax = argmax(Q).first, % Ties are handled by picking any of the indices for the smallest value AMin2 = argmin_random_ties(Q), AMax2 = argmax_random_ties(Q), add("amin",AMin), add("amax",AMax), add("amin2",AMin2), add("amax2",AMax2).