Skip to content

Exported constructor for loo objects from pointwise values? #429

Backlog

Description

@avehtari

Different packages build loo, kfold and psis_loo_ss objects by hand from pointwise elpd values. They copy the structure of the object. This includes the element names, the estimates table (sum and sqrt(N * var)), the classes, the dims attribute and, for subsampling, the loo_subsampling list. If the object structure changes (for example in v3.0.0, #379 and #363), this code breaks without notice. An exported constructor would keep the structure in one place.

Code that builds loo objects by hand now:

package where builds or updates
brms kfold.brmsfit() (R/kfold.R) kfold
brms reloo() (R/reloo.R) pointwise and estimates
rstanarm 2.32.2 kfold.stanreg() (R/loo-kfold.R) kfold
brms, branch projpred-brmsfit as_brmsfit() projpred_loo, kfold
brms, branch projpred-brmsfit as_brmsfit() psis_loo_ss
loo loo_moment_match() pointwise and estimates

In the brms branch projpred-brmsfit, projpred cv_varsel(validate_search = TRUE, nloo = m) gives validated pointwise elpd values for m < N observations and fast approximations for all N observations. as_brmsfit() makes a psis_loo_ss object from these values. Then loo_compare() with the reference model gives the subsampling SE. For this, the branch builds the object by hand and calls loo:::loo_subsample_estimation_diff_srs() and loo:::assert_psis_loo_ss(). loo_subsample() does not fit this case, because it computes the pointwise elpd values itself from a log-likelihood function. With the export of srs_diff_est() (#340), brms can compute the estimates without internals, but it must still copy the object structure.

A Discourse answer (https://discourse.mc-stan.org/t/36666) recommends a similar step to users. They average the pointwise elpd values of several cross-validations and save them back to a loo or kfold object.

Sketch

Two exported functions that wrap existing internals:

loo_object(pointwise, dims, type = c("psis", "sis", "tis", "kfold", "elpd"),
           diagnostics = NULL, psis_object = NULL)
loo_ss_object(pointwise, observations, elpd_loo_approx, dims,
              estimator = c("diff_srs", "srs"), diagnostics = NULL)
  • loo_object() checks the column names of pointwise for type and computes estimates. It reuses importance_sampling_loo_object() (types psis, sis, tis), elpd_object() (type elpd) and table_of_estimates() (type kfold).
  • loo_ss_object() takes the pointwise values of the m subsampled observations, their indices observations, and elpd_loo_approx for all N observations. It reuses psis_loo_ss_object() and the existing estimators. psis_loo_ss_object() requires a log-likelihood function .llfun today. It must accept .llfun = NULL. update.psis_loo_ss() must then stop with an error that says the object has no log-likelihood function.
  • loo_moment_match(), brms::reloo() and similar code can call loo_object() again with the changed pointwise values to get new estimates.

Questions

  1. Should the constructor go into 2.x, or only into v3.0.0 with the new object types of Adding pred_measure feature #363?
  2. One function with a type argument, or one function per class?
  3. Names: loo_object() and loo_ss_object(), or other names?
  4. Which elements of such objects do the pred_measure objects of Adding pred_measure feature #363 and model_compare() need?

ping @florence-bockting, @jgabry

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions