likelihood = self.Likelihood(data, hypo) self.Mult(hypo, likelihood) self.Normalize() def Likelihood(self, data, hypo): proportion = self.proportions[hypo] likelihood = proportion[data] return likelihood # Set up the hypotheses for the Cookie problem hypos = ['Bowl 1', 'Bowl 2'] # Initialise the prior for the Cookie problem pmf = Cookie(hypos) print("Prior:") for hypo, prob in pmf.Items(): print(hypo, prob) # Type p14: print hypo, prob should read print(hypo, prob) # Update the prior given one data point (we drew a vanilla cookie) pmf.Update('vanilla') print("Posterior:") for hypo, prob in pmf.Items(): print(hypo, prob) # Draw some more cookies (with replacement) and update the prior dataset = ['vanilla', 'chocolate', 'vanilla'] for data in dataset: pmf.Update(data) print("Posterior:") for hypo, prob in pmf.Items(): print(hypo, prob)
from thinkbayes2 import Suite, Pmf, SampleSum, MakeMixture import thinkplot from simulationDD02 import Die pmf_dice = Pmf() pmf_dice.Set(Die(4), 2) pmf_dice.Set(Die(6), 3) pmf_dice.Set(Die(8), 2) pmf_dice.Set(Die(12), 1) pmf_dice.Set(Die(20), 1) pmf_dice.Normalize() print(pmf_dice) print("#################################################") mix = Pmf() for die, weight in pmf_dice.Items(): for outcome, prob in die.Items(): mix.Incr(outcome, weight * prob) #Shorthand for above #mix = MakeMixture(pmf_dice) print(mix) thinkplot.Hist(mix) thinkplot.Save(root='bar', xlabel='Mixture over a set of dice', ylabel='Probability', formats=['pdf']) print("Program Finished")