一种使 array/matrix 乘法更快的方法?在 R

a way to make this array/matrix multiplication faster? in R

我想不出让这段代码更快的方法。有没有可以 运行 更快的应用函数?现在,我正在使用一个 for each 循环来并行 运行 这个循环,它仍然需要很长时间。

ndraws=20000
nhousehold=18831
m=12


    elasticitydraws = array(0,c(m,ndraws,nhousehold))
    MAPelasticity = matrix(0,nhousehold,m)
    medianpricemat = matrix(rnorm(12,15,1),12,1)                                 


# dim(out$betadraw) = 18831, 12, 20000
# dim(medianpricemat) = 12, 1

    library(foreach)
    library(doMC)
    registerDoMC(10)

    elasticitylist = foreach(i=1:nhousehold) %dopar% {

            pricedraws = out$betadraw[i,12,] 
            elasticitydraws[,,i]= probarray[,,i] %*% diag(pricedraws)
            elasticitydraws[,,i] = elasticitydraws[,,i] * as.vector(medianpricemat)
            MAPelasticity[i,] = apply(elasticitydraws[,,i],1,mean)

    } 

代码中的瓶颈是创建一个大的密集对角矩阵和矩阵乘法。最好使用稀疏矩阵和 Matrix 包。这节省了内存和计算时间。我还包含了 Carl 的评论并在循环外创建了一些向量。

library(Matrix)

medianpricemat <-  as.vector(medianpricemat)
D1 <- Diagonal(x=pricedraws)

elasticitylist = foreach(i=1:nhousehold) %dopar% {
    pricedraws = out$betadraw[i,12,] 
    tmp = probarray[,,i] %*% D1
    elasticitydraws[,,i] = as.matrix(tmp) * medianpricemat
    MAPelasticity[i,] = rowMeans(elasticitydraws[,,i])
} 

一个不太明显的技巧是避免创建对角矩阵和 矩阵乘法:

D2 <- rep(pricedraws, each=m)

elasticitylist = foreach(i=1:nhousehold) %dopar% {
    pricedraws = out$betadraw[i,12,] 
    tmp = probarray[,,i] * D2  # element wise multiplication 
    elasticitydraws[,,i] = as.matrix(tmp) * medianpricemat
    MAPelasticity[i,] = rowMeans(elasticitydraws[,,i]) 
}