Skip to content

Commit

Permalink
catch Stan log_prob exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
avehtari committed Mar 19, 2024
1 parent 52bc270 commit 77b289a
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions R/loo_moment_matching.R
Original file line number Diff line number Diff line change
Expand Up @@ -286,14 +286,20 @@ loo_moment_match_i <- function(i,
# 1. match means
trans <- shift(x, uparsi, lwi)
# gather updated quantities
quantities_i <- update_quantities_i(x, trans$upars, i = i,
quantities_i <- tryCatch(update_quantities_i(x, trans$upars, i = i,
orig_log_prob = orig_log_prob,
log_prob_upars = log_prob_upars,
log_lik_i_upars = log_lik_i_upars,
r_eff_i = r_eff_i,
cores = 1,
is_method = is_method,
...)
)
if (is_try_error(quantities_i)) {
# Stan log prob caused an exception probably due to under- or
# overflow of parameters to invalid values
break
}
if (quantities_i$ki < ki) {
uparsi <- trans$upars
total_shift <- total_shift + trans$shift
Expand All @@ -310,14 +316,20 @@ loo_moment_match_i <- function(i,
# 2. match means and marginal variances
trans <- shift_and_scale(x, uparsi, lwi)
# gather updated quantities
quantities_i <- update_quantities_i(x, trans$upars, i = i,
quantities_i <- try(update_quantities_i(x, trans$upars, i = i,
orig_log_prob = orig_log_prob,
log_prob_upars = log_prob_upars,
log_lik_i_upars = log_lik_i_upars,
r_eff_i = r_eff_i,
cores = 1,
is_method = is_method,
...)
)
if (is_try_error(quantities_i)) {
# Stan log prob caused an exception probably due to under- or
# overflow of parameters to invalid values
break
}
if (quantities_i$ki < ki) {
uparsi <- trans$upars
total_shift <- total_shift + trans$shift
Expand All @@ -336,15 +348,21 @@ loo_moment_match_i <- function(i,
if (cov) {
trans <- shift_and_cov(x, uparsi, lwi)
# gather updated quantities
quantities_i <- update_quantities_i(x, trans$upars, i = i,
quantities_i <- try(update_quantities_i(x, trans$upars, i = i,
orig_log_prob = orig_log_prob,
log_prob_upars = log_prob_upars,
log_lik_i_upars = log_lik_i_upars,
r_eff_i = r_eff_i,
cores = 1,
is_method = is_method,
...)
)

if (is_try_error(quantities_i)) {
# Stan log prob caused an exception probably due to under- or
# overflow of parameters to invalid values
break
}
if (quantities_i$ki < ki) {
uparsi <- trans$upars
total_shift <- total_shift + trans$shift
Expand Down

0 comments on commit 77b289a

Please sign in to comment.