Repository for a workshop on Bayesian statistics
"""This file contains code used in "Think Stats",1by Allen B. Downey, available from greenteapress.com23Copyright 2015 Allen B. Downey4License: GNU GPLv3 http://www.gnu.org/licenses/gpl.html5"""67from __future__ import print_function, division89import numpy10import thinkbayes11import thinkplot121314"""15This problem presents a solution to the "Bayesian Billiards Problem",16presented in this video:1718https://www.youtube.com/watch?v=KhAUfqhLakw1920Based on the formulation in this paper:2122http://www.nature.com/nbt/journal/v22/n9/full/nbt0904-1177.html2324Of a problem originally posed by Bayes himself.25"""2627class Billiards(thinkbayes.Suite):2829def Likelihood(self, data, hypo):30"""Computes the likelihood of the data under the hypothesis.3132data: tuple (#wins, #losses)33hypo: float probability of win34"""35p = hypo36win, lose = data37like = p**win * (1-p)**lose38return like394041def ProbWinMatch(pmf):42total = 043for p, prob in pmf.Items():44total += prob * (1-p)**345return total464748def main():49ps = numpy.linspace(0, 1, 101)50bill = Billiards(ps)51bill.Update((5, 3))52thinkplot.Pdf(bill)53thinkplot.Save(root='billiards1',54xlabel='probability of win',55ylabel='PDF',56formats=['png'])5758bayes_result = ProbWinMatch(bill)59print(thinkbayes.Odds(1-bayes_result))6061mle = 5 / 862freq_result = (1-mle)**363print(thinkbayes.Odds(1-freq_result))646566if __name__ == '__main__':67main()686970