Setting a prior on a correlation coefficient (which is a transformed parameter)

I am recoding Bayesian Cognitive Modeling in Stan + cmdstanr.
In the Pearson correlation model, r is a transformed parameter (see model below), r code to run it here (https://github.com/fusaroli/CognitiveModelingRecoded/blob/main/10_PearsonCorrelation/10_PearsonCorrelation.R)

I have been wondering how it’d be possible to specify a prior on r - e.g. lkj(3) - given it’s a transformed parameter.

data { 
  int<lower=0> n;
  vector[2] x[n];
}

// The parameters accepted by the model. Our model
// accepts two parameters 'mu' and 'sigma'.
parameters {
  vector[2] mu;
  real muprior;
  vector<lower=0>[2] sigma;
  real<lower=0> sigmaprior;
  real<lower=-1, upper=1> r;
  real<lower=-1, upper=1> rprior;
}

transformed parameters {
  cov_matrix[2] T;
  cov_matrix[2] Tprior;
  
  // Reparameterization
  T[1,1] = square(sigma[1]);
  T[1,2] = r * sigma[1] * sigma[2];
  T[2,1] = r * sigma[1] * sigma[2];
  T[2,2] = square(sigma[2]);
  
  Tprior[1,1] = square(sigmaprior);
  Tprior[1,2] = r * sigmaprior * sigmaprior;
  Tprior[2,1] = r * sigmaprior * sigmaprior;
  Tprior[2,2] = square(sigmaprior);
}

model {
  // Priors
  mu ~ normal(0, 10);
  sigma ~ normal(0, 10);
  muprior ~ normal(0, 10);
  sigmaprior ~ normal(0, 10);
  
  // Data
  x ~ multi_normal(mu, T);
}
1 Like

In the model you posted, r is a parameter (hence declared in the parameters block), so you can just put a prior on it in the model block as usual.

1 Like

thanks! and sorry for the naive question!

1 Like

Not naive at all; this stuff is complicated so keep asking Qs! They help form an archive for others that come after you :)

so, just for the archive :-)
I tried applying lkj_corr to r, got an error and therefore thought the issue was in specifying that prior.
However, the issue was that lkj_corr() is a prior for a correlation matrix, while a more appropriate prior for r would be e.g. normal(0, .2)

1 Like