Pymc3 sampling processes

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)

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

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

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?

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

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/
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?

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.

3 Likes