Repository navigation
Description
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 ofpointwisefortypeand computesestimates. It reusesimportance_sampling_loo_object()(typespsis,sis,tis),elpd_object()(typeelpd) andtable_of_estimates()(typekfold).loo_ss_object()takes the pointwise values of themsubsampled observations, their indicesobservations, andelpd_loo_approxfor allNobservations. It reusespsis_loo_ss_object()and the existing estimators.psis_loo_ss_object()requires a log-likelihood function.llfuntoday. 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 callloo_object()again with the changedpointwisevalues to get newestimates.
Questions
- Should the constructor go into 2.x, or only into v3.0.0 with the new object types of Adding
pred_measurefeature #363? - One function with a
typeargument, or one function per class? - Names:
loo_object()andloo_ss_object(), or other names? - Which elements of such objects do the
pred_measureobjects of Addingpred_measurefeature #363 andmodel_compare()need?
ping @florence-bockting, @jgabry
Activity