-
Notifications
You must be signed in to change notification settings - Fork 114
Description
It seems that some algorithmic changes could increase the speed of the two functions.
computeSigmaHat.LISREL
In this function,
LAMBDA..IB.inv <- LAMBDA %*% solve(IB)
A generally faster way to do this would be
LAMBDA..IB.inv <- solve(IB, t(LAMBDA))
For recursive models (which I assume represent the majority of fitted models), IB should be a triangular matrix. In that case, the approach below should be the fastest:
forwardsolve(IB, t(LAMBDA), transpose = TRUE)
I tried the forwardsolve approach on the political democracy model, and the speedup was about 1.3×. For larger B matrices, I would expect the speedup to increase, since the forwardsolve approach has lower complexity than the inverse + matrix multiplication approach.
computeSigmaHat
Here, an eigenvalue decomposition is performed to check whether Sigma-hat is positive definite. I think this should be replaced by a Cholesky decomposition, since it is faster and is used anyway in the success case (positive definite Sigma). The Cholesky decomposition fails if the matrix is not positive definite, so in the success case one eigenvalue decomposition would be saved.
I tested this on a 70×70 sample covariance matrix, and it reduced lavInspect(fit, "timing")$optim from 0.66 seconds to 0.5 seconds.
I see that you are moving to C++, but since these are algorithmic changes, I thought I would report them anyway. I am curious what you think since I expect that you already considered these changes.