Skip to content

Commit 1ccfc44

Browse files
authored
Merge pull request #262 from stan-dev/moment-matching-catch-stan-exceptions
catch Stan log_prob exceptions inside moment matching
2 parents 52bc270 + 36ffa4d commit 1ccfc44

1 file changed

Lines changed: 21 additions & 3 deletions

File tree

R/loo_moment_matching.R

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -286,14 +286,20 @@ loo_moment_match_i <- function(i,
286286
# 1. match means
287287
trans <- shift(x, uparsi, lwi)
288288
# gather updated quantities
289-
quantities_i <- update_quantities_i(x, trans$upars, i = i,
289+
quantities_i <- try(update_quantities_i(x, trans$upars, i = i,
290290
orig_log_prob = orig_log_prob,
291291
log_prob_upars = log_prob_upars,
292292
log_lik_i_upars = log_lik_i_upars,
293293
r_eff_i = r_eff_i,
294294
cores = 1,
295295
is_method = is_method,
296296
...)
297+
)
298+
if (inherits(quantities_i, "try-error")) {
299+
# Stan log prob caused an exception probably due to under- or
300+
# overflow of parameters to invalid values
301+
break
302+
}
297303
if (quantities_i$ki < ki) {
298304
uparsi <- trans$upars
299305
total_shift <- total_shift + trans$shift
@@ -310,14 +316,20 @@ loo_moment_match_i <- function(i,
310316
# 2. match means and marginal variances
311317
trans <- shift_and_scale(x, uparsi, lwi)
312318
# gather updated quantities
313-
quantities_i <- update_quantities_i(x, trans$upars, i = i,
319+
quantities_i <- try(update_quantities_i(x, trans$upars, i = i,
314320
orig_log_prob = orig_log_prob,
315321
log_prob_upars = log_prob_upars,
316322
log_lik_i_upars = log_lik_i_upars,
317323
r_eff_i = r_eff_i,
318324
cores = 1,
319325
is_method = is_method,
320326
...)
327+
)
328+
if (inherits(quantities_i, "try-error")) {
329+
# Stan log prob caused an exception probably due to under- or
330+
# overflow of parameters to invalid values
331+
break
332+
}
321333
if (quantities_i$ki < ki) {
322334
uparsi <- trans$upars
323335
total_shift <- total_shift + trans$shift
@@ -336,15 +348,21 @@ loo_moment_match_i <- function(i,
336348
if (cov) {
337349
trans <- shift_and_cov(x, uparsi, lwi)
338350
# gather updated quantities
339-
quantities_i <- update_quantities_i(x, trans$upars, i = i,
351+
quantities_i <- try(update_quantities_i(x, trans$upars, i = i,
340352
orig_log_prob = orig_log_prob,
341353
log_prob_upars = log_prob_upars,
342354
log_lik_i_upars = log_lik_i_upars,
343355
r_eff_i = r_eff_i,
344356
cores = 1,
345357
is_method = is_method,
346358
...)
359+
)
347360

361+
if (inherits(quantities_i, "try-error")) {
362+
# Stan log prob caused an exception probably due to under- or
363+
# overflow of parameters to invalid values
364+
break
365+
}
348366
if (quantities_i$ki < ki) {
349367
uparsi <- trans$upars
350368
total_shift <- total_shift + trans$shift

0 commit comments

Comments
 (0)