📚 The CoCalc Library - books, templates and other resources
License: OTHER
"""This file contains code for use with "Think Bayes",1by Allen B. Downey, available from greenteapress.com23Copyright 2012 Allen B. Downey4License: GNU GPLv3 http://www.gnu.org/licenses/gpl.html5"""67"""This file contains a partial solution to a problem from8MacKay, "Information Theory, Inference, and Learning Algorithms."910Exercise 3.15 (page 50): A statistical statement appeared in11"The Guardian" on Friday January 4, 2002:1213When spun on edge 250 times, a Belgian one-euro coin came14up heads 140 times and tails 110. 'It looks very suspicious15to me,' said Barry Blight, a statistics lecturer at the London16School of Economics. 'If the coin were unbiased, the chance of17getting a result as extreme as that would be less than 7%.'1819MacKay asks, "But do these data give evidence that the coin is biased20rather than fair?"2122"""2324import thinkbayes25import thinkplot262728class Euro(thinkbayes.Suite):29"""Represents hypotheses about the probability of heads."""3031def Likelihood(self, data, hypo):32"""Computes the likelihood of the data under the hypothesis.3334hypo: integer value of x, the probability of heads (0-100)35data: string 'H' or 'T'36"""37x = hypo / 100.038if data == 'H':39return x40else:41return 1-x424344class Euro2(thinkbayes.Suite):45"""Represents hypotheses about the probability of heads."""4647def Likelihood(self, data, hypo):48"""Computes the likelihood of the data under the hypothesis.4950hypo: integer value of x, the probability of heads (0-100)51data: tuple of (number of heads, number of tails)52"""53x = hypo / 100.054heads, tails = data55like = x**heads * (1-x)**tails56return like575859def UniformPrior():60"""Makes a Suite with a uniform prior."""61suite = Euro(xrange(0, 101))62return suite636465def TrianglePrior():66"""Makes a Suite with a triangular prior."""67suite = Euro()68for x in range(0, 51):69suite.Set(x, x)70for x in range(51, 101):71suite.Set(x, 100-x)72suite.Normalize()73return suite747576def RunUpdate(suite, heads=140, tails=110):77"""Updates the Suite with the given number of heads and tails.7879suite: Suite object80heads: int81tails: int82"""83dataset = 'H' * heads + 'T' * tails8485for data in dataset:86suite.Update(data)878889def Summarize(suite):90"""Prints summary statistics for the suite."""91print suite.Prob(50)9293print 'MLE', suite.MaximumLikelihood()9495print 'Mean', suite.Mean()96print 'Median', thinkbayes.Percentile(suite, 50)9798print '5th %ile', thinkbayes.Percentile(suite, 5)99print '95th %ile', thinkbayes.Percentile(suite, 95)100101print 'CI', thinkbayes.CredibleInterval(suite, 90)102103104def PlotSuites(suites, root):105"""Plots two suites.106107suite1, suite2: Suite objects108root: string filename to write109"""110thinkplot.Clf()111thinkplot.PrePlot(len(suites))112thinkplot.Pmfs(suites)113114thinkplot.Save(root=root,115xlabel='x',116ylabel='Probability',117formats=['pdf', 'eps'])118119120def main():121# make the priors122suite1 = UniformPrior()123suite1.name = 'uniform'124125suite2 = TrianglePrior()126suite2.name = 'triangle'127128# plot the priors129PlotSuites([suite1, suite2], 'euro2')130131# update132RunUpdate(suite1)133Summarize(suite1)134135RunUpdate(suite2)136Summarize(suite2)137138# plot the posteriors139PlotSuites([suite1], 'euro1')140PlotSuites([suite1, suite2], 'euro3')141142143if __name__ == '__main__':144main()145146147