1 context("alignMatrices")
3 # Helper to generate a random series of matrices to align
4 .generateMatrices = function(d, K, N, noise)
6 matrices = list( matrix(runif(d*K, min=-1, max=1),ncol=K) ) #reference
9 matrices[[i]] <- matrices[[1]][,sample(1:K)]
11 matrices[[i]] = matrices[[i]] + matrix(rnorm(d*K, sd=0.05), ncol=K)
16 test_that("labelSwitchingAlign correctly aligns de-noised parameters",
18 N <- 30 #number of matrices
19 d_K_list <- list(c(2,2), c(5,3))
25 # 1] Generate matrix series
26 Ms <- .generateMatrices(d, K, N, noise=FALSE)
28 # 2] Call align function with mode=approx1
29 aligned <- alignMatrices(Ms[2:(N+1)], ref=Ms[[1]], ls_mode="approx1")
33 expect_equal(aligned[[j]], Ms[[1]])
35 # 2bis] Call align function with mode=approx2
36 aligned <- alignMatrices(Ms[2:(N+1)], ref=Ms[[1]], ls_mode="approx2")
38 # 3bis] Check alignment
40 expect_equal(aligned[[j]], Ms[[1]])
44 test_that("labelSwitchingAlign correctly aligns noisy parameters",
46 N <- 30 #number of matrices
47 d_K_list <- list(c(2,2), c(5,3))
52 max_error <- d * 0.2 #TODO: what value to choose ?
54 # 1] Generate matrix series
55 Ms <- .generateMatrices(d, K, N, noise=TRUE)
57 # 2] Call align function
58 aligned <- alignMatrices(Ms[2:(N+1)], ref=Ms[[1]], ls_mode="exact")
62 expect_that( norm(aligned[[j]] - Ms[[1]]), is_less_than(max_error) )