def main(): pmf = Pmf() pmf.Set('Bowl 1', 0.5) pmf.Set('Bowl 2', 0.5) pmf.Mult('Bowl 1', 0.75) pmf.Mult('Bowl 2', 0.5) pmf.Normalize() print(pmf.Prob('Bowl 1')) print(pmf.Prob('Bowl 2'))
"""This file contains code for use with "Think Bayes", by Allen B. Downey, available from greenteapress.com Copyright 2012 Allen B. Downey License: GNU GPLv3 http://www.gnu.org/licenses/gpl.html """ from __future__ import print_function, division from thinkbayes2 import Pmf pmf = Pmf() pmf.Set('Bowl 1', 0.5) pmf.Set('Bowl 2', 0.5) pmf.Mult('Bowl 1', 0.75) pmf.Mult('Bowl 2', 0.5) pmf.Normalize() print(pmf.Prob('Bowl 1'))
pmf2 = Pmf() for word in ['a', 'in', 'or', 'to', 'a', 'me', 'in']: pmf2.Incr(word, 1) pmf2.Normalize() print(pmf2) print("Probability of letter a:", pmf2.Prob( 'a')) # Typo p12 print pmf.Prob('the') should read print(pmf.Prob('the')) # PMF for the Cookie problem pmf = Pmf() # Prior: pmf.Set("Bowl 1", 0.5) pmf.Set("Bowl 2", 0.5) # Posterior: # First multiply prior by likelihood pmf.Mult("Bowl 1", 0.75) pmf.Mult("Bowl 2", 0.5) # Then normalise (we can do this because the hypotheses are mutually exclusive and collectively exhaustive, # i.e. only one of the hypotheses can be true and there can be no other hypothesis) pmf.Normalize() print(pmf) # Create a Cookie class that inherits from Pmf and represents the Cookie problem class Cookie(Pmf): proportions = { 'Bowl 1': dict(vanilla=0.75, chocolate=0.25), 'Bowl 2': dict(vanilla=0.5, chocolate=0.5), }
# http://www.greenteapress.com/thinkbayes/thinkbayes.pdf # http://thinkbayes.com/thinkbayes.py # python -m pip install scipy numpy matplotlib pandas from thinkbayes2 import Pmf pmf = Pmf() pmf.Set('tazon1', 0.5) pmf.Set('tazon2', 0.5) pmf.Mult('tazon1', 0.75) pmf.Mult('tazon2', 0.5) pmf.Normalize() print(pmf.Prob('tazon1'))