/* Weight scale in Picat. Port of Pyro's Weight scale model """ Suppose we are trying to figure out how much something weighs, but the scale we're using is unreliable and gives slightly different answers every time we weigh the same object. We could try to compensate for this variability by integrating the noisy measurement information with a guess based on some prior knowledge about the object, like its density or material properties. The following model encodes this process: weight | guess ~ Normal(guess,1) measurement | guess, weight ~ Normal(weight,0.75) Note that this is a model not only for our belief over weight, but also for the result of taking a measurement of it. The model corresponds to the following stochastic function: def scale(guess): weight = pyro.sample("weight", dist.Normal(guess, 1.0)) return pyro.sample("measurement", dist.Normal(weight, 0.75)) """ Cf my Gamble model gamble_weight_scale.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. /* var : measurement Probabilities (truncated): 12.253919179378526: 0.0018181818181818 12.098928196413965: 0.0018181818181818 12.009342435856567: 0.0018181818181818 11.981853919742962: 0.0018181818181818 ......... 6.88093704891777: 0.0018181818181818 6.751605447760374: 0.0018181818181818 6.66386005527235: 0.0018181818181818 6.596305032214469: 0.0018181818181818 mean = 9.48868 variance = 1.06211 Histogram: 6.596: 2 ### (0.004 / 0.004) 6.738: 1 ## (0.002 / 0.005) 6.879: 3 ##### (0.005 / 0.011) 7.021: 2 ### (0.004 / 0.015) 7.162: 3 ##### (0.005 / 0.020) 7.304: 0 (0.000 / 0.020) 7.445: 4 ###### (0.007 / 0.027) 7.586: 6 ########## (0.011 / 0.038) 7.728: 7 ########### (0.013 / 0.051) 7.869: 5 ######## (0.009 / 0.060) 8.011: 8 ############# (0.015 / 0.075) 8.152: 17 ############################ (0.031 / 0.105) 8.294: 13 ##################### (0.024 / 0.129) 8.435: 14 ####################### (0.025 / 0.155) 8.576: 23 ##################################### (0.042 / 0.196) 8.718: 28 ############################################# (0.051 / 0.247) 8.859: 27 ############################################ (0.049 / 0.296) 9.001: 33 ###################################################### (0.060 / 0.356) 9.142: 28 ############################################# (0.051 / 0.407) 9.284: 22 #################################### (0.040 / 0.447) 9.425: 37 ############################################################ (0.067 / 0.515) 9.567: 35 ######################################################### (0.064 / 0.578) 9.708: 21 ################################## (0.038 / 0.616) 9.849: 31 ################################################## (0.056 / 0.673) 9.991: 19 ############################### (0.035 / 0.707) 10.132: 22 #################################### (0.040 / 0.747) 10.274: 19 ############################### (0.035 / 0.782) 10.415: 29 ############################################### (0.053 / 0.835) 10.557: 14 ####################### (0.025 / 0.860) 10.698: 13 ##################### (0.024 / 0.884) 10.840: 13 ##################### (0.024 / 0.907) 10.981: 11 ################## (0.020 / 0.927) 11.122: 8 ############# (0.015 / 0.942) 11.264: 13 ##################### (0.024 / 0.965) 11.405: 3 ##### (0.005 / 0.971) 11.547: 5 ######## (0.009 / 0.980) 11.688: 5 ######## (0.009 / 0.989) 11.830: 0 (0.000 / 0.989) 11.971: 4 ###### (0.007 / 0.996) 12.112: 2 ### (0.004 / 1.000) var : weight Probabilities (truncated): 11.239152000225634: 0.0018181818181818 11.166575134078759: 0.0018181818181818 11.071310172681882: 0.0018181818181818 10.990861763829445: 0.0018181818181818 ......... 7.942825080048227: 0.0018181818181818 7.837110257638394: 0.0018181818181818 7.824240194732448: 0.0018181818181818 7.820814558543659: 0.0018181818181818 mean = 9.47104 variance = 0.476068 Histogram: 7.821: 3 ###### (0.005 / 0.005) 7.906: 1 ## (0.002 / 0.007) 7.992: 3 ###### (0.005 / 0.013) 8.077: 9 ################# (0.016 / 0.029) 8.163: 8 ############### (0.015 / 0.044) 8.248: 4 ######## (0.007 / 0.051) 8.334: 8 ############### (0.015 / 0.065) 8.419: 10 ################### (0.018 / 0.084) 8.504: 9 ################# (0.016 / 0.100) 8.590: 12 ####################### (0.022 / 0.122) 8.675: 8 ############### (0.015 / 0.136) 8.761: 14 ########################## (0.025 / 0.162) 8.846: 21 ####################################### (0.038 / 0.200) 8.932: 22 ######################################### (0.040 / 0.240) 9.017: 22 ######################################### (0.040 / 0.280) 9.103: 18 ################################## (0.033 / 0.313) 9.188: 30 ######################################################## (0.055 / 0.367) 9.274: 32 ############################################################ (0.058 / 0.425) 9.359: 22 ######################################### (0.040 / 0.465) 9.445: 21 ####################################### (0.038 / 0.504) 9.530: 30 ######################################################## (0.055 / 0.558) 9.615: 28 ##################################################### (0.051 / 0.609) 9.701: 24 ############################################# (0.044 / 0.653) 9.786: 21 ####################################### (0.038 / 0.691) 9.872: 22 ######################################### (0.040 / 0.731) 9.957: 27 ################################################### (0.049 / 0.780) 10.043: 15 ############################ (0.027 / 0.807) 10.128: 12 ####################### (0.022 / 0.829) 10.214: 15 ############################ (0.027 / 0.856) 10.299: 12 ####################### (0.022 / 0.878) 10.385: 12 ####################### (0.022 / 0.900) 10.470: 16 ############################## (0.029 / 0.929) 10.555: 12 ####################### (0.022 / 0.951) 10.641: 7 ############# (0.013 / 0.964) 10.726: 6 ########### (0.011 / 0.975) 10.812: 4 ######## (0.007 / 0.982) 10.897: 6 ########### (0.011 / 0.993) 10.983: 1 ## (0.002 / 0.995) 11.068: 1 ## (0.002 / 0.996) 11.154: 2 #### (0.004 / 1.000) */ go ?=> GuessVal = 9.5, reset_store, run_model(10_000,$model(GuessVal),[show_probs_trunc,mean,variance,show_histogram]), nl, % show_store_lengths, % fail, nl. go => true. model(GuessVal) => Guess = GuessVal, Weight = normal_dist(Guess,1.0), Measurement = normal_dist(Weight,0.75), observe(abs(normal_dist(Weight,1)-Guess)<0.1), if observed_ok then add("weight",Weight), add("measurement",Measurement), end.