/* Hiring your or old in Picat. I'm buying some stuff in two different stores and have observed that store A is staffed mainly by older people (for any definition of older) and store C is staffed only by young people (for any definition of young). Let's add another store, store B, that age indifferent and don't care about the age. What can be said of the outcome hiring people in such a stores? For simplicity - and to be able to work a little with Markov processes :-) - let's assume that the last person hired is the one who hires the next. Cf my Gamble model gamble_hiring_young_or_old.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. /* transitions = [[0.9,0.1,0.0],[0.333333,0.333333,0.333333],[0.2,0.2,0.6]] init = [1,0,0] Random: [2,1,2,2,3,1,1,2,1,1,1,1,1,1,3,3,1,1,1,1,1,1,1,2,1,1,1,1,3,1] PDF: young = 0.736514 neutral = 0.145455 old = 0.118031 CDF: young = 0.736514 neutral = 0.881969 old = 1.0 Quantile: 0.000001 = young 0.00001 = young 0.001 = young 0.01 = young 0.025 = young 0.05 = young 0.1 = young 0.25 = young 0.5 = young 0.75 = neutral 0.84 = neutral 0.9 = old 0.975 = old 0.99 = old 0.999 = old 0.99999 = old 0.999999 = old Markov chains: young starts = [1,1,1,1,1,1,1,2,1,1,1,1,1,1,1,1,1,2,1,2,2,1,1,1,1,1,1,1,1,1] neutral starts = [2,3,3,3,2,2,2,3,3,3,3,1,1,2,3,3,2,3,3,2,3,1,1,1,1,1,1,1,1,1] old starts = [3,3,3,3,3,3,2,3,3,3,3,2,3,3,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2] Stationary: young : 0.73170731707317 neutral : 0.14634146341463 old : 0.12195121951220 young : 0.73170731707176 neutral : 0.14634146341489 old : 0.12195121951334 */ go ?=> [Young,Neutral,Old] = [1,2,3], Map = new_map([Young=young,Neutral=neutral,Old=old]), % young neural old Transitions = [[9/10, 1/10, 0/10], % young (almost always hire younger people [1/3 , 1/3 , 1/3 ], % neutral [2/10, 2/10, 6/10]], % old (mostly hire older people) println(transitions=Transitions), Init = one_hot(3,1), println(init=Init), println("Random:"), println(discrete_markov_process_dist_n(Transitions,Init,10,30)), nl, println("PDF:"), println(young=discrete_markov_process_dist_pdf(Transitions,Init,10,Young)), println(neutral=discrete_markov_process_dist_pdf(Transitions,Init,10,Neutral)), println(old=discrete_markov_process_dist_pdf(Transitions,Init,10,Old)), nl, println("CDF:"), println(young=discrete_markov_process_dist_cdf(Transitions,Init,10,Young)), println(neutral=discrete_markov_process_dist_cdf(Transitions,Init,10,Neutral)), println(old=discrete_markov_process_dist_cdf(Transitions,Init,10,Old)), nl, println("Quantile:"), foreach(Q in quantile_qs()) println(Q=Map.get(discrete_markov_process_dist_quantile(Transitions,Init,10,Q))) end, nl, println("Markov chains:"), println("young starts"=markov_chain(Transitions,one_hot(3,Young),30)), println("neutral starts"=markov_chain(Transitions,one_hot(3,Neutral),30)), println("old starts"=markov_chain(Transitions,one_hot(3,Old),30)), nl, println("Stationary:"), Stationary1 = stationary_dist1(Transitions,1.0e-16), foreach({I,S} in zip(Young..Old,Stationary1)) printf("%-10w: %.14f\n",Map.get(I),S) end, nl, Stationary2 = stationary_dist(Transitions), foreach({I,S} in zip(Young..Old,Stationary2)) printf("%-10w: %.14f\n",Map.get(I),S) end, nl. /* Simulation of this using markov_chain/3. first_hirer = young var : v mean = 1.3682 var : v ref mean = [young = 0.7441,neutral = 0.1436,old = 0.1123] var : num young mean = 8.06 var : num neutral mean = 1.216 var : num old mean = 0.724 first_hirer = neutral var : v mean = 1.4187 var : v ref mean = [young = 0.7148,neutral = 0.1517,old = 0.1335] var : num young mean = 5.4855 var : num neutral mean = 2.6585 var : num old mean = 1.856 first_hirer = old var : v mean = 1.4319 var : v ref mean = [young = 0.7091,neutral = 0.1499,old = 0.141] var : num young mean = 4.9275 var : num neutral mean = 1.58 var : num old mean = 3.4925 */ go2 => [Young,Neutral,Old] = [1,2,3], Map = new_map([Young=young,Neutral=neutral,Old=old]), % young neural old Transitions = [[9/10, 1/10, 0/10], % young (almost always hire younger people [1/3 , 1/3 , 1/3 ], % neutral [2/10, 2/10, 6/10]], % old (mostly hire older people) N = 10, member(FirstHirer,[Young,Neutral,Old]), println(first_hirer=Map.get(FirstHirer)), reset_store, run_model(10_000,$model2(Transitions,FirstHirer,N,Map),[mean]), nl, fail, nl. go2 => true. model2(Transitions,FirstHirer,N,Map) => NumTransitions = Transitions.len, Init = one_hot(NumTransitions,FirstHirer), X = markov_chain(Transitions,Init,N), % Who was the last hired? V = X.last, VRef = Map.get(V), NumYoung = count_occurrences(1,X), NumNeutral = count_occurrences(2,X), NumOld = count_occurrences(3,X), add("v",V), add("v ref",VRef), add("num young",NumYoung), add("num neutral",NumNeutral), add("num old",NumOld).