I am writing a stan code for the CDF of the Inverse-Gaussian distribution. But when I tried to run the code I get the following error.
SYNTAX ERROR, MESSAGE(S) FROM PARSER:
variable “real” does not exist.
ERROR at line 13
12:
13: real myIG_ccdf(vector x, real mu, real lambda){
^
14: real a = sqrt (lambda/x)*(x/mu - 1);
Error in stanc(file = file, model_code = model_code, model_name = model_name, :
failed to parse Stan model ‘15c519c69731922b93d547aa754ec64f’ due to the above error.
Can someone please help me to resolve this problem? Thank you.
real IG_log (vector x, real mu, real lambda){
vector [num_elements (x)] prob;
real lprob;
for (i in 1:num_elements(x)) {
prob[i] <- (lambda/(2*pi()*(x[i]^3)))^0.5*exp(-lambda*(x[i] - mu)^2/(2*mu^2*x[i]));
}
lprob <- sum (log(prob));
return lprob;
real myIG_ccdf(vector x, real mu, real lambda){
real a = sqrt (lambda/x)*(x/mu - 1);
real b = -sqrt(lambda/x)*(x/mu + 1);
return log(1 - normal_cdf(a, 0, 1) + exp(2*lambda/mu)*normal_cdf(b, 0, 1));
}
}
}
data {
int<lower=0> J; // number of observations
int<lower=0> N_cen; // number of censored observations
vector [J]sampledata; // failure in hours
vector [N_cen]y_cen; //values of censored data
}
parameters {
real<lower=0> mu; // mean
real<lower=0> lambda; // shape
}
model {
//define priors of mu and lambda
mu ~ gamma (0.1, 0.1);
lambda ~ gamma (0.1, 0.1);
//Likelihood of the data
sampledata ~ IG(mu, lambda);
increment_log_prob(IG_ccdf(y_cen, mu, lambda));
}
"
mod_IG <- stan_model(model_code = model_IG)
Thank you @nhuurre. I corrected that and the edited code is as follows.
model_IG <- "functions {
real IG_log (vector x, real mu, real lambda){
vector [num_elements (x)] prob;
real lprob;
for (i in 1:num_elements(x)) {
prob[i] <- (lambda/(2*pi()*(x[i]^3)))^0.5*exp(-lambda*(x[i] - mu)^2/(2*mu^2*x[i]));
}
lprob <- sum (log(prob));
return lprob;
}
real myIG_ccdf(vector x, real mu, real lambda){
real a = (sqrt (lambda/x)*(x/mu - 1));
real b = -sqrt(lambda/x)*(x/mu + 1);
return log(1 - normal_cdf(a, 0, 1) + exp(2*lambda/mu)*normal_cdf(b, 0, 1));
}
}
data {
int<lower=0> J; // number of observations
int<lower=0> N_cen; // number of censored observations
vector [J]sampledata; // failure in hours
vector [N_cen]y_cen; //values of censored data
}
parameters {
real<lower=0> mu; // mean
real<lower=0> lambda; // shape
}
model {
//define priors of mu and lambda
mu ~ gamma (0.1, 0.1);
lambda ~ gamma (0.1, 0.1);
//Likelihood of the data
sampledata ~ IG(mu, lambda);
increment_log_prob(myIG_ccdf(y_cen, mu, lambda));
}
"
But now I get the following error.
SYNTAX ERROR, MESSAGE(S) FROM PARSER:
ERROR at line 14
13: real myIG_ccdf(vector x, real mu, real lambda){
14: real a = (sqrt (lambda/x)(x/mu - 1));
^
15: real b = -sqrt(lambda/x)(x/mu + 1);
PARSER EXPECTED: “;”
Error in stanc(file = file, model_code = model_code, model_name = model_name, :
failed to parse Stan model ‘5458ffa1ec28bc85b5833f5cb8f2a9b2’ due to the above error.
That error message is not helpful but I think the problem is that x is a vector and Stan doesn’t know how to divide by a vector. You can write a loop
real myIG_ccdf(vector x, real mu, real lambda){
real lp = 0.0;
for (xx in x) {
real a = (sqrt (lambda/xx)*(xx/mu - 1));
real b = -sqrt(lambda/xx)*(xx/mu + 1);
lp += log(1 - normal_cdf(a, 0, 1) + exp(2*lambda/mu)*normal_cdf(b, 0, 1));
}
return lp;
}