# Outputting covariance matrices from CmdStan

**URL:** https://discourse.mc-stan.org/t/outputting-covariance-matrices-from-cmdstan/17407
**Category:** Developers
**Created:** [August 12, 2020, 8:29pm UTC](https://discourse.mc-stan.org/t/outputting-covariance-matrices-from-cmdstan/17407 "2020-08-12T20:29:57Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![seantalts](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/seantalts/32/32_2.png) [@seantalts](https://discourse.mc-stan.org/u/seantalts)
#### Post date: [August 12, 2020, 8:29pm UTC](https://discourse.mc-stan.org/t/outputting-covariance-matrices-from-cmdstan/17407/1 "2020-08-12T20:29:57Z")

</div>

I can’t seem to round trip covariance matrices in CmdStanPy, though I think this may be an underlying CmdStan issue that is just easier to see with CmdStanPy. I’m generating some covariance matrices with the wishart\_rng and then trying to read them in as data into another model. When I try that, I get errors about some small percentage of these matrices not being positive definite.

It looks like the CSVs that CmdStan outputs are not super precise; is there a way to change that (and see that change in CmdStanPy)? Am I doing something else wrong?

Generate:

```stan
data {
  int K;
  int N;
}
generated quantities {
  cov_matrix[K] covs[N];
  for (n in 1:N) 
    covs[n] = wishart_rng(K, diag_matrix(rep_vector(1.0, K)));
}

```

check:

```stan
data {
  int K;
  int N;
  cov_matrix[K] covs[N];
}

```

This works in Rstan though:

> **R code**
>
> ```
> library("rstan")
> options(mc.cores = parallel::detectCores()).
> rstan_options(auto_write = TRUE)
> draws =stan(model_code="
> data {
> int K;
> int N;
> }
> generated quantities {
> cov_matrix[K] covs[N];
> for (n in 1:N) 
> covs[n] = wishart_rng(K, diag_matrix(rep_vector(1.0, K)));
> }",
> data=list(N=2, K=10),
> algorithm="Fixed_param")
> 
> draws = extract(draws)
> draws$covs[1,]
> 
> checker = stan_model(model_code="
> data {
> int K;
> int N;
> cov_matrix[K] covs[N];
> }")
> bleh = lapply(1:(dim(draws$covs)[1]), 
> function (i) {
> sampling(checker,
> data=list(N=2, K=10, covs=draws$covs[1,,,]),
> algorithm="Fixed_param",
> iter=1, chains=1)
> })
> sum(is.na(bleh))
> 
> ```

Thanks,  
Sean

---

<div class="post-metadata">

### Author: ![rok\_cesnovar](https://avatars.discourse-cdn.com/v4/letter/r/7bcc69/32.png) [@rok\_cesnovar](https://discourse.mc-stan.org/u/rok_cesnovar)
#### Post date: [August 12, 2020, 8:53pm UTC](https://discourse.mc-stan.org/t/outputting-covariance-matrices-from-cmdstan/17407/2 "2020-08-12T20:53:36Z")

</div>

The precision in cmdstan CSVs is definitely the problem here as you need a lot of precision here to pass the strict positive definitnes check in Stan. Especially with large matrices.

Rstan reads the doubles directly, as far as I understand.

We would have to make the precision in the CSV files an argument at some point probably.  
So not sure you can do anything about that at this point.

---

<div class="post-metadata">

### Author: ![syclik](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/syclik/32/6_2.png) [@syclik](https://discourse.mc-stan.org/u/syclik)
#### Post date: [August 12, 2020, 11:02pm UTC](https://discourse.mc-stan.org/t/outputting-covariance-matrices-from-cmdstan/17407/3 "2020-08-12T23:02:48Z")

</div>

I thought there was an option to increase the output precision in CmdStan. If not, we could add it.

---

<div class="post-metadata">

### Author: ![rok\_cesnovar](https://avatars.discourse-cdn.com/v4/letter/r/7bcc69/32.png) [@rok\_cesnovar](https://discourse.mc-stan.org/u/rok_cesnovar)
#### Post date: [August 13, 2020, 7:27am UTC](https://discourse.mc-stan.org/t/outputting-covariance-matrices-from-cmdstan/17407/4 "2020-08-13T07:27:22Z")

</div>

I agree. Issue created: [https://github.com/stan-dev/cmdstan/issues/922](https://github.com/stan-dev/cmdstan/issues/922)
