You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When performing moment matching with save_psis = TRUE, it seems like the normalizing constant in attr(loo$psis_object, "norm_const_log") does not get updated (even though the PSIS weights are updated).
where log_weights gets updated, but their normalizing constant in attr(loo$psis_object, "norm_const_log") not. I'll create a corresponding pull request for this issue. Here is a reprex:
options(mc.cores=4)
### From <https://mc-stan.org/loo/articles/loo2-moment-matching.html>, but### with `save_psis = TRUE`:stancode<-"data { int<lower=1> K; int<lower=1> N; matrix[N,K] x; int y[N]; vector[N] offset; real beta_prior_scale; real alpha_prior_scale;}parameters { vector[K] beta; real intercept;}model { y ~ poisson(exp(x * beta + intercept + offset)); beta ~ normal(0,beta_prior_scale); intercept ~ normal(0,alpha_prior_scale);}generated quantities { vector[N] log_lik; for (n in 1:N) log_lik[n] = poisson_lpmf(y[n] | exp(x[n] * beta + intercept + offset[n]));}"
library("rstan")
library("loo")
seed<-9547
set.seed(seed)
# Prepare data
data(roaches, package="rstanarm")
roaches$roach1<- sqrt(roaches$roach1)
y<-roaches$yx<-roaches[,c("roach1", "treatment", "senior")]
offset<- log(roaches[,"exposure2"])
n<- dim(x)[1]
k<- dim(x)[2]
standata<-list(N=n, K=k, x= as.matrix(x), y=y, offset=offset, beta_prior_scale=2.5, alpha_prior_scale=5.0)
# Compilestanmodel<- stan_model(model_code=stancode)
# Fit modelfit<- sampling(stanmodel, data=standata, seed=seed, refresh=0)
print(fit, pars="beta")
loo1<- loo(fit, save_psis=TRUE)
# available in rstan >= 2.21loo2<- loo(fit, moment_match=TRUE, save_psis=TRUE)
### # We now have:
identical(
attr(loo1$psis_object, "norm_const_log"),
attr(loo2$psis_object, "norm_const_log")
)
# --> TRUE# We should have (I think):
all.equal(
attr(loo2$psis_object, "norm_const_log"),
matrixStats::colLogSumExps(loo2$psis_object$log_weights)
)
# --> "Mean relative difference: 1"
When performing moment matching with
save_psis = TRUE
, it seems like the normalizing constant inattr(loo$psis_object, "norm_const_log")
does not get updated (even though the PSIS weights are updated).I think the issue is with these lines
loo/R/loo_moment_matching.R
Lines 149 to 155 in 51fb2d0
where
log_weights
gets updated, but their normalizing constant inattr(loo$psis_object, "norm_const_log")
not. I'll create a corresponding pull request for this issue. Here is a reprex:Session info:
The text was updated successfully, but these errors were encountered: