Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

avoid under and overflows in stacking #273

Merged
merged 2 commits into from
Aug 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 6 additions & 9 deletions R/loo_model_weights.R
Original file line number Diff line number Diff line change
Expand Up @@ -257,15 +257,12 @@ stacking_weights <-
stop("At least two models are required for stacking weights.")
}

exp_lpd_point <- exp(lpd_point)
negative_log_score_loo <- function(w) {
# objective function: log score
stopifnot(length(w) == K - 1)
w_full <- c(w, 1 - sum(w))
sum <- 0
for (i in 1:N) {
sum <- sum + log(exp(lpd_point[i, ]) %*% w_full)
}
# avoid over- and underflows using log weights and rowLogSumExps
sum <- sum(matrixStats::rowLogSumExps(sweep(lpd_point[1:N,], 2, log(w_full), '+')))
return(-as.numeric(sum))
}

Expand All @@ -274,11 +271,11 @@ stacking_weights <-
stopifnot(length(w) == K - 1)
w_full <- c(w, 1 - sum(w))
grad <- rep(0, K - 1)
# avoid over- and underflows using log weights, rowLogSumExps,
# and by subtracting the row maximum of lpd_point
mlpd <- matrixStats::rowMaxs(lpd_point)
for (k in 1:(K - 1)) {
for (i in 1:N) {
grad[k] <- grad[k] +
(exp_lpd_point[i, k] - exp_lpd_point[i, K]) / (exp_lpd_point[i,] %*% w_full)
}
grad[k] <- sum((exp(lpd_point[, k] - mlpd) - exp(lpd_point[, K] - mlpd)) / exp(matrixStats::rowLogSumExps(sweep(lpd_point, 2, log(w_full), '+')) - mlpd))
}
return(-grad)
}
Expand Down
Loading