# Multivariate normal with diagonal covariance matrix

**URL:** <https://discourse.mc-stan.org/t/multivariate-normal-with-diagonal-covariance-matrix/1633>\
**Category:** Modeling\
**Created:** [August 21, 2017, 6:49pm UTC](https://discourse.mc-stan.org/t/multivariate-normal-with-diagonal-covariance-matrix/1633 "2017-08-21T18:49:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Matthew\_Spencer](https://avatars.discourse-cdn.com/v4/letter/m/9f8e36/32.png) [@Matthew\_Spencer](https://discourse.mc-stan.org/u/Matthew_Spencer)\
**Post date:** [August 21, 2017, 6:49pm UTC](https://discourse.mc-stan.org/t/multivariate-normal-with-diagonal-covariance-matrix/1633/1 "2017-08-21T18:49:24Z")

</div>

In my Stan time series model, the s-dimensional vector-valued latent variable x[k] has a multivariate normal distribution with mean vector returned by the function getEXdiag(), and a covariance matrix that is a scalar multiple of the identity matrix. The function getVXdiag() returns the required scalar. I’ve skipped the details of the arguments to these functions. Both the following sampling statements compile. I had expected them to give the same answer. On simulated data, the multi\_normal version leads to sensible parameter estimates, while the normal version leads to estimates that are completely wrong.

x[k] ~ multi\_normal(getEXdiag(eBT, x[k-1], b1, a[j]), diag\_matrix(rep\_vector(1.0, s)) \* getVXdiag(eBT, b1, sigma2));

x[k] ~ normal(getEXdiag(eBT, x[k - 1], b1, a[j]), getVXdiag(eBT, b1, sigma2));

Did I just do something stupid with the syntax in the normal version? I’d like to use it if I could, because it’s much faster (although I expect that I could speed up the multi\_normal version a bit by not first making an identity matrix and then scaling it).

---

<div class="post-metadata">

**Author:** ![bbbales2](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bbbales2/32/77_2.png) [@bbbales2](https://discourse.mc-stan.org/u/bbbales2)\
**Post date:** [August 21, 2017, 8:23pm UTC](https://discourse.mc-stan.org/t/multivariate-normal-with-diagonal-covariance-matrix/1633/2 "2017-08-21T20:23:48Z")

</div>

Seems the same to me. Can you post the rest of your model here? I’m curious about the exact types floating around. Might be something else going on (or maybe one of these types is funky).

---

<div class="post-metadata">

**Author:** ![bgoodri](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bgoodri/32/4451_2.png) [@bgoodri](https://discourse.mc-stan.org/u/bgoodri)\
**Post date:** [August 21, 2017, 9:16pm UTC](https://discourse.mc-stan.org/t/multivariate-normal-with-diagonal-covariance-matrix/1633/3 "2017-08-21T21:16:32Z")

</div>

> [@Matthew\_Spencer](#):
>
> x[k] ~ normal(getEXdiag(eBT, x[k - 1], b1, a[j]), getVXdiag(eBT, b1, sigma2));

Just do

```no-highlight
real sigma = sqrt(sigma2); // I suspect you forgot that normal accepts a standard deviation
for (k in 1:K)
  x[k] ~ normal(getEXdiag(eBT, x[k - 1], b1, a[j]), sigma);

```

---

<div class="post-metadata">

**Author:** ![Matthew\_Spencer](https://avatars.discourse-cdn.com/v4/letter/m/9f8e36/32.png) [@Matthew\_Spencer](https://discourse.mc-stan.org/u/Matthew_Spencer)\
**Post date:** [August 22, 2017, 11:19am UTC](https://discourse.mc-stan.org/t/multivariate-normal-with-diagonal-covariance-matrix/1633/4 "2017-08-22T11:19:44Z")

</div>

Thank you! I had forgotten that normal wants the standard deviation, not the variance. Fixed that and now the univariate version is working nicely, and much faster than the multivariate version.
