# Conditional multivariate sampling

**URL:** https://discourse.pymc.io/t/conditional-multivariate-sampling/1393
**Category:** Questions
**Created:** [June 20, 2018, 9:52am UTC](https://discourse.pymc.io/t/conditional-multivariate-sampling/1393 "2018-06-20T09:52:05Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![maximelecactus](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/maximelecactus/32/922_2.png) [@maximelecactus](https://discourse.pymc.io/u/maximelecactus)
#### Post date: [June 20, 2018, 9:52am UTC](https://discourse.pymc.io/t/conditional-multivariate-sampling/1393/1 "2018-06-20T09:52:05Z")

</div>

Hello,

I fitted a multivariate normal distribution over a rhee dimension matrix of random variables using the following code:

```
model = pm.Model()
shape = x.shape[1] # shape of x is [N, 3]

with model:
    packed_L = pm.LKJCholeskyCov('packed_L', n=shape,
                                 eta=2., sd_dist=pm.HalfCauchy.dist(2.5))
    L = pm.expand_packed_triangular(shape, packed_L)
    Σ = pm.Deterministic('Σ', L.dot(L.T))
    
    μ = pm.Normal('μ', 0., 10., shape=shape)
    
    obs = pm.MvNormal('obs', mu=μ, chol=L, observed=x)
    trace_ = pm.sample(5000, cores=1)

```

I would like to fix one variable of the distribution to a scalar value and have the resulting conditional distribution of the two remaining variables. How can that be achieved?

Thank you very much for your help.

Maxime.

---

<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 20, 2018, 10:27am UTC](https://discourse.pymc.io/t/conditional-multivariate-sampling/1393/2 "2018-06-20T10:27:33Z")

</div>

> [@maximelecactus](#):
>
> I would like to fix one variable of the distribution to a scalar value and have the resulting conditional distribution of the two remaining variables.

You mean like fixing μ[0] = scalar with the observed being the same `observed=x`? or you would like to marginalized the MvNormal to have the `observed=x[:, 1:]`?

---

<div class="post-metadata">

### Author: ![maximelecactus](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/maximelecactus/32/922_2.png) [@maximelecactus](https://discourse.pymc.io/u/maximelecactus)
#### Post date: [June 20, 2018, 11:24am UTC](https://discourse.pymc.io/t/conditional-multivariate-sampling/1393/3 "2018-06-20T11:24:34Z")

</div>

Thank you for your answer.

Let’s say that X1 is a random variable describing a person’s age, X2 is the annual revenue and X3 represents this person’s number of children.

The objective of the first code snippet was to model these variables with a multivariate normal distribution. Now, I would like to have the normal multivariate distribution of say X1 and X2 given that X3=3 children. Would that be possible ?

`x = [X1, X2, X3]`

Thank you in advance,

Maxime.

---

<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 20, 2018, 11:44am UTC](https://discourse.pymc.io/t/conditional-multivariate-sampling/1393/4 "2018-06-20T11:44:55Z")

</div>

Since [X1, X2, X3] are some observed quantities, I would either model `x_tilt = [X1[X3==3], X2[X3==3]]` and find the posterior of `x_tilt`; or model `X = [X1, X2, X3]` directly and find the posterior of `X`, then compute the conditional of this posterior when `X3==3`.

The later cases you can use the code you have, and use `sample_ppc` after inference to generate large among of ppc samples, and plot the conditional:

```python
index = (ppc['obs'][:, 2] > 2.99) or (ppc['obs'][:, 2] < 3.01)
scatter(ppc['obs'][index, 0], ppc['obs'][index, 1])

```

---

<div class="post-metadata">

### Author: ![maximelecactus](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/maximelecactus/32/922_2.png) [@maximelecactus](https://discourse.pymc.io/u/maximelecactus)
#### Post date: [June 20, 2018, 11:55am UTC](https://discourse.pymc.io/t/conditional-multivariate-sampling/1393/5 "2018-06-20T11:55:04Z")

</div>

As X3 represents a continuous variable in my model, I will use the second method.

Thank you for your help.

Maxime.

---

<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 20, 2018, 12:00pm UTC](https://discourse.pymc.io/t/conditional-multivariate-sampling/1393/6 "2018-06-20T12:00:12Z")

</div>

FYI, using the second approach you can also compute the posterior conditional using the posterior sample `mu` and `cov` of the MvNormal from the trace: [https://en.wikipedia.org/wiki/Multivariate\_normal\_distribution#Conditional\_distributions](https://en.wikipedia.org/wiki/Multivariate_normal_distribution#Conditional_distributions)
