X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=pkg%2Fsrc%2Fsources%2FEMGLLF.c;h=e8b3b84051097b6e1c62a4eab39e446008140d37;hb=05834cceaddb8942c850d5b24f69c6dcfb7134ae;hp=e0195880c84faff4f70dc3db405ca7c4baa525f0;hpb=8be79c465ecbbe849c9ee43e2a25c2760134e07a;p=valse.git diff --git a/pkg/src/sources/EMGLLF.c b/pkg/src/sources/EMGLLF.c index e019588..e8b3b84 100644 --- a/pkg/src/sources/EMGLLF.c +++ b/pkg/src/sources/EMGLLF.c @@ -1,5 +1,6 @@ #include "utils.h" #include +#include #include // TODO: don't recompute indexes ai(...) and mi(...) when possible @@ -15,12 +16,13 @@ void EMGLLF_core( Real lambda, // valeur du paramètre de régularisation du Lasso const Real* X, // régresseurs const Real* Y, // réponse - Real tau, // seuil pour accepter la convergence + Real eps, // seuil pour accepter la convergence // OUT parameters (all pointers, to be modified) Real* phi, // parametre de moyenne renormalisé, calculé par l'EM Real* rho, // parametre de variance renormalisé, calculé par l'EM Real* pi, // parametre des proportions renormalisé, calculé par l'EM - Real* LLF, // log vraisemblance associée à cet échantillon, pour les valeurs estimées des paramètres + Real* llh, // (derniere) log vraisemblance associée à cet échantillon, + // pour les valeurs estimées des paramètres Real* S, int* affec, // additional size parameters @@ -33,29 +35,23 @@ void EMGLLF_core( copyArray(phiInit, phi, p*m*k); copyArray(rhoInit, rho, m*m*k); copyArray(piInit, pi, k); - zeroArray(LLF, maxi); //S is already allocated, and doesn't need to be 'zeroed' //Other local variables: same as in R Real* gam = (Real*)malloc(n*k*sizeof(Real)); + Real* logGam = (Real*)malloc(k*sizeof(Real)); copyArray(gamInit, gam, n*k); Real* Gram2 = (Real*)malloc(p*p*k*sizeof(Real)); Real* ps2 = (Real*)malloc(p*m*k*sizeof(Real)); Real* b = (Real*)malloc(k*sizeof(Real)); Real* X2 = (Real*)malloc(n*p*k*sizeof(Real)); Real* Y2 = (Real*)malloc(n*m*k*sizeof(Real)); - Real dist = 0.; - Real dist2 = 0.; - int ite = 0; + *llh = -INFINITY; Real* pi2 = (Real*)malloc(k*sizeof(Real)); - Real* ps = (Real*)malloc(m*k*sizeof(Real)); - Real* nY2 = (Real*)malloc(m*k*sizeof(Real)); - Real* ps1 = (Real*)malloc(n*m*k*sizeof(Real)); - Real* Gam = (Real*)malloc(n*k*sizeof(Real)); - const Real EPS = 1e-15; // Additional (not at this place, in R file) Real* gam2 = (Real*)malloc(k*sizeof(Real)); Real* sqNorm2 = (Real*)malloc(k*sizeof(Real)); + Real* detRho = (Real*)malloc(k*sizeof(Real)); gsl_matrix* matrix = gsl_matrix_alloc(m, m); gsl_permutation* permutation = gsl_permutation_alloc(m); Real* YiRhoR = (Real*)malloc(m*sizeof(Real)); @@ -65,7 +61,7 @@ void EMGLLF_core( Real* Rho = (Real*)malloc(m*m*k*sizeof(Real)); Real* Pi = (Real*)malloc(k*sizeof(Real)); - while (ite < mini || (ite < maxi && (dist >= tau || dist2 >= sqrt(tau)))) + for (int ite=1; ite<=maxi; ite++) { copyArray(phi, Phi, p*m*k); copyArray(rho, Rho, m*m*k); @@ -143,8 +139,8 @@ void EMGLLF_core( } //tant que les proportions sont negatives - int kk = 0; - int pi2AllPositive = 0; + int kk = 0, + pi2AllPositive = 0; Real invN = 1./n; while (!pi2AllPositive) { @@ -209,28 +205,21 @@ void EMGLLF_core( { for (int mm=0; mm Real dotProduct = 0.; for (int u=0; udata[u*m+v] = rho[ai(u,v,r,m,m,k)]; + } + gsl_linalg_LU_decomp(matrix, permutation, &signum); + detRho[r] = gsl_linalg_LU_det(matrix, signum); + } + + Real sumLogLLH = 0.; for (int i=0; idata[u*m+v] = rho[ai(u,v,r,m,m,k)]; - } - gsl_linalg_LU_decomp(matrix, permutation, &signum); - Real detRhoR = gsl_linalg_LU_det(matrix, signum); - Gam[mi(i,r,n,k)] = pi[r] * exp(-.5*sqNorm2[r]) * detRhoR; - sumLLF1 += Gam[mi(i,r,n,k)] / gaussConstM; - sumGamI += Gam[mi(i,r,n,k)]; + logGam[r] = log(pi[r]) - .5 * sqNorm2[r] + log(detRho[r]); + if (maxLogGam < logGam[r]) + maxLogGam = logGam[r]; } - - sumLogLLF2 += log(sumLLF1); + Real norm_fact = 0.; for (int r=0; r EPS ? Gam[mi(i,r,n,k)] / sumGamI : 0.; + logGam[r] = logGam[r] - maxLogGam; //adjust without changing proportions + gam[mi(i,r,n,k)] = exp(logGam[r]); //gam[i, ] <- exp(logGam) + norm_fact += gam[mi(i,r,n,k)]; //norm_fact <- sum(gam[i, ]) } + // gam[i, ] <- gam[i, ] / norm_fact + for (int r=0; r dist2) dist2 = Dist2; if (Dist3 > dist2) dist2 = Dist3; - ite++; + if (ite >= mini && (dist >= eps || dist2 >= sqrt(eps))) + break; } //affec = apply(gam, 1, which.max) @@ -400,15 +401,13 @@ void EMGLLF_core( //free memory free(b); free(gam); - free(Gam); + free(logGam); free(Phi); free(Rho); free(Pi); - free(ps); - free(nY2); - free(ps1); free(Gram2); free(ps2); + free(detRho); gsl_matrix_free(matrix); gsl_permutation_free(permutation); free(XiPhiR); @@ -418,4 +417,4 @@ void EMGLLF_core( free(X2); free(Y2); free(sqNorm2); -} +}