Why the SE of waic is zero

hi, i meet an issue and need some suggestions.
I would like to replicate the results for rat tumor in BDA chapter 5.1
my model is
y_i \sim Binomial(n_i,\theta_i) \\ \theta_i \sim Beta (\alpha, \beta) \\ \pi (\alpha, \beta) = 1


I want to get the value of waic. The Stan codes is as follows,

bb_stan <- '
data {
  int<lower = 0> N;
  array[N] int  n;
  array[N] int y;
}

parameters {
  real<lower=0> alpha;
  real<lower=0> beta;
  array[N] real<lower=0, upper=1>  theta;
}

model {
  for (i in 1:N) {
    theta[i] ~ beta(alpha, beta);
  }
   y ~ binomial(n,theta);
}

generated quantities {
  vector[N] log_lik;
  for (i in 1:N) {
    log_lik[i] = binomial_lpmf(y|n,theta);
  }
}

'
write(bb_stan,file ='bb.stan')

data(rat,package = 'bang')

data_for_bb <- list(N = length(rat$n),n = rat$n,y = rat$y)
library(rstan)
fit.bb <- stan(file = "bb.stan", data = data_for_bb)
library(loo)
log_lik <- extract_log_lik(fit.bb, parameter_name = "log_lik", merge_chains = TRUE)
waic(log_lik)

however, the ouput looks like wrong because the SE of waic is zero.

This one returns the same value for all i. Replace it by:
log_lik[i] = binomial_lpmf(y|n,theta[i]);

Should be

    log_lik[i] = binomial_lpmf(y[i] | n,theta[i]);

that is, we need to compute log_lik[i] for each observation y[i] separately.

Note also that it is better to use elpd_loo as it is generally more accurate and has better diagnostic when it might be unreliable.

thank you. the code can work.

if my model is

y_j \sim binomial(n_j,\theta_j) \\ \theta_j \sim beta(\alpha,\beta) \\ \pi(\alpha,\beta) \propto (\alpha+\beta)^{-2/5} \\

my stan code for model block is,

model {
  target += -2.5*log(alpha + beta);
  for (i in 1:N) {
    theta[i] ~ beta(alpha, beta);
    target += binomial_lpmf(y[i]|n[i],theta[i]);
  }
}

the message is Stan model ‘anon_model’ does not contain samples.
why?

why not

log_lik[i] = binomial_lpmf(y[i] | n[i],theta[i]);

Of course! Missed n[i] in my answer

i think the result is reasonable.


i believe the codes are right.

1 Like