Assign half-normal prior on inversed parameter

Hi all,

I have a parameter kappa, but I am investigating whether 1 / kappa results in a better fit. I would like to assign a half-normal prior on inv_kappa

I write the following:

parameters {
   real<lower=0> sigma;
   vector<lower=0>[n] kappa;

transformed parameters {
   vector<lower=0>[n] inv_kappa = 1 / kappa;

model {
   sigma ~ normal(0, 1);
   inv_kappa ~ sigma;

This results in an error because the last “~”-statement is ill-formed. How can I rewrite this so it works?

You need to specify inv_kappa in the parameters block (because that’s the quantity you’re sampling) and define kappa as the transformed parameter (after which you can use it for whatever in the model). So the other way around compared to what you’re doing now.

Also “sigma” is not a distribution. So the compiler doesn’t know what to do with the statement ‘inv_kappa ~ sigma’ and throws the quoted error. Instead, combined with the point above, the model block should say ‘inv_kappa ~ normal(0,1);’. And if you would stick to your current architecture (with kappa defined as a parameter) and state ‘inv_kappa ~ normal(0,1);’, the compiler would throw a warning about a change of variables and that you (may) need correct for this (Jacobian stuff). You can avoid that by implementing my first point above and letting Stan deal with the change of variables by deining kappa in the transformed parameters bliek and letting inv_kappa be the parameter instead. The online Stan docs provide more background about changes of variables, if you’re interested.