# Multivariate Normal Imputation Error

**URL:** <https://discourse.pymc.io/t/multivariate-normal-imputation-error/10829>\
**Category:** v5\
**Tags:** aesara\
**Created:** [November 14, 2022, 5:35pm UTC](https://discourse.pymc.io/t/multivariate-normal-imputation-error/10829 "2022-11-14T17:35:11Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![yahrMason](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/yahrmason/32/5826_2.png) [@yahrMason](https://discourse.pymc.io/u/yahrMason)\
**Post date:** [November 14, 2022, 5:35pm UTC](https://discourse.pymc.io/t/multivariate-normal-imputation-error/10829/1 "2022-11-14T17:35:11Z")

</div>

Hello,

I am currently working through converting the code in the pymc-resources/Rethinking\_2 directory to pymc v4. I have forked the repo and have been working on updating the code as I read the book. In code block [15.22](https://github.com/pymc-devs/pymc-resources/blob/main/Rethinking_2/Chp_15.ipynb) I get this error message:

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/pymc3/original/2X/a/ad2b19d8e62d569acf378d74f174d7a3c10ff70f.png)

Here are the package versions I am running:  
 ![image](https://canada1.discourse-cdn.com/flex036/uploads/pymc3/original/2X/5/549d86fc3e81da0511c9f5e89636ea75a66200d3.png)

It appears that this was a possibility on pymc3 according to the issue/discussion on [this discourse link.](https://discourse.pymc.io/t/multivariate-normal-with-missing-data-imputation-operands-could-not-be-broadcast-together-with-shapes-29-2-29/5705) Is there a different way that imputing multivariate normal random variables should be handled in v4?

Thanks

---

<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, 10:05pm UTC](https://discourse.pymc.io/t/multivariate-normal-imputation-error/10829/2 "2022-12-19T22:05:43Z")

</div>

I had the same question you had and ended up getting a response in [here](https://discourse.pymc.io/t/automatic-imputation-of-multivariate-models/11029/3) that worked, so I just wanted to share

```auto
d = pd.read_csv('https://raw.githubusercontent.com/rmcelreath/rethinking/master/data/milk.csv', sep=";")

d["neocortex.prop"] = d["neocortex.perc"] / 100
d["logmass"] = np.log(d["mass"])

K = stats.zscore(d["kcal.per.g"], nan_policy = 'omit')
B = stats.zscore(d["neocortex.prop"], nan_policy = 'omit')
M = stats.zscore(d["logmass"], nan_policy = 'omit')

MB_vals = np.stack([M,B], axis=1)

with pm.Model() as m15_7:
    sigma = pm.Exponential("sigma", 1)
    muM = pm.Normal("muM", 0, 0.5)
    muB = pm.Normal("muB", 0, 0.5)
    bM = pm.Normal("bM", 0, 0.5)
    bB = pm.Normal("bB", 0, 0.5)
    a = pm.Normal("a", 0, 0.5)

    chol, _, _ = pm.LKJCholeskyCov(
        "chol_cov", n=2, eta=2, sd_dist=pm.Exponential.dist(1), compute_corr=True
    )

    # Create a vector of flat variables for the unobserved components of the MvNormal
    MB_impute = pm.Flat("MB_impute", shape=(np.isnan(MB_vals).sum(), ))

    # Create the symbolic value of MB, combining observed data and unobserved variables
    MB = at.as_tensor(MB_vals)

    MB = pm.Deterministic("MB", at.set_subtensor(MB[np.isnan(MB_vals)], MB_impute))

    # Add a Potential with the logp of the variable conditioned on `MB`

    pm.Potential("MB_logp", pm.logp(value = MB, rv=pm.MvNormal.dist(mu = at.stack([muM, muB]),chol = chol))) 
    mu = a + bB * MB[:, 1] + bM * M
    Ki = pm.Normal("Ki", mu, sigma, observed=K)
    idata_m15_7 = pm.sample()

```
