📚 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 Version1():60suite = Euro(xrange(0, 101))61heads, tails = 140, 11062dataset = 'H' * heads + 'T' * tails6364for data in dataset:65suite.Update(data)6667return suite686970def Version2():71suite = Euro(xrange(0, 101))72heads, tails = 140, 11073dataset = 'H' * heads + 'T' * tails7475suite.UpdateSet(dataset)76return suite777879def Version3():80suite = Euro2(xrange(0, 101))81heads, tails = 140, 1108283suite.Update((heads, tails))84return suite858687def main():8889suite = Version3()90print suite.Mean()9192thinkplot.Pmf(suite)93thinkplot.Show()94959697if __name__ == '__main__':98main()99100101