Converting WinBUGS model

177 views
Skip to first unread message

Saeed Siam

unread,
Jun 20, 2010, 6:27:25 PM6/20/10
to PyMC
Hi,

I am new to PyMC. For starting, I was trying to convert WinBUGS <a
href="http://users.aims.ac.za/~mackay/BUGS/Examples/Eyes.html"> Eyes
example </a> to PyMC.

I have assumed that Categorical() in the PyMC is equivalent the
WinBUGS' dcat(), So for the following WinBUGS model,

model
{
for( i in 1 : N ) {
y[i] ~ dnorm(mu[i], tau)
mu[i] <- lambda[T[i]]
T[i] ~ dcat(P[])
}
P[1:2] ~ ddirch(alpha[])
theta ~ dnorm(0.0, 1.0E-6)I(0.0, )
lambda[2] <- lambda[1] + theta
lambda[1] ~ dnorm(0.0, 1.0E-6)
tau ~ dgamma(0.001, 0.001) sigma <- 1 / sqrt(tau)
}

I have done similar to <a href="http://pastebin.com/GXwLs5bC">this </
a>. But it raises pymc.Node.ZeroProbability exception with the
message "Stochastic T_1's value is outside its support,
or it forbids its parents' current values."

I would really appreciate if you can point out what I am doing wrong.

regards,
Saeed

John Salvatier

unread,
Jun 21, 2010, 3:08:53 AM6/21/10
to py...@googlegroups.com
I have not gone through all your code yet, but the main difference between how you would code a BUGS model and a PyMC model relevent to your model is that in PyMC is that you should vectorize your model. Instead of looping through your variables, you should create arrays of variables and give those as arguments to other parts of your model.

For example, instead of your second loop, you should do something like:

    T = pymc.Categorical('T', p=p1,p2)
    mu = pymc.Deterministic(eval=lambda t=T,lambda_0=lambdas[0], lambda_1=lambdas[1]: t < 0.5 & lambda_0 | lambda_1,
            name='mu')
 
    Y = pymc.Binomial('Y', n=mu, p=tau, value=y_data, observed=True)


(note that numpy arrays sadly do not work with the 'and' and 'or' operators. You must use & and | or use the functions.
PyMC *should* be able to deal with how you have written you model, but it is not how PyMC is meant to be used so it will be harder to debug your model. PyMC should also normally be able to deal with natural expressions of your variables to create deterministics, but I am not certain whether this works with the & and | operators. For example, you might try,

mu = T < .5 & (lambdas[0]  |  lambdas[1])

However, I am not 100% certain that will work (some of the operators might not be implemented).

I also notice you have the expression "tau**(-1/2)" in your program. Unfortunately Python does integer division for 1/2 so this will evaluate as  -1 (this will be fixed soon), so use tau**(-.5)  or just sigma = tau **-.5.

I am not certain what your specific error is due to.

John


--
You received this message because you are subscribed to the Google Groups "PyMC" group.
To post to this group, send email to py...@googlegroups.com.
To unsubscribe from this group, send email to pymc+uns...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/pymc?hl=en.


John Salvatier

unread,
Jun 21, 2010, 3:15:56 AM6/21/10
to py...@googlegroups.com
That particular error means that the value of parameters of T are invalid for a Categorical distribution. "parents" refers to the parameters of the distribution or deterministic. Can you look at prior.value[0] and prior.value[1]?

John
Reply all
Reply to author
Forward
0 new messages