-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add separable argument and implement TGCCA for tau != 1
- Loading branch information
Showing
18 changed files
with
231 additions
and
23 deletions.
There are no files selected for viewing
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#' estimate_separable_covariance estimates the covariance matrix of a set of | ||
#' random variables with an underlying tensor structure making the assumption | ||
#' that the real covariance matrix has a separable structure. | ||
#' @param x A numerical array with at least 3 dimensions. | ||
#' @return The list composed of the estimated terms in the separable covariance. | ||
#' @references Hoff, P. D. (2011), Separable covariance arrays via the Tucker | ||
#' product, with applications to multivariate relational data. | ||
#' Eun Jeong Min et al (2019), Tensor canonical correlation analysis. | ||
#' @title Separable covariance estimator | ||
#' @noRd | ||
estimate_separable_covariance <- function(x) { | ||
dim_x <- dim(x) | ||
n <- dim_x[1] | ||
d <- length(dim_x) - 1 | ||
x_bar <- apply(x, -1, mean) | ||
r <- (1 / n) * norm( | ||
matrix(x, nrow = n) - matrix(rep(x_bar, n), nrow = n, byrow = TRUE), | ||
type = "F" | ||
)^2 | ||
x_bar <- array(x_bar, dim = dim_x[-1]) | ||
lapply(seq_len(d), function(m) { | ||
x_bar_m <- t(apply(x_bar, m, c)) | ||
x_bar_m <- x_bar_m %x% t(rep(1, n)) | ||
x_m <- t(apply(x, m + 1, c)) | ||
(1 / (n * r^((d - 1) / d))) * tcrossprod(x_m - x_bar_m) | ||
}) | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#' Compute the mode product between a tensor X and a matrix y on a given mode m. | ||
#' @param X An array. | ||
#' @param y A matrix. | ||
#' @param m A scalar designating a mode of the tensor X. | ||
#' @examples | ||
#' X <- array(rnorm(40 * 5 * 7), dim = c(40, 5, 7)) | ||
#' y <- rnorm(5) | ||
#' res <- mode_product(X, y, m = 2) | ||
#' print(dim(X)) | ||
#' print(dim(res)) | ||
#' @noRd | ||
mode_product <- function(X, y, m = 1) { | ||
dim_X <- dim(X) | ||
|
||
# Unfold the tensor on dimension m | ||
perm <- c(m, seq_along(dim_X)[-m]) | ||
X <- aperm(X, perm) | ||
|
||
# Compute the product | ||
X <- matrix(X, nrow = nrow(X)) | ||
X <- t(y) %*% X | ||
|
||
# Reshape the result back to a tensor | ||
dim_X[m] <- NCOL(y) | ||
X <- array(X, dim = dim_X[perm]) | ||
|
||
X <- aperm(X, c(1 + seq_len(m - 1), 1, m + seq_len(length(dim_X) - m))) | ||
return(X) | ||
} |
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
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#' Compute the square root or the inverse of the square root of a | ||
#' symmetric matrix. | ||
#' @param X A symmetric matrix. | ||
#' @param tol A relative tolerance to detect zero singular values. | ||
#' @param inv A boolean indicating if the inverse of the square root must be | ||
#' computed. | ||
#' @noRd | ||
sqrt_matrix <- function(X, tol = sqrt(.Machine$double.eps), inv = FALSE) { | ||
eig <- eigen(X, symmetric = TRUE) | ||
positive <- eig$values > max(tol * eig$values[1], 0) | ||
d <- eig$values | ||
if (inv) { | ||
d[positive] <- 1 / sqrt(d[positive]) | ||
} else { | ||
d[positive] <- sqrt(d[positive]) | ||
} | ||
d[!positive] <- 0 | ||
eig$vectors %*% diag(d, nrow = length(d)) %*% t(eig$vectors) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.