X-Git-Url: https://git.auder.net/?p=valse.git;a=blobdiff_plain;f=pkg%2Fsrc%2Fsources%2FEMGLLF.c;h=e0195880c84faff4f70dc3db405ca7c4baa525f0;hp=86b606053076242cb6a623861990a4f7517089c4;hb=8be79c465ecbbe849c9ee43e2a25c2760134e07a;hpb=f87ff0f5116c0c1c59c5608e46563ff0f79e5d43 diff --git a/pkg/src/sources/EMGLLF.c b/pkg/src/sources/EMGLLF.c index 86b6060..e019588 100644 --- a/pkg/src/sources/EMGLLF.c +++ b/pkg/src/sources/EMGLLF.c @@ -2,7 +2,7 @@ #include #include -// TODO: don't recompute indexes every time...... +// TODO: don't recompute indexes ai(...) and mi(...) when possible void EMGLLF_core( // IN parameters const Real* phiInit, // parametre initial de moyenne renormalisé @@ -22,6 +22,7 @@ void EMGLLF_core( 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* S, + int* affec, // additional size parameters int n, // nombre d'echantillons int p, // nombre de covariables @@ -35,34 +36,34 @@ void EMGLLF_core( zeroArray(LLF, maxi); //S is already allocated, and doesn't need to be 'zeroed' - //Other local variables + //Other local variables: same as in R Real* gam = (Real*)malloc(n*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* Phi = (Real*)malloc(p*m*k*sizeof(Real)); - Real* Rho = (Real*)malloc(m*m*k*sizeof(Real)); - Real* Pi = (Real*)malloc(k*sizeof(Real)); - Real* gam2 = (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; Real* pi2 = (Real*)malloc(k*sizeof(Real)); - Real* Gram2 = (Real*)malloc(p*p*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* ps2 = (Real*)malloc(p*m*k*sizeof(Real)); - Real* nY21 = (Real*)malloc(n*m*k*sizeof(Real)); Real* Gam = (Real*)malloc(n*k*sizeof(Real)); - Real* X2 = (Real*)malloc(n*p*k*sizeof(Real)); - Real* Y2 = (Real*)malloc(n*m*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)); gsl_matrix* matrix = gsl_matrix_alloc(m, m); gsl_permutation* permutation = gsl_permutation_alloc(m); Real* YiRhoR = (Real*)malloc(m*sizeof(Real)); Real* XiPhiR = (Real*)malloc(m*sizeof(Real)); - Real dist = 0.; - Real dist2 = 0.; - int ite = 0; - const Real EPS = 1e-15; const Real gaussConstM = pow(2.*M_PI,m/2.); + Real* Phi = (Real*)malloc(p*m*k*sizeof(Real)); + 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)))) { @@ -77,7 +78,7 @@ void EMGLLF_core( { //Y2[,mm,r] = sqrt(gam[,r]) * Y[,mm] for (int u=0; u + //< X2[i,,r] , phi[,mm,r] > Real dotProduct = 0.; for (int u=0; u1) sum(phi[1:(j-1),mm,r] * Gram2[j,1:(j-1),r]) else 0) + - // (if(j n*lambda*pow_pir_gamma) + //sum(phi[-j,mm,r] * Gram2[j, setdiff(1:p,j),r]) + Real phiDotGram2 = 0.; + for (int u=0; u n*lambda*pirPowGamma) + { + phi[ai(j,mm,r,p,m,k)] = (n*lambda*pirPowGamma - S[ai(j,mm,r,p,m,k)]) / Gram2[ai(j,j,r,p,p,k)]; } else { - phi[ai(j,mm,r,p,m,k)] = -(n*lambda*pow_pir_gamma + S[ai(j,mm,r,p,m,k)]) + phi[ai(j,mm,r,p,m,k)] = -(n*lambda*pirPowGamma + S[ai(j,mm,r,p,m,k)]) / Gram2[ai(j,j,r,p,p,k)]; } } @@ -271,40 +271,35 @@ void EMGLLF_core( ///////////// int signum; - Real sumLogLLF2 = 0.0; + Real sumLogLLF2 = 0.; for (int i=0; i Dist1) Dist1 = tmpDist; } } } //Dist2 = max( (abs(rho-Rho)) / (1+abs(rho)) ) - Real Dist2 = 0.0; + Real Dist2 = 0.; for (int u=0; u Dist2) Dist2 = tmpDist; } } } //Dist3 = max( (abs(pi-Pi)) / (1+abs(Pi))) - Real Dist3 = 0.0; + Real Dist3 = 0.; for (int u=0; u Dist3) Dist3 = tmpDist; } @@ -388,6 +382,21 @@ void EMGLLF_core( ite++; } + //affec = apply(gam, 1, which.max) + for (int i=0; i rowMax) + { + affec[i] = j+1; //R indices start at 1 + rowMax = gam[mi(i,j,n,k)]; + } + } + } + //free memory free(b); free(gam); @@ -398,7 +407,6 @@ void EMGLLF_core( free(ps); free(nY2); free(ps1); - free(nY21); free(Gram2); free(ps2); gsl_matrix_free(matrix);