2e85ce6fd019f9c836cc26d82210b1fc7d3a1556
[valse.git] / src / sources / EMGrank.c
1 #include "EMGrank.h"
2 #include <gsl/gsl_linalg.h>
3
4 // Compute pseudo-inverse of a square matrix
5 static double* pinv(const double* matrix, int dim)
6 {
7 gsl_matrix* U = gsl_matrix_alloc(dim,dim);
8 gsl_matrix* V = gsl_matrix_alloc(dim,dim);
9 gsl_vector* S = gsl_vector_alloc(dim);
10 gsl_vector* work = gsl_vector_alloc(dim);
11 double EPS = 1e-10; //threshold for singular value "== 0"
12
13 //copy matrix into U
14 copyArray(matrix, U->data, dim*dim);
15
16 //U,S,V = SVD of matrix
17 gsl_linalg_SV_decomp(U, V, S, work);
18 gsl_vector_free(work);
19
20 // Obtain pseudo-inverse by V*S^{-1}*t(U)
21 double* inverse = (double*)malloc(dim*dim*sizeof(double));
22 for (int i=0; i<dim; i++)
23 {
24 for (int ii=0; ii<dim; ii++)
25 {
26 double dotProduct = 0.0;
27 for (int j=0; j<dim; j++)
28 dotProduct += V->data[i*dim+j] * (S->data[j] > EPS ? 1.0/S->data[j] : 0.0) * U->data[ii*dim+j];
29 inverse[i*dim+ii] = dotProduct;
30 }
31 }
32
33 gsl_matrix_free(U);
34 gsl_matrix_free(V);
35 gsl_vector_free(S);
36 return inverse;
37 }
38
39 // TODO: comment EMGrank purpose
40 void EMGrank(
41 // IN parameters
42 const double* Pi, // parametre de proportion
43 const double* Rho, // parametre initial de variance renormalisé
44 int mini, // nombre minimal d'itérations dans l'algorithme EM
45 int maxi, // nombre maximal d'itérations dans l'algorithme EM
46 const double* X, // régresseurs
47 const double* Y, // réponse
48 double tau, // seuil pour accepter la convergence
49 const int* rank, // vecteur des rangs possibles
50 // OUT parameters
51 double* phi, // parametre de moyenne renormalisé, calculé par l'EM
52 double* LLF, // log vraisemblance associé à cet échantillon, pour les valeurs estimées des paramètres
53 // additional size parameters
54 int n, // taille de l'echantillon
55 int p, // nombre de covariables
56 int m, // taille de Y (multivarié)
57 int k) // nombre de composantes
58 {
59 // Allocations, initializations
60 double* Phi = (double*)calloc(p*m*k,sizeof(double));
61 double* hatBetaR = (double*)malloc(p*m*sizeof(double));
62 int signum;
63 double invN = 1.0/n;
64 int deltaPhiBufferSize = 20;
65 double* deltaPhi = (double*)malloc(deltaPhiBufferSize*sizeof(double));
66 int ite = 0;
67 double sumDeltaPhi = 0.0;
68 double* YiRhoR = (double*)malloc(m*sizeof(double));
69 double* XiPhiR = (double*)malloc(m*sizeof(double));
70 double* Xr = (double*)malloc(n*p*sizeof(double));
71 double* Yr = (double*)malloc(n*m*sizeof(double));
72 double* tXrXr = (double*)malloc(p*p*sizeof(double));
73 double* tXrYr = (double*)malloc(p*m*sizeof(double));
74 gsl_matrix* matrixM = gsl_matrix_alloc(p, m);
75 gsl_matrix* matrixE = gsl_matrix_alloc(m, m);
76 gsl_permutation* permutation = gsl_permutation_alloc(m);
77 gsl_matrix* V = gsl_matrix_alloc(m,m);
78 gsl_vector* S = gsl_vector_alloc(m);
79 gsl_vector* work = gsl_vector_alloc(m);
80
81 //Initialize class memberships (all elements in class 0; TODO: randomize ?)
82 int* Z = (int*)calloc(n, sizeof(int));
83
84 //Initialize phi to zero, because some M loops might exit before phi affectation
85 for (int i=0; i<p*m*k; i++)
86 phi[i] = 0.0;
87
88 while (ite<mini || (ite<maxi && sumDeltaPhi>tau))
89 {
90 /////////////
91 // Etape M //
92 /////////////
93
94 //M step: Mise à jour de Beta (et donc phi)
95 for (int r=0; r<k; r++)
96 {
97 //Compute Xr = X(Z==r,:) and Yr = Y(Z==r,:)
98 int cardClustR=0;
99 for (int i=0; i<n; i++)
100 {
101 if (Z[i] == r)
102 {
103 for (int j=0; j<p; j++)
104 Xr[mi(cardClustR,j,n,p)] = X[mi(i,j,n,p)];
105 for (int j=0; j<m; j++)
106 Yr[mi(cardClustR,j,n,m)] = Y[mi(i,j,n,m)];
107 cardClustR++;
108 }
109 }
110 if (cardClustR == 0)
111 continue;
112
113 //Compute tXrXr = t(Xr) * Xr
114 for (int j=0; j<p; j++)
115 {
116 for (int jj=0; jj<p; jj++)
117 {
118 double dotProduct = 0.0;
119 for (int u=0; u<cardClustR; u++)
120 dotProduct += Xr[mi(u,j,n,p)] * Xr[mi(u,jj,n,p)];
121 tXrXr[mi(j,jj,p,p)] = dotProduct;
122 }
123 }
124
125 //Get pseudo inverse = (t(Xr)*Xr)^{-1}
126 double* invTXrXr = pinv(tXrXr, p);
127
128 // Compute tXrYr = t(Xr) * Yr
129 for (int j=0; j<p; j++)
130 {
131 for (int jj=0; jj<m; jj++)
132 {
133 double dotProduct = 0.0;
134 for (int u=0; u<cardClustR; u++)
135 dotProduct += Xr[mi(u,j,n,p)] * Yr[mi(u,j,n,m)];
136 tXrYr[mi(j,jj,p,m)] = dotProduct;
137 }
138 }
139
140 //Fill matrixM with inverse * tXrYr = (t(Xr)*Xr)^{-1} * t(Xr) * Yr
141 for (int j=0; j<p; j++)
142 {
143 for (int jj=0; jj<m; jj++)
144 {
145 double dotProduct = 0.0;
146 for (int u=0; u<p; u++)
147 dotProduct += invTXrXr[mi(j,u,p,p)] * tXrYr[mi(u,jj,p,m)];
148 matrixM->data[j*m+jj] = dotProduct;
149 }
150 }
151 free(invTXrXr);
152
153 //U,S,V = SVD of (t(Xr)Xr)^{-1} * t(Xr) * Yr
154 gsl_linalg_SV_decomp(matrixM, V, S, work);
155
156 //Set m-rank(r) singular values to zero, and recompose
157 //best rank(r) approximation of the initial product
158 for (int j=rank[r]; j<m; j++)
159 S->data[j] = 0.0;
160
161 //[intermediate step] Compute hatBetaR = U * S * t(V)
162 double* U = matrixM->data;
163 for (int j=0; j<p; j++)
164 {
165 for (int jj=0; jj<m; jj++)
166 {
167 double dotProduct = 0.0;
168 for (int u=0; u<m; u++)
169 dotProduct += U[j*m+u] * S->data[u] * V->data[jj*m+u];
170 hatBetaR[mi(j,jj,p,m)] = dotProduct;
171 }
172 }
173
174 //Compute phi(:,:,r) = hatBetaR * Rho(:,:,r)
175 for (int j=0; j<p; j++)
176 {
177 for (int jj=0; jj<m; jj++)
178 {
179 double dotProduct=0.0;
180 for (int u=0; u<m; u++)
181 dotProduct += hatBetaR[mi(j,u,p,m)] * Rho[ai(u,jj,r,m,m,k)];
182 phi[ai(j,jj,r,p,m,k)] = dotProduct;
183 }
184 }
185 }
186
187 /////////////
188 // Etape E //
189 /////////////
190
191 double sumLogLLF2 = 0.0;
192 for (int i=0; i<n; i++)
193 {
194 double sumLLF1 = 0.0;
195 double maxLogGamIR = -INFINITY;
196 for (int r=0; r<k; r++)
197 {
198 //Compute
199 //Gam(i,r) = Pi(r) * det(Rho(:,:,r)) * exp( -1/2 * (Y(i,:)*Rho(:,:,r) - X(i,:)...
200 //*phi(:,:,r)) * transpose( Y(i,:)*Rho(:,:,r) - X(i,:)*phi(:,:,r) ) );
201 //split in several sub-steps
202
203 //compute det(Rho(:,:,r)) [TODO: avoid re-computations]
204 for (int j=0; j<m; j++)
205 {
206 for (int jj=0; jj<m; jj++)
207 matrixE->data[j*m+jj] = Rho[ai(j,jj,r,m,m,k)];
208 }
209 gsl_linalg_LU_decomp(matrixE, permutation, &signum);
210 double detRhoR = gsl_linalg_LU_det(matrixE, signum);
211
212 //compute Y(i,:)*Rho(:,:,r)
213 for (int j=0; j<m; j++)
214 {
215 YiRhoR[j] = 0.0;
216 for (int u=0; u<m; u++)
217 YiRhoR[j] += Y[mi(i,u,n,m)] * Rho[ai(u,j,r,m,m,k)];
218 }
219
220 //compute X(i,:)*phi(:,:,r)
221 for (int j=0; j<m; j++)
222 {
223 XiPhiR[j] = 0.0;
224 for (int u=0; u<p; u++)
225 XiPhiR[j] += X[mi(i,u,n,p)] * phi[ai(u,j,r,p,m,k)];
226 }
227
228 //compute dotProduct < Y(:,i)*rho(:,:,r)-X(i,:)*phi(:,:,r) . Y(:,i)*rho(:,:,r)-X(i,:)*phi(:,:,r) >
229 double dotProduct = 0.0;
230 for (int u=0; u<m; u++)
231 dotProduct += (YiRhoR[u]-XiPhiR[u]) * (YiRhoR[u]-XiPhiR[u]);
232 double logGamIR = log(Pi[r]) + log(detRhoR) - 0.5*dotProduct;
233
234 //Z(i) = index of max (gam(i,:))
235 if (logGamIR > maxLogGamIR)
236 {
237 Z[i] = r;
238 maxLogGamIR = logGamIR;
239 }
240 sumLLF1 += exp(logGamIR) / pow(2*M_PI,m/2.0);
241 }
242
243 sumLogLLF2 += log(sumLLF1);
244 }
245
246 // Assign output variable LLF
247 *LLF = -invN * sumLogLLF2;
248
249 //newDeltaPhi = max(max((abs(phi-Phi))./(1+abs(phi))));
250 double newDeltaPhi = 0.0;
251 for (int j=0; j<p; j++)
252 {
253 for (int jj=0; jj<m; jj++)
254 {
255 for (int r=0; r<k; r++)
256 {
257 double tmpDist = fabs(phi[ai(j,jj,r,p,m,k)]-Phi[ai(j,jj,r,p,m,k)])
258 / (1.0+fabs(phi[ai(j,jj,r,p,m,k)]));
259 if (tmpDist > newDeltaPhi)
260 newDeltaPhi = tmpDist;
261 }
262 }
263 }
264
265 //update distance parameter to check algorithm convergence (delta(phi, Phi))
266 //TODO: deltaPhi should be a linked list for perf.
267 if (ite < deltaPhiBufferSize)
268 deltaPhi[ite] = newDeltaPhi;
269 else
270 {
271 sumDeltaPhi -= deltaPhi[0];
272 for (int u=0; u<deltaPhiBufferSize-1; u++)
273 deltaPhi[u] = deltaPhi[u+1];
274 deltaPhi[deltaPhiBufferSize-1] = newDeltaPhi;
275 }
276 sumDeltaPhi += newDeltaPhi;
277
278 // update other local variables
279 for (int j=0; j<m; j++)
280 {
281 for (int jj=0; jj<p; jj++)
282 {
283 for (int r=0; r<k; r++)
284 Phi[ai(j,jj,r,p,m,k)] = phi[ai(j,jj,r,p,m,k)];
285 }
286 }
287 ite++;
288 }
289
290 //free memory
291 free(hatBetaR);
292 free(deltaPhi);
293 free(Phi);
294 gsl_matrix_free(matrixE);
295 gsl_matrix_free(matrixM);
296 gsl_permutation_free(permutation);
297 gsl_vector_free(work);
298 gsl_matrix_free(V);
299 gsl_vector_free(S);
300 free(XiPhiR);
301 free(YiRhoR);
302 free(Xr);
303 free(Yr);
304 free(tXrXr);
305 free(tXrYr);
306 free(Z);
307 }