diff --git a/R/forge.R b/R/forge.R index cfed28ce..2ac8705d 100644 --- a/R/forge.R +++ b/R/forge.R @@ -106,9 +106,7 @@ forge <- function( } omega <- omega[wt > 0] - if (nrow(omega) == 0) { - stop("All leaves have zero likelihood.") - } else if (nrow(omega) == 1) { + if (nrow(omega) == 1) { draws <- omega[, .(f_idx)] } else { # Draw random leaves with probability proportional to weight diff --git a/R/utils.R b/R/utils.R index 03770de5..4d8523d8 100644 --- a/R/utils.R +++ b/R/utils.R @@ -235,8 +235,13 @@ leaf_posterior <- function(params, evidence) { , by = f_idx] # Normalize, export - out <- unique(psi[wt > 0, .(f_idx, wt)]) + if (nrow(out) == 0) { + # If all leaves have zero weight, choose one randomly + warning("All leaves have zero likelihood. This is probably because evidence contains an (almost) impossible combination. For categorical data, consider setting alpha>0 in forde().") + out <- unique(psi[, .(f_idx)]) + out[, wt := 1] + } out[, wt := (wt / max(wt, na.rm = T))^(nrow(evidence) + 1)][wt > 0, wt := wt / sum(wt)] return(out[]) } @@ -256,7 +261,7 @@ leaf_posterior <- function(params, evidence) { post_x <- function(x, params) { # To avoid data.table check issues - variable <- NULL + variable <- val <- NULL # Order, classify features meta_tmp <- params$meta[variable %in% colnames(x)] diff --git a/man/post_x.Rd b/man/post_x.Rd index eafce015..32c072a9 100644 --- a/man/post_x.Rd +++ b/man/post_x.Rd @@ -12,5 +12,5 @@ post_x(x, params) \item{params}{Circuit parameters learned via \code{\link{forde}}.} } \description{ -This function prepares output data for map and forge. +This function prepares output data for forge. }