Skip to content

Add psis_object argument to loo() matrix and array methods - #427

Open
avehtari wants to merge 1 commit into
masterfrom
loo-psis-object
Open

avehtari wants to merge 1 commit into
masterfrom
loo-psis-object

Conversation

@avehtari

Copy link
Copy Markdown
Member

Summary

loo.matrix() and loo.array() get a new argument psis_object. With it, loo() uses the weights of a given psis, sis or tis object and does not compute the importance ratios from -x. The result has the same structure and classes as the result of loo() without psis_object.

Motivation

Some downstream code computes LOO for a log-likelihood x with importance ratios that are not -x. An example is the new brms function as_brmsfit() for projpred projections. There, x is the log-likelihood of the projected draws. The importance ratios are -ll_ref, the negative log-likelihood of the reference model draws from which the projected draws come. To get a psis_loo object, the brms branch now calls three loo internals: pointwise_loo_calcs(), importance_sampling_loo_object() and psis_n_eff(). With this PR, the same object can come from one exported call:

psis_ref <- psis(-ll_ref, r_eff = 1)
loo(ll_sub, psis_object = psis_ref, r_eff = r_eff, save_psis = TRUE)

The argument name and its meaning are the same as in E_loo() and in loo_pred_measure() of #363 (for v3.0.0).

Behavior

  • psis_object must have the same number of draws and observations as x. For an array x, the draw order is that of psis() for an array of the same shape (chains stacked).
  • is_method comes from psis_object. An error occurs if the user gives a different is_method. A wrapper method that forwards the full default vector c("psis", "tis", "sis") does not cause an error.
  • loo() ignores cores.
  • r_eff: if it is NULL or not given, loo() uses the r_eff of psis_object (1 if the object has none). Otherwise r_eff replaces the r_eff of psis_object for n_eff and the MCSE of elpd_loo. NA values in r_eff become 1, as in psis().
  • loo() does not repeat the Pareto smoothing, so the tail length and the Pareto k values stay those of psis_object. You can therefore combine a tail length for r_eff = 1 with an MCSE that uses the r_eff of the chains, as in brms as_brmsfit(). posterior::ps_tail_length() also ignores r_eff.
  • With save_psis = TRUE, the result stores psis_object, with the r_eff and n_eff that loo() used. For a sis or tis object, the stored element is sis_object or tis_object, as before this PR.
  • loo.function() stops with an error if it gets psis_object. Before this PR, the argument went through ... to the log-likelihood function.
  • The documentation says that the Pareto k values diagnose the importance ratios only. It also says that loo() does not repeat the Pareto k warnings of psis().

Note for downstream packages: loo 2.10.1 accepts psis_object through ... and ignores it without an error or warning. A package that uses the argument must require the first loo version that contains this PR.

Tests

New file tests/testthat/test_loo_psis_object.R (9 test_that() blocks, 48 expectations). It checks that:

  • loo(x, psis_object = psis(-x, r_eff = r)) is identical to loo(x, r_eff = r, save_psis = TRUE), for the matrix and the array method;
  • with ratios from other draws, elpd_loo, p_loo and mcse_elpd_loo equal direct computations, also log(E_loo(exp(x), psis_object, log_ratios = ...)$value);
  • a given r_eff changes n_eff and the MCSE but not pareto_k, the log weights or the tail length;
  • NULL, NA and scalar r_eff work as documented;
  • sis and tis objects give sis_loo and tis_loo objects;
  • a wrapper method works, and loo_compare() and print() work;
  • wrong input gives errors (class, dimensions, method, loo.function()).

NEWS item (proposal)

loo() matrix and array methods get the argument psis_object to use given importance weights.

This PR was assisted by Claude

@codecov-commenter

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 96.00000% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 92.78%. Comparing base (3079dca) to head (8aa7aff).

Files with missing lines Patch % Lines
R/loo.R 96.00% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #427      +/-   ##
==========================================
- Coverage   92.88%   92.78%   -0.11%     
==========================================
  Files          31       31              
  Lines        3289     3117     -172     
==========================================
- Hits         3055     2892     -163     
+ Misses        234      225       -9     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@github-actions

Copy link
Copy Markdown
Contributor

This is how benchmark results would change (along with a 95% confidence interval in relative change) if 8aa7aff is merged into master:

Status Name Current Incoming Interval
✔️ loo_matrix 1.39s 1.39s [-2.11%, +1.35%]
✔️ loo_function 1.22s 1.22s [-1.32%, +0.66%]

Further explanation regarding interpretation and methodology can be found in the documentation.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants