pymc-devs / pymc2

THIS IS THE **OLD** PYMC PROJECT (VERSION 2). PLEASE USE PYMC INSTEAD:
http://pymc-devs.github.com/pymc/
Other
877 stars 229 forks source link

Question about multivariate proposals #38

Closed pgrinaway closed 9 years ago

pgrinaway commented 9 years ago

Apologies for any ignorance here, but I'm confused as to how, exactly, PyMC samples a multivariate model.

If I request (for example) 10 samples, a profiler indicates that the likelihood is evaluated ~140 times (approximately n_parameters * n_iterations). Now, I know there was a question and answer here, but there are a still a few confusing points.

The documentation suggests that

If a Metropolis step method handles an array-valued variable, it proposes all elements independently but simultaneously. That is, it decides whether to accept or reject all elements together but it does not attempt to take the posterior correlation between elements into account.

Correct me if I'm wrong, but this sounds like drawing n_parameters samples from a multivariate distribution with a diagonal covariance matrix, then proposing them all at once. Why would this require so many likelihood evaluations?

Looking at the docstrings in the code, the Metropolis StepMethod seems to:

Applies the one-at-a-time Metropolis-Hastings algorithm to the Stochastic over which self has jurisdiction.

Watching the verbose output, this makes sense, as it seems to be updating only one parameter at a time. If this is the case, though, shouldn't I get a sample for each accept/reject step? That is, shouldn't I then have n_parameters * n_iterations samples? Or am I mistaken as to how one at a time Metropolis works?

Thanks so much for all your help, and apologies for any ignorant/stupid questions.

fonnesbeck commented 9 years ago

Multivariate nodes are updated as a single unit. Metropolis is one-at-a-time in terms of the nodes in the graph, but if a node is multivariate, all the elements are sampled together. In other words, proposals are drawn of size k, and accepted/rejected simultaneously. Have a peek at the propose method.

pgrinaway commented 9 years ago

Sorry, perhaps I misspoke. I suppose the pressing question is this:

I have a model with array-valued parameters, and so PyMC samples using one-at-a-time Metropolis. What happens to the individual accept/rejects for each parameter? Should those not also be samples?

fonnesbeck commented 9 years ago

If I am not still misunderstanding: One Metropolis step entails a sweep through all the parameters. At the end of that sweep, each parameter will have one sample for that step, not n_parameters samples.

At the end of the MCMC run, there will be n_parameters traces, each containing n_iterations samples.

pgrinaway commented 9 years ago

Ok, that explains it. Thanks!