Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Chunked version of covariance
cov_chunked = function(x, nchunks = 2L)
{
p = ncol(x)
indices = parallel::splitIndices(p, nchunks)
diagonal_blocks = lapply(indices, function(idx) cov(x[, idx, drop = FALSE]))
upper_right_indices = combn(indices, 2, simplify = FALSE)
upper_right_blocks = lapply(upper_right_indices, function(index){
cov(x[, index[[1]]], x[, index[[2]]])
})
# All computation is done, just assemble the results in the right way
output = matrix(numeric(p*p), nrow = p)
for(i in seq_along(indices)){
idx = indices[[i]]
output[idx, idx] = diagonal_blocks[[i]]
}
for(i in seq_along(upper_right_indices)){
index = upper_right_indices[[i]]
output[index[[1]], index[[2]]] = upper_right_blocks[[i]]
output[index[[2]], index[[1]]] = t(upper_right_blocks[[i]])
}
output
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment