# Specifying Correlation in Stan

**URL:** https://discourse.mc-stan.org/t/specifying-correlation-in-stan/24618
**Category:** Modeling
**Created:** [September 29, 2021, 12:25pm UTC](https://discourse.mc-stan.org/t/specifying-correlation-in-stan/24618 "2021-09-29T12:25:06Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![nick](https://avatars.discourse-cdn.com/v4/letter/n/f9ae1b/32.png) [@nick](https://discourse.mc-stan.org/u/nick)
#### Post date: [September 29, 2021, 12:25pm UTC](https://discourse.mc-stan.org/t/specifying-correlation-in-stan/24618/1 "2021-09-29T12:25:07Z")

</div>

```stan
model {
  y1 ~ normal(mu1, sigma1); 
  y2 ~ normal(mu2, sigma2); 
} 

```

Hi fellas,

I am relatively new to Stan and have a simple question which I have not been able to figure out how to code in stan for quite sometime now…

I simply wanted to specify a correlation between sigma1 and simga2, the correlation is unknown. Based on my (extensive searching), it seems the best way to do it is by specifying it in the parameters block, using a covariance matrix. However I have no idea how to do this and I haven’t even used the parameters block before.

Would really appreciate it if anyone can help with the code.

---

<div class="post-metadata">

### Author: ![mathDR](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/mathdr/32/1997_2.png) [@mathDR](https://discourse.mc-stan.org/u/mathDR)
#### Post date: [September 29, 2021, 5:31pm UTC](https://discourse.mc-stan.org/t/specifying-correlation-in-stan/24618/2 "2021-09-29T17:31:05Z")

</div>

Do you have any prior information on how `sigma1` and `sigma2` are correlated?

(I think) the most general case is:

```
parameters {
  cholesky_factor_corr[2] Lcorr;
  vector[2] prior_mu;
  vector<lower=0>[2] prior_scale;
  vector[2] Z;
}

transformed parameters {
  vector[2] sigmas = prior_mu + diag_pre_multiply(prior_scale, Lcorr)*Z;
}

model {
  prior_mu ~ multi_normal(0, 1); // you can put prior information about the sigmas here
  Z ~ std_normal();
  prior_scale ~ std_normal();
  Lcorr ~ lkj_corr_cholesky(2.0); 

  y1 ~ normal(mu1, sigmas[1]); 
  y2 ~ normal(mu2, sigmas[2]); 
}
```

---

<div class="post-metadata">

### Author: ![mathDR](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/mathdr/32/1997_2.png) [@mathDR](https://discourse.mc-stan.org/u/mathDR)
#### Post date: [September 29, 2021, 5:32pm UTC](https://discourse.mc-stan.org/t/specifying-correlation-in-stan/24618/3 "2021-09-29T17:32:36Z")

</div>

[This](https://stla.github.io/stlapblog/posts/StanLKJprior.html) is also a good read to understand better what is going on.

---

<div class="post-metadata">

### Author: ![Stephen\_Martin](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/stephen_martin/32/7879_2.png) [@Stephen\_Martin](https://discourse.mc-stan.org/u/Stephen_Martin)
#### Post date: [September 29, 2021, 5:57pm UTC](https://discourse.mc-stan.org/t/specifying-correlation-in-stan/24618/4 "2021-09-29T17:57:35Z")

</div>

I would recommend imposing the correlation on the log scale instead. The non-centered approach here doesn’t guarantee that the sigmas remain positive, so you get into rejection sampling and slow mixing. Instead, can parameterize in terms of log(sigma), then just exponentiate to get the positive values out.

---

<div class="post-metadata">

### Author: ![nick](https://avatars.discourse-cdn.com/v4/letter/n/f9ae1b/32.png) [@nick](https://discourse.mc-stan.org/u/nick)
#### Post date: [September 30, 2021, 10:15am UTC](https://discourse.mc-stan.org/t/specifying-correlation-in-stan/24618/5 "2021-09-30T10:15:16Z")

</div>

Thanks a lot, mathDR! It worked well and the reading was also very useful.

There are actually contemporaneous covariances between the 2. I tried to modify the code to make it work but failed and the link you gave didn’t talk about this. Would you mind taking another look?:)

Here’s my modified code:

```stan
parameters {
cholesky_factor_corr[2] Lcorr;
matrix[N,2] prior_mu;
vector<lower=0>[2] prior_scale;
matrix[N,2] Z;
}

transformed parameters {
  matrix[N,2] sigma = prior_mu + diag_pre_multiply(prior_scale, Lcorr)*Z;
}

model {
Z ~ std_normal();
prior_scale ~ std_normal();
Lcorr ~ lkj_corr_cholesky(2.0); 

y1[n] ~ normal(mu1[n], sigma[n,1]); 
y2[n] ~ normal(mu2[n], sigma[n,2]); 
}

```

## Duplicate declaration of variable, name=error; attempt to redeclare as matrix in transformed parameter; previously declared as matrix in parameter error in ‘model14ae1914545\_2ef64b1018424bfaa8c4b0cbcd92eefb’ at line 18, column 72

## 16: 17: transformed parameters { 18: matrix[N,2] error = prior\_mu + diag\_pre\_multiply(prior\_scale, Lcorr)\*Z; ^ 19: }

Error in stanc(file = file, model\_code = model\_code, model\_name = model\_name, :  
failed to parse Stan model ‘2ef64b1018424bfaa8c4b0cbcd92eefb’ due to the above error.

---

<div class="post-metadata">

### Author: ![nick](https://avatars.discourse-cdn.com/v4/letter/n/f9ae1b/32.png) [@nick](https://discourse.mc-stan.org/u/nick)
#### Post date: [September 30, 2021, 10:16am UTC](https://discourse.mc-stan.org/t/specifying-correlation-in-stan/24618/6 "2021-09-30T10:16:13Z")

</div>

Thanks for the great suggestion! I will update it in my final model!

---

<div class="post-metadata">

### Author: ![mathDR](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/mathdr/32/1997_2.png) [@mathDR](https://discourse.mc-stan.org/u/mathDR)
#### Post date: [September 30, 2021, 12:59pm UTC](https://discourse.mc-stan.org/t/specifying-correlation-in-stan/24618/7 "2021-09-30T12:59:30Z")

</div>

Oh! This is a really nice idea! Is this a standard idea for positive parameters?

---

<div class="post-metadata">

### Author: ![mathDR](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/mathdr/32/1997_2.png) [@mathDR](https://discourse.mc-stan.org/u/mathDR)
#### Post date: [September 30, 2021, 1:03pm UTC](https://discourse.mc-stan.org/t/specifying-correlation-in-stan/24618/8 "2021-09-30T13:03:54Z")

</div>

Hmmm, this seems to think you have `Lcorr` declared in both the parameters block _and_ the transformed parameters block.

Can you post a minimal example (for example, the psuedocode above has `n` but it isn’t declared anywhere, etc.)

---

<div class="post-metadata">

### Author: ![nick](https://avatars.discourse-cdn.com/v4/letter/n/f9ae1b/32.png) [@nick](https://discourse.mc-stan.org/u/nick)
#### Post date: [September 30, 2021, 2:10pm UTC](https://discourse.mc-stan.org/t/specifying-correlation-in-stan/24618/9 "2021-09-30T14:10:33Z")

</div>

Sure, my code is rather long (200+ rows) and the only thing I am trying to modify is to use correlated contemporaneous error. This code should represent the setup…

```
  "data 
{
int<lower=0> N;
vector[N] y1;
vector[N] y2;
}

parameters {
matrix[N,2] sigma;

cholesky_factor_corr[2] Lcorr;
matrix[N,2] prior_mu;
matrix<lower=0>[2,N] prior_scale;
matrix[N,2] Z;
}

transformed parameters {
  matrix[N,2] sigma = prior_mu + diag_pre_multiply(prior_scale, Lcorr)*Z;
}

model {
Z ~ std_normal();
prior_scale ~ std_normal();
Lcorr ~ lkj_corr_cholesky(2.0); 

y1[n] ~ normal(mu1[n], sigma[n,1]); 
y2[n] ~ normal(mu2[n], sigma[n,2]); 
}

```

I think my goal is close to this thread([Covariance Matrix Not Symmetric for LKJ prior](https://discourse.mc-stan.org/t/covariance-matrix-not-symmetric-for-lkj-prior/20932/1)), however I am too unfamiliar with Stan’s syntax(never used BUGS, etc, before) to figure out the correct way to setup the model.

---

<div class="post-metadata">

### Author: ![mathDR](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/mathdr/32/1997_2.png) [@mathDR](https://discourse.mc-stan.org/u/mathDR)
#### Post date: [October 9, 2021, 12:48am UTC](https://discourse.mc-stan.org/t/specifying-correlation-in-stan/24618/10 "2021-10-09T00:48:00Z")

</div>

Okay I don’t see an issue with the snippet you posted. Is that from your code? Also, can you post the stack trace you get (the error messages)?

---

<div class="post-metadata">

### Author: ![jsocolar](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jsocolar/32/2486_2.png) [@jsocolar](https://discourse.mc-stan.org/u/jsocolar)
#### Post date: [October 9, 2021, 2:30am UTC](https://discourse.mc-stan.org/t/specifying-correlation-in-stan/24618/11 "2021-10-09T02:30:31Z")

</div>

The issue with the posted snippet is that `sigma` is declared twice. If we delete the declaration of `sigma` from the `parameters` block, then we progress to a different error because the arguments to `diag_pre_multiply` are ill-typed. I can’t tell what the intent is behind modifying @mathDR’s code so that `prior_scale` is a matrix rather than a vector. If we change it back to vector, then next we get an issue with the `Z ~ std_normal()` statement. We can fix with `to_vector(Z) ~ std_normal()`. Then we get some errors with out-of-scope variables `n`, `mu1`, and `mu2`. But if we comment these lines out, then the program compiles at least.
