Created
August 10, 2017 23:57
-
-
Save clarkfitzg/04c88efa3a426d08cd5618a01bc9356c to your computer and use it in GitHub Desktop.
Chunked version of covariance
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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