Commit | Line | Data |
---|---|---|
cbd88fe5 BA |
1 | context("jointDiag::ajd") |
2 | ||
3 | #auxiliary to test diagonality | |
4 | .computeMuCheckDiag = function(X, Y, K, jd_method, β_ref) | |
5 | { | |
2b3a6af5 | 6 | d <- ncol(X) |
6dd5c2ac | 7 | #TODO: redundant code, same as computeMu() main method. Comments are stripped |
2b3a6af5 BA |
8 | M3 <- .Moments_M3(X,Y) |
9 | M2_t <- array(dim=c(d,d,d)) | |
6dd5c2ac BA |
10 | for (i in 1:d) |
11 | { | |
2b3a6af5 BA |
12 | ρ <- c(rep(0,i-1),1,rep(0,d-i)) |
13 | M2_t[,,i] <- .T_I_I_w(M3,ρ) | |
6dd5c2ac | 14 | } |
2b3a6af5 BA |
15 | jd <- jointDiag::ajd(M2_t, method=jd_method) |
16 | V <- if (jd_method=="uwedge") jd$B else solve(jd$A) | |
17 | M2_t <- array(dim=c(d,d,K)) | |
6dd5c2ac | 18 | for (i in 1:K) |
2b3a6af5 | 19 | M2_t[,,i] <- .T_I_I_w(M3,V[,i]) |
6dd5c2ac | 20 | #END of computeMu() code |
cbd88fe5 | 21 | |
2b3a6af5 BA |
22 | max_error <- 1.0 #TODO: tune ? |
23 | invβ <- MASS::ginv(β_ref) | |
6dd5c2ac BA |
24 | for (i in 1:K) |
25 | { | |
2b3a6af5 | 26 | shouldBeDiag <- invβ %*% M2_t[,,i] %*% t(invβ) |
6dd5c2ac BA |
27 | expect_that( |
28 | mean( abs(shouldBeDiag[upper.tri(shouldBeDiag) | lower.tri(shouldBeDiag)]) ), | |
29 | is_less_than(max_error) ) | |
30 | } | |
cbd88fe5 BA |
31 | } |
32 | ||
33 | test_that("'jedi' and 'uwedge' joint-diagonalization methods return a correct matrix", | |
34 | { | |
2b3a6af5 BA |
35 | n <- 10000 |
36 | d_K <- list( c(2,2), c(5,3), c(20,13) ) | |
cbd88fe5 | 37 | |
6dd5c2ac BA |
38 | for (dk_index in 1:length(d_K)) |
39 | { | |
2b3a6af5 BA |
40 | d <- d_K[[dk_index]][1] |
41 | K <- d_K[[dk_index]][2] | |
6dd5c2ac BA |
42 | #NOTE: sometimes large errors if pr is not balanced enough (e.g. random); |
43 | # same note for β. However we could be more random than that... | |
2b3a6af5 BA |
44 | β_ref <- rbind(diag(K),matrix(0,nrow=d-K,ncol=K)) |
45 | io <- generateSampleIO(n, p=rep(1/K,K-1), β=β_ref, rep(0,K), link="logit") | |
6dd5c2ac BA |
46 | .computeMuCheckDiag(io$X, io$Y, K, jd_method="uwedge", β_ref) |
47 | #TODO: some issues with jedi method (singular system) | |
48 | #.computeMuCheckDiag(io$X, io$Y, K, jd_method="jedi", β_ref) | |
49 | } | |
cbd88fe5 | 50 | }) |