Corr_matrix of dimension 1?


I noticed that my model which includes inference on P-dimensional correlation matrix kept crashing in simple cases. Minimal example below: as soon as I sample with P=1, Stan run crashes for me without a warning.

Not sure if it’s just me or is it not possible to have a 1x1 corr_matrix? I think it’s obviously useful in case you want to have a single Stan program that can cover both univariate and multivariate cases.

data {
  int<lower=1> P;
parameters {
  corr_matrix[P] Omega;


I get
Must use algorithm=\"Fixed_param\" for model that has no parameters
which is sort of reasonable for the example you posted. Do you get a different error in a more complicated model.

I sometimes do stuff like

parameters {
  corr_matrix[p] Omega[p > 1];

in which case I get an array of length zero when p <= 1.


Thanks Ben, the variant with conditional zero length array works. So that should solve it for me, but for a replicable error & just FYI, this is enough to crash my R session with no warning:

code <- "data {
  int<lower=1> P;
parameters {
  //corr_matrix[P] Omega[P > 1];
  corr_matrix[P] Omega;
model <- stan_model(model_code = code)
sampling(model, data = list(P = 1), algorithm = "Fixed_param")


Thanks. @Bob_Carpenter what is supposed to happen in this case, plus simplex[0] and unit_vector[0]?


simplex[0] and unit_vector[0] should fail with an exception. I already fixed simplex on the dev branch.

corr_matrix[1] and cholesky_corr[1] should not fail.

I created a Stan issue.

I already fixed simplex, but we apparently still need to fix unit_vector and corr_matrix.

simplex[0] and unit_vector[0] should throw exceptions. I think I fixed simplex, but not unit_vector. I’ll open an issue.

A 1D correlation matrix should be legal, though, right? Presumably our correlation matrix code is busted on the boundary conditions of n = 1.


That sounds right