# Marginal distribution estimation

**URL:** https://discourse.pymc.io/t/marginal-distribution-estimation/6485
**Category:** Questions
**Created:** [December 28, 2020, 1:32pm UTC](https://discourse.pymc.io/t/marginal-distribution-estimation/6485 "2020-12-28T13:32:51Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Alex\_Sokolsky](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/alex_sokolsky/32/3450_2.png) [@Alex\_Sokolsky](https://discourse.pymc.io/u/Alex_Sokolsky)
#### Post date: [December 28, 2020, 1:32pm UTC](https://discourse.pymc.io/t/marginal-distribution-estimation/6485/1 "2020-12-28T13:32:51Z")

</div>

For example, I have a 100-dimensional normal distribution with a known covariance matrix.  
But, for the location parameters (μ) each dimension is given by an independent Beta distribution.  
How can I measure the marginal distribution of each dimension of the final distribution?

---

<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: [December 28, 2020, 3:05pm UTC](https://discourse.pymc.io/t/marginal-distribution-estimation/6485/2 "2020-12-28T15:05:44Z")

</div>

I am not sure I understand what you are asking for, so sorry if I only confuse you.

Analytically you get the marginal of the `i`th dimension by looking at a Gaussian with `mean = mu[i]` and `sigma = cov[i,i]`, where `mu` and `cov` are the posterior mean and covariance parameters of your 100th dimensional gaussian. If you have samples from the posterior, you should be able to simply select `trace["my_gaussian"][:, i]` where `my_gaussian` is the name you gave to the 100th dimensional gaussian in your pymc3 model.

If this does not answer your question, maybe it would be helpful to have a snippet of your model to understand exactly what you are looking for.

---

<div class="post-metadata">

### Author: ![Alex\_Sokolsky](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/alex_sokolsky/32/3450_2.png) [@Alex\_Sokolsky](https://discourse.pymc.io/u/Alex_Sokolsky)
#### Post date: [December 28, 2020, 4:25pm UTC](https://discourse.pymc.io/t/marginal-distribution-estimation/6485/4 "2020-12-28T16:25:39Z")

</div>

Thank you!  
I try to something like (Obviously, that is just pseudocode):

with pm.Model() as model1:  
teta1 = pm.Beta(‘teta1’,1, 2)  
teta2 = pm.Beta(‘teta2’,1, 2)  
teta3 = pm.Beta(‘teta3’,1, 2)  
teta4 = pm.Beta(‘teta4’,1, 2)  
…  
teta100 = pm.Beta(‘teta100’,1, 2)

```
mnorm = pm.MvNormal('mnorm', mu=[theta1,theta2,...,theta100], cov=cov)

```

…  
mnorm.sample(10000)

Where parameters for each Beta are obtained by experiment.  
I’m not sure that is exists easy solution to get that analytically.

---

<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: [December 28, 2020, 5:07pm UTC](https://discourse.pymc.io/t/marginal-distribution-estimation/6485/5 "2020-12-28T17:07:14Z")

</div>

It is still a bit unclear for me what you are trying to achieve.

Where does the data come in your model exactly? At the `mnorm` or at each of the `thetas`? What kind of data do you have? Unless you are just trying to sample from an already known model (in which case you don’t really need PyMC), you should have an `observed` argument somewhere in your model, where you input your data, in order to infer the posterior parameters (those without the observed argument).

To make inference, sampling would be called by `trace = pm.sample()` (still inside the `with pm.Model() as model1:` block), and not by `mnorm.sample(10000)` like you wrote in your pseudocode.

---

<div class="post-metadata">

### Author: ![brandonwillard](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/brandonwillard/32/1464_2.png) [@brandonwillard](https://discourse.pymc.io/u/brandonwillard)
#### Post date: [December 29, 2020, 8:28am UTC](https://discourse.pymc.io/t/marginal-distribution-estimation/6485/6 "2020-12-29T08:28:58Z")

</div>

For simplicity, you can use `betas = pm.beta(1, 2, size=100)` instead.

With that, as @ricardoV94 mentioned, you can define the observation distribution as `mnorm = pm.MvNormal('mnorm', mu=betas, cov=cov, observed=obs)` given an array of observations, `obs`. From there you can use `pm.sample`.
