# Slow pymc sampling using the introductory example

**URL:** <https://discourse.pymc.io/t/slow-pymc-sampling-using-the-introductory-example/12122>\
**Category:** v5\
**Created:** [May 14, 2023, 6:12am UTC](https://discourse.pymc.io/t/slow-pymc-sampling-using-the-introductory-example/12122 "2023-05-14T06:12:44Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![EKtheSage](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/ekthesage/32/6556_2.png) [@EKtheSage](https://discourse.pymc.io/u/EKtheSage)\
**Post date:** [May 14, 2023, 6:12am UTC](https://discourse.pymc.io/t/slow-pymc-sampling-using-the-introductory-example/12122/1 "2023-05-14T06:12:44Z")

</div>

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:

```auto
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](https://canada1.discourse-cdn.com/flex036/uploads/pymc3/original/2X/7/79d3604731b26ddd9fe1f7b4fdfcbe39e8086ebb.png)

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

---

<div class="post-metadata">

**Author:** ![cluhmann](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/cluhmann/32/3083_2.png) [@cluhmann](https://discourse.pymc.io/u/cluhmann)\
**Post date:** [May 14, 2023, 8:18pm UTC](https://discourse.pymc.io/t/slow-pymc-sampling-using-the-introductory-example/12122/2 "2023-05-14T20:18:11Z")

</div>

Welcome!

I would begin by following the official installation instructions found [here](https://www.pymc.io/projects/docs/en/latest/installation.html). Are there any warnings or other messages upon importing PyMC or when running your code?

---

<div class="post-metadata">

**Author:** ![twiecki](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/twiecki/32/6930_2.png) [@twiecki](https://discourse.pymc.io/u/twiecki)\
**Post date:** [May 14, 2023, 8:54pm UTC](https://discourse.pymc.io/t/slow-pymc-sampling-using-the-introductory-example/12122/3 "2023-05-14T20:54:07Z")

</div>

Also, which OS is this?

---

<div class="post-metadata">

**Author:** ![EKtheSage](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/ekthesage/32/6556_2.png) [@EKtheSage](https://discourse.pymc.io/u/EKtheSage)\
**Post date:** [May 14, 2023, 11:42pm UTC](https://discourse.pymc.io/t/slow-pymc-sampling-using-the-introductory-example/12122/4 "2023-05-14T23:42:56Z")

</div>

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

```auto
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](https://github.com/pymc-devs/pytensor/issues/234#issuecomment-1455075780)

My OS is Windows 11.

---

<div class="post-metadata">

**Author:** ![cluhmann](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/cluhmann/32/3083_2.png) [@cluhmann](https://discourse.pymc.io/u/cluhmann)\
**Post date:** [May 15, 2023, 12:09am UTC](https://discourse.pymc.io/t/slow-pymc-sampling-using-the-introductory-example/12122/5 "2023-05-15T00:09:46Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![EKtheSage](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/ekthesage/32/6556_2.png) [@EKtheSage](https://discourse.pymc.io/u/EKtheSage)\
**Post date:** [May 15, 2023, 12:29am UTC](https://discourse.pymc.io/t/slow-pymc-sampling-using-the-introductory-example/12122/6 "2023-05-15T00:29:11Z")

</div>

I am running this in VS Code Notebook cells.

---

<div class="post-metadata">

**Author:** ![EKtheSage](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/ekthesage/32/6556_2.png) [@EKtheSage](https://discourse.pymc.io/u/EKtheSage)\
**Post date:** [May 15, 2023, 12:34am UTC](https://discourse.pymc.io/t/slow-pymc-sampling-using-the-introductory-example/12122/7 "2023-05-15T00:34:53Z")

</div>

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

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/pymc3/original/2X/f/f272466759b3441f8a0d3b170489483d96c2c854.png)

---

<div class="post-metadata">

**Author:** ![cluhmann](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/cluhmann/32/3083_2.png) [@cluhmann](https://discourse.pymc.io/u/cluhmann)\
**Post date:** [May 15, 2023, 12:37am UTC](https://discourse.pymc.io/t/slow-pymc-sampling-using-the-introductory-example/12122/8 "2023-05-15T00:37:18Z")

</div>

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:

```python
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()

```

---

<div class="post-metadata">

**Author:** ![EKtheSage](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/ekthesage/32/6556_2.png) [@EKtheSage](https://discourse.pymc.io/u/EKtheSage)\
**Post date:** [May 15, 2023, 12:41am UTC](https://discourse.pymc.io/t/slow-pymc-sampling-using-the-introductory-example/12122/9 "2023-05-15T00:41:12Z")

</div>

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!
