Repository navigation
Review #363 (3/19): Fix pred_measure input checks: psis_object, dims, posthoc, custom names - #426
florence-bockting wants to merge 2 commits into
Conversation
… posthoc, custom names
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## pred_measure #426 +/- ##
================================================
+ Coverage 91.89% 92.03% +0.13%
================================================
Files 38 38
Lines 4751 4796 +45
================================================
+ Hits 4366 4414 +48
+ Misses 385 382 -3 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
This is how benchmark results would change (along with a 95% confidence interval in relative change) if fe79552 is merged into pred_measure:
Further explanation regarding interpretation and methodology can be found in the documentation. |
jgabry
left a comment
There was a problem hiding this comment.
Thanks, I think this is in good shape. I made a couple of suggestions.
| ylp_test = ylp_test, | ||
| psis_object = psis_object$log_weights | ||
| ) | ||
| draws <- draws[!vapply(draws, is.null, logical(1L))] |
There was a problem hiding this comment.
Do we want to support mupred or ypred being a vector? If mupred or ypred is a vector, this line keeps it that way, but then nrow() below at line 361 returns NULL, so the vapply() at line 361 errors (values must be length 1, but FUN(X[[1]]) result is length 0). The measure_*() docs say a length-n vector is accepted, and there's a message about "Coercing mupred from vector to 1 x n matrix". For a vector ypred, the old error (ypred must be a numeric matrix or array) I think was more helpful too.
I think skipping inputs that have no dimensions fixes both cases and lets the later checks throw the errors:
| draws <- draws[!vapply(draws, is.null, logical(1L))] | |
| draws <- Filter(function(x) length(dim(x)) >= 2L, draws) |
| #' @noRd | ||
| ps_khat_threshold <- function(S, ...) { | ||
| min(1 - 1 / log10(S), 0.7) | ||
| min(1 - 1 / log10(S), 0.7, na.rm = TRUE) |
There was a problem hiding this comment.
What's the reason for the NA handling here? I'm not actually sure that we can get an NA the way things currently are coded. Not a big deal either way, just curious.
| if (source == "loo") { | ||
| if (is.null(predperf)) { | ||
| if (!is.null(loo) && is.null(loo$psis_object)) { | ||
| if (!is.null(loo) && is.null(loo$psis_object) && is.null(psis_object)) { |
There was a problem hiding this comment.
Before this PR, a loo without a psis_object plus a separate psis_object errored with No psis_object found in loo, and I think that's probably fine.
Supporting this seems to require a bunch of things:
- the Pareto k branch in .check_psis_match() (L422-426), including the k = 0 skip for reloo
- the pareto_k line at L369 of this file, which skips the check for posthoc
- the new psis_object argument to .detect_posthoc() (L290 and L293)
- the test at L406-424 in test_pred_measure.R and the second half of the test at L124 (L134-143)
Is there a use case for this? If so that's fine. Who would have a matching psis_object but not have it saved in the loo object? Am I overlooking something? If there's not a real use case for this then I'd lean towards keeping the old error. And then .check_psis_match() only needs to compare log weights.
| if (is.null(log_weights)) { | ||
| log_weights <- predperf$psis_object$log_weights | ||
| } | ||
| .check_psis_match(psis_object, log_weights, NULL, "predperf") |
There was a problem hiding this comment.
For a plain pred_measure() update, above on L133-136 it takes psis_object from predperf. Then in this check it compares the stored weights with themselves, so every update runs all.equal() on the full S x N matrix, which adds a little bit of overhead. It's not a lot, so not a big deal, but maybe only run the check when the user actually passed a psis_object?
PR 3/19 for the review of #363.
1. No
is.psis()check onpsis_objectReview comments: r4544558130, main review
Implementation: a new helper
.validate_psis_object()(R/pred_measure-helpers.R) runs at the start ofloo_pred_measure()andpred_measure(). ANULLor apsisobject passes. Anything else gives an error. If the object is alooobject, the error says to pass it tolooinstead. The other wrappers already reject apsis_object.Tests: a
looobject aspsis_objectinloo_pred_measure()and a list inpred_measure()give the error.2. A new
psis_objectreplaces the saved weightsReview comment: r4544529644
Decision: an error, with the
all.equal()check you suggested.Implementation: a new helper
.check_psis_match()(R/pred_measure-compute.R) is used in.get_psis_object()for bothlooandpredperf:all.equal(). A difference gives an error.looobject withsave_psis = FALSEand a separatepsis_object), it compares the k values. It skips observations with k = 0, becausebrms::reloo()sets k to 0 for refitted observations and sets no attribute. It skips the check ifloohas aposthocattribute (moment matching changes k).Before, a
looobject withsave_psis = FALSEplus apsis_objectalways gave an error, because the code compared the weights withNULL. That case now works.Tests: different weights in
pred_measure()give the error, the same weights work. Asave_psis = FALSElooobject with a matchingpsis_objectworks, with different k values it gives the error.3. No post-hoc warning when measures are added
Review comment: r4544507747
Implementation:
pred_measure()gets nolooobject, so it cannot detect the post-hoc method again. The method is now detected once when the result is created and stored asattr(x, "posthoc")..warn_posthoc()takes the method instead of thelooobject and runs for new results and for updates..detect_posthoc()reads the k values from thepsis_objectin use, so reloo is also detected when the weights come from a separatepsis_object.Tests: after moment matching,
loo_pred_measure(measures = "elpd")gives no warning and stores the attribute. Adding"mae"withpred_measure()then gives the warning. A reloo case with a separatepsis_objectgives the warning too.4.
dimsis unreliable, andNis not checkedReview comments: r4544658480, r4544661337, r4544939581, main review
Implementation:
.pred_measure_dims()(R/pred_measure-helpers.R) runs once at the start ofdo_pred_measure(). It reads the number of drawsSand observationsNfrom every input:y,ypred,mupred,ylp,ylp_test, thepsis_objectweights, thelooandkfoldobjects, and the storeddimsofpredperf. If two inputs disagree, it gives one error that lists each input and its size. It returnsc(S, N).SisNAif no input has draws (akfoldobject only). A later update with draws fills it in. Fortest_pred_measure(), the trainingylphas a differentNand is not read..add_attributes()setsdimsfrom this value. Its twodimsblocks and the five arguments that only fed them are removed.print()shows "Computed from N observations." ifSisNA. The fallback to the dimensions oflog_weightsis removed.ps_khat_threshold()(R/diagnostics.R) returns the 0.7 cap ifSisNA, sodiag_elpd()inmodel_compare()does not fail. The other callers always knowS.Limit: a transposed matrix with
S = Ncannot be detected.Tests:
dimsisc(S, N)for in-sample, LOO from apsis_objectonly, k-fold (c(NA, N), and the print header), and a k-fold update. A transposedylpgives the error for observations. Aypredwith fewer draws gives the error for draws, also inpred_measure(). Two results from apsis_objectonly compare withmodel_compare()without an error.ps_khat_threshold(NA)is 0.7.5.
custom_measure()does not checknameReview comment: r4544492688
Implementation: the checks are in
.measure_entry_custom()(R/pred_measure-helpers.R), not incustom_measure(). Bothcustom_measure(fun, name)and a list name such asmeasures = list(elpd = fun)go through this function. The name must be a single non-empty string (checkmate::test_string()), and must not be a built-in measure or"p".custom_measure()usescli::cli_abort()for thefuncheck.Tests:
1,NA,"",NULLandc("a", "b")give the "needs a name" error."elpd","rmse","srps","p"andlist(elpd = fun)give the "reserved" error.Test runs
test_pred_measure.R,test_pred_measure_helpers.R,test_pred_measure_builtin.R,test_compare.Randtest_print_plot.Rpass. The new tests in test_pred_measure.R, test_pred_measure_helpers.R, test_compare.R and test_print_plot.R fail on the base branch. The exception is the .get_psis_object() mismatch test: the old code already gave an error there, with a different message.This PR was assisted by use of LLM.