FYI: how to easily toggle between centered/non-centered

Because I haven’t seen it anywhere else, I thought I’d share what I came up with to encode a model with runtime selection of centered versus non-centered parameterizations for hierarchical parameters:

data {
  // K: number of identifiable-units-of-observation (IUOO)
  int K ;
  // N: number of observations total (must be at least one per IUOO)
  int<lower=K> N;
  // which_K: index associating each observation with its IUOO
  int<lower=1,upper=K> which_K[N] ;
  // Y: vector of observations
  vector[N] Y ;
  // centered: binary toggle for intercept centered/non-centered parameterization
  int<lower=0,upper=1> centered ;
}
parameters {
  // mu: mean (across-IUOOs) of X
  real mu ;
  // sigma: sd (across-IUOOs) of X
  real<lower=0> sigma ;
  // X: mean (across observations) for each IUOO
  vector<
    offset = (centered ? 0 : mu)
    , multiplier = (centered ? 1 : sigma)
  >[K] X ;
}
model {
  //hyper-priors
  mu ~ std_normal() ; //must be changed to reflect domain expertise
  sigma ~ std_normal() ; //must be changed to reflect domain expertise
  //hierarchical prior for X:
  X ~ normal( mu, sigma ) ;
  //likelihood:
  Y ~ normal( X[which_K], 1 ) ;
}
18 Likes

comment to enable me to mark this as solved so it doesn’t get included in the “needs love” category.

Cool, thanks for sharing that.

I think you can also do that by using the no-solution-needed tag.

2 Likes

@jonah did that tag exist and get removed? I don’t see it now.

The tag is visible to admins/mods only to reduce clutter… But I am not fundamentally opposed to making it visible to everyone…