Hello Stan users,
I wanted to create a function which will sample from N(mu, sigma) T[0,1]
with some help I manage to create the function and it worked well. Here is the code:
functions {
real normal_lub_rng(real mu, real sigma, real lb, real ub) {
real p_lb = normal_cdf(lb, mu, sigma);
real p_ub = normal_cdf(ub, mu, sigma);
real v = uniform_rng(p_lb, p_ub);
real y = mu + sigma * inv_Phi(v);
return y;
}
}
generated quantities {
real u[N];
real alpha_u;
real<lower=0> sigma_u;
alpha_u = normal_rng(0.20,0.10);
sigma_u = gamma_rng(0.15,1);
for (i in 1:N)
u[i] = normal_lub_rng(alpha_u, sigma_u,0.01,1);
}
In the above function, The mean mu is real. But now I want it to be a vector so I came up with the code
functions {
real normal_lub_rng(real mu, real sigma, real lb, real ub) {
real p_lb = normal_cdf(lb, mu, sigma);
real p_ub = normal_cdf(ub, mu, sigma);
real v = uniform_rng(p_lb, p_ub);
real y = mu + sigma * inv_Phi(v);
return y;
}
}
data {
int<lower=0> N; // Number of obs
}
generated quantities {
real u[N];
real alpha_u[N] ;
real<lower=0> sigma_u;
sigma_u = gamma_rng(0.15,1);
for (i in 1:N) {
alpha_u[i] = normal_rng(0.20,0.10);
u[i] = normal_lub_rng(alpha_u[i], sigma_u,0.01,1);
}
}
But I am getting an error here
> SYNTAX ERROR, MESSAGE(S) FROM PARSER:
Exception: Exception: uniform_rng: Upper bound parameter is 1, but must be greater than 1 (in ‘unknown file name’ at line 8)
I am not sure what I am doing wrong. Can some one please help.
Cheers