# Samples from Marginal Posterior Distribution

**URL:** https://discourse.pymc.io/t/samples-from-marginal-posterior-distribution/8983
**Category:** Questions
**Created:** [March 6, 2022, 3:15pm UTC](https://discourse.pymc.io/t/samples-from-marginal-posterior-distribution/8983 "2022-03-06T15:15:44Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Giorgos\_Nikola](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/giorgos_nikola/32/4924_2.png) [@Giorgos\_Nikola](https://discourse.pymc.io/u/Giorgos_Nikola)
#### Post date: [March 6, 2022, 3:15pm UTC](https://discourse.pymc.io/t/samples-from-marginal-posterior-distribution/8983/1 "2022-03-06T15:15:44Z")

</div>

Let us consider the following Hierarchical Bayesian model:

- mu \sim\ Beta(1, 1)
- k \sim\ Exponential(1)
- a = k\*mu
- b = (1-mu) \* k
- theta \sim\ Beta(a, b)
- y \sim\ Bern(theta)

The above example is a simplification of the example in figure 2.19 in the book Bayesian Analysis with Python by Osvaldo Martin.

```python
    import numpy as np
    import pymc3 as pm
    import arviz as az
    
    data = np.random.binomial(1, 0.3, 10000)
    
    with pm.Model() as model:
        mu = pm.Beta('mu', 1., 1.)
        k = pm.Exponential('k', 1)
        a = pm.Deterministic('a', mu*k)
        b = pm.Deterministic('b', (1.0-mu)*k)
        theta = pm.Beta('theta', alpha=a, beta=b)
        y = pm.Bernoulli('y', p=theta, observed=data)
        trace = pm.sample(1000)

```

I have the following two questions:

- The trace variable contains samples from the posterior distribution for the three random variables mu, k and theta. Does the samples correspond to each of the marginal posterior distribution i.e. p(mu|data), p(k|data), p(theta|data) or they correspond to the joint posterior i.e. p(theta,mu,k|data) ?
- If the samples correspond to the joint posterior, how can I obtain samples from each of the marginal posterior distribution i.e. p(mu|data), p(k|data), p(theta|data)?

---

<div class="post-metadata">

### Author: ![ricardoV94](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/ricardov94/32/5775_2.png) [@ricardoV94](https://discourse.pymc.io/u/ricardoV94)
#### Post date: [March 6, 2022, 4:01pm UTC](https://discourse.pymc.io/t/samples-from-marginal-posterior-distribution/8983/2 "2022-03-06T16:01:35Z")

</div>

Your samples correspond to the joint posterior of all parameters conditioned on the data: p(theta,mu,k|data)

The wonderful thing about MCMC samples is that the marginal of each (or the joint-posterior subset of any n variables, or some summary statistics) is also given or retrievable from these samples.

For the marginal, you just ignore the variables you don’t care about:

```python
marginal_mu = trace["mu"] 

```

Similarly, if you wanted to only look at the joint posterior of mu and k you would just include those and ignore the other variables:

```python
joint_mu_k = np.stack((trace["mu"], trace["k"]))

```

---

<div class="post-metadata">

### Author: ![Giorgos\_Nikola](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/giorgos_nikola/32/4924_2.png) [@Giorgos\_Nikola](https://discourse.pymc.io/u/Giorgos_Nikola)
#### Post date: [March 6, 2022, 9:30pm UTC](https://discourse.pymc.io/t/samples-from-marginal-posterior-distribution/8983/3 "2022-03-06T21:30:40Z")

</div>

Thank you very much
