Skip to content

Vectorized Bayesian bootstrap#368

Open
VisruthSK wants to merge 3 commits into
masterfrom
vectorization
Open

Vectorized Bayesian bootstrap#368
VisruthSK wants to merge 3 commits into
masterfrom
vectorization

Conversation

@VisruthSK
Copy link
Copy Markdown
Member

@VisruthSK VisruthSK commented Jun 6, 2026

This PR was made with assistance from Codex.

Swaps the Bayesian bootstrap implementation from a loop to matrix ops. Adds a test for BB weights correctness before changing implementation.

Benchmark:

set.seed(0)
N <- 400
K <- 6
BB_n <- 1000
alpha <- 1
lpd_point <- matrix(rnorm(N * K, mean = -1), nrow = N, ncol = K)
BB_weighting <- loo:::dirichlet_rng(BB_n, rep(alpha, N))

pseudobma_bb_loop <- function(lpd_point, BB_weighting) {
  N <- nrow(lpd_point)
  K <- ncol(lpd_point)
  BB_n <- nrow(BB_weighting)
  temp <- matrix(NA, BB_n, K)

  for (bb in 1:BB_n) {
    z_bb <- BB_weighting[bb, ] %*% lpd_point * N
    uwts <- exp(z_bb - max(z_bb))
    temp[bb, ] <- uwts / sum(uwts)
  }

  colMeans(temp)
}

pseudobma_bb_vectorized <- function(lpd_point, BB_weighting) {
  N <- nrow(lpd_point)
  z <- BB_weighting %*% lpd_point * N
  uwts <- exp(z - matrixStats::rowMaxs(z))

  colMeans(uwts / rowSums(uwts))
}

results <- bench::mark(
  loop = pseudobma_bb_loop(lpd_point, BB_weighting),
  vectorized = pseudobma_bb_vectorized(lpd_point, BB_weighting),
  iterations = 1e3
)

summary(results)
#> # A tibble: 2 × 6
#>   expression      min   median `itr/sec` mem_alloc `gc/sec`
#>   <bch:expr> <bch:tm> <bch:tm>     <dbl> <bch:byt>    <dbl>
#> 1 loop          4.4ms   5.25ms      188.    9.16MB    21.8 
#> 2 vectorized   1.02ms   1.19ms      814.  167.95KB     2.45
summary(results, relative = TRUE)
#> # A tibble: 2 × 6
#>   expression   min median `itr/sec` mem_alloc `gc/sec`
#>   <bch:expr> <dbl>  <dbl>     <dbl>     <dbl>    <dbl>
#> 1 loop        4.32   4.41      1         55.8     8.91
#> 2 vectorized  1      1         4.33       1       1

Created on 2026-06-06 with reprex v2.1.1

@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented Jun 6, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 92.69%. Comparing base (286263d) to head (230331c).
⚠️ Report is 2 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #368      +/-   ##
==========================================
- Coverage   92.70%   92.69%   -0.01%     
==========================================
  Files          31       31              
  Lines        3029     3025       -4     
==========================================
- Hits         2808     2804       -4     
  Misses        221      221              

☔ 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.

@VisruthSK VisruthSK changed the title Vectorized bayesian bootstrap Vectorized Bayesian bootstrap Jun 6, 2026
@VisruthSK VisruthSK marked this pull request as ready for review June 6, 2026 23:51
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Jun 7, 2026

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

  • ✔️loo_function: 1.77s -> 1.77s [-0.24%, +0.54%]
  • ❗🐌loo_matrix: 1.29s -> 1.31s [+0.54%, +1.3%]
    Further explanation regarding interpretation and methodology can be found in the documentation.

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