Skip to content

⚡ Bolt: [performance improvement] Replace double matrix inversion with direct covariance matrix usage#10

Open
seonghobae wants to merge 1 commit into
masterfrom
bolt-performance-double-inverse-3585486672558178911
Open

⚡ Bolt: [performance improvement] Replace double matrix inversion with direct covariance matrix usage#10
seonghobae wants to merge 1 commit into
masterfrom
bolt-performance-double-inverse-3585486672558178911

Conversation

@seonghobae

Copy link
Copy Markdown
Collaborator

💡 What: Prevent unnecessary double inversions of the covariance matrix in R/vuongtest.R by returning the un-inverted covariance matrix tmpvc as Ainv in calcAB and using it directly in calcLambda instead of redundantly running chol2inv(chol(AB$A)).
🎯 Why: Double inverting a dense matrix creates unnecessary $O(p^3)$ performance bottlenecks and risks precision loss due to floating-point representation limits.
📊 Impact: Considerably faster computation speeds for the vuongtest function, specially when testing models with a large number of parameters $p$. Better numerical stability.
🔬 Measurement: Comparing the time profile of vuongtest() operations on larger datasets should show an elimination of overhead associated with redundant Cholesky and inverse calculations. The optimization was also recorded in .jules/bolt.md.


PR created automatically by Jules for task 3585486672558178911 started by @seonghobae

…h direct covariance matrix usage

What: Prevent unnecessary double inversions of the covariance matrix in `R/vuongtest.R` by saving the un-inverted covariance matrix in `calcAB` and using it directly in `calcLambda`.
Why: Double inverting matrices (e.g. `chol2inv(chol(A))` where `A` is already inverted) adds significant $O(p^3)$ time complexity per execution, and can lead to precision loss.
Impact: Substantially reduces computational overhead for Vuong tests, especially with larger numbers of parameters.
Measurement: Compare execution time of `vuongtest(fit1, fit2)` for models with many parameters. Time reduction and exactness of results guarantee improvement.
Copilot AI review requested due to automatic review settings June 24, 2026 04:07
@google-labs-jules

Copy link
Copy Markdown

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Optimizes vuongtest()’s calcLambda() path by avoiding redundant inversion of the information/covariance matrix, aiming to reduce cubic-time overhead and improve numerical stability for large parameter counts.

Changes:

  • calcAB() now returns tmpvc as Ainv so calcLambda() can use the covariance matrix directly.
  • calcLambda() replaces repeated chol2inv(chol(AB$A)) calls with AB$Ainv to eliminate the extra inversion.
  • Adds a Bolt performance note in .jules/bolt.md documenting the optimization.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
R/vuongtest.R Uses Ainv to avoid re-inverting matrices when building W in calcLambda().
.jules/bolt.md Documents the double-inversion performance pitfall and the intended remediation.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread R/vuongtest.R
Comment on lines 234 to +238
A <- chol2inv(chol(tmpvc))
## Bolt performance optimization: return un-inverted matrix `tmpvc` as `Ainv`
## to avoid the $O(p^3)$ performance overhead and precision loss of
## double-inverting in `calcLambda`.
Ainv <- tmpvc
Comment thread R/vuongtest.R
Comment on lines +278 to +281
W <- cbind(rbind(-AB1$B %*% AB1$Ainv,
t(Bc) %*% AB1$Ainv),
rbind(-Bc %*% AB2$Ainv,
AB2$B %*% AB2$Ainv))
Comment thread .jules/bolt.md
Comment on lines +2 to +3
**Learning:** Found a performance bottleneck where a covariance matrix is inverted, passed to another function, and then inverted again (`chol2inv(chol(A))`). This is not only an unnecessary $O(p^3)$ performance overhead, but also risks precision loss due to repeated floating point calculations.
**Action:** When a matrix and its inverse are needed, consider whether passing the original un-inverted matrix is sufficient, or pass both. In `vuongtest.R`, we can just pass the original variance-covariance matrix (`tmpvc`) rather than inverting it in `calcAB` and inverting it back in `calcLambda`.
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