1 # Helper to generate a random series of matrices to align
2 .generateMatrices = function(d, K, N, noise)
4 matrices = list( matrix(runif(d*K, min=-1, max=1),ncol=K) ) #reference
7 matrices[[i]] <- matrices[[1]][,sample(1:K)]
9 matrices[[i]] = matrices[[i]] + matrix(rnorm(d*K, sd=0.05), ncol=K)
14 test_that("labelSwitchingAlign correctly aligns de-noised parameters",
16 N <- 30 #number of matrices
17 d_K_list <- list(c(2,2), c(5,3))
23 # 1] Generate matrix series
24 Ms <- .generateMatrices(d, K, N, noise=FALSE)
26 # 2] Call align function with mode=approx1
27 aligned <- alignMatrices(Ms[2:(N+1)], ref=Ms[[1]], ls_mode="approx1")
31 expect_equal(aligned[[j]], Ms[[1]])
33 # 2bis] Call align function with mode=approx2
34 aligned <- alignMatrices(Ms[2:(N+1)], ref=Ms[[1]], ls_mode="approx2")
36 # 3bis] Check alignment
38 expect_equal(aligned[[j]], Ms[[1]])
42 test_that("labelSwitchingAlign correctly aligns noisy parameters",
44 N <- 30 #number of matrices
45 d_K_list <- list(c(2,2), c(5,3))
50 max_error <- d * 0.2 #TODO: what value to choose ?
52 # 1] Generate matrix series
53 Ms <- .generateMatrices(d, K, N, noise=TRUE)
55 # 2] Call align function
56 aligned <- alignMatrices(Ms[2:(N+1)], ref=Ms[[1]], ls_mode="exact")
60 expect_lt( norm(aligned[[j]] - Ms[[1]]), max_error )