Skip to content

Instantly share code, notes, and snippets.

@clarkfitzg
Created August 10, 2017 23:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save clarkfitzg/04c88efa3a426d08cd5618a01bc9356c to your computer and use it in GitHub Desktop.
Save clarkfitzg/04c88efa3a426d08cd5618a01bc9356c to your computer and use it in GitHub Desktop.
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