# Cholesky factor corr matrix with same corr on each element

**URL:** <https://discourse.mc-stan.org/t/cholesky-factor-corr-matrix-with-same-corr-on-each-element/29216>\
**Category:** Modeling\
**Created:** [October 22, 2022, 9:44pm UTC](https://discourse.mc-stan.org/t/cholesky-factor-corr-matrix-with-same-corr-on-each-element/29216 "2022-10-22T21:44:04Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![CerulloE](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/cerulloe/32/15968_2.png) [@CerulloE](https://discourse.mc-stan.org/u/CerulloE)\
**Post date:** [October 22, 2022, 9:44pm UTC](https://discourse.mc-stan.org/t/cholesky-factor-corr-matrix-with-same-corr-on-each-element/29216/1 "2022-10-22T21:44:04Z")

</div>

Hi,

How do I make a cholesky factor correlation matrix (`cholesky_factor_corr`) with the same correlation across the entire matrix?

Thanks

---

<div class="post-metadata">

**Author:** ![andrjohns](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/andrjohns/32/15297_2.png) [@andrjohns](https://discourse.mc-stan.org/u/andrjohns)\
**Post date:** [October 24, 2022, 7:55am UTC](https://discourse.mc-stan.org/t/cholesky-factor-corr-matrix-with-same-corr-on-each-element/29216/2 "2022-10-24T07:55:51Z")

</div>

I think the easiest option here is going to be to construct the matrix on the correlation scale and then take the cholesky factor:

```r
modcode = "
functions {
  matrix corr_to_chol(real x, int J) {
    matrix[J,J] corr = add_diag(rep_matrix(x, J, J), 1 - x);
    return cholesky_decompose(corr);
  }
}
"

rstan::expose_stan_functions(rstan::stanc(model_code=modcode))

chol = corr_to_chol(0.2, 5)
chol %*% t(chol)
#> [,1] [,2] [,3] [,4] [,5]
#> [1,] 1.0 0.2 0.2 0.2 0.2
#> [2,] 0.2 1.0 0.2 0.2 0.2
#> [3,] 0.2 0.2 1.0 0.2 0.2
#> [4,] 0.2 0.2 0.2 1.0 0.2
#> [5,] 0.2 0.2 0.2 0.2 1.0

```

Created on 2022-10-24 with [reprex v2.0.2](https://reprex.tidyverse.org)

---

<div class="post-metadata">

**Author:** ![CerulloE](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/cerulloe/32/15968_2.png) [@CerulloE](https://discourse.mc-stan.org/u/CerulloE)\
**Post date:** [October 25, 2022, 10:40pm UTC](https://discourse.mc-stan.org/t/cholesky-factor-corr-matrix-with-same-corr-on-each-element/29216/3 "2022-10-25T22:40:49Z")

</div>

Will this work if x is a parameter?

---

<div class="post-metadata">

**Author:** ![andrjohns](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/andrjohns/32/15297_2.png) [@andrjohns](https://discourse.mc-stan.org/u/andrjohns)\
**Post date:** [October 26, 2022, 11:11am UTC](https://discourse.mc-stan.org/t/cholesky-factor-corr-matrix-with-same-corr-on-each-element/29216/4 "2022-10-26T11:11:15Z")

</div>

Can’t see why not

---

<div class="post-metadata">

**Author:** ![CerulloE](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/cerulloe/32/15968_2.png) [@CerulloE](https://discourse.mc-stan.org/u/CerulloE)\
**Post date:** [October 26, 2022, 11:25am UTC](https://discourse.mc-stan.org/t/cholesky-factor-corr-matrix-with-same-corr-on-each-element/29216/5 "2022-10-26T11:25:24Z")

</div>

It seems to work just fine - thank you!!
