Slow pymc sampling using the introductory example

Hi, I am new to PyMC and am trying the new PyMC v4 with the introductory example, but the sampling is taking forever.

Below is how I installed PyMC:

mamba create -n newenv python=3.10 arviz matplotlib numpy pandas seaborn jupyterlab
mamba activate newenv
mamba install -c conda-forge "pymc>=4"

Then I ran the example code:

import pymc as pm
import numpy as np
import arviz as az

RANDOM_SEED = 8927
rng = np.random.default_rng(RANDOM_SEED)

alpha, sigma = 1, 1
beta = [1, 2.5]

size = 100

X1 = np.random.randn(size)
X2 = np.random.randn(size) * 0.2

Y = alpha + beta[0] * X1 + beta[1] * X2 + rng.normal(size=size) * sigma`

basic_model = pm.Model()

with basic_model:
    alpha = pm.Normal('alpha', mu=0, sigma=10)
    beta = pm.Normal('beta', mu=0, sigma=10, shape=2)
    sigma = pm.HalfNormal('sigma', sigma=1)

    mu = alpha + beta[0] * X1 + beta[1] * X2

    Y_obs = pm.Normal('Y_obs', mu=mu, sigma=sigma, observed=Y)

with basic_model:
    idata = pm.sample(draws=1)

Even with only 1 draw the model have been running for almost an hour:

image

What could have gone wrong in either the package installation or the example code?

Welcome!

I would begin by following the official installation instructions found here. Are there any warnings or other messages upon importing PyMC or when running your code?

Also, which OS is this?

There are no warnings when I import PyMC. The official instructions to install is below:

conda create -c conda-forge -n pymc_env "pymc>=4"
conda activate pymc_env

I basically just replaced conda with mamba based on the suggestion in this github issue

My OS is Windows 11.

Are you running this in a notebook for as a script invoked from the command line (or some other way?)?

I am running this in VS Code Notebook cells.

Actually I found the issue, I was running the code in a quarto (qmd) document, moving the code to jupyter notebook fixed the issue.

1 Like

I might try opening a python at the terminal (e.g., ipython) and importing PyMC there. Notebooks have a nasty habit of hiding warnings and other diagnostics. If nothing comes up there, I might suggest trying to run the following and see what happens:

import pymc as pm
if __name__ == "__main__":
  with pm.Model() as model:
      a = pm.Normal("a")
      b = pm.Normal("b", mu=a, sigma=1, observed=[1,2,3])
      idata = pm.sample()

Thank you, I will try that. I think Notebook is working, and Quarto is not working yet with PyMC. I may open up an issue there instead. Thank you for your help!

1 Like