3
$\begingroup$

My overall goal is to work through the "Bayesian Methods for Hackers" book. So far I understand how to do simple things with pymc (like determining the parameters for a linear model and for a gaussian - e.g beginner stuff). I feel like I understand the example in the first chapter of the above Bayesian Methods for Hackers book as well (chat frequency trends). To test myself I've been trying to use pymc to work through "Thinking Bayes". I'm stuck on the following "M&M" problem: http://greenteapress.com/thinkbayes/html/thinkbayes002.html#toc12

I can solve this problem on paper and with a short snippet of python, but I'm at a loss how to think of this from a pymc direction.

Any help pointing me towards how to think about this would be appreciated.

$\endgroup$

1 Answer 1

9
$\begingroup$

Please use with PyMC 2.3.2

It looks like there is a bug in 2.3.4 (the most recent version) that is causing the wrong inference. This took me a while to discover, but it was solved when I downgraded to PyMC 2.3.2.

Model:

import pymc as pm

p = [
 #brown, yellow, red, green, orange, tan, blue
 [.3,  .2,  .2,  .1, .1,  .1, .0 ], # 1994 bag
 [.13, .14, .13, .2, .16, .0, .24]  # 1996 bag
]


bag = pm.Categorical('which_bag', [0.5, 0.5]) # prior on bag selected

@pm.deterministic
def first_bag_selection(p=p, bag=bag):
    return p[bag]

@pm.deterministic
def second_bag_selection(p=p, bag=bag):
    return p[1-bag]

# observe yellow in bag 1
obs_1 = pm.Categorical('bag_1', first_bag_selection, value=1, observed=True)

#observe green in bag 2
obs_2 = pm.Categorical('bag_2', second_bag_selection, value=3, observed=True)

Inference

mcmc = pm.MCMC([bag, p, first_bag_selection, second_bag_selection, obs_1, obs_2])
mcmc.sample(15000,3000)

bag_trace = mcmc.trace('which_bag')[:]


# what is the probability the bag chosen is from 1996?
print (bag_trace == 1).sum()*1./bag_trace.shape[0]
# should be about .26

Let me know if there are any issues!

$\endgroup$
2
  • $\begingroup$ Don't have enough karma (or whatever) to upvote, but that's exactly what I was looking for. Now to spend some time absorbing this. Awesome book by the way! $\endgroup$
    – qhfgva
    Apr 16, 2015 at 16:38
  • 1
    $\begingroup$ One change I made to get it to work was to change "data" to "bag_trace" in the final line. $\endgroup$
    – qhfgva
    Apr 16, 2015 at 18:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.