Non-centered parameterization with boundaries for multiple coefficients

Hi, I would like to know if it is possible to declare bounded random effects with non-centered parameterization for multiple coefficients.

I read a very elegant solution here. This is great for one coefficient.

I am trying to do this for multiple coefficients. Currently my code goes like this:

data {
  int<lower=0> I; // number of groups
  int<lower=0> K; // number of coefficients
  int<lower=0> R; // number of  group-level covariates
  matrix[I, R] Z; // group-level covariates (with intercept)
...
}

parameters{
  matrix[R, K] beta; //mean-slopes for all groups
  row_vector<lower=0>[K] sig; // scales for group-level coefficients
  matrix[I, K] eta;  // std_normal errors for group-level coefficients
...
}

transformed parameters{
  matrix[I, K] zeta; // group-level coefficients

  for (k in 1:K)
    zeta[,k] = Z*beta[,k] + eta[,k]*sig[k];
...
}

When the group-level coefficients are unbounded, I just need to specify the the number of variables and this code runs quickly and well.

I don’t know how to code it up so that it can handle the boundaries in my situation, even if the boundaries are the same for all the coefficients. I understand that if the number of coefficients (K) is fixed, I can just code up K different zeta’s brute-force. But I would like to know if there is a more flexible solution.

If one can offer any suggestion, I would much appreciate it.

Hi @sonicking, wondering if you found a solution to this or if anything changed in 2025

I have my parameters setup like

  vector<lower=0>[M] mu_beta;
  vector<lower=0>[M] sigma_beta;
  array[G] vector<lower=-mu_beta/sigma_beta>[M] beta_raw;

Not sure if this makes sense from a design or feasibility standpoint, but can we setup something in STAN where the the vector bounds are broadcast element-wise across the G arrays for each element in M? @Bob_Carpenter

Do you mean you have multiple coefficients with different constraints? Or that you have multiple coefficients and you want the linear predictor to be constrained? The latter is much much harder and requires one-off problem-specific solutions. If you can say more about what variable you are trying to constrain and what the constraint is, it’ll be easier to suggest a solution.

In some cases, yes, but I’m pretty sure that what you wrote is beyond what Stan can handle. I’m guessing you meant to write mu_beta ./ sigma_beta for elementwise division (note the . before the \) and you wanted the constraint to hold for each of the G elements of beta_raw[m, 1:G]. You can write something like this:

data {
  int<lower=0> N;
  vector[N] lb;
  ...
parameters {
  vector<lower=lb> alpha[N];  // alpha[n] gets lower bound lb[n]
}

P.S. It's "Stan" because it's named after Stanislaw Ulam, rather than being an acronym.