Repository navigation
Reweight the clusters in get_p_clust() without a loop over the clusters where possible - #601
Conversation
# Conflicts: # NEWS.md # R/RcppExports.R # src/RcppExports.cpp
|
Thanks. Fewer cases need the second loop than The new commit adds the attribute The table gives the time of one
For binomial and Poisson, the skip saves 2-3 ms of 8 ms per call. New tests check that |
Summary
In
cv_varsel()withvalidate_search = TRUEand LOO CV,get_p_clust()reweights the clusters of the reference draws twice per left-out observation (for the search and for the evaluation). It loops over the clusters and callswhich(cl == j)in each iteration. Each iteration computes the center, dispersion and predictive variance of one cluster.With 400 clusters, this PR reduces the time of
cv_varsel()withvalidate_search = TRUEand full LOO by 54% (table below).This PR was assisted by Claude.
Change
This PR replaces the loop in two cases:
1 - 1e-10as the loop. The results are identical.clust_moments_c()makes two passes over the draws. It computes the weighted means ofmu,mu_offs, the dispersion and the squared dispersion, and the weighted variance ofmu_offs. The new family elementpredvar_mixturns these moments into the predictive variances of all clusters. The Gaussian, Student-t, binomial and Poisson families have it, and so does the latent projection (through its Gaussian family).A user can replace
family$predvar. Soget_p_clust()usespredvar_mixonly iffamily$predvaris identical to the"predvar"attribute ofpredvar_mix. In all other cases,get_p_clust()computes the predictive variances in the old loop over the clusters. Examples are the Gamma family, augmented-data families and a replacedpredvar.get_p_clust()also uses the old loop for clusters with a weight sum of 0 or a non-finite weight sum.Speed benchmark
The table gives the time of one run of
cv_varsel()withvalidate_search = TRUEand full LOO per version. Both versions ran in the same SLURM job.nclusters = 400,ndraws_pred = 4000nclusters = 20,ndraws_pred = 400In row 1, the predictor ranking is the same, and all 24 compared elements of the
vselobject are identical. In row 2, the predictor ranking is the same. With 20 clusters, the time does not change measurably.Tests
test_get_p_clust.R(535 expectations) keeps a copy of the old loop. It comparesget_p_clust()with the loop for several families and weights, clusters with weight 0, non-finite values and a replacedpredvar. It checks that the Gaussian, Student-t, binomial, Poisson and latent families take thepredvar_mixpath, and it tests the input checks ofclust_moments_c(). The full test suite passes on Triton (R 4.5.3, testthat 3.3.2) with 0 failures and the same 19 warnings as on master 7da1b5a.