3 naive_f = function(link, M1,M2,M3, p,β,b)
7 λ <- sqrt(colSums(β^2))
9 # Compute β x2,3 (self) tensorial products
10 β2 = array(0, dim=c(d,d,K))
11 β3 = array(0, dim=c(d,d,d,K))
18 β2[i,j,k] = β[i,k]*β[j,k]
20 β3[i,j,l,k] = β[i,k]*β[j,k]*β[l,k]
30 term = term + p[k]*.G(link,1,λ[k],b[k])*β[i,k]
31 res = res + (term - M1[i])^2
36 term = term + p[k]*.G(link,2,λ[k],b[k])*β2[i,j,k]
37 res = res + (term - M2[i,j])^2
42 term = term + p[k]*.G(link,3,λ[k],b[k])*β3[i,j,l,k]
43 res = res + (term - M3[i,j,l])^2
50 test_that("naive computation provides the same result as vectorized computations",
52 h <- 1e-7 #for finite-difference tests
53 tol <- 1e-3 #large tolerance, necessary in some cases... (generally 1e-6 is OK)
54 for (dK in list( c(2,2), c(5,3)))
60 M2 = matrix(runif(d*d,-1,1), ncol=d)
61 M3 = array(runif(d*d*d,-1,1), dim=c(d,d,d))
63 for (link in c("logit","probit"))
65 op = new("OptimParams", "li"=link, "M1"=as.double(M1),
66 "M2"=as.double(M2), "M3"=as.double(M3), "K"=as.integer(K))
68 for (var in seq_len((2+d)*K-1))
72 β <- matrix(runif(d*K,-5,5),ncol=K)
74 x <- c(p[1:(K-1)],as.double(β),b)
76 # Test functions values
77 expect_equal( op$f(x), naive_f(link,M1,M2,M3, p,β,b) )
79 # Test finite differences ~= gradient values
80 dir_h <- rep(0, (2+d)*K-1)
83 expect_equal( op$grad_f(x)[var], ( op$f(x+dir_h) - op$f(x) ) / h, tol )
89 test_that("W computed in C and in R are the same",
91 # TODO: provide data X,Y + parameters theta
95 λ <- sqrt(colSums(β^2))
97 β2 <- apply(β, 2, function(col) col %o% col)
98 β3 <- apply(β, 2, function(col) col %o% col %o% col)
100 β %*% (p * .G(li,1,λ,b)),
101 β2 %*% (p * .G(li,2,λ,b)),
102 β3 %*% (p * .G(li,3,λ,b)))
103 Id <- as.double(diag(d))
106 v2 <- Y * t( apply(X, 1, function(Xi) Xi %o% Xi - Id) )
107 v3 <- Y * t( apply(X, 1, function(Xi) { return (Xi %o% Xi %o% Xi
108 - Reduce('+', lapply(1:d, function(j) as.double(Xi %o% E[j,] %o% E[j,])), rep(0, d*d*d))
109 - Reduce('+', lapply(1:d, function(j) as.double(E[j,] %o% Xi %o% E[j,])), rep(0, d*d*d))
110 - Reduce('+', lapply(1:d, function(j) as.double(E[j,] %o% E[j,] %o% Xi)), rep(0, d*d*d))) } ) )
111 Omega1 <- matrix(0, nrow=dd, ncol=dd)
114 gi <- t(as.matrix(c(v1[i,], v2[i,], v3[i,]) - M))
115 Omega1 <- Omega1 + t(gi) %*% gi / n
117 Omega2 <- matrix( .C("Compute_Omega",
118 X=as.double(X), Y=as.double(Y), M=as.double(M),
119 pn=as.integer(n), pd=as.integer(d),
120 W=as.double(W), PACKAGE="morpheus")$W, nrow=dd, ncol=dd )
121 rg <- range(Omega1 - Omega2)
122 expect_that(rg[2] - rg[1] <= 1e8)