reasoning under uncertainty particles [{**pnvar:obs[nvar]for in resample(particlesweightsnumber_samples)elsefor part in particlespart[nvarsample_one({ :fac get_value({**partnvar: }for in nvar domain}self display( ,part[nvar],end="\ "counts {val: for val in qvar domainfor part in particlescounts[part[qvar]+ tot sum(counts values()as well as the distribution we also include the raw counts dist { : /tot for ( ,vin counts items()dist["raw_counts"counts return dist resampling resample is based on sample multiple but works with an array of particles (asidepython doesn' let us use sample multiple directly as it uses dictionaryand particlesrepresented as dictionaries can' be the key of dictionariesprobstochsim py -(continued def resample(particlesweightsnum_samples)"""returns num_samples copies of particles resampled according to weights particles is list of particles weights is list of positive numbersof same length as particles num_samples is integer ""total sum(weightsrands sorted(random random()*total for in range(num_samples)result [cum weights[ cumulative sum index for in randswhile >cumindex + cum +weights[indexresult append(particles[index]return result examples probstochsim py -(continued from probgraphicalmodels import bn_ cha, , , bn_ chr rejectionsampling(bn_ chbn_ chl likelihoodweighting(bn_ chversion june