From: Benjamin Auder Date: Mon, 20 Mar 2017 03:07:03 +0000 (+0100) Subject: align C version of EMGLLF on R version X-Git-Url: https://git.auder.net/game/doc/screen_players.png?a=commitdiff_plain;h=b42f0f4014e9f455a92851b1e707095bbcb45103;p=valse.git align C version of EMGLLF on R version --- diff --git a/pkg/src/sources/EMGLLF.c b/pkg/src/sources/EMGLLF.c index 86b6060..e41fe3c 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é @@ -35,34 +35,35 @@ 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* nY21 = (Real*)malloc(n*m*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)))) { @@ -238,16 +239,14 @@ void EMGLLF_core( { for (int mm=0; mm1) sum(phi[1:(j-1),mm,r] * Gram2[j,1:(j-1),r]) else 0) + - // (if(j 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; } diff --git a/test/generate_test_data/EMGLLF.R b/test/generate_test_data/EMGLLF.R index f1eeffa..673b807 100644 --- a/test/generate_test_data/EMGLLF.R +++ b/test/generate_test_data/EMGLLF.R @@ -17,7 +17,6 @@ EMGLLF_R = function(phiInit,rhoInit,piInit,gamInit,mini,maxi,gamma,lambda,X,Y,ta Gram2 = array(0, dim=c(p,p,k)) ps2 = array(0, dim=c(p,m,k)) b = rep(0, k) - pen = matrix(0, maxi, k) X2 = array(0, dim=c(n,p,k)) Y2 = array(0, dim=c(n,m,k)) dist = 0 @@ -116,7 +115,8 @@ EMGLLF_R = function(phiInit,rhoInit,piInit,gamInit,mini,maxi,gamma,lambda,X,Y,ta ########## #Etape E # ########## - sumLogLLF2 = 0 + + sumLogLLF2 = 0 for (i in 1:n) { #precompute sq norms to numerically adjust their values @@ -124,11 +124,11 @@ EMGLLF_R = function(phiInit,rhoInit,piInit,gamInit,mini,maxi,gamma,lambda,X,Y,ta for (r in 1:k){ sqNorm2[r] = sum( (Y[i,]%*%rho[,,r]-X[i,]%*%phi[,,r])^2 )} - #compute Gam(:,:) using shift determined above + #compute Gam(:,:) sumLLF1 = 0.0; for (r in 1:k) { - Gam[i,r] = pi[r] * exp(-0.5*sqNorm2[r]) * det(rho[,,r]) #FIXME: still issues here ?!?! + Gam[i,r] = pi[r] * exp(-0.5*sqNorm2[r]) * det(rho[,,r]) sumLLF1 = sumLLF1 + Gam[i,r] / (2*base::pi)^(m/2) } sumLogLLF2 = sumLogLLF2 + log(sumLLF1)