# Help setting up pymc to solve this problem relating to distribution of colors in M&M's

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.

## 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]