Fix a parameter for model exploration

Hi am after some advice. I often find that when setting up a model I want to fix a parameters during model exploration. An example of this is would be exploring if incorporating auto correlation in a Covariance matrix is better than the independence assumption \rho = 0 vs \rho \neq 0. I have used three methods

  1. move the parameter declaration from the parameter section to the data section and recompile. However this ends up creating many models and messy directory.

  2. have data values for bounds of a parameter, and set the upper and lower bound to the value you want to fix the parameter at.

  3. Have a ridiculous prior that basically constrains a parameter to a fixed value

I worry that 2 & 3 will effect sampling of the posterior. Are there better implementation than these? Apologies if I have missed an obvious solution.

I think you can use zero size parameter arrays to do this. It can be a bit awkward, but check this as a model to optionally estimate a standard deviation:

data {
  int N;
  real y[N];
  int<lower = 0, upper = 1> include_param; // 1 for include, 0 for not
  real sigma_fixed;
}

params {
  real mu;
  real<lower = 0.0> sigma[include_param];
}

model {
  mu ~ normal(0, 1);
  if(include_param == 0) {
    y ~ normal(mu, sigma_fixed);
  } else {
    sigma ~ normal(0, 1);
    y ~ normal(mu, sigma);
  }
}
3 Likes

Hey! You can also use #include and then let the various models share the same data{...} declaration You can actually use #includes everywhere in your model to keep parts consistent across models. Maybe that’ll help with the “mess”. I tend to treat each approach as the same model (so basically what you mentioned in 1.). You can obviously combine this with Ben’s suggestion. :)

2 Likes

Thanks @bbbales2 & @Max_Mantei for those solutions

Can I get some detail about how to implement the includes solution? I am only familiar with using the #include for including additional functions. My latest attempt was the one below, but no cigar

//sigma.stan 
real<lower = 0.0> sigma;
//model.stan
data {
  int N;
  real y[N];
  //#include sigma.stan
}

parameters {
  real mu;
  #include sigma.stan
}

model {
  mu ~ normal(0, 1);
  sigma ~ normal(0, 1);
  y ~ normal(mu, sigma);
}

This works for me. Make sure you do not indent the #include sigma.stan line.

Also note, that you might have to manually recompile, if you change something in the included files.

2 Likes