Repository for a workshop on Bayesian statistics
"""This file contains code used in "Think Stats",1by Allen B. Downey, available from greenteapress.com23Copyright 2013 Allen B. Downey4License: GNU GPLv3 http://www.gnu.org/licenses/gpl.html5"""67from __future__ import print_function, division89import thinkbayes10import thinkplot111213"""This file contains a partial solution to a problem from14MacKay, "Information Theory, Inference, and Learning Algorithms."1516Exercise 3.15 (page 50): A statistical statement appeared in17"The Guardian" on Friday January 4, 2002:1819When spun on edge 250 times, a Belgian one-euro coin came20up heads 140 times and tails 110. 'It looks very suspicious21to me,' said Barry Blight, a statistics lecturer at the London22School of Economics. 'If the coin weere unbiased, the chance of23getting a result as extreme as that would be less than 7%.'2425MacKay asks, "But do these data give evidence that the coin is biased26rather than fair?"2728"""2930class Euro(thinkbayes.Suite):3132def Likelihood(self, data, hypo):33"""Computes the likelihood of the data under the hypothesis.3435data: tuple (#heads, #tails)36hypo: integer value of x, the probability of heads (0-100)37"""38x = hypo / 100.039heads, tails = data40like = x**heads * (1-x)**tails41return like424344def AverageLikelihood(suite, data):45"""Computes the average likelihood over all hypothesis in suite.4647Args:48suite: Suite of hypotheses49data: some representation of the observed data5051Returns:52float53"""54total = 05556for hypo, prob in suite.Items():57like = suite.Likelihood(data, hypo)58total += prob * like5960return total616263def main():64fair = Euro()65fair.Set(50, 1)6667bias = Euro()68for x in range(0, 51):69bias.Set(x, x)70for x in range(51, 101):71bias.Set(x, 100-x)72bias.Normalize()7374thinkplot.Pdf(bias)75thinkplot.Show()7677# notice that we've changed the representation of the data78data = 140, 1107980like_bias = AverageLikelihood(bias, data)81print('like_bias', like_bias)8283like_fair = AverageLikelihood(fair, data)84print('like_fair', like_fair)8586ratio = like_bias / like_fair87print('Bayes factor', ratio)888990if __name__ == '__main__':91main()929394