# How to sample from parameter distribution within specific range

I want to be incorporate dynamic effects in a multilevel model and therefore I want to incorporate the term \rho y_{t-1} in my model. Because of stationarity restrictions \rho has to be on the interval [-1,1].

Therefore I model \rho as \rho = 2*\rho_{raw} -1, with rho_{raw} \in [0,1]. Or in stan code:

parameters{
real<lower=0,upper=1> delta_raw;
}
transformed parameters{
real delta = delta_raw * 2 - 1;
}


Now, I want to be \delta to be in the range [-0.9,0.9]. Can I now just use real<lower=0.05,upper=0.95> delta_raw; in the transformed parameters part? I tried this, but the results of my model are not any different from real<lower=0.05,upper=0.95> delta_raw;. Can someone explain this?

Next to that, I want to model an individual specific \delta_i in my model using the non-centered parameterization approach which would look like this:

parameters {
...
real delta_raw;
real u_delta_raw[Nk];// Nk number of individuals i
real<lower=0> sigma_u_delta
...
}
transformed parameters{
real u_delta[Nk];
for(i in 1:Nk){//number of individuals i
u_delta[i] = sigma_u_delta*u_delta_raw[i];
}

for(k in 1:Nk){//number of individuals i
delta[k] = delta_raw + u_delta[k];
}
}
model {
...
u_delta_raw ~ normal(0, 1);
sigma_u_delta~normal(0,1);
...
}


However, I do not know how to keep \delta_i on the interval [-1,1]. Does someone know how to do this?

This seems needlessly convoluted. Why not just directly use “real<lower=-1.0, upper=1.0> rho” in your parameter declaration?

Because I read in a Stan tutorial that Stan did not support variables on such intervals

Which tutorial was this? I suspect that you have misunderstood, because I have never seen such a limitation documented.

It is in a tutorial written by Andrew Gelman about minimizing the number of cockroach compliants and it says exactly the following: Stan doesn’t implement any densities that have support on $[−1,1]$. We can use variable transformation of a raw variable defined on [0,1][0,1] to to give us a density on $[−1,1]$

But maybe this is already old information. However, do you maybe also know how this works in a multilevel setting mentioned above?

I think that you misunderstood what Gelman meant by “Stan doesn’t implement any densities that have support on [−1,1].” If you look at the section in the Stan Functions Reference on continuous distributions, you indeed won’t find any functions in Stan that define probability densities with support exactly on [-1,1]. However, there is a uniform density, which does have support over the interval [a,b] where a and b are arbitrary (except of course for the condition a < b). Furthermore, that is the prior density that a variable with explicitly specified upper and lower bounds will automatically have.

Thanks for the explanation!

However in my multilevel setting I get initialization errors because I define my \delta_i with <lower = -1, upper = 1> but I get errors because my delta_raw and u_delta[k] do not have such restrictions. Do you maybe know how to handle this?

Either put constraints on delta_raw and u_delta[k] or rewrite your model so that you don’t need delta_raw and u_delta[k].

I’m unsure what purpose delta_raw and u_delta[k] serve. Initially, you seemed to use delta_raw because you appeared to think that you couldn’t set lower to -1.0, but now it seems that delta_raw and u_delta are used for a different reason.

In this case, I am using non-centered parameterization for estimating \delta_i and now I estimate it using \delta_i = \delta_{raw} + u_{\delta}. However, putting constraints on both delta_raw and u_delta[k] does not necessarily mean that delta is also constrained on the given interval, right? (in my case [-1,1]). Rewriting my model not using non-centered parameterization causes a lot of divergent transitions so I have to use non-centered parameterization.

That depends on your choice of constraints for delta_raw and u_delta[k].

Your choices for the priors of u_delta_raw and sigma_u_delta, which can range from (-\infty,\infty), are inconsistent with the goal of a finite range for delta[k].

Could you maybe give an example of constraints that will do the trick in my case or prior distributions which are constrained on the interval [-1,1]?

If you have a = b + c, where b \in [b_l, b_h] and c \in [c_l,c_h], then a \in [b_l + c_l, b_h + c_h], right?