0

I don't understand why MAP is giving error where as MCMC works fine in the same scenario? I am writing below the relevant part of code.

tau = Uniform('tau', lower=0.01, upper=5, doc='tau') rv = [ Multinomial("rv"+str(i), count[i], prob_distribution[i], value = data[i], observed =True) for i in xrange(0, len(count)) ] M = MAP([rv, tau]) M.fit() tau_hat = M.tau.value()

Error: AttributeError: 'MAP' object has no attribute 'tau' (for the last line M.tau.value())

On the other hand if I use MCMC in place of MAP, it works fine :

m = MCMC([tau, rv]) m.sample(iter = 500) print m.trace('tau')

I want the point estimate of tau at which posterior has max probability and compare it with Bayesian prediction (for which I use MCMC)

Some information about variables:

prob_distribution is a deterministic function which, given tau and some other information returns a list of predictive probability distribution for each game. I have around 200 games, so prob_distribution is a list of 200 lists (each containing probability distribution over actions for that game eg [0.4, 0.4, 0.2]) Similarly count is a list of 200 numbers, count[i] denotes number of times ith game was played. data[i] is the observed information for ith game eg if data[i] = [10 10 6], count[i] will be 26

Additional note

If I include the following line:

model = Model([rv, tau])

then Irrsepective whether I use

M = MAP(model) or m = MCMC(model)

this gives an error as follows:

TypeError: hasattr(): attribute name must be string

Can somebody please explain what is going on?

turing
  • 577
  • 1
  • 4
  • 12

1 Answers1

1

In PyMC2, the MCMC object exposes model nodes as instance variables but the MAP object does not. It could be worth filing a feature request with the pymc-devs. You can get the MAP value of tau by replacing your final line with

tau_hat = tau.value

A complete, minimal example of this workaround is the following:

from pymc import *

count = [10, 10]
prob_distribution = [[.5, .5], [.1, .2, .7]]
data = [[2, 8], [2, 3, 5]]

tau = Uniform('tau', lower=0.01, upper=5)
rv = [ Multinomial("rv"+str(i), count[i], prob_distribution[i], value = data[i], observed=True) for i in xrange(0, len(count)) ]
M = MAP([rv, tau])
M.fit()
tau_hat = tau.value

Note that tau is not informed by the data in this model as written...

Abraham D Flaxman
  • 2,969
  • 21
  • 39
  • I want that tau should have that value which maximizes its posterior probability and hence I am using MAP but in your code, if I print the value of tau before and after using MAP, it's not changing. If it can't be done using MAP, is there some other way to do so ?? – turing Jun 05 '14 at 16:37
  • This is an issue with the model you have constructed, not the MAP optimization. – Abraham D Flaxman Jun 09 '14 at 18:46
  • can ypu please suggest any better model wherein I can use MAP optimization ? Agai , kindly provide a small working example, thanks – turing Jun 09 '14 at 19:11
  • Unfortunately, I have no idea what you are trying to model, so no suggestions as to what you should change. Good luck! – Abraham D Flaxman Jun 10 '14 at 22:15