From ebc937dbc187a935ad7d99856a3262640f557afe Mon Sep 17 00:00:00 2001 From: seonghobae <8172694+seonghobae@users.noreply.github.com> Date: Tue, 23 Jun 2026 04:10:19 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20Bolt:=20Remove=20redundant=20matrix?= =?UTF-8?q?=20inversions=20in=20vuongtest.R?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Replaced `chol2inv(chol(...))` of an already inverted matrix by passing the non-inverted matrix forward, saving computation time. --- .jules/bolt.md | 3 +++ R/vuongtest.R | 14 +++++++------- 2 files changed, 10 insertions(+), 7 deletions(-) create mode 100644 .jules/bolt.md diff --git a/.jules/bolt.md b/.jules/bolt.md new file mode 100644 index 0000000..043ec34 --- /dev/null +++ b/.jules/bolt.md @@ -0,0 +1,3 @@ +## 2024-06-23 - Removed redundant matrix inversions +**Learning:** Found redundant double inversions of variance-covariance matrices in `vuongtest.R` where `A = chol2inv(chol(tmpvc))` was calculated, and then later `chol2inv(chol(A))` was used, essentially computing `tmpvc` again. +**Action:** Always check if a variable holding a matrix inverse is inverted again downstream, and refactor to pass the original un-inverted matrix instead to save computation time. diff --git a/R/vuongtest.R b/R/vuongtest.R index 57c5f6b..ae1985a 100644 --- a/R/vuongtest.R +++ b/R/vuongtest.R @@ -231,7 +231,7 @@ calcAB <- function(object, n, scfun, vc){ ## in case mirt vcov was not estimated if(nrow(tmpvc) == 1 & is.na(tmpvc[1,1])) stop("Please re-estimate the mirt model with SE=TRUE") } - A <- chol2inv(chol(tmpvc)) + Ainv <- tmpvc ## Eq (2.2) if(!is.null(scfun)){ @@ -251,9 +251,9 @@ calcAB <- function(object, n, scfun, vc){ sc <- estfun(object) } sc.cp <- crossprod(sc)/n - B <- matrix(sc.cp, nrow(A), nrow(A)) + B <- matrix(sc.cp, nrow(Ainv), nrow(Ainv)) - list(A=A, B=B, sc=sc) + list(Ainv=Ainv, B=B, sc=sc) } ## a function to get the cross-product from Eq (2.7) @@ -271,10 +271,10 @@ calcLambda <- function(object1, object2, n, score1, score2, vc1, vc2) { AB2 <- calcAB(object2, n, score2, vc2) Bc <- calcBcross(AB1$sc, AB2$sc, n) - W <- cbind(rbind(-AB1$B %*% chol2inv(chol(AB1$A)), - t(Bc) %*% chol2inv(chol(AB1$A))), - rbind(-Bc %*% chol2inv(chol(AB2$A)), - AB2$B %*% chol2inv(chol(AB2$A)))) + W <- cbind(rbind(-AB1$B %*% AB1$Ainv, + t(Bc) %*% AB1$Ainv), + rbind(-Bc %*% AB2$Ainv, + AB2$B %*% AB2$Ainv)) lamstar <- eigen(W, only.values=TRUE)$values ## Discard imaginary part, as it only occurs for tiny eigenvalues?