Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to sample multiple chains in PyMC3

Tags:

pymc

pymc3

I'm trying to sample multiple chains in PyMC3. In PyMC2 I would do something like this:

for i in range(N):
    model.sample(iter=iter, burn=burn, thin = thin)

How should I do the same thing in PyMC3? I saw there is a 'njobs' argument in the 'sample' method, but it throws an error when I set a value for it. I want to use sampled chains to get 'pymc.gelman_rubin' output.

like image 359
Amir Dezfouli Avatar asked Dec 12 '14 15:12

Amir Dezfouli


People also ask

What is a chain in PyMC3?

A chain is a single run of MCMC. So if you have six 2-d parameters in your model and ask for 1000 samples, you will get six 2x1000 arrays for each chain. When running MCMC, it is a best practice to use multiple chains, as they can help diagnose problems.

What is tune in PyMC3?

A design goal of PyMC3 is to let the user worry about statistical modelling, and not worry about inference, and tuning attempts to automatically set some of the dozens of knobs available in modern MCMC methods.

What is PyMC3 used for?

PyMC3 is a probabilistic programming package for Python that allows users to fit Bayesian models using a variety of numerical methods, most notably Markov chain Monte Carlo (MCMC) and variational inference (VI). Its flexibility and extensibility make it applicable to a large suite of problems.

Is PyMC3 open source?

PyMC3 is the current version of the PyMC open source probabilistic programming framework for Python, having been forked over 1,300 times and starred over 5,400 times on GitHub.


1 Answers

Better is to use njobs to run chains in parallel:

#!/usr/bin/env python3

import pymc3 as pm
import numpy as np

from pymc3.backends.base import merge_traces

xobs = 4 + np.random.randn(20)

model = pm.Model()
with model:
    mu = pm.Normal('mu', mu=0, sd=20)
    x = pm.Normal('x', mu=mu, sd=1., observed=xobs)
    step = pm.NUTS()

with model:
    trace = pm.sample(1000, step, njobs=2)
like image 189
John Salvatier Avatar answered Sep 21 '22 11:09

John Salvatier