# Generating population level samples for heirarchical parameters

**URL:** <https://discourse.pymc.io/t/generating-population-level-samples-for-heirarchical-parameters/4383>\
**Category:** Questions\
**Created:** [January 17, 2020, 9:55pm UTC](https://discourse.pymc.io/t/generating-population-level-samples-for-heirarchical-parameters/4383 "2020-01-17T21:55:21Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![dpananos](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/dpananos/32/925_2.png) [@dpananos](https://discourse.pymc.io/u/dpananos)\
**Post date:** [January 17, 2020, 9:55pm UTC](https://discourse.pymc.io/t/generating-population-level-samples-for-heirarchical-parameters/4383/1 "2020-01-17T21:55:21Z")

</div>

In one of my models, I estimate parameters for several subjects through the following way:

```
    log_param_mean = pm.Bound(pm.Normal, lower=0)("log_param_mean", tt.log(3.5), 1)
    rfx = pm.Normal("rfx", 0, 1, shape=len(np.unique(subject_ids)))
    sigma_param = pm.HalfCauchy("sigma_param", 1)

    params = pm.Deterministic("params", tt.exp(log_param + rfx[np.unique(subject_ids)] * sigma_param))

```

If I were to sample the posterior predictive distribution with PyMC3, then I would get posterior samples for each of my subjects. What I’m really interested in is population level samples. So right now, this parameter has a log-normal distribution. How can I use PyMC3 to sample that log-normal distribution and get population level samples for my parameter (either when using `pm.sample` or `pm.sample_posterior_predictive`)?

Could I do something like

> ```
> rng = tt.shared_randomstreams.RandomStreams()
> sampled_param = pm.math.exp(log_param_mean + rng.normal()*sigma_param)
> pop_param = pm.Deterministic('pop_param', sampled_param)
> 
> ```

I hope that is clear. If not, I can gladly add context.

---

<div class="post-metadata">

**Author:** ![AlexAndorra](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/alexandorra/32/9142_2.png) [@AlexAndorra](https://discourse.pymc.io/u/AlexAndorra)\
**Post date:** [January 20, 2020, 10:42am UTC](https://discourse.pymc.io/t/generating-population-level-samples-for-heirarchical-parameters/4383/2 "2020-01-20T10:42:06Z")

</div>

Hi Demetri!  
I’m not sure I completely understand your issue, but yeah your proposal looks good to me. Then you can use `var_names` inside `pm.sample_posterior_predictive` to sample `pop_param` too – instead of only the observed variable.  
Hope this helps 🖖

---

<div class="post-metadata">

**Author:** ![dpananos](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/dpananos/32/925_2.png) [@dpananos](https://discourse.pymc.io/u/dpananos)\
**Post date:** [January 20, 2020, 5:31pm UTC](https://discourse.pymc.io/t/generating-population-level-samples-for-heirarchical-parameters/4383/3 "2020-01-20T17:31:26Z")

</div>

Hey Alex,

I think what I’m trying to do is something similar to Stan’s `generated quantites` block. I want to use my samples to make new quantities. Looking for the safest way to do that.

---

<div class="post-metadata">

**Author:** ![AlexAndorra](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/alexandorra/32/9142_2.png) [@AlexAndorra](https://discourse.pymc.io/u/AlexAndorra)\
**Post date:** [January 21, 2020, 8:59pm UTC](https://discourse.pymc.io/t/generating-population-level-samples-for-heirarchical-parameters/4383/4 "2020-01-21T20:59:31Z")

</div>

Mmmh I’d say `pm.sample_posterior_predictive` would be the way to do that, or just pushing your parameters through the model, but maybe I don’t understand what you’re trying to do 🤔  
Perhaps @ahartikainen would have valuable inputs here?

---

<div class="post-metadata">

**Author:** ![ahartikainen](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/ahartikainen/32/34_2.png) [@ahartikainen](https://discourse.pymc.io/u/ahartikainen)\
**Post date:** [January 21, 2020, 9:08pm UTC](https://discourse.pymc.io/t/generating-population-level-samples-for-heirarchical-parameters/4383/5 "2020-01-21T21:08:47Z")

</div>

Hi,

One way to do this is using e.g. ArviZ and calculating your values after the posterior has been sampled.

I don’t know if `pm.Deterministic` could work in this case.

---

<div class="post-metadata">

**Author:** ![AlexAndorra](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/alexandorra/32/9142_2.png) [@AlexAndorra](https://discourse.pymc.io/u/AlexAndorra)\
**Post date:** [January 22, 2020, 8:28am UTC](https://discourse.pymc.io/t/generating-population-level-samples-for-heirarchical-parameters/4383/6 "2020-01-22T08:28:33Z")

</div>

“calculating your values after the posterior has been sampled”: That’s what I meant by “pushing your parameters through the model” – thanks Ari for putting well-defined words on my confused thoughts 😆

And I’d say `pm.Deterministic` would work then, as it’s just a wrapper to compute determined combinations of posterior parameter values.
