# Automatic Imputation of Multivariate models

**URL:** <https://discourse.pymc.io/t/automatic-imputation-of-multivariate-models/11029>\
**Category:** Questions\
**Created:** [December 17, 2022, 10:31pm UTC](https://discourse.pymc.io/t/automatic-imputation-of-multivariate-models/11029 "2022-12-17T22:31:25Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Imohiuddin](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/imohiuddin/32/5945_2.png) [@Imohiuddin](https://discourse.pymc.io/u/Imohiuddin)\
**Post date:** [December 17, 2022, 10:31pm UTC](https://discourse.pymc.io/t/automatic-imputation-of-multivariate-models/11029/1 "2022-12-17T22:31:25Z")

</div>

I am trying to work with automatic imputation in a multivariate model and am seeing that while there previously used to be multivariate imputation, there is currently not.

For example Using this notebook: [[pymcon] Missing value handling.ipynb · GitHub](https://gist.github.com/junpenglao/7c505c6c76f99c928a4e2c1161cff43a)

```auto

loc = np.array([-2., 4.])
scale = np.array([2., 3.])
corr = np.array([[1., .65],
                 [.65, 1.]])
cov = np.outer(scale, scale) * corr
chol = np.linalg.cholesky(cov)

n = 500
sample = np.einsum(
    "ij,...j->...i",
    chol, np.random.randn(n, *loc.shape)
    ) + loc

n_mask = 50
row_idx = np.random.choice(n, n_mask, replace=False)
col_idx = np.random.binomial(1, .5, size=n_mask)
mask = np.zeros(sample.shape)
mask[row_idx, col_idx] = np.nan
sample_masked = np.ma.array(sample, mask=np.isnan(mask))

masked_element_idx = np.stack(np.where(sample_masked.mask))

with pm.Model() as m4:
    mu = pm.Normal('mu', mu=0, sigma=10, shape=2)
    sd_dist = pm.HalfCauchy.dist(2.5)
    chol_cov, *_ = pm.LKJCholeskyCov(
        'chol_cov', n=2, eta=1, sd_dist=sd_dist,
        compute_corr=True, store_in_trace=False)
    obs = pm.MvNormal(
        'obs', mu=mu, chol=chol_cov, observed=sample_masked)
    trace4 = pm.sample(2000, cores=4, return_inferencedata=True)

```

I am getting the current error:  
NotImplementedError: Automatic inputation is only supported for univariate RandomVariables, but obs is multivariate .

I am currently running pymc 5.0.0.

Furthermore, if imputation from multivariate random variables has been removed in pymc, I was wondering how I would try and deal with this

---

<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:** [December 19, 2022, 4:15pm UTC](https://discourse.pymc.io/t/automatic-imputation-of-multivariate-models/11029/2 "2022-12-19T16:15:39Z")

</div>

cc @ricardoV94

---

<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 19, 2022, 7:31pm UTC](https://discourse.pymc.io/t/automatic-imputation-of-multivariate-models/11029/3 "2022-12-19T19:31:27Z")

</div>

We don’t have an API in place yet, but you can do it easy enough with a Potential

```python
import pymc as pm
import numpy as np

import aesara.tensor as at

data = np.zeros((3,2))
data[[0, 1, 2, 2], [0, 1, 0, 1]] = np.nan
print(data)

with pm.Model() as m:
    # Create a vector of flat variables for the unobserved components of the MvNormal
    x_unobs = pm.Flat("x_unobs", shape=(np.isnan(data).sum(),))

    # Create the symbolic value of x, combining observed data and unobserved variables
    x = at.as_tensor(data)
    x = pm.Deterministic("x", at.set_subtensor(x[np.isnan(data)], x_unobs))

    # Add a Potential with the logp of the variable conditioned on `x`
    pm.Potential("x_logp", pm.logp(rv=pm.MvNormal.dist([0, 0], [[1,0],[0,1]]), value=x))
    idata = pm.sample()

```

---

<div class="post-metadata">

**Author:** ![Imohiuddin](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/imohiuddin/32/5945_2.png) [@Imohiuddin](https://discourse.pymc.io/u/Imohiuddin)\
**Post date:** [December 19, 2022, 9:55pm UTC](https://discourse.pymc.io/t/automatic-imputation-of-multivariate-models/11029/4 "2022-12-19T21:55:55Z")

</div>

Thank you I was able to implement this into my code and get the solution that I was expecting!

I am not so familiar with working with pm.Flat and pm.Potential and would really like to learn more about them if you have anything you could direct me to

---

<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 19, 2022, 9:57pm UTC](https://discourse.pymc.io/t/automatic-imputation-of-multivariate-models/11029/5 "2022-12-19T21:57:36Z")

</div>

Flat is just a way to sample a variable without an intrinsic prior, and Potential is a way to directly add a log-probability term to the model.
