From 77b289a0a42ebe5edab2a286a93ba713041151df Mon Sep 17 00:00:00 2001 From: Aki Vehtari Date: Tue, 19 Mar 2024 20:55:34 +0200 Subject: [PATCH 1/2] catch Stan log_prob exceptions --- R/loo_moment_matching.R | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/R/loo_moment_matching.R b/R/loo_moment_matching.R index b0c2cff5..bcfcc296 100644 --- a/R/loo_moment_matching.R +++ b/R/loo_moment_matching.R @@ -286,7 +286,7 @@ 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, @@ -294,6 +294,12 @@ loo_moment_match_i <- function(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 @@ -310,7 +316,7 @@ 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, @@ -318,6 +324,12 @@ loo_moment_match_i <- function(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 @@ -336,7 +348,7 @@ 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, @@ -344,7 +356,13 @@ loo_moment_match_i <- function(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 From 36ffa4dab1dacf6bcbf31ee47cbff8a0889d0d5b Mon Sep 17 00:00:00 2001 From: Aki Vehtari Date: Wed, 20 Mar 2024 09:33:46 +0200 Subject: [PATCH 2/2] Apply suggestions from code review by jgabry Co-authored-by: Jonah Gabry --- R/loo_moment_matching.R | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/R/loo_moment_matching.R b/R/loo_moment_matching.R index bcfcc296..b8832e7e 100644 --- a/R/loo_moment_matching.R +++ b/R/loo_moment_matching.R @@ -286,7 +286,7 @@ loo_moment_match_i <- function(i, # 1. match means trans <- shift(x, uparsi, lwi) # gather updated quantities - quantities_i <- tryCatch(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, @@ -295,7 +295,7 @@ loo_moment_match_i <- function(i, is_method = is_method, ...) ) - if (is_try_error(quantities_i)) { + if (inherits(quantities_i, "try-error")) { # Stan log prob caused an exception probably due to under- or # overflow of parameters to invalid values break @@ -325,7 +325,7 @@ loo_moment_match_i <- function(i, is_method = is_method, ...) ) - if (is_try_error(quantities_i)) { + if (inherits(quantities_i, "try-error")) { # Stan log prob caused an exception probably due to under- or # overflow of parameters to invalid values break @@ -358,7 +358,7 @@ loo_moment_match_i <- function(i, ...) ) - if (is_try_error(quantities_i)) { + if (inherits(quantities_i, "try-error")) { # Stan log prob caused an exception probably due to under- or # overflow of parameters to invalid values break