Hi,
At least when using stan 2.11 on Windows 7, I was able to run the following program and estimate q
.
-
model.stan
:
data {
int N;
int<lower=0, upper=1> Y[N];
}
parameters {
real<lower=0, upper=1> q;
}
model {
for (n in 1:N)
target += log_sum_exp(
log(0.5) + bernoulli_lpmf(Y[n] | q),
log(0.5) + bernoulli_lpmf(Y[n] | 1)
);
}
-
run-model.R
:
library(rstan)
Y <- c(1,0,1,0,0,1,1,0,1,1,0,1,0)
N <- length(Y)
data <- list(N=N, Y=Y)
fit <- stan(file='model.stan', data=data, seed=1234)
However, if I use the current version (stan 2.17) on Windows 7, the following error message will appear.
SAMPLING FOR MODEL 'model' NOW (CHAIN 1).
Rejecting initial value:
Log probability evaluates to log(0), i.e. negative infinity.
Stan can't start sampling from this initial value.
...
[1] "Error in sampler$call_sampler(args_list[[i]]) : Initialization failed."
[1] "error occurred during calling the sampler; sampling not done"
The reason is that log(0.5) + bernoulli_lpmf(Y[n] | 1)
will be negative infinity when Y[n] = 0
. Does the current version of log_sum_exp
evaluate all cases first? Is this expected behavior?
If so, do I need to write as follows?
data {
int N;
int<lower=0, upper=1> Y[N];
}
parameters {
real<lower=0, upper=1> q;
}
model {
for (n in 1:N) {
if (Y[n] == 0) {
target += bernoulli_lpmf(Y[n] | q);
} else {
target += log_sum_exp(
log(0.5) + bernoulli_lpmf(Y[n] | q),
log(0.5) + bernoulli_lpmf(Y[n] | 1)
);
}
}
}
Regards,
Kentaro