Skip to content

Commit

Permalink
random leaf if all zero + warning
Browse files Browse the repository at this point in the history
  • Loading branch information
mnwright committed Jan 19, 2024
1 parent fd8f36a commit b784680
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 6 deletions.
4 changes: 1 addition & 3 deletions R/forge.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 7 additions & 2 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -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[])
}
Expand All @@ -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)]
Expand Down
2 changes: 1 addition & 1 deletion man/post_x.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b784680

Please sign in to comment.