I’m trying to run a state-space model using rstan. I want the sum of alpha[k] and beta[k] to fall between 0 and 1 in this model. That is, 0≤alpha[1]+beta[1]≤1, 0≤alpha[2]+beta[2]≤1, … , I want to estimate alpha and beta so that 0≤alpha[k]+beta[k]≤1.
data {
int<lower=0> N_data;
int<lower=0> N_category_ism;
matrix[N_data, N_category_ism] Rews_low;
matrix[N_data, N_category_ism] Rews_med;
matrix[N_data, N_category_ism] Rews_high;
int<lower=1, upper=N_category_ism> Y[N_data];
}
parameters {
real<lower=0, upper=1> alpha[N_data];
real<lower=0, upper=1> beta[N_data];
real<lower=0> sd_alpha;
real<lower=0> sd_beta;
}
transformed parameters {
matrix[N_data, N_category_ism] Q;
for (k in 1:N_data){
for (kk in 1:N_category_ism){
Q[k, kk] = (1-alpha[k]-beta[k])*(Rews_low[k, kk]+Rews_med[k, kk]+Rews_high[k, kk]) + alpha[k]*Rews_low[k, kk] + beta[k]*((Rews_low[k, kk]+Rews_med[k, kk]+Rews_high[k, kk])-((Rews_med[k, kk]-Rews_low[k, kk])+(Rews_high[k, kk]-Rews_low[k, kk])+(Rews_high[k, kk]-Rews_med[k, kk])));
}
}
}
model {
for (k in 1:N_data){
Y[k] ~ categorical_logit(Q[k,]');
}
for (k in 2:N_data){
if ((alpha[k]+beta[k]) > 1 || (alpha[k]+beta[k]) == 0){
target += negative_infinity();
}
alpha[k] ~ normal(alpha[k-1], sd_alpha);
beta[k] ~ normal(beta[k-1], sd_beta);
}
}
generated quantities{
int<lower=1, upper=N_category_ism> Y_generated[N_data];
real log_lik[N_data];
for (k in 1:N_data){
Y_generated[k] = categorical_logit_rng(Q[k,]');
log_lik[k] = categorical_logit_lpmf(Y[k]|Q[k,]');
}
}
Initially, I thought of the following code:
parameters{
real<lower=0, upper=1> alpha[N_data];
real<lower=0, upper=1> beta[N_data];
}
transformed parameters{
real<lower=0, upper=1> gamma[N_data];
for (k in 1:N_data){
gamma[k] = alpha[k] + beta[k];
}
}
However, this approach of defining variables in the transformed parameter block and restricting them is not recommended because it is very inefficient for sampling.
So, I wrote the first code I presented, but I got the following error:
Chain 1 Rejecting initial value:
Chain 1 Log probability evaluates to log(0), i.e. negative infinity.
Chain 1 Stan can't start sampling from this initial value.
Chain 1 Initialization between (-2, 2) failed after 100 attempts.
Chain 1 Try specifying initial values, reducing ranges of constrained values, or reparameterizing the model.
I changed the init
parameter of cmdstanr::sample
to 0.5, 0.1, etc., but the same error appeared.
Is there any solution to this problem? Any comments are welcome. Thank you in advance!
R version 4.2.0 (2022-04-22)
Platform: aarch64-apple-darwin20 (64-bit)
Running under: macOS Monterey 12.2.1
cmdstanr_0.5.3