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 thinkbayes101112"""This file contains a partial solution to a problem from13MacKay, "Information Theory, Inference, and Learning Algorithms."1415Exercise 3.15 (page 50): A statistical statement appeared in16"The Guardian" on Friday January 4, 2002:1718When spun on edge 250 times, a Belgian one-euro coin came19up heads 140 times and tails 110. 'It looks very suspicious20to me,' said Barry Blight, a statistics lecturer at the London21School of Economics. 'If the coin weere unbiased, the chance of22getting a result as extreme as that would be less than 7%.'2324MacKay asks, "But do these data give evidence that the coin is biased25rather than fair?"2627"""282930class 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, 101):69if x != 50:70bias.Set(x, 1)71bias.Normalize()7273# notice that we've changed the representation of the data74data = 140, 1107576like_bias = AverageLikelihood(bias, data)77print('like_bias', like_bias)7879like_fair = AverageLikelihood(fair, data)80print('like_fair', like_fair)8182ratio = like_bias / like_fair83print('Bayes factor', ratio)848586if __name__ == '__main__':87main()888990