Skip to content

Commit

Permalink
Use crossprod() and tcrossprod() to replace t(x) %*% y and x %*% t(y)…
Browse files Browse the repository at this point in the history
… respectively
  • Loading branch information
statwangz committed Jan 30, 2024
1 parent ec7d342 commit bf89d38
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 29 deletions.
4 changes: 2 additions & 2 deletions R/mfairPredict.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ setMethod(
stop("The model has not been fitted!")
} # End

Y_hat <- object@Z[, which_factors] %*% t(object@W[, which_factors])
Y_hat <- tcrossprod(object@Z[, which_factors], object@W[, which_factors])
if (add_mean) {
Y_hat <- Y_hat + object@Y_mean
}
Expand All @@ -46,7 +46,7 @@ setMethod(
stop("The model has not been fitted!")
} # End

return(Y_hat = as.matrix(object@mu) %*% t(object@nu))
return(Y_hat = tcrossprod(as.matrix(object@mu), as.matrix(object@nu)))
}
)

Expand Down
68 changes: 41 additions & 27 deletions R/mfairSingleFactor.R
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,25 @@ fitSFSparse <- function(Y, X, init,
E_zsq <- (init@mu)^2 + init@a_sq
init@b_sq <- 1 / (1 + as.vector(t(tau_mat) %*% as.matrix(E_zsq)))
init@nu <- init@b_sq *
as.vector(init@tau * (t(Y) %*% as.matrix(init@mu)))
as.vector(init@tau * crossprod(Y, as.matrix(init@mu)))

# M-step
mu_sq <- (init@mu)^2
nu_sq <- (init@nu)^2

init@tau <- init@n_obs /
sum((Y - projSparse(as.matrix(init@mu) %*% t(init@nu), obs_indices))^2 +
projSparse(as.matrix(mu_sq + init@a_sq) %*% t(nu_sq + init@b_sq), obs_indices) -
projSparse(as.matrix(mu_sq) %*% t(nu_sq), obs_indices))
sum((Y - projSparse(
tcrossprod(as.matrix(init@mu), as.matrix(init@nu)),
obs_indices
))^2 +
projSparse(
tcrossprod(as.matrix(mu_sq + init@a_sq), as.matrix(nu_sq + init@b_sq)),
obs_indices
) -
projSparse(
tcrossprod(as.matrix(mu_sq), as.matrix(nu_sq)),
obs_indices
))
tau_mat@x <- rep(init@tau, length(tau_mat@x))

init@beta <- N / (sum((init@mu - init@FX)^2) + sum(init@a_sq))
Expand Down Expand Up @@ -111,21 +120,26 @@ fitSFSparse <- function(Y, X, init,
as.vector(init@tau * (Y %*% as.matrix(init@nu))))

E_zsq <- (init@mu)^2 + init@a_sq
init@b_sq <- 1 / (1 + as.vector(t(tau_mat) %*% as.matrix(E_zsq)))
init@b_sq <- 1 / (1 + as.vector(crossprod(tau_mat, as.matrix(E_zsq))))
init@nu <- init@b_sq *
as.vector(init@tau * (t(Y) %*% as.matrix(init@mu)))
as.vector(init@tau * (crossprod(Y, as.matrix(init@mu))))

# M-step
mu_sq <- (init@mu)^2
nu_sq <- (init@nu)^2

init@tau <- init@n_obs /
sum((Y - projSparse(as.matrix(init@mu) %*% t(init@nu), obs_indices))^2 +
sum((Y - projSparse(
tcrossprod(as.matrix(init@mu), as.matrix(init@nu)),
obs_indices
))^2 +
projSparse(
as.matrix(mu_sq + init@a_sq) %*% t(nu_sq + init@b_sq), obs_indices
tcrossprod(as.matrix(mu_sq + init@a_sq), as.matrix(nu_sq + init@b_sq)),
obs_indices
) -
projSparse(
as.matrix(mu_sq) %*% t(nu_sq), obs_indices
tcrossprod(as.matrix(mu_sq), as.matrix(nu_sq)),
obs_indices
))
tau_mat@x <- rep(init@tau, length(tau_mat@x))

Expand Down Expand Up @@ -219,19 +233,19 @@ fitSFMissing <- function(Y, X, init,
as.vector(init@tau * (ProjY %*% as.matrix(init@nu))))

E_zsq <- (init@mu)^2 + init@a_sq
init@b_sq <- 1 / (1 + as.vector(t(tau_mat) %*% as.matrix(E_zsq)))
init@b_sq <- 1 / (1 + as.vector(crossprod(tau_mat, as.matrix(E_wsq))))
init@nu <- init@b_sq *
as.vector(init@tau * (t(ProjY) %*% as.matrix(init@mu)))
as.vector(init@tau * (crossprod(ProjY, as.matrix(init@mu))))

# M-step
mu_sq <- (init@mu)^2
nu_sq <- (init@nu)^2

init@tau <- init@n_obs /
sum(
(Y - as.matrix(init@mu) %*% t(init@nu))^2 +
as.matrix(mu_sq + init@a_sq) %*% t(nu_sq + init@b_sq) -
as.matrix(mu_sq) %*% t(nu_sq),
(Y - tcrossprod(as.matrix(init@mu), as.matrix(init@nu)))^2 +
tcrossprod(as.matrix(mu_sq + init@a_sq), as.matrix(nu_sq + init@b_sq)) -
tcrossprod(as.matrix(mu_sq), as.matrix(nu_sq)),
na.rm = TRUE
)
tau_mat <- matrix(init@tau, N, M)
Expand Down Expand Up @@ -283,19 +297,19 @@ fitSFMissing <- function(Y, X, init,
as.vector(init@tau * (ProjY %*% as.matrix(init@nu))))

E_zsq <- (init@mu)^2 + init@a_sq
init@b_sq <- 1 / (1 + as.vector(t(tau_mat) %*% as.matrix(E_zsq)))
init@b_sq <- 1 / (1 + as.vector(crossprod(tau_mat, as.matrix(E_zsq))))
init@nu <- init@b_sq *
as.vector(init@tau * (t(ProjY) %*% as.matrix(init@mu)))
as.vector(init@tau * crossprod(ProjY, as.matrix(init@mu)))

# M-step
mu_sq <- (init@mu)^2
nu_sq <- (init@nu)^2

init@tau <- init@n_obs /
sum(
(Y - as.matrix(init@mu) %*% t(init@nu))^2 +
as.matrix(mu_sq + init@a_sq) %*% t(nu_sq + init@b_sq) -
as.matrix(mu_sq) %*% t(nu_sq),
(Y - tcrossprod(as.matrix(init@mu), as.matrix(init@nu)))^2 +
tcrossprod(as.matrix(mu_sq + init@a_sq), as.matrix(nu_sq + init@b_sq)) -
tcrossprod(as.matrix(mu_sq), as.matrix(nu_sq)),
na.rm = TRUE
)
tau_mat <- matrix(init@tau, N, M)
Expand Down Expand Up @@ -379,15 +393,15 @@ fitSFFully <- function(Y, X, init,
init@tau * as.vector(Y %*% as.matrix(init@nu)))
init@b_sq <- 1 / (1 + init@tau * (sum((init@mu)^2) + N * init@a_sq))
init@nu <- init@b_sq *
init@tau * as.vector(t(Y) %*% as.matrix(init@mu))
init@tau * as.vector(crossprod(Y, as.matrix(init@mu)))

# M-step
mu_sq <- (init@mu)^2
nu_sq <- (init@nu)^2
init@tau <- N * M /
sum((Y - as.matrix(init@mu) %*% t(init@nu))^2 +
as.matrix(mu_sq + init@a_sq) %*% t(nu_sq + init@b_sq) -
as.matrix(mu_sq) %*% t(nu_sq))
sum((Y - tcrossprod(as.matrix(init@mu), as.matrix(init@nu)))^2 +
tcrossprod(as.matrix(mu_sq + init@a_sq), as.matrix(nu_sq + init@b_sq)) -
tcrossprod(as.matrix(mu_sq), as.matrix(nu_sq)))
init@beta <- N / (sum((init@mu - init@FX)^2) + N * init@a_sq)
# init@beta <- ifelse(init@beta < 1e-8, 1e-8, init@beta)

Expand Down Expand Up @@ -433,15 +447,15 @@ fitSFFully <- function(Y, X, init,
init@tau * as.vector(Y %*% as.matrix(init@nu)))
init@b_sq <- 1 / (1 + init@tau * (sum((init@mu)^2) + N * init@a_sq))
init@nu <- init@b_sq *
init@tau * as.vector(t(Y) %*% as.matrix(init@mu))
init@tau * as.vector(crossprod(Y, as.matrix(init@mu)))

# M-step
mu_sq <- (init@mu)^2
nu_sq <- (init@nu)^2
init@tau <- N * M /
sum((Y - as.matrix(init@mu) %*% t(init@nu))^2 +
as.matrix(mu_sq + init@a_sq) %*% t(nu_sq + init@b_sq) -
as.matrix(mu_sq) %*% t(nu_sq))
sum((Y - tcrossprod(as.matrix(init@mu), as.matrix(init@nu)))^2 +
tcrossprod(as.matrix(mu_sq + init@a_sq), as.matrix(nu_sq + init@b_sq)) -
tcrossprod(as.matrix(mu_sq), as.matrix(nu_sq)))
init@beta <- N / (sum((init@mu - init@FX)^2) + N * init@a_sq)
# init@beta <- ifelse(init@beta < 1e-8, 1e-8, init@beta)

Expand Down

0 comments on commit bf89d38

Please sign in to comment.