Skip to content

Commit

Permalink
Updating for PyCon 2015
Browse files Browse the repository at this point in the history
  • Loading branch information
AllenDowney committed Apr 2, 2015
1 parent 9d9d4dd commit 4ea0c59
Show file tree
Hide file tree
Showing 16 changed files with 1,840 additions and 569 deletions.
6 changes: 4 additions & 2 deletions dice.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
License: GNU GPLv3 http://www.gnu.org/licenses/gpl.html
"""

from __future__ import print_function, division

from thinkbayes import Suite


Expand All @@ -25,13 +27,13 @@ def main():
suite = Dice([4, 6, 8, 12, 20])

suite.Update(6)
print 'After one 6'
print('After one 6')
suite.Print()

for roll in [8, 7, 7, 5, 4]:
suite.Update(roll)

print 'After more rolls'
print('After more rolls')
suite.Print()


Expand Down
6 changes: 4 additions & 2 deletions dice_soln.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
License: GNU GPLv3 http://www.gnu.org/licenses/gpl.html
"""

from __future__ import print_function, division

from thinkbayes import Suite


Expand All @@ -27,13 +29,13 @@ def main():
suite = Dice([4, 6, 8, 12, 20])

suite.Update(6)
print 'After one 6'
print('After one 6')
suite.Print()

for roll in [8, 7, 7, 5, 4]:
suite.Update(roll)

print 'After more rolls'
print('After more rolls')
suite.Print()


Expand Down
15 changes: 9 additions & 6 deletions euro.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
License: GNU GPLv3 http://www.gnu.org/licenses/gpl.html
"""

from __future__ import print_function, division

import thinkbayes
import thinkplot


"""This file contains a partial solution to a problem from
MacKay, "Information Theory, Inference, and Learning Algorithms."
Expand All @@ -22,10 +28,6 @@
"""

import thinkbayes
import thinkplot


class Euro(thinkbayes.Suite):

def Likelihood(self, data, hypo):
Expand All @@ -43,9 +45,10 @@ def main():

suite.Update('H')

thinkplot.Pmf(suite)
thinkplot.Pdf(suite)
thinkplot.Show(xlabel='x',
ylabel='Probability')
ylabel='Probability',
legend=False)


if __name__ == '__main__':
Expand Down
15 changes: 9 additions & 6 deletions euro2.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
License: GNU GPLv3 http://www.gnu.org/licenses/gpl.html
"""

from __future__ import print_function, division

import thinkbayes


"""This file contains a partial solution to a problem from
MacKay, "Information Theory, Inference, and Learning Algorithms."
Expand All @@ -22,16 +27,14 @@
"""

import thinkbayes


class Euro(thinkbayes.Suite):

def Likelihood(self, data, hypo):
"""Computes the likelihood of the data under the hypothesis.
hypo: integer value of x, the probability of heads (0-100)
data: tuple (#heads, #tails)
hypo: integer value of x, the probability of heads (0-100)
"""
x = hypo / 100.0
heads, tails = data
Expand Down Expand Up @@ -72,13 +75,13 @@ def main():
data = 140, 110

like_bias = AverageLikelihood(bias, data)
print 'like_bias', like_bias
print('like_bias', like_bias)

like_fair = AverageLikelihood(fair, data)
print 'like_fair', like_fair
print('like_fair', like_fair)

ratio = like_bias / like_fair
print 'Bayes factor', ratio
print('Bayes factor', ratio)


if __name__ == '__main__':
Expand Down
24 changes: 13 additions & 11 deletions euro2_soln.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
License: GNU GPLv3 http://www.gnu.org/licenses/gpl.html
"""

from __future__ import print_function, division

import thinkbayes
import thinkplot


"""This file contains a partial solution to a problem from
MacKay, "Information Theory, Inference, and Learning Algorithms."
Expand All @@ -22,17 +28,13 @@
"""

import thinkbayes
import thinkplot


class Euro(thinkbayes.Suite):

def Likelihood(self, hypo, data):
def Likelihood(self, data, hypo):
"""Computes the likelihood of the data under the hypothesis.
hypo: integer value of x, the probability of heads (0-100)
data: tuple (#heads, #tails)
hypo: integer value of x, the probability of heads (0-100)
"""
x = hypo / 100.0
heads, tails = data
Expand All @@ -53,7 +55,7 @@ def AverageLikelihood(suite, data):
total = 0

for hypo, prob in suite.Items():
like = suite.Likelihood(hypo, data)
like = suite.Likelihood(data, hypo)
total += prob * like

return total
Expand All @@ -70,20 +72,20 @@ def main():
bias.Set(x, 100-x)
bias.Normalize()

thinkplot.Pmf(bias)
thinkplot.Pdf(bias)
thinkplot.Show()

# notice that we've changed the representation of the data
data = 140, 110

like_bias = AverageLikelihood(bias, data)
print 'like_bias', like_bias
print('like_bias', like_bias)

like_fair = AverageLikelihood(fair, data)
print 'like_fair', like_fair
print('like_fair', like_fair)

ratio = like_bias / like_fair
print 'Bayes factor', ratio
print('Bayes factor', ratio)


if __name__ == '__main__':
Expand Down
15 changes: 9 additions & 6 deletions euro_soln.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
License: GNU GPLv3 http://www.gnu.org/licenses/gpl.html
"""

from __future__ import print_function, division

import thinkbayes
import thinkplot


"""This file contains a partial solution to a problem from
MacKay, "Information Theory, Inference, and Learning Algorithms."
Expand All @@ -22,10 +28,6 @@
"""

import thinkbayes
import thinkplot


class Euro(thinkbayes.Suite):

def Likelihood(self, data, hypo):
Expand All @@ -46,9 +48,10 @@ def main():

suite.Update('H')

thinkplot.Pmf(suite)
thinkplot.Pdf(suite)
thinkplot.Show(xlabel='x',
ylabel='Probability')
ylabel='Probability',
legend=False)


if __name__ == '__main__':
Expand Down
4 changes: 3 additions & 1 deletion install_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
License: GNU GPLv3 http://www.gnu.org/licenses/gpl.html
"""

from __future__ import print_function, division

import math
import numpy

Expand All @@ -20,7 +22,7 @@ def RenderPdf(mu, sigma, n=101):
n: number of places to evaluate the PDF
"""
xs = numpy.linspace(mu-4*sigma, mu+4*sigma, n)
ys = [thinkbayes.EvalGaussianPdf(x, mu, sigma) for x in xs]
ys = [thinkbayes.EvalNormalPdf(x, mu, sigma) for x in xs]
return xs, ys


Expand Down
16 changes: 8 additions & 8 deletions lincoln.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
License: GNU GPLv3 http://www.gnu.org/licenses/gpl.html
"""

from __future__ import print_function, division

import thinkbayes
import thinkplot

Expand All @@ -24,13 +26,13 @@
there are at least 20 bugs, and if you have supreme confidence in your
tester, you may suppose there are around 20 bugs. But maybe your
tester isn't very good. Maybe there are hundreds of bugs. How can you
have any idea how many bugs there are? Theres no way to know with one
have any idea how many bugs there are? There's no way to know with one
tester. But if you have two testers, you can get a good idea, even if
you dont know how skilled the testers are."
you don't know how skilled the testers are."
Then he presents the Lincoln index, an estimator "described by
Then he presents the Lincoln index, an estimator "described by
Frederick Charles Lincoln in 1930," where Wikpedia's use of
"described" is a hint that the index is another example of Stigler's
"described" is a hint that the index is another example of Stigler's
law of eponymy.
"Suppose two testers independently search for bugs. Let k1 be the
Expand Down Expand Up @@ -98,9 +100,7 @@ def Likelihood(self, data, hypo):
return part1 * part2



def main():

data = 20, 15, 3
probs = numpy.linspace(0, 1, 101)
hypos = []
Expand All @@ -120,8 +120,8 @@ def main():
ylabel='PMF',
formats=['pdf', 'png'])

print n_marginal.Mean()
print n_marginal.MaximumLikelihood()
print(n_marginal.Mean())
print(n_marginal.MaximumLikelihood())


if __name__ == '__main__':
Expand Down
18 changes: 11 additions & 7 deletions sat.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
License: GNU GPLv3 http://www.gnu.org/licenses/gpl.html
"""

from __future__ import print_function, division

import csv

import thinkbayes
Expand Down Expand Up @@ -168,23 +170,25 @@ def PmfProbGreater(pmf1, pmf2):


def main():

exam = Exam()

alice = Sat(exam)
alice.name = 'alice'
alice.label = 'alice'
alice.Update(780)

bob = Sat(exam)
bob.name = 'bob'
bob.label = 'bob'
bob.Update(760)

print 'Prob Alice is "smarter":', PmfProbGreater(alice, bob)
print 'Prob Bob is "smarter":', PmfProbGreater(bob, alice)
print('Prob Alice is "smarter":', PmfProbGreater(alice, bob))
print('Prob Bob is "smarter":', PmfProbGreater(bob, alice))

thinkplot.Pmfs([alice, bob])
thinkplot.PrePlot(2)
thinkplot.Pdfs([alice, bob])
thinkplot.Show(xlabel='x',
ylabel='Probability')
ylabel='Probability',
loc='upper left',
xlim=[0.7, 1.02])


if __name__ == '__main__':
Expand Down
18 changes: 11 additions & 7 deletions sat_soln.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
License: GNU GPLv3 http://www.gnu.org/licenses/gpl.html
"""

from __future__ import print_function, division

import csv

import thinkbayes
Expand Down Expand Up @@ -168,23 +170,25 @@ def PmfProbGreater(pmf1, pmf2):


def main():

exam = Exam()

alice = Sat(exam)
alice.name = 'alice'
alice.label = 'alice'
alice.Update(780)

bob = Sat(exam)
bob.name = 'bob'
bob.label = 'bob'
bob.Update(760)

print 'Prob Alice is "smarter":', PmfProbGreater(alice, bob)
print 'Prob Bob is "smarter":', PmfProbGreater(bob, alice)
print('Prob Alice is "smarter":', PmfProbGreater(alice, bob))
print('Prob Bob is "smarter":', PmfProbGreater(bob, alice))

thinkplot.Pmfs([alice, bob])
thinkplot.PrePlot(2)
thinkplot.Pdfs([alice, bob])
thinkplot.Show(xlabel='x',
ylabel='Probability')
ylabel='Probability',
loc='upper left',
xlim=[0.7, 1.02])


if __name__ == '__main__':
Expand Down
Loading

0 comments on commit 4ea0c59

Please sign in to comment.