indent everything: google rules...
[valse.git] / pkg / R / EMGLLF.R
CommitLineData
ffdf9447 1#' EMGLLF
4fed76cc
BA
2#'
3#' Description de EMGLLF
4#'
43d76c49 5#' @param phiInit an initialization for phi
6#' @param rhoInit an initialization for rho
7#' @param piInit an initialization for pi
8#' @param gamInit initialization for the a posteriori probabilities
9#' @param mini integer, minimum number of iterations in the EM algorithm, by default = 10
10#' @param maxi integer, maximum number of iterations in the EM algorithm, by default = 100
11#' @param gamma integer for the power in the penaly, by default = 1
12#' @param lambda regularization parameter in the Lasso estimation
13#' @param X matrix of covariates (of size n*p)
14#' @param Y matrix of responses (of size n*m)
15#' @param eps real, threshold to say the EM algorithm converges, by default = 1e-4
4fed76cc 16#'
c280fe59
BA
17#' @return A list ... phi,rho,pi,LLF,S,affec:
18#' phi : parametre de moyenne renormalisé, calculé par l'EM
19#' rho : parametre de variance renormalisé, calculé par l'EM
20#' pi : parametre des proportions renormalisé, calculé par l'EM
21#' LLF : log vraisemblance associée à cet échantillon, pour les valeurs estimées des paramètres
22#' S : ... affec : ...
4fed76cc 23#'
4fed76cc 24#' @export
ffdf9447
BA
25EMGLLF <- function(phiInit, rhoInit, piInit, gamInit, mini, maxi, gamma, lambda,
26 X, Y, eps, fast = TRUE)
27 {
fb6e49cb 28 if (!fast)
29 {
30 # Function in R
ffdf9447
BA
31 return(.EMGLLF_R(phiInit, rhoInit, piInit, gamInit, mini, maxi, gamma, lambda,
32 X, Y, eps))
fb6e49cb 33 }
34
35 # Function in C
ffdf9447
BA
36 n <- nrow(X) #nombre d'echantillons
37 p <- ncol(X) #nombre de covariables
38 m <- ncol(Y) #taille de Y (multivarié)
39 k <- length(piInit) #nombre de composantes dans le mélange
40 .Call("EMGLLF", phiInit, rhoInit, piInit, gamInit, mini, maxi, gamma, lambda,
41 X, Y, eps, phi = double(p * m * k), rho = double(m * m * k), pi = double(k),
42 LLF = double(maxi), S = double(p * m * k), affec = integer(n), n, p, m, k,
43 PACKAGE = "valse")
4fed76cc 44}
aa480ac1
BA
45
46# R version - slow but easy to read
ffdf9447
BA
47.EMGLLF_R <- function(phiInit, rhoInit, piInit, gamInit, mini, maxi, gamma, lambda,
48 X2, Y, eps)
49 {
fb6e49cb 50 # Matrix dimensions
ffdf9447
BA
51 n <- dim(Y)[1]
52 if (length(dim(phiInit)) == 2)
53 {
54 p <- 1
55 m <- dim(phiInit)[1]
56 k <- dim(phiInit)[2]
57 } else
58 {
59 p <- dim(phiInit)[1]
60 m <- dim(phiInit)[2]
61 k <- dim(phiInit)[3]
fb6e49cb 62 }
ffdf9447
BA
63 X <- matrix(nrow = n, ncol = p)
64 X[1:n, 1:p] <- X2
fb6e49cb 65 # Outputs
ffdf9447
BA
66 phi <- array(NA, dim = c(p, m, k))
67 phi[1:p, , ] <- phiInit
68 rho <- rhoInit
69 pi <- piInit
70 llh <- -Inf
71 S <- array(0, dim = c(p, m, k))
fb6e49cb 72
73 # Algorithm variables
ffdf9447
BA
74 gam <- gamInit
75 Gram2 <- array(0, dim = c(p, p, k))
76 ps2 <- array(0, dim = c(p, m, k))
77 X2 <- array(0, dim = c(n, p, k))
78 Y2 <- array(0, dim = c(n, m, k))
79 EPS <- 1e-15
fb6e49cb 80
81 for (ite in 1:maxi)
82 {
83 # Remember last pi,rho,phi values for exit condition in the end of loop
ffdf9447
BA
84 Phi <- phi
85 Rho <- rho
86 Pi <- pi
fb6e49cb 87
88 # Computations associated to X and Y
89 for (r in 1:k)
90 {
ffdf9447
BA
91 for (mm in 1:m) Y2[, mm, r] <- sqrt(gam[, r]) * Y[, mm]
92 for (i in 1:n) X2[i, , r] <- sqrt(gam[i, r]) * X[i, ]
93 for (mm in 1:m) ps2[, mm, r] <- crossprod(X2[, , r], Y2[, mm, r])
fb6e49cb 94 for (j in 1:p)
95 {
ffdf9447 96 for (s in 1:p) Gram2[j, s, r] <- crossprod(X2[, j, r], X2[, s, r])
fb6e49cb 97 }
98 }
99
ffdf9447 100 ######### M step #
fb6e49cb 101
102 # For pi
ffdf9447
BA
103 b <- sapply(1:k, function(r) sum(abs(phi[, , r])))
104 gam2 <- colSums(gam)
105 a <- sum(gam %*% log(pi))
fb6e49cb 106
107 # While the proportions are nonpositive
ffdf9447
BA
108 kk <- 0
109 pi2AllPositive <- FALSE
fb6e49cb 110 while (!pi2AllPositive)
111 {
ffdf9447
BA
112 pi2 <- pi + 0.1^kk * ((1/n) * gam2 - pi)
113 pi2AllPositive <- all(pi2 >= 0)
114 kk <- kk + 1
fb6e49cb 115 }
116
117 # t(m) is the largest value in the grid O.1^k such that it is nonincreasing
ffdf9447
BA
118 while (kk < 1000 && -a/n + lambda * sum(pi^gamma * b) < -sum(gam2 * log(pi2))/n +
119 lambda * sum(pi2^gamma * b))
120 {
121 pi2 <- pi + 0.1^kk * (1/n * gam2 - pi)
122 kk <- kk + 1
fb6e49cb 123 }
ffdf9447
BA
124 t <- 0.1^kk
125 pi <- (pi + t * (pi2 - pi))/sum(pi + t * (pi2 - pi))
fb6e49cb 126
ffdf9447 127 # For phi and rho
fb6e49cb 128 for (r in 1:k)
129 {
130 for (mm in 1:m)
131 {
ffdf9447
BA
132 ps <- 0
133 for (i in 1:n) ps <- ps + Y2[i, mm, r] * sum(X2[i, , r] * phi[, mm,
134 r])
135 nY2 <- sum(Y2[, mm, r]^2)
136 rho[mm, mm, r] <- (ps + sqrt(ps^2 + 4 * nY2 * gam2[r]))/(2 * nY2)
fb6e49cb 137 }
138 }
139
140 for (r in 1:k)
141 {
142 for (j in 1:p)
143 {
144 for (mm in 1:m)
145 {
ffdf9447
BA
146 S[j, mm, r] <- -rho[mm, mm, r] * ps2[j, mm, r] + sum(phi[-j, mm,
147 r] * Gram2[j, -j, r])
148 if (abs(S[j, mm, r]) <= n * lambda * (pi[r]^gamma))
149 phi[j, mm, r] <- 0 else if (S[j, mm, r] > n * lambda * (pi[r]^gamma))
150 phi[j, mm, r] <- (n * lambda * (pi[r]^gamma) - S[j, mm, r])/Gram2[j,
151 j, r] else phi[j, mm, r] <- -(n * lambda * (pi[r]^gamma) + S[j, mm, r])/Gram2[j,
152 j, r]
fb6e49cb 153 }
154 }
155 }
156
ffdf9447 157 ######## E step#
fb6e49cb 158
159 # Precompute det(rho[,,r]) for r in 1...k
ffdf9447
BA
160 detRho <- sapply(1:k, function(r) det(rho[, , r]))
161 gam1 <- matrix(0, nrow = n, ncol = k)
fb6e49cb 162 for (i in 1:n)
163 {
164 # Update gam[,]
165 for (r in 1:k)
166 {
ffdf9447
BA
167 gam1[i, r] <- pi[r] * exp(-0.5 * sum((Y[i, ] %*% rho[, , r] - X[i,
168 ] %*% phi[, , r])^2)) * detRho[r]
fb6e49cb 169 }
170 }
ffdf9447
BA
171 gam <- gam1/rowSums(gam1)
172 sumLogLLH <- sum(log(rowSums(gam)) - log((2 * base::pi)^(m/2)))
173 sumPen <- sum(pi^gamma * b)
174 last_llh <- llh
175 llh <- -sumLogLLH/n + lambda * sumPen
176 dist <- ifelse(ite == 1, llh, (llh - last_llh)/(1 + abs(llh)))
177 Dist1 <- max((abs(phi - Phi))/(1 + abs(phi)))
178 Dist2 <- max((abs(rho - Rho))/(1 + abs(rho)))
179 Dist3 <- max((abs(pi - Pi))/(1 + abs(Pi)))
180 dist2 <- max(Dist1, Dist2, Dist3)
fb6e49cb 181
ffdf9447 182 if (ite >= mini && (dist >= eps || dist2 >= sqrt(eps)))
fb6e49cb 183 break
184 }
185
ffdf9447 186 list(phi = phi, rho = rho, pi = pi, llh = llh, S = S)
aa480ac1 187}