Folder reorganization
[valse.git] / test / generate_test_data / EMGrank.R
CommitLineData
c3bc4705
BA
1#helper to always have matrices as arg (TODO: put this elsewhere? improve?)
2matricize <- function(X)
3{
4 if (!is.matrix(X))
5 return (t(as.matrix(X)))
6 return (X)
7}
8
c3b2c1ab 9require(MASS)
ef67d338
BA
10EMGrank = function(Pi, Rho, mini, maxi, X, Y, tau, rank)
11{
c2028869
BG
12 #matrix dimensions
13 n = dim(X)[1]
14 p = dim(X)[2]
15 m = dim(Rho)[2]
16 k = dim(Rho)[3]
17
18 #init outputs
19 phi = array(0, dim=c(p,m,k))
20 Z = rep(1, n)
c2028869
BG
21 LLF = 0
22
23 #local variables
24 Phi = array(0, dim=c(p,m,k))
ef67d338
BA
25 deltaPhi = c()
26 sumDeltaPhi = 0.
c2028869
BG
27 deltaPhiBufferSize = 20
28
29 #main loop
30 ite = 1
ef67d338 31 while (ite<=mini || (ite<=maxi && sumDeltaPhi>tau))
c3bc4705 32 {
c2028869 33 #M step: Mise à jour de Beta (et donc phi)
c3bc4705
BA
34 for(r in 1:k)
35 {
36 Z_indice = seq_len(n)[Z==r] #indices où Z == r
37 if (length(Z_indice) == 0)
c2028869 38 next
c2028869 39 #U,S,V = SVD of (t(Xr)Xr)^{-1} * t(Xr) * Yr
c3bc4705
BA
40 s = svd( ginv(crossprod(matricize(X[Z_indice,]))) %*%
41 crossprod(matricize(X[Z_indice,]),matricize(Y[Z_indice,])) )
42 S = s$d
c2028869
BG
43 #Set m-rank(r) singular values to zero, and recompose
44 #best rank(r) approximation of the initial product
c3bc4705
BA
45 if(rank[r] < length(S))
46 S[(rank[r]+1):length(S)] = 0
ef67d338 47 phi[,,r] = s$u %*% diag(S) %*% t(s$v) %*% Rho[,,r]
c2028869 48 }
ef67d338 49
c3bc4705
BA
50 #Etape E et calcul de LLF
51 sumLogLLF2 = 0
ef67d338
BA
52 for(i in seq_len(n))
53 {
c3bc4705
BA
54 sumLLF1 = 0
55 maxLogGamIR = -Inf
ef67d338
BA
56 for (r in seq_len(k))
57 {
c3bc4705
BA
58 dotProduct = tcrossprod(Y[i,]%*%Rho[,,r]-X[i,]%*%phi[,,r])
59 logGamIR = log(Pi[r]) + log(det(Rho[,,r])) - 0.5*dotProduct
60 #Z[i] = index of max (gam[i,])
ef67d338
BA
61 if(logGamIR > maxLogGamIR)
62 {
c3bc4705
BA
63 Z[i] = r
64 maxLogGamIR = logGamIR
65 }
ef67d338 66 sumLLF1 = sumLLF1 + exp(logGamIR) / (2*pi)^(m/2)
c3bc4705
BA
67 }
68 sumLogLLF2 = sumLogLLF2 + log(sumLLF1)
69 }
c2028869 70
c3bc4705 71 LLF = -1/n * sumLogLLF2
ef67d338 72
c3bc4705 73 #update distance parameter to check algorithm convergence (delta(phi, Phi))
ef67d338
BA
74 deltaPhi = c( deltaPhi, max( (abs(phi-Phi)) / (1+abs(phi)) ) ) #TODO: explain?
75 if (length(deltaPhi) > deltaPhiBufferSize)
76 deltaPhi = deltaPhi[2:length(deltaPhi)]
c3bc4705 77 sumDeltaPhi = sum(abs(deltaPhi))
ef67d338 78
c3bc4705
BA
79 #update other local variables
80 Phi = phi
81 ite = ite+1
c2028869 82 }
ef67d338 83 return(list("phi"=phi, "LLF"=LLF))
9ade3f1b 84}