if __name__ ="__main__"inferencemethod testim(ve stochastic simulation sampling from discrete distribution the method sample one generates single sample from (possible unnormalizeddistribution dist is {value weightdictionarywhere weight > this returns value with probability in proportion to its weight probstochsim py -probabilistic inference using stochastic simulation import random from probgraphicalmodels import inferencemethod def sample_one(dist)"""returns the index of single sample from normalized distribution dist ""rand random random()*sum(dist values()cum cumulative weights for in distcum +dist[vif cum randreturn if we want to generate multiple samplesrepeatedly calling sample one may not be efficient if we want to generate samplesand the distribution is over valuessample one takes time (mnif and are of the same order of magnitudewe can do better the method sample multiple generates multiple samples from distribution defined by distwhere dist is {value weightdictionarywhere weight > and the weights cannot all be zero this returns list of valuesof length num sampleswhere each sample is selected with probability proportional to its weight the method generates all of the random numberssorts themand then goes through the distribution oncesaving the selected samples probstochsim py -(continued def sample_multiple(distnum_samples)"""returns list of num_samples values selected using distribution dist dist is {value:weightdictionary that does not need to be normalized ""total sum(dist values()rands sorted(random random()*total for in range(num_samples)result [dist_items list(dist items()version june