我正在用 R 编写一个期望最大化算法。为了加快计算速度,我想对这个瓶颈进行矢量化。我知道N大约是k的一百倍。
MyLoglik = 0
for (i in c(1:N))
{
for (j in c(1:k))
{
MyLoglik = MyLoglik + MyTau[i,j]*log(MyP[j]*MyF(MyD[i,], MyMu[j,], MyS[[j]]))
}
}
还有这个矩阵列表:
MyDf.list <- vector("list", k)
for(i in 1:k)
{
MyDf.list[[i]] <- matrix(0,d,d)
for (j in c(1:N))
{
MyDf.list[[i]] = MyDf.list[[i]] + MyTau[j,i]*as.numeric((MyD[j,]-MyMu[i,])) %*% t(as.numeric(MyD[j,]-MyMu[i,]))
}
MyDf.list[[i]] = MyDf.list[[i]] / MyM[i]
}
我使用以下方法加快了速度:
MyLoglik = 0
for (j in c(1:k))
{
MyR= apply(MyD, 1, function(x) log(MyP[j]*MyF(x, MyMu[j,], MyS[[j]])))
MyLoglik = MyLoglik + sum(MyTau[,j]*MyR)
}
和:
d = dim(MyD)[2]
MyDf.list <- vector("list", k)
for(i in 1:k)
{
MyDf.list[[i]] <- matrix(0,d,d)
MyR= apply(MyD, 1, function(x) as.numeric((x-MyMu[i,])) %*% t(as.numeric(x-MyMu[i,])))
MyDf.list[[i]] = matrix(rowSums(t(MyTau[,i]*t(MyR))) / MyM[i],d,d)
}