Skip to content

Commit

Permalink
Addining py files
Browse files Browse the repository at this point in the history
  • Loading branch information
AllenDowney committed Mar 20, 2014
1 parent fa655a2 commit 78f7fea
Show file tree
Hide file tree
Showing 13 changed files with 3,225 additions and 0 deletions.
39 changes: 39 additions & 0 deletions dice.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
"""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 thinkbayes import Suite


class Dice(Suite):
"""Represents hypotheses about which die was rolled."""

def Likelihood(self, data, hypo):
"""Computes the likelihood of the data under the hypothesis.
hypo: integer number of sides on the die
data: integer die roll
"""
# write this method
return 1


def main():
suite = Dice([4, 6, 8, 12, 20])

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

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

print 'After more rolls'
suite.Print()


if __name__ == '__main__':
main()
41 changes: 41 additions & 0 deletions dice_soln.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
"""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 thinkbayes import Suite


class Dice(Suite):
"""Represents hypotheses about which die was rolled."""

def Likelihood(self, data, hypo):
"""Computes the likelihood of the data under the hypothesis.
hypo: integer number of sides on the die
data: integer die roll
"""
if hypo < data:
return 0
else:
return 1.0/hypo


def main():
suite = Dice([4, 6, 8, 12, 20])

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

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

print 'After more rolls'
suite.Print()


if __name__ == '__main__':
main()
52 changes: 52 additions & 0 deletions euro.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
"""This file contains code used in "Think Stats",
by Allen B. Downey, available from greenteapress.com
Copyright 2013 Allen B. Downey
License: GNU GPLv3 http://www.gnu.org/licenses/gpl.html
"""

"""This file contains a partial solution to a problem from
MacKay, "Information Theory, Inference, and Learning Algorithms."
Exercise 3.15 (page 50): A statistical statement appeared in
"The Guardian" on Friday January 4, 2002:
When spun on edge 250 times, a Belgian one-euro coin came
up heads 140 times and tails 110. 'It looks very suspicious
to me,' said Barry Blight, a statistics lecturer at the London
School of Economics. 'If the coin weere unbiased, the chance of
getting a result as extreme as that would be less than 7%.'
MacKay asks, "But do these data give evidence that the coin is biased
rather than fair?"
"""

import thinkbayes
import thinkplot


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: string 'H' or 'T'
"""
# fill this in!
return 1


def main():
suite = Euro(range(0, 101))

suite.Update('H')

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


if __name__ == '__main__':
main()
85 changes: 85 additions & 0 deletions euro2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
"""This file contains code used in "Think Stats",
by Allen B. Downey, available from greenteapress.com
Copyright 2013 Allen B. Downey
License: GNU GPLv3 http://www.gnu.org/licenses/gpl.html
"""

"""This file contains a partial solution to a problem from
MacKay, "Information Theory, Inference, and Learning Algorithms."
Exercise 3.15 (page 50): A statistical statement appeared in
"The Guardian" on Friday January 4, 2002:
When spun on edge 250 times, a Belgian one-euro coin came
up heads 140 times and tails 110. 'It looks very suspicious
to me,' said Barry Blight, a statistics lecturer at the London
School of Economics. 'If the coin weere unbiased, the chance of
getting a result as extreme as that would be less than 7%.'
MacKay asks, "But do these data give evidence that the coin is biased
rather than fair?"
"""

import thinkbayes


class Euro(thinkbayes.Suite):

def Likelihood(self, hypo, data):
"""Computes the likelihood of the data under the hypothesis.
hypo: integer value of x, the probability of heads (0-100)
data: tuple (#heads, #tails)
"""
x = hypo / 100.0
heads, tails = data
like = x**heads * (1-x)**tails
return like


def AverageLikelihood(suite, data):
"""Computes the average likelihood over all hypothesis in suite.
Args:
suite: Suite of hypotheses
data: some representation of the observed data
Returns:
float
"""
total = 0

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

return total


def main():
fair = Euro()
fair.Set(50, 1)

bias = Euro()
for x in range(0, 101):
if x != 50:
bias.Set(x, 1)
bias.Normalize()

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

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

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

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


if __name__ == '__main__':
main()
90 changes: 90 additions & 0 deletions euro2_soln.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
"""This file contains code used in "Think Stats",
by Allen B. Downey, available from greenteapress.com
Copyright 2013 Allen B. Downey
License: GNU GPLv3 http://www.gnu.org/licenses/gpl.html
"""

"""This file contains a partial solution to a problem from
MacKay, "Information Theory, Inference, and Learning Algorithms."
Exercise 3.15 (page 50): A statistical statement appeared in
"The Guardian" on Friday January 4, 2002:
When spun on edge 250 times, a Belgian one-euro coin came
up heads 140 times and tails 110. 'It looks very suspicious
to me,' said Barry Blight, a statistics lecturer at the London
School of Economics. 'If the coin weere unbiased, the chance of
getting a result as extreme as that would be less than 7%.'
MacKay asks, "But do these data give evidence that the coin is biased
rather than fair?"
"""

import thinkbayes
import thinkplot


class Euro(thinkbayes.Suite):

def Likelihood(self, hypo, data):
"""Computes the likelihood of the data under the hypothesis.
hypo: integer value of x, the probability of heads (0-100)
data: tuple (#heads, #tails)
"""
x = hypo / 100.0
heads, tails = data
like = x**heads * (1-x)**tails
return like


def AverageLikelihood(suite, data):
"""Computes the average likelihood over all hypothesis in suite.
Args:
suite: Suite of hypotheses
data: some representation of the observed data
Returns:
float
"""
total = 0

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

return total


def main():
fair = Euro()
fair.Set(50, 1)

bias = Euro()
for x in range(0, 51):
bias.Set(x, x)
for x in range(51, 101):
bias.Set(x, 100-x)
bias.Normalize()

thinkplot.Pmf(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

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

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


if __name__ == '__main__':
main()
55 changes: 55 additions & 0 deletions euro_soln.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
"""This file contains code used in "Think Stats",
by Allen B. Downey, available from greenteapress.com
Copyright 2013 Allen B. Downey
License: GNU GPLv3 http://www.gnu.org/licenses/gpl.html
"""

"""This file contains a partial solution to a problem from
MacKay, "Information Theory, Inference, and Learning Algorithms."
Exercise 3.15 (page 50): A statistical statement appeared in
"The Guardian" on Friday January 4, 2002:
When spun on edge 250 times, a Belgian one-euro coin came
up heads 140 times and tails 110. 'It looks very suspicious
to me,' said Barry Blight, a statistics lecturer at the London
School of Economics. 'If the coin weere unbiased, the chance of
getting a result as extreme as that would be less than 7%.'
MacKay asks, "But do these data give evidence that the coin is biased
rather than fair?"
"""

import thinkbayes
import thinkplot


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: string 'H' or 'T'
"""
x = hypo / 100.0
if data == 'H':
return x
else:
return 1-x


def main():
suite = Euro(range(0, 101))

suite.Update('H')

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


if __name__ == '__main__':
main()
Loading

0 comments on commit 78f7fea

Please sign in to comment.