|
| 1 | +#!/usr/bin/env Rscript |
| 2 | +# Benchmark: Phase 1 optimization (XtX*beta cache + precomputed constants) |
| 3 | +# |
| 4 | +# This script compares the optimized code against a simulated "no-cache" baseline |
| 5 | +# by manually running the dominant O(P^2) operations the number of times they |
| 6 | +# would occur with and without caching. |
| 7 | +# |
| 8 | +# The optimization eliminates 2 of 3 redundant XtX %*% beta computations per |
| 9 | +# iteration per outcome. |
| 10 | + |
| 11 | +library(MASS) |
| 12 | + |
| 13 | +cat("=== ColocBoost Phase 1 Optimization Benchmark ===\n\n") |
| 14 | + |
| 15 | +# ---- Generate test data at different scales ---- |
| 16 | +run_benchmark <- function(p, L, M, n_ref = 500, seed = 42) { |
| 17 | + set.seed(seed) |
| 18 | + |
| 19 | + cat(sprintf("P = %d variants, L = %d outcomes, M = %d iterations, N_ref = %d\n", p, L, M, n_ref)) |
| 20 | + |
| 21 | + # Generate LD matrix (P x P) |
| 22 | + sigma <- matrix(0, p, p) |
| 23 | + for (i in 1:p) { |
| 24 | + for (j in 1:p) { |
| 25 | + sigma[i, j] <- 0.9^abs(i - j) |
| 26 | + } |
| 27 | + } |
| 28 | + # Ensure positive definite |
| 29 | + LD <- sigma |
| 30 | + |
| 31 | + # Generate random beta vector |
| 32 | + beta <- rnorm(p) * 0.01 |
| 33 | + |
| 34 | + cat(sprintf(" LD matrix size: %.1f MB\n", object.size(LD) / 1024^2)) |
| 35 | + |
| 36 | + # ---- Benchmark: XtX %*% beta ---- |
| 37 | + # Before optimization: 3 * M * L calls to XtX %*% beta |
| 38 | + # After optimization: 1 * M * L calls to XtX %*% beta |
| 39 | + |
| 40 | + n_calls_before <- 3 * M * L |
| 41 | + n_calls_after <- 1 * M * L |
| 42 | + |
| 43 | + # Time a single XtX %*% beta |
| 44 | + t_single <- system.time({ |
| 45 | + for (rep in 1:100) { |
| 46 | + result <- LD %*% beta |
| 47 | + } |
| 48 | + })[["elapsed"]] / 100 |
| 49 | + |
| 50 | + cat(sprintf(" Single XtX %%*%% beta: %.4f seconds\n", t_single)) |
| 51 | + cat(sprintf(" Before optimization: %d calls = %.2f seconds\n", |
| 52 | + n_calls_before, n_calls_before * t_single)) |
| 53 | + cat(sprintf(" After optimization: %d calls = %.2f seconds\n", |
| 54 | + n_calls_after, n_calls_after * t_single)) |
| 55 | + cat(sprintf(" Speedup on dominant cost: %.1fx\n", |
| 56 | + n_calls_before * t_single / (n_calls_after * t_single))) |
| 57 | + cat(sprintf(" Time saved: %.2f seconds\n\n", |
| 58 | + (n_calls_before - n_calls_after) * t_single)) |
| 59 | + |
| 60 | + invisible(list( |
| 61 | + p = p, L = L, M = M, |
| 62 | + t_single = t_single, |
| 63 | + t_before = n_calls_before * t_single, |
| 64 | + t_after = n_calls_after * t_single |
| 65 | + )) |
| 66 | +} |
| 67 | + |
| 68 | +# ---- Run end-to-end colocboost benchmark ---- |
| 69 | +run_colocboost_benchmark <- function(p, L, M, n = 200, seed = 42) { |
| 70 | + set.seed(seed) |
| 71 | + |
| 72 | + cat(sprintf("\n--- End-to-end colocboost: P=%d, L=%d, M=%d ---\n", p, L, M)) |
| 73 | + |
| 74 | + sigma <- matrix(0, p, p) |
| 75 | + for (i in 1:p) { |
| 76 | + for (j in 1:p) { |
| 77 | + sigma[i, j] <- 0.9^abs(i - j) |
| 78 | + } |
| 79 | + } |
| 80 | + X <- mvrnorm(n, rep(0, p), sigma) |
| 81 | + colnames(X) <- paste0("SNP", 1:p) |
| 82 | + Y <- matrix(rnorm(n * L), n, L) |
| 83 | + # Add signal to SNP5 for all traits |
| 84 | + for (l in 1:L) { |
| 85 | + Y[, l] <- Y[, l] + X[, 5] * 0.5 |
| 86 | + } |
| 87 | + LD <- cor(X) |
| 88 | + |
| 89 | + # Generate summary statistics |
| 90 | + sumstat_list <- list() |
| 91 | + for (i in 1:L) { |
| 92 | + z <- rep(0, p) |
| 93 | + beta_hat <- rep(0, p) |
| 94 | + se_hat <- rep(0, p) |
| 95 | + for (j in 1:p) { |
| 96 | + fit <- summary(lm(Y[, i] ~ X[, j]))$coef |
| 97 | + if (nrow(fit) == 2) { |
| 98 | + beta_hat[j] <- fit[2, 1] |
| 99 | + se_hat[j] <- fit[2, 2] |
| 100 | + z[j] <- beta_hat[j] / se_hat[j] |
| 101 | + } |
| 102 | + } |
| 103 | + sumstat_list[[i]] <- data.frame( |
| 104 | + beta = beta_hat, sebeta = se_hat, z = z, |
| 105 | + n = n, variant = colnames(X) |
| 106 | + ) |
| 107 | + } |
| 108 | + |
| 109 | + # Time full colocboost run |
| 110 | + t_full <- system.time({ |
| 111 | + suppressWarnings(suppressMessages({ |
| 112 | + result <- colocboost::colocboost( |
| 113 | + sumstat = sumstat_list, |
| 114 | + LD = LD, |
| 115 | + M = M, |
| 116 | + output_level = 1 |
| 117 | + ) |
| 118 | + })) |
| 119 | + })[["elapsed"]] |
| 120 | + |
| 121 | + cat(sprintf(" Total wall time: %.2f seconds\n", t_full)) |
| 122 | + invisible(t_full) |
| 123 | +} |
| 124 | + |
| 125 | +# ---- Scenarios ---- |
| 126 | + |
| 127 | +cat("--- Micro-benchmark: XtX %*% beta operation ---\n\n") |
| 128 | + |
| 129 | +results <- list() |
| 130 | +results[[1]] <- run_benchmark(p = 1000, L = 2, M = 100) |
| 131 | +results[[2]] <- run_benchmark(p = 2000, L = 5, M = 200) |
| 132 | +results[[3]] <- run_benchmark(p = 5000, L = 3, M = 300) |
| 133 | +results[[4]] <- run_benchmark(p = 5000, L = 10, M = 500) |
| 134 | + |
| 135 | +cat("\n--- Summary Table ---\n") |
| 136 | +cat(sprintf("%-8s %-4s %-5s %-12s %-12s %-8s\n", |
| 137 | + "P", "L", "M", "Before(s)", "After(s)", "Speedup")) |
| 138 | +for (r in results) { |
| 139 | + cat(sprintf("%-8d %-4d %-5d %-12.2f %-12.2f %-8.1fx\n", |
| 140 | + r$p, r$L, r$M, r$t_before, r$t_after, r$t_before / r$t_after)) |
| 141 | +} |
| 142 | + |
| 143 | +cat("\n--- End-to-end colocboost timings ---\n") |
| 144 | +run_colocboost_benchmark(p = 100, L = 2, M = 50) |
| 145 | +run_colocboost_benchmark(p = 100, L = 5, M = 100) |
0 commit comments