Efficient way to perform matrix multiplication repeatedly
I am trying to do the matrix multiplication S_g for each i, and each g
with i. This is what I have tried so far, but it takes a huge amount of
time to complete. Is there a more computationally efficient method to do
exactly the same thing?
The main thing to note from this formula is the S_g uses X_gamma and Y[,i]
in matrix multiplication set-up. X_gamma is dependent on value g.
Therefore, for each i, I have to perform g matrix multiplications.
I apologize in advance, but there is no easy way for me to post a
reproducible example of this. I only need suggestions as to what methods
to use that would speed up the computation process.
library(foreach)
library(doParallel) ## parallel backend for the foreach function
registerDoParallel()
c = 100
denom <- foreach(i=1:ncol(Y)) %dopar% {
library(zoo)
library(stats)
library(Matrix)
library(base)
result = c()
for(g in 1:nrow(set)) {
X_gamma = X[,which(colnames(X) %in% colnames(set[which(set[g,] !=
0)]))]
S_g = Y[,i] %*% (I - (c/(1+c))*(X_gamma %*%
solve(crossprod(X_gamma)) %*% t(X_gamma))) %*% Y[,i]
result[g] = ((1+c)^(-sum(set[g,])/2)) * ((S_g)^(-T/2))
}
sum(result)
}
Thank you for your help!
No comments:
Post a Comment