# Is there any material gently explain how PYMC3 class/function works

**URL:** <https://discourse.pymc.io/t/is-there-any-material-gently-explain-how-pymc3-class-function-works/3497>\
**Category:** Questions\
**Created:** [June 29, 2019, 2:34am UTC](https://discourse.pymc.io/t/is-there-any-material-gently-explain-how-pymc3-class-function-works/3497 "2019-06-29T02:34:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![George](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/george/32/2012_2.png) [@George](https://discourse.pymc.io/u/George)\
**Post date:** [June 29, 2019, 2:34am UTC](https://discourse.pymc.io/t/is-there-any-material-gently-explain-how-pymc3-class-function-works/3497/1 "2019-06-29T02:34:14Z")

</div>

Dear pymc3ers:

I am new to Bayesian Statistics and pymc3. I follow tutorials to build models, but want to understand more about how pm.sample() do. Let’s use a simple example. The following model is from official tutrial:

```
basic_model = pm.Model()

with basic_model:

    # Priors for unknown model parameters
    alpha = pm.Normal('alpha', mu=0, sigma=10)
    beta = pm.Normal('beta', mu=0, sigma=10, shape=2)
    sigma = pm.HalfNormal('sigma', sigma=1)

    # Expected value of outcome
    mu = alpha + beta[0]*X1 + beta[1]*X2

    # Likelihood (sampling distribution) of observations
    Y_obs = pm.Normal('Y_obs', mu=mu, sigma=sigma, observed=Y)

with basic_model:
    # draw 500 posterior samples
    trace = pm.sample(500)

```

Is there an explanation how pm.sample(500) work step by step to actually do sampling the posterior distribution defined by basic\_model? When and how the priors and likelihood are used in the sampling? What is the difference between pm.Normal() with observed=Y argument and that without such argument? How and when to use them?

Thank you very much.

---

<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:** [June 29, 2019, 5:48am UTC](https://discourse.pymc.io/t/is-there-any-material-gently-explain-how-pymc3-class-function-works/3497/2 "2019-06-29T05:48:05Z")

</div>

Many of your question is related to concepts in Bayesian Statistics, I suggest you to follow some book and get some idea of them first. [Statistical Rethinking](https://xcelab.net/rm/statistical-rethinking/) is a good one to start. Otherwise we have a lot of resource in [Learn PyMC & Bayesian modeling — PyMC 5.10.0 documentation](https://docs.pymc.io/learn.html).

As for code logic, you can have a look at the dev guide [https://docs.pymc.io/developer\_guide.html](https://docs.pymc.io/developer_guide.html) (but I suggest you to get conformable to Bayesian Concepts first).

Below are some short answers to your questions:

> Is there an explanation how pm.sample(500) work step by step to actually do sampling the posterior distribution defined by basic\_model?

See [https://docs.pymc.io/developer\_guide.html#logp-and-dlogp](https://docs.pymc.io/developer_guide.html#logp-and-dlogp). Basically, `pymc3.Model` computes everything in the `with` context and generate a logp\_dlogp function, and then a sampler will use that logp\_dlogp function to do sampling

> When and how the priors and likelihood are used in the sampling?

They are combined into a single model likelihood function, see my talk @ PyData Berlin: [GitHub - junpenglao/All-that-likelihood-with-PyMC3](https://github.com/junpenglao/All-that-likelihood-with-PyMC3)

> What is the difference between pm.Normal() with observed=Y argument and that without such argument? How and when to use them?

Without `observed=Y` it is a free parameter that you want to infer it’s posterior distribution. In principle you use it when you want to associate your data (i.e., observation) to some distribution.

---

<div class="post-metadata">

**Author:** ![George](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/george/32/2012_2.png) [@George](https://discourse.pymc.io/u/George)\
**Post date:** [June 29, 2019, 1:15pm UTC](https://discourse.pymc.io/t/is-there-any-material-gently-explain-how-pymc3-class-function-works/3497/3 "2019-06-29T13:15:52Z")

</div>

Thanks for your response. I would like to read the book and codes and get back to you shortly.
