# Pymc3 sampling processes

**URL:** <https://discourse.pymc.io/t/pymc3-sampling-processes/6432>\
**Category:** Questions\
**Created:** [December 15, 2020, 2:49pm UTC](https://discourse.pymc.io/t/pymc3-sampling-processes/6432 "2020-12-15T14:49:37Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![BjornHartmann](https://avatars.discourse-cdn.com/v4/letter/b/a87d85/32.png) [@BjornHartmann](https://discourse.pymc.io/u/BjornHartmann)\
**Post date:** [December 15, 2020, 2:49pm UTC](https://discourse.pymc.io/t/pymc3-sampling-processes/6432/1 "2020-12-15T14:49:37Z")

</div>

Simple question for you:

In a sampling process, where no inference is being done, is pymc3 simply taking one sample from each distribution 500 times for a draws=500 process?

In other words is:

```
    with pm.Model() as model:
        a = pm.Normal('a')
        b = pm.Normal('b')
        c = pm.Deterministic('c', a+b)
        pm.sample(draws=500)

```

equivalent to

```
a = np.random.normal(size=500)
b = np.random.normal(size=500)
c=[]
for i in a:
    c.append(random.sample(list(a),1)+random.sample(list(a),1)
```

---

<div class="post-metadata">

**Author:** ![junpenglao](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/junpenglao/32/8_2.png) [@junpenglao](https://discourse.pymc.io/u/junpenglao)\
**Post date:** [December 15, 2020, 5:52pm UTC](https://discourse.pymc.io/t/pymc3-sampling-processes/6432/2 "2020-12-15T17:52:00Z")

</div>

Other than the fact that PyMC3 use MCMC to draw samples when `pm.sample` is called, the model code will roughly equivalent to

```python
a = np.random.normal(size=500)
b = np.random.normal(size=500)
c = a + b # <= deterministic

```

---

<div class="post-metadata">

**Author:** ![BjornHartmann](https://avatars.discourse-cdn.com/v4/letter/b/a87d85/32.png) [@BjornHartmann](https://discourse.pymc.io/u/BjornHartmann)\
**Post date:** [December 17, 2020, 11:41am UTC](https://discourse.pymc.io/t/pymc3-sampling-processes/6432/3 "2020-12-17T11:41:02Z")

</div>

awesome, thank you for getting back to me.  
Follow-up question:  
If i do such a simple sampling process with pymc3, my complete runtime including compilation is about 10 seconds, while with the np/scipy case it is done in 0.5 seconds. Is there a way to speed up pymc3 for easy cases like these? Is pymc3 not the right tool for such easy naive monte carlo processes?

---

<div class="post-metadata">

**Author:** ![junpenglao](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/junpenglao/32/8_2.png) [@junpenglao](https://discourse.pymc.io/u/junpenglao)\
**Post date:** [December 17, 2020, 12:52pm UTC](https://discourse.pymc.io/t/pymc3-sampling-processes/6432/4 "2020-12-17T12:52:53Z")

</div>

Usually, for simple forward simulation, you should use `pm.sample_prior_predictive` instead.

---

<div class="post-metadata">

**Author:** ![BjornHartmann](https://avatars.discourse-cdn.com/v4/letter/b/a87d85/32.png) [@BjornHartmann](https://discourse.pymc.io/u/BjornHartmann)\
**Post date:** [December 17, 2020, 4:59pm UTC](https://discourse.pymc.io/t/pymc3-sampling-processes/6432/5 "2020-12-17T16:59:13Z")

</div>

thank you once again for getting back to me.

I tried the pm.sample\_prior\_predictive as well, but i did not see a significant increase in computation-time.

So in order to understand the computational advantages with pymc3 over numpy, i implemented the Metropolis-sampling described in [https://twiecki.io/blog/2015/11/10/mcmc-sampling/](https://twiecki.io/blog/2015/11/10/mcmc-sampling/)  
and compared that to the same model set up in pymc3.

For small number of draws, I saw that the numpy-way was significantly faster, while as for bigger number of draws/samples pymc3 outperformed numpy, as expected.

Can i by this conclude that for my simple “forward simulation”, pymc3 is too much of a powerful tool for a job simple enough for numpy to handle, and that pymc3 is slower for such jobs?

---

<div class="post-metadata">

**Author:** ![junpenglao](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/junpenglao/32/8_2.png) [@junpenglao](https://discourse.pymc.io/u/junpenglao)\
**Post date:** [December 18, 2020, 9:14am UTC](https://discourse.pymc.io/t/pymc3-sampling-processes/6432/6 "2020-12-18T09:14:40Z")

</div>

Depending on what you want to do - the nice thing of using a full PPL is you can write the forward sampling and reverse inference just once, instead of writing the forward simulation and then later on write the model again.
