Cholesky factor corr matrix with same corr on each element

Hi,

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

Thanks

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

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

1 Like

Will this work if x is a parameter?

Can’t see why not

It seems to work just fine - thank you!!