From 8e92c49c15bdacebf46190e7c8279bd227873928 Mon Sep 17 00:00:00 2001 From: Benjamin Auder <benjamin.auder@somewhere> Date: Fri, 16 Dec 2016 16:48:27 +0100 Subject: [PATCH] finished translation MATLAB --> R,C ; start debugging... --- R/main.R | 149 +++++++++--------- src/Makevars | 6 +- src/adapters/a.EMGLLF.c | 6 +- src/adapters/a.EMGrank.c | 6 +- src/adapters/a.constructionModelesLassoMLE.c | 6 +- src/adapters/a.constructionModelesLassoRank.c | 10 +- src/adapters/a.selectiontotale.c | 6 +- .../.constructionModelesLassoRank.c.swp | Bin 16384 -> 0 bytes src/sources/EMGLLF.c | 60 +++---- src/sources/EMGLLF.h | 46 +++--- src/sources/EMGrank.c | 6 +- src/sources/EMGrank.h | 34 ++-- src/sources/constructionModelesLassoMLE.c | 4 +- src/sources/constructionModelesLassoMLE.h | 54 +++---- src/sources/constructionModelesLassoRank.c | 8 +- src/sources/constructionModelesLassoRank.h | 44 +++--- src/sources/selectiontotale.c | 134 ++++++++-------- src/sources/selectiontotale.h | 52 +++--- src/sources/utils.h | 52 ++++++ src/sources/utils/io.h | 26 --- src/sources/utils/tune_parallelisms.h | 10 -- 21 files changed, 366 insertions(+), 353 deletions(-) delete mode 100644 src/sources/.constructionModelesLassoRank.c.swp create mode 100644 src/sources/utils.h delete mode 100644 src/sources/utils/io.h delete mode 100644 src/sources/utils/tune_parallelisms.h diff --git a/R/main.R b/R/main.R index 9817b00..b9b8d5b 100644 --- a/R/main.R +++ b/R/main.R @@ -1,5 +1,5 @@ -SelMix = setRefClass( - Class = "selmix", +Valse = setRefClass( + Class = "Valse", fields = c( # User defined @@ -7,7 +7,7 @@ SelMix = setRefClass( # regression data (size n*p, where n is the number of observations, # and p is the number of regressors) X = "numeric", - # response data (size n*m, where n is the number of observations, + # response data (size n*m, where n is the number of observations, # and m is the number of responses) Y = "numeric", @@ -61,12 +61,12 @@ SelMix = setRefClass( ####################### initialize = function(X,Y,...) { - "Initialize SelMix object" + "Initialize Valse object" callSuper(...) - X <<- X; - Y <<- Y; + X <<- X + Y <<- Y gamma <<- ifelse (hasArg("gamma"), gamma, 1.) mini <<- ifelse (hasArg("mini"), mini, as.integer(5)) maxi <<- ifelse (hasArg("maxi"), maxi, as.integer(10)) @@ -88,11 +88,11 @@ SelMix = setRefClass( #smallEM initializes parameters by k-means and regression model in each component, #doing this 20 times, and keeping the values maximizing the likelihood after 10 #iterations of the EM algorithm. - init = initSmallEM(k,sx.X,sx.Y,sx.eps); - phiInit <<- init$phi0; - rhoInit <<- init$rho0; - piInit <<- init$pi0; - tauInit <<- init$tau0; + init = initSmallEM(k,X,Y,eps) + phiInit <<- init$phi0 + rhoInit <<- init$rho0 + piInit <<- init$pi0 + tauInit <<- init$tau0 }, computeGridLambda = function() @@ -100,8 +100,7 @@ SelMix = setRefClass( "computation of the regularization grid" #(according to explicit formula given by EM algorithm) - gridLambda <<- gridLambda(sx.phiInit,sx.rhoInit,sx.piInit,sx.tauInit,sx.X,sx.Y, - sx.gamma,sx.mini,sx.maxi,sx.eps); + gridLambda <<- gridLambda(phiInit,rhoInit,piInit,tauInit,X,Y,gamma,mini,maxi,eps) }, computeRelevantParameters = function() @@ -109,10 +108,10 @@ SelMix = setRefClass( "Compute relevant parameters" #select variables according to each regularization parameter - #from the grid: sx.A1 corresponding to selected variables, and - #sx.A2 corresponding to unselected variables. - params = selectiontotale(sx.phiInit,sx.rhoInit,sx.piInit,sx.tauInit,sx.mini,sx.maxi, - sx.gamma,sx.gridLambda,sx.X,sx.Y,sx.seuil,sx.eps); + #from the grid: A1 corresponding to selected variables, and + #A2 corresponding to unselected variables. + params = selectiontotale( + phiInit,rhoInit,piInit,tauInit,mini,maxi,gamma,gridLambda,X,Y,seuil,eps) A1 <<- params$A1 A2 <<- params$A2 Rho <<- params$Rho @@ -125,9 +124,8 @@ SelMix = setRefClass( #compute parameter estimations, with the Maximum Likelihood #Estimator, restricted on selected variables. - res = constructionModelesLassoMLE(sx.phiInit,sx.rhoInit,sx.piInit,sx.tauInit, - sx.mini,sx.maxi,sx.gamma,sx.gridLambda,sx.X,sx.Y,sx.seuil,sx.eps,sx.A1,sx.A2); - return (list( phi=res$phi, rho=res$rho, pi=res$pi)) + return ( constructionModelesLassoMLE( + phiInit,rhoInit,piInit,tauInit,mini,maxi,gamma,gridLambda,X,Y,seuil,eps,A1,A2) ) }, runProcedure2 = function() @@ -136,75 +134,76 @@ SelMix = setRefClass( #compute parameter estimations, with the Low Rank #Estimator, restricted on selected variables. - return (constructionModelesLassoRank(sx.Pi,sx.Rho,sx.mini,sx.maxi,sx.X,sx.Y,sx.eps, - sx.A1,sx.rangmin,sx.rangmax)$phi) + return ( constructionModelesLassoRank(Pi,Rho,mini,maxi,X,Y,eps, + A1,rangmin,rangmax) ) }, - run = function(procedure) + run = function() { "main loop: over all k and all lambda" # Run the all procedure, 1 with the #maximum likelihood refitting, and 2 with the Low Rank refitting. - p = dim(phiInit)[1] - m = dim(phiInit)[2] - for (k in kmin:kmax) + p = dim(phiInit)[1] + m = dim(phiInit)[2] + for (k in kmin:kmax) + { + print(k) + initParameters(k) + computeGridLambda() + computeRelevantParameters() + if (procedure == 1) { - print(k) - initParameters(k) - computeGridLambda() - computeRelevantParameters() - if (procedure == 1) + r1 = runProcedure1() + Phi2 = Phi + Rho2 = Rho + Pi2 = Pi + p = ncol(X) + m = ncol(Y) + if size(Phi2) == 0 + { + Phi[,,1:k] = r1$phi + Rho[,,1:k] = r1$rho + Pi[1:k,] = r1$pi + } else + { + Phi = array(0., dim=c(p,m,kmax,dim(Phi2)[4]+dim(r1$phi)[4])) + Phi[,,1:(dim(Phi2)[3]),1:(dim(Phi2)[4])] = Phi2 + Phi[,,1:k,dim(Phi2)[4]+1] = r1$phi + Rho = array(0., dim=c(m,m,kmax,dim(Rho2)[4]+dim(r1$rho)[4])) + Rho[,,1:(dim(Rho2)[3]),1:(dim(Rho2)[4])] = Rho2 + Rho[,,1:k,dim(Rho2)[4]+1] = r1$rho + Pi = array(0., dim=c(kmax,dim(Pi2)[2]+dim(r1$pi)[2])) + Pi[1:nrow(Pi2),1:ncol(Pi2)] = Pi2 + Pi[1:k,ncol(Pi2)+1] = r1$pi + } + } else + { + phi = runProcedure2()$phi + Phi2 = Phi + if (dim(Phi2)[1] == 0) + { + Phi(:,:,1:k,:) = phi + } else { - r1 = runProcedure1(sx) - Phi2 = Phi - Rho2 = Rho - Pi2 = Pi - p = ncol(X) - m = ncol(Y) - if size(Phi2) == 0 #TODO: continue translation MATLAB --> R - Phi(:,:,1:k,:) = r1$phi; - Rho(:,:,1:k,:) = r1$rho; - Pi(1:k,:) = r1$pi; - else - Phi = zeros(p,m,sx.kmax,size(Phi2,4)+size(r1$phi,4)); - Phi(:,:,1:size(Phi2,3),1:size(Phi2,4)) = Phi2; - Phi(:,:,1:k,size(Phi2,4)+1:end) = r1$phi; - Rho = zeros(m,m,sx.kmax,size(Rho2,4)+size(r1$rho,4)); - Rho(:,:,1:size(Rho2,3),1:size(Rho2,4)) = Rho2; - Rho(:,:,1:k,size(Rho2,4)+1:end) = r1$rho; - Pi = zeros(sx.kmax,size(Pi2,2)+size(r1$pi,2)); - Pi(1:size(Pi2,1),1:size(Pi2,2)) = Pi2; - Pi(1:k,size(Pi2,2)+1:end) = r1$pi; - end - else - [phi] = runProcedure2(sx); - phi - Phi2 = sx.Phi; - if size(Phi2,1) == 0 - sx.Phi(:,:,1:k,:) = phi; - else size(Phi2) - sx.Phi = zeros(p,m,sx.kmax,size(Phi2,4)+size(phi,4)); - size(sx.Phi) - sx.Phi(:,:,1:size(Phi2,3),1:size(Phi2,4)) = Phi2; - sx.Phi(:,:,1:k,size(Phi2,4)+1:end) = phi; - end - - end - - - end - end - + Phi = zeros(p,m,kmax,size(Phi2,4)+size(phi,4)) + size(Phi) + Phi(:,:,1:size(Phi2,3),1:size(Phi2,4)) = Phi2 + Phi(:,:,1:k,size(Phi2,4)+1:end) = phi + } + } + } + } + ################################################## - #pruning: select only one (or a few best ?!) model + #TODO: pruning: select only one (or a few best ?!) model ################################################## # - # function[model] selectModel(sx) + # function[model] selectModel( # #TODO - # #model = sxModel(...); + # #model = odel(...) # end - + ) ) diff --git a/src/Makevars b/src/Makevars index bec17f0..6068f39 100644 --- a/src/Makevars +++ b/src/Makevars @@ -1,4 +1,8 @@ -PKG_CFLAGS=-g -I. +#Debug flags +PKG_CFLAGS=-g -I./sources + +#Prod flags: +#PKG_CFLAGS=-O2 -I./sources PKG_LIBS=-lm diff --git a/src/adapters/a.EMGLLF.c b/src/adapters/a.EMGLLF.c index 747a3c9..f70d69d 100644 --- a/src/adapters/a.EMGLLF.c +++ b/src/adapters/a.EMGLLF.c @@ -1,6 +1,6 @@ #include <R.h> #include <Rdefines.h> -#include "sources/EMGLLF.h" +#include "EMGLLF.h" SEXP EMGLLF( SEXP phiInit_, @@ -17,7 +17,7 @@ SEXP EMGLLF( ) { // Get matrices dimensions int n = INTEGER(getAttrib(X_, R_DimSymbol))[0]; - SEXP dim = getAttrib(phiInit_, R_DimSymbol) + SEXP dim = getAttrib(phiInit_, R_DimSymbol); int p = INTEGER(dim)[0]; int m = INTEGER(dim)[1]; int k = INTEGER(dim)[2]; @@ -57,7 +57,7 @@ SEXP EMGLLF( PROTECT(pi = allocVector(REALSXP, k)); PROTECT(LLF = allocVector(REALSXP, maxi-mini+1)); PROTECT(S = allocArray(REALSXP, dimPhiS)); - double* pPhi=REAL(phi), pRho=REAL(rho), pPi=REAL(pi), pLLF=REAL(LLF), pS=REAL(S); + double *pPhi=REAL(phi), *pRho=REAL(rho), *pPi=REAL(pi), *pLLF=REAL(LLF), *pS=REAL(S); //////////////////// // Call to EMGLLF // diff --git a/src/adapters/a.EMGrank.c b/src/adapters/a.EMGrank.c index f7fc192..763ff4e 100644 --- a/src/adapters/a.EMGrank.c +++ b/src/adapters/a.EMGrank.c @@ -1,6 +1,6 @@ #include <R.h> #include <Rdefines.h> -#include "sources/EMGLLF.h" +#include "EMGrank.h" SEXP EMGLLF( SEXP Pi_, @@ -16,7 +16,7 @@ SEXP EMGLLF( SEXP dimX = getAttrib(X_, R_DimSymbol); int n = INTEGER(dimX)[0]; int p = INTEGER(dimX)[1]; - SEXP dimRho = getAttrib(Rho_, R_DimSymbol) + SEXP dimRho = getAttrib(Rho_, R_DimSymbol); int m = INTEGER(dimRho)[0]; int k = INTEGER(dimRho)[2]; @@ -46,7 +46,7 @@ SEXP EMGLLF( pDimPhi[0] = p; pDimPhi[1] = m; pDimPhi[2] = k; PROTECT(phi = allocArray(REALSXP, dimPhi)); PROTECT(LLF = allocVector(REALSXP, 1)); - double* pPhi=REAL(phi), pLLF=REAL(LLF); + double *pPhi=REAL(phi), *pLLF=REAL(LLF); ///////////////////// // Call to EMGrank // diff --git a/src/adapters/a.constructionModelesLassoMLE.c b/src/adapters/a.constructionModelesLassoMLE.c index 7f578fb..8543658 100644 --- a/src/adapters/a.constructionModelesLassoMLE.c +++ b/src/adapters/a.constructionModelesLassoMLE.c @@ -1,6 +1,6 @@ #include <R.h> #include <Rdefines.h> -#include "sources/EMGLLF.h" +#include "constructionModelesLassoMLE.h" SEXP EMGLLF( SEXP phiInit_, @@ -20,7 +20,7 @@ SEXP EMGLLF( ) { // Get matrices dimensions int n = INTEGER(getAttrib(X_, R_DimSymbol))[0]; - SEXP dim = getAttrib(phiInit_, R_DimSymbol) + SEXP dim = getAttrib(phiInit_, R_DimSymbol); int p = INTEGER(dim)[0]; int m = INTEGER(dim)[1]; int k = INTEGER(dim)[2]; @@ -63,7 +63,7 @@ SEXP EMGLLF( PROTECT(rho = allocArray(REALSXP, dimRho)); PROTECT(pi = allocMatrix(REALSXP, k, L)); PROTECT(lvraisemblance = allocMatrix(REALSXP, L, 2)); - double* pPhi=REAL(phi), pRho=REAL(rho), pPi=REAL(pi), pLvraisemblance=REAL(lvraisemblance); + double *pPhi=REAL(phi), *pRho=REAL(rho), *pPi=REAL(pi), *pLvraisemblance=REAL(lvraisemblance); ///////////////////////////////////////// // Call to constructionModelesLassoMLE // diff --git a/src/adapters/a.constructionModelesLassoRank.c b/src/adapters/a.constructionModelesLassoRank.c index 359290c..0d056c8 100644 --- a/src/adapters/a.constructionModelesLassoRank.c +++ b/src/adapters/a.constructionModelesLassoRank.c @@ -1,6 +1,6 @@ #include <R.h> #include <Rdefines.h> -#include "sources/EMGLLF.h" +#include "constructionModelesLassoRank.h" SEXP EMGLLF( SEXP Pi_, @@ -12,13 +12,13 @@ SEXP EMGLLF( SEXP tau_, SEXP A1_, SEXP rangmin_, - SEXP rangmax + SEXP rangmax_ ) { // Get matrices dimensions SEXP dimX = getAttrib(X_, R_DimSymbol); int n = INTEGER(dimX)[0]; int p = INTEGER(dimX)[1]; - SEXP dimRho = getAttrib(Rho_, R_DimSymbol) + SEXP dimRho = getAttrib(Rho_, R_DimSymbol); int m = INTEGER(dimRho)[0]; int k = INTEGER(dimRho)[2]; int L = INTEGER(getAttrib(A1_, R_DimSymbol))[1]; @@ -39,7 +39,7 @@ SEXP EMGLLF( double* Rho = REAL(Rho_); double* X = REAL(X_); double* Y = REAL(Y_); - double* A1 = REAL(A1_); + int* A1 = INTEGER(A1_); ///////////// // OUTPUTS // @@ -52,7 +52,7 @@ SEXP EMGLLF( pDimPhi[0] = p; pDimPhi[1] = m; pDimPhi[2] = k; pDimPhi[3] = L*Size; PROTECT(phi = allocArray(REALSXP, dimPhi)); PROTECT(lvraisemblance = allocMatrix(REALSXP, L*Size, 2)); - double* pPhi=REAL(phi), pLvraisemblance=REAL(lvraisemblance); + double *pPhi=REAL(phi), *pLvraisemblance=REAL(lvraisemblance); ////////////////////////////////////////// // Call to constructionModelesLassoRank // diff --git a/src/adapters/a.selectiontotale.c b/src/adapters/a.selectiontotale.c index 1124d71..3d647fb 100644 --- a/src/adapters/a.selectiontotale.c +++ b/src/adapters/a.selectiontotale.c @@ -1,6 +1,6 @@ #include <R.h> #include <Rdefines.h> -#include "sources/EMGLLF.h" +#include "selectiontotale.h" SEXP EMGLLF( SEXP phiInit_, @@ -20,7 +20,7 @@ SEXP EMGLLF( SEXP dimX = getAttrib(X_, R_DimSymbol); int n = INTEGER(dimX)[0]; int p = INTEGER(dimX)[1]; - SEXP dimRho = getAttrib(rhoInit_, R_DimSymbol) + SEXP dimRho = getAttrib(rhoInit_, R_DimSymbol); int m = INTEGER(dimRho)[0]; int k = INTEGER(dimRho)[2]; int L = INTEGER(getAttrib(glambda_, R_LengthSymbol))[0]; @@ -58,7 +58,7 @@ SEXP EMGLLF( PROTECT(A2 = allocArray(REALSXP, dimA)); PROTECT(rho = allocArray(REALSXP, dimRho); PROTECT(pi = allocMatrix(REALSXP, k, L)); - double* pA1=REAL(A1), pA2=REAL(A2), pRho=REAL(rho), pPi=REAL(pi); + double *pA1=REAL(A1), *pA2=REAL(A2), *pRho=REAL(rho), *pPi=REAL(pi); ///////////////////////////// // Call to selectiontotale // diff --git a/src/sources/.constructionModelesLassoRank.c.swp b/src/sources/.constructionModelesLassoRank.c.swp deleted file mode 100644 index ba4ef92ddf008ce6f0e3dec44826b37a528e6883..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 16384 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 diff --git a/src/sources/EMGLLF.c b/src/sources/EMGLLF.c index 0c39d6e..6966e9c 100644 --- a/src/sources/EMGLLF.c +++ b/src/sources/EMGLLF.c @@ -1,31 +1,32 @@ -#include "EMGLLF.h" +#include "utils.h" +#include <stdlib.h> #include <gsl/gsl_linalg.h> // TODO: don't recompute indexes every time...... void EMGLLF( // IN parameters - const double* phiInit, // parametre initial de moyenne renormalisé - const double* rhoInit, // parametre initial de variance renormalisé - const double* piInit, // parametre initial des proportions - const double* gamInit, // paramètre initial des probabilités a posteriori de chaque échantillon - int mini, // nombre minimal d'itérations dans l'algorithme EM - int maxi, // nombre maximal d'itérations dans l'algorithme EM - double gamma, // valeur de gamma : puissance des proportions dans la pénalisation pour un Lasso adaptatif + const double* phiInit, // parametre initial de moyenne renormalisé + const double* rhoInit, // parametre initial de variance renormalisé + const double* piInit, // parametre initial des proportions + const double* gamInit, // paramètre initial des probabilités a posteriori de chaque échantillon + int mini, // nombre minimal d'itérations dans l'algorithme EM + int maxi, // nombre maximal d'itérations dans l'algorithme EM + double gamma, // puissance des proportions dans la pénalisation pour un Lasso adaptatif double lambda, // valeur du paramètre de régularisation du Lasso - const double* X, // régresseurs - const double* Y, // réponse - double tau, // seuil pour accepter la convergence + const double* X, // régresseurs + const double* Y, // réponse + double tau, // seuil pour accepter la convergence // OUT parameters (all pointers, to be modified) - double* phi, // parametre de moyenne renormalisé, calculé par l'EM - double* rho, // parametre de variance renormalisé, calculé par l'EM - double* pi, // parametre des proportions renormalisé, calculé par l'EM - double* LLF, // log vraisemblance associé à cet échantillon, pour les valeurs estimées des paramètres - double* S, + double* phi, // parametre de moyenne renormalisé, calculé par l'EM + double* rho, // parametre de variance renormalisé, calculé par l'EM + double* pi, // parametre des proportions renormalisé, calculé par l'EM + double* LLF, // log vraisemblance associée à cet échantillon, pour les valeurs estimées des paramètres + double* S, // additional size parameters - int n, // nombre d'echantillons - int p, // nombre de covariables - int m, // taille de Y (multivarié) - int k) // nombre de composantes dans le mélange + int n, // nombre d'echantillons + int p, // nombre de covariables + int m, // taille de Y (multivarié) + int k) // nombre de composantes dans le mélange { //Initialize outputs copyArray(phiInit, phi, p*m*k); @@ -174,7 +175,8 @@ void EMGLLF( double prodGam2logPi2 = 0.; for (int v=0; v<k; v++) prodGam2logPi2 += gam2[v] * log(pi2[v]); - while (-invN*a + lambda*piPowGammaDotB < -invN*prodGam2logPi2 + lambda*pi2PowGammaDotB && kk<1000) + while (-invN*a + lambda*piPowGammaDotB < -invN*prodGam2logPi2 + lambda*pi2PowGammaDotB + && kk<1000) { //pi2=pi+0.1^kk*(1/n*gam2-pi); for (int v=0; v<k; v++) @@ -223,7 +225,7 @@ void EMGLLF( sumNy21 += nY21[ai(u,mm,r,n,m,k)]; nY2[mi(mm,r,m,k)] = sumNy21; //rho(mm,mm,r)=((ps(mm,r)+sqrt(ps(mm,r)^2+4*nY2(mm,r)*(gam2(r))))/(2*nY2(mm,r))); - rho[ai(mm,mm,k,m,m,k)] = ( ps[mi(mm,r,m,k)] + sqrt( ps[mi(mm,r,m,k)]*ps[mi(mm,r,m,k)] + rho[ai(mm,mm,k,m,m,k)] = ( ps[mi(mm,r,m,k)] + sqrt( ps[mi(mm,r,m,k)]*ps[mi(mm,r,m,k)] + 4*nY2[mi(mm,r,m,k)] * (gam2[r]) ) ) / (2*nY2[mi(mm,r,m,k)]); } } @@ -233,22 +235,23 @@ void EMGLLF( { for (int mm=0; mm<m; mm++) { - //sum(phi(1:j-1,mm,r).*transpose(Gram2(j,1:j-1,r)))+sum(phi(j+1:p,mm,r).*transpose(Gram2(j,j+1:p,r))) + //sum(phi(1:j-1,mm,r).*transpose(Gram2(j,1:j-1,r)))+sum(phi(j+1:p,mm,r) + // .*transpose(Gram2(j,j+1:p,r))) double dotPhiGram2 = 0.0; for (int u=0; u<j; u++) dotPhiGram2 += phi[ai(u,mm,r,p,m,k)] * Gram2[ai(j,u,r,p,p,k)]; for (int u=j+1; u<p; u++) dotPhiGram2 += phi[ai(u,mm,r,p,m,k)] * Gram2[ai(j,u,r,p,p,k)]; //S(j,r,mm)=-rho(mm,mm,r)*ps2(j,mm,r)+sum(phi(1:j-1,mm,r).*transpose(Gram2(j,1:j-1,r))) - // +sum(phi(j+1:p,mm,r).*transpose(Gram2(j,j+1:p,r))); + // +sum(phi(j+1:p,mm,r).*transpose(Gram2(j,j+1:p,r))); S[ai(j,mm,r,p,m,k)] = -rho[ai(mm,mm,r,m,m,k)] * ps2[ai(j,mm,r,p,m,k)] + dotPhiGram2; if (fabs(S[ai(j,mm,r,p,m,k)]) <= n*lambda*pow(pi[r],gamma)) phi[ai(j,mm,r,p,m,k)] = 0; else if (S[ai(j,mm,r,p,m,k)] > n*lambda*pow(pi[r],gamma)) - phi[ai(j,mm,r,p,m,k)] = (n*lambda*pow(pi[r],gamma) - S[ai(j,mm,r,p,m,k)]) + phi[ai(j,mm,r,p,m,k)] = (n*lambda*pow(pi[r],gamma) - 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(pi[r],gamma) + S[ai(j,mm,r,p,m,k)]) + phi[ai(j,mm,r,p,m,k)] = -(n*lambda*pow(pi[r],gamma) + S[ai(j,mm,r,p,m,k)]) / Gram2[ai(j,j,r,p,p,k)]; } } @@ -270,7 +273,7 @@ void EMGLLF( { //Compute //Gam(i,r) = Pi(r) * det(Rho(:,:,r)) * exp( -1/2 * (Y(i,:)*Rho(:,:,r) - X(i,:)... - // *phi(:,:,r)) * transpose( Y(i,:)*Rho(:,:,r) - X(i,:)*phi(:,:,r) ) ); + // *phi(:,:,r)) * transpose( Y(i,:)*Rho(:,:,r) - X(i,:)*phi(:,:,r) ) ); //split in several sub-steps //compute Y(i,:)*rho(:,:,r) @@ -289,7 +292,8 @@ void EMGLLF( XiPhiR[u] += X[mi(i,v,n,p)] * phi[ai(v,u,r,p,m,k)]; } - // compute dotProduct < Y(:,i)*rho(:,:,r)-X(i,:)*phi(:,:,r) . Y(:,i)*rho(:,:,r)-X(i,:)*phi(:,:,r) > + //compute dotProduct + // < Y(:,i)*rho(:,:,r)-X(i,:)*phi(:,:,r) . Y(:,i)*rho(:,:,r)-X(i,:)*phi(:,:,r) > dotProducts[r] = 0.0; for (int u=0; u<m; u++) dotProducts[r] += (YiRhoR[u]-XiPhiR[u]) * (YiRhoR[u]-XiPhiR[u]); diff --git a/src/sources/EMGLLF.h b/src/sources/EMGLLF.h index c75b89d..df4ae7c 100644 --- a/src/sources/EMGLLF.h +++ b/src/sources/EMGLLF.h @@ -1,31 +1,29 @@ -#ifndef select_EMGLLF_H -#define select_EMGLLF_H - -#include "ioutils.h" +#ifndef valse_EMGLLF_H +#define valse_EMGLLF_H void EMGLLF( // IN parameters - const Real* phiInit, - const Real* rhoInit, - const Real* piInit, - const Real* gamInit, - Int mini, - Int maxi, - Real gamma, - Real lambda, - const Real* X, - const Real* Y, - Real tau, + const double* phiInit, + const double* rhoInit, + const double* piInit, + const double* gamInit, + int mini, + int maxi, + double gamma, + double lambda, + const double* X, + const double* Y, + double tau, // OUT parameters - Real* phi, - Real* rho, - Real* pi, - Real* LLF, - Real* S, + double* phi, + double* rho, + double* pi, + double* LLF, + double* S, // additional size parameters - mwSize n, - mwSize p, - mwSize m, - mwSize k); + int n, + int p, + int m, + int k); #endif diff --git a/src/sources/EMGrank.c b/src/sources/EMGrank.c index 2e85ce6..5ee44e3 100644 --- a/src/sources/EMGrank.c +++ b/src/sources/EMGrank.c @@ -1,5 +1,6 @@ -#include "EMGrank.h" +#include <stdlib.h> #include <gsl/gsl_linalg.h> +#include "utils.h" // Compute pseudo-inverse of a square matrix static double* pinv(const double* matrix, int dim) @@ -82,8 +83,7 @@ void EMGrank( int* Z = (int*)calloc(n, sizeof(int)); //Initialize phi to zero, because some M loops might exit before phi affectation - for (int i=0; i<p*m*k; i++) - phi[i] = 0.0; + zeroArray(phi, p*m*k); while (ite<mini || (ite<maxi && sumDeltaPhi>tau)) { diff --git a/src/sources/EMGrank.h b/src/sources/EMGrank.h index 8c0e657..53a1d01 100644 --- a/src/sources/EMGrank.h +++ b/src/sources/EMGrank.h @@ -1,25 +1,23 @@ -#ifndef select_EMGrank_H -#define select_EMGrank_H - -#include "ioutils.h" +#ifndef valse_EMGrank_H +#define valse_EMGrank_H void EMGrank( // IN parameters - const Real* Pi, - const Real* Rho, - Int mini, - Int maxi, - const Real* X, - const Real* Y, - Real tau, - const Int* rank, + const double* Pi, + const double* Rho, + int mini, + int maxi, + const double* X, + const double* Y, + double tau, + const int* rank, // OUT parameters - Real* phi, - Real* LLF, + double* phi, + double* LLF, // additional size parameters - mwSize n, - mwSize p, - mwSize m, - mwSize k); + int n, + int p, + int m, + int k); #endif diff --git a/src/sources/constructionModelesLassoMLE.c b/src/sources/constructionModelesLassoMLE.c index 91119e9..272f01b 100644 --- a/src/sources/constructionModelesLassoMLE.c +++ b/src/sources/constructionModelesLassoMLE.c @@ -1,8 +1,8 @@ #include "EMGLLF.h" -#include "constructionModelesLassoMLE.h" +#include "utils.h" +#include <stdlib.h> #include <gsl/gsl_linalg.h> #include <omp.h> -#include "omp_num_threads.h" // TODO: comment on constructionModelesLassoMLE purpose void constructionModelesLassoMLE( diff --git a/src/sources/constructionModelesLassoMLE.h b/src/sources/constructionModelesLassoMLE.h index 90712e3..bc79a1e 100644 --- a/src/sources/constructionModelesLassoMLE.h +++ b/src/sources/constructionModelesLassoMLE.h @@ -1,34 +1,32 @@ -#ifndef select_constructionModelesLassoMLE_H -#define select_constructionModelesLassoMLE_H - -#include "ioutils.h" +#ifndef valse_constructionModelesLassoMLE_H +#define valse_constructionModelesLassoMLE_H void constructionModelesLassoMLE( - // IN parameters - const Real* phiInit, - const Real* rhoInit, - const Real* piInit, - const Real* gamInit, - Int mini, - Int maxi, - Real gamma, - const Real* glambda, - const Real* X, - const Real* Y, - Real seuil, - Real tau, - const Int* A1, - const Int* A2, + // IN parameters + const double* phiInit, + const double* rhoInit, + const double* piInit, + const double* gamInit, + int mini, + int maxi, + double gamma, + const double* glambda, + const double* X, + const double* Y, + double seuil, + double tau, + const int* A1, + const int* A2, // OUT parameters - Real* phi, - Real* rho, - Real* pi, - Real* lvraisemblance, + double* phi, + double* rho, + double* pi, + double* lvraisemblance, // additional size parameters - mwSize n, - mwSize p, - mwSize m, - mwSize k, - mwSize L); + int n, + int p, + int m, + int k, + int L); #endif diff --git a/src/sources/constructionModelesLassoRank.c b/src/sources/constructionModelesLassoRank.c index eaa83c4..943d478 100644 --- a/src/sources/constructionModelesLassoRank.c +++ b/src/sources/constructionModelesLassoRank.c @@ -1,8 +1,8 @@ -#include "EMGrank.h" -#include "constructionModelesLassoRank.h" -#include <gsl/gsl_linalg.h> +#include <stdlib.h> #include <omp.h> -#include "omp_num_threads.h" +#include <gsl/gsl_linalg.h> +#include "EMGrank.h" +#include "utils.h" // TODO: comment on constructionModelesLassoRank purpose void constructionModelesLassoRank( diff --git a/src/sources/constructionModelesLassoRank.h b/src/sources/constructionModelesLassoRank.h index 3cecfab..d1be4a8 100644 --- a/src/sources/constructionModelesLassoRank.h +++ b/src/sources/constructionModelesLassoRank.h @@ -1,29 +1,27 @@ -#ifndef select_constructionModelesLassoRank_H -#define select_constructionModelesLassoRank_H - -#include "ioutils.h" +#ifndef valse_constructionModelesLassoRank_H +#define valse_constructionModelesLassoRank_H // Main job on raw inputs (after transformation from mxArray) void constructionModelesLassoRank( - // IN parameters - const Real* Pi, - const Real* Rho, - Int mini, - Int maxi, - const Real* X, - const Real* Y, - Real tau, - const Int* A1, - Int rangmin, - Int rangmax, + // IN parameters + const double* Pi, + const double* Rho, + int mini, + int maxi, + const double* X, + const double* Y, + double tau, + const int* A1, + int rangmin, + int rangmax, // OUT parameters - Real* phi, - Real* lvraisemblance, - // additional size parameters - mwSize n, - mwSize p, - mwSize m, - mwSize k, - mwSize L); + double* phi, + double* lvraisemblance, + // additional size parameters + int n, + int p, + int m, + int k, + int L); #endif diff --git a/src/sources/selectiontotale.c b/src/sources/selectiontotale.c index f3ed95b..e7e32c8 100644 --- a/src/sources/selectiontotale.c +++ b/src/sources/selectiontotale.c @@ -1,34 +1,34 @@ -#include "selectiontotale.h" -#include "EMGLLF.h" +#include <stdlib.h> #include <omp.h> -#include "omp_num_threads.h" +#include "EMGLLF.h" +#include "utils.h" // Main job on raw inputs (after transformation from mxArray) void selectiontotale( - // IN parameters - const Real* phiInit, // parametre initial de moyenne renormalisé - const Real* rhoInit, // parametre initial de variance renormalisé - const Real* piInit, // parametre initial des proportions - const Real* gamInit, // paramètre initial des probabilités a posteriori de chaque échantillon - Int mini, // nombre minimal d'itérations dans lambdaIndex'algorithme EM - Int maxi, // nombre maximal d'itérations dans lambdaIndex'algorithme EM - Real gamma, // valeur de gamma : puissance des proportions dans la pénalisation pour un Lasso adaptatif - const Real* glambda, // valeur des paramètres de régularisation du Lasso - const Real* X, // régresseurs - const Real* Y, // réponse - Real seuil, // seuil pour prendre en compte une variable - Real tau, // seuil pour accepter la convergence + // IN parameters + const double* phiInit, // parametre initial de moyenne renormalisé + const double* rhoInit, // parametre initial de variance renormalisé + const double* piInit,// parametre initial des proportions + const double* gamInit, // paramètre initial des probabilités a posteriori de chaque échantillon + int mini, // nombre minimal d'itérations dans lambdaIndex'algorithme EM + int maxi, // nombre maximal d'itérations dans lambdaIndex'algorithme EM + double gamma, // valeur de gamma : puissance des proportions dans la pénalisation pour un Lasso adaptatif + const double* glambda, // valeur des paramètres de régularisation du Lasso + const double* X,// régresseurs + const double* Y,// réponse + double seuil, // seuil pour prendre en compte une variable + double tau, // seuil pour accepter la convergence // OUT parameters (all pointers, to be modified) - Int* A1, // matrice des coefficients des parametres selectionnes - Int* A2, // matrice des coefficients des parametres non selectionnes - Real* Rho, // estimateur ainsi calculé par le Lasso - Real* Pi, // estimateur ainsi calculé par le Lasso + int* A1, // matrice des coefficients des parametres selectionnes + int* A2, // matrice des coefficients des parametres non selectionnes + double* Rho,// estimateur ainsi calculé par le Lasso + double* Pi,// estimateur ainsi calculé par le Lasso // additional size parameters - mwSize n, // taille de lambdaIndex'echantillon - mwSize p, // nombre de covariables - mwSize m, // taille de Y (multivarié) - mwSize k, // nombre de composantes - mwSize L) // taille de glambda + int n,// taille de lambdaIndex'echantillon + int p,// nombre de covariables + int m,// taille de Y (multivarié) + int k,// nombre de composantes + int L) // taille de glambda { // Fill outputs with zeros: they might not be assigned for (int u=0; u<p*(m+1)*L; u++) @@ -40,9 +40,9 @@ void selectiontotale( Rho[u] = 0.0; for (int u=0; u<k*L; u++) Pi[u] = 0.0; - + //initiate parallel section - mwSize lambdaIndex; + int lambdaIndex; omp_set_num_threads(OMP_NUM_THREADS); #pragma omp parallel default(shared) private(lambdaIndex) { @@ -50,89 +50,89 @@ void selectiontotale( for (lambdaIndex=0; lambdaIndex<L; lambdaIndex++) { //allocate output variables - 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* LLF = (Real*)malloc(maxi*sizeof(Real)); - Real* S = (Real*)malloc(p*m*k*sizeof(Real)); + double* phi = (double*)malloc(p*m*k*sizeof(double)); + double* rho = (double*)malloc(m*m*k*sizeof(double)); + double* pi = (double*)malloc(k*sizeof(double)); + double* LLF = (double*)malloc(maxi*sizeof(double)); + double* S = (double*)malloc(p*m*k*sizeof(double)); EMGLLF(phiInit,rhoInit,piInit,gamInit,mini,maxi,gamma,glambda[lambdaIndex],X,Y,tau, phi,rho,pi,LLF,S, n,p,m,k); free(LLF); free(S); - + //Si un des coefficients est supérieur au seuil, on garde cette variable - mwSize* selectedVariables = (mwSize*)calloc(p*m,sizeof(mwSize)); - mwSize* discardedVariables = (mwSize*)calloc(p*m,sizeof(mwSize)); + int* selectedVariables = (int*)calloc(p*m,sizeof(int)); + int* discardedVariables = (int*)calloc(p*m,sizeof(int)); int atLeastOneSelectedVariable = 0; - for (mwSize j=0; j<p; j++) + for (int j=0; j<p; j++) { - mwSize cpt = 0; - mwSize cpt2 = 0; - for (mwSize jj=0; jj<m; jj++) + int cpt = 0; + int cpt2 = 0; + for (int jj=0; jj<m; jj++) { - Real maxPhi = 0.0; - for (mwSize r=0; r<k; r++) + double maxPhi = 0.0; + for (int r=0; r<k; r++) { - if (fabs(phi[j*m*k+jj*k+r]) > maxPhi) - maxPhi = fabs(phi[j*m*k+jj*k+r]); + if (fabs(phi[ai(j,jj,r,p,m,k)]) > maxPhi) + maxPhi = fabs(phi[ai(j,jj,r,p,m,k)]); } if (maxPhi > seuil) { - selectedVariables[j*m+cpt] = jj+1; + selectedVariables[mi(j,cpt,p,m)] = jj+1; atLeastOneSelectedVariable = 1; cpt++; } else { - discardedVariables[j*m+cpt2] = jj+1; + discardedVariables[mi(j,cpt2,p,m)] = jj+1; cpt2++; } } } free(phi); - + if (atLeastOneSelectedVariable) { - Int* vec = (Int*)malloc(p*sizeof(Int)); - mwSize vecSize = 0; - for (mwSize j=0; j<p; j++) + int* vec = (int*)malloc(p*sizeof(int)); + int vecSize = 0; + for (int j=0; j<p; j++) { - if (selectedVariables[j*m+0] != 0) + if (selectedVariables[mi(j,0,p,m)] != 0) vec[vecSize++] = j; } //A1 - for (mwSize j=0; j<p; j++) - A1[j*(m+1)*L+0*L+lambdaIndex] = (j < vecSize ? vec[j]+1 : 0); - for (mwSize j=0; j<vecSize; j++) + for (int j=0; j<p; j++) + A1[ai(j,0,lambdaIndex,p,m+1,L)] = (j < vecSize ? vec[j]+1 : 0); + for (int j=0; j<vecSize; j++) { - for (mwSize jj=1; jj<=m; jj++) - A1[j*(m+1)*L+jj*L+lambdaIndex] = selectedVariables[vec[j]*m+jj-1]; + for (int jj=1; jj<=m; jj++) + A1[ai(j,jj,lambdaIndex,p,m+1,L)] = selectedVariables[mi(vec[j],jj-1,p,m)]; } //A2 - for (mwSize j=0; j<p; j++) - A2[j*(m+1)*L+0*L+lambdaIndex] = j+1; - for (mwSize j=0; j<p; j++) + for (int j=0; j<p; j++) + A2[ai(j,0,lambdaIndex,p,m+1,L)] = j+1; + for (int j=0; j<p; j++) { - for (mwSize jj=1; jj<=m; jj++) - A2[j*(m+1)*L+jj*L+lambdaIndex] = discardedVariables[j*m+jj-1]; + for (int jj=1; jj<=m; jj++) + A2[ai(j,jj,lambdaIndex,p,m+1,L)] = discardedVariables[mi(j,jj-1,p,m)]; } //Rho - for (mwSize j=0; j<m; j++) + for (int j=0; j<m; j++) { - for (mwSize jj=0; jj<m; jj++) + for (int jj=0; jj<m; jj++) { - for (mwSize r=0; r<k; r++) - Rho[j*m*k*L+jj*k*L+r*L+lambdaIndex] = rho[j*m*k+jj*k+r]; + for (int r=0; r<k; r++) + Rho[ai4(j,jj,r,lambdaIndex,m,m,k,L)] = rho[ai(j,jj,r,m,m,k)]; } } //Pi - for (mwSize r=0; r<k; r++) - Pi[r*L+lambdaIndex] = pi[r]; + for (int r=0; r<k; r++) + Pi[mi(r,lambdaIndex,k,L)] = pi[r]; free(vec); } - + free(selectedVariables); free(discardedVariables); free(rho); diff --git a/src/sources/selectiontotale.h b/src/sources/selectiontotale.h index 2d02da8..5145695 100644 --- a/src/sources/selectiontotale.h +++ b/src/sources/selectiontotale.h @@ -1,33 +1,31 @@ -#ifndef select_selectiontotale_H -#define select_selectiontotale_H - -#include "ioutils.h" +#ifndef valse_selectiontotale_H +#define valse_selectiontotale_H // Main job on raw inputs (after transformation from mxArray) void selectiontotale( - // IN parameters - const Real* phiInit, - const Real* rhoInit, - const Real* piInit, - const Real* gamInit, - Int mini, - Int maxi, - Real gamma, - const Real* glambda, - const Real* X, - const Real* Y, - Real seuil, - Real tau, + // IN parameters + const double* phiInit, + const double* rhoInit, + const double* piInit, + const double* gamInit, + int mini, + int maxi, + double gamma, + const double* glambda, + const double* X, + const double* Y, + double seuil, + double tau, // OUT parameters - Int* A1, - Int* A2, - Real* Rho, - Real* Pi, + int* A1, + int* A2, + double* Rho, + double* Pi, // additional size parameters - mwSize n, - mwSize p, - mwSize m, - mwSize k, - mwSize L); - + int n, + int p, + int m, + int k, + int L); + #endif diff --git a/src/sources/utils.h b/src/sources/utils.h new file mode 100644 index 0000000..0c5a8d8 --- /dev/null +++ b/src/sources/utils.h @@ -0,0 +1,52 @@ +#ifndef valse_utils_H +#define valse_utils_H + +/******************* + * tune parallelism + *******************/ + +// Number of OpenMP threads +#define OMP_NUM_THREADS 8 + +// CHUNK_SIZE = number of lambda values to be treated sequentially by a single core +#define CHUNK_SIZE 1 + +/******************************* + * Matrix and arrays indexation + *******************************/ + +// Matrix Index ; TODO? ncol unused +#define mi(i,j,nrow,ncol)\ + j*nrow + i + +// Array Index ; TODO? d3 unused +#define ai(i,j,k,d1,d2,d3)\ + k*d1*d2 + j*d1 + i + +// Array4 Index ; TODO? ... +#define ai4(i,j,k,m,d1,d2,d3,d4)\ + m*d1*d2*d3 + k*d1*d2 + j*d1 + i + +// Array5 Index ; TODO? ... +#define ai5(i,j,k,m,n,d1,d2,d3,d4,d5)\ + n*d1*d2*d3*d4 + m*d1*d2*d3 + k*d1*d2 + j*d1 + i + +/************************* + * Array copy & "zeroing" + ************************/ + +// Fill an array with zeros +#define zeroArray(array, size)\ +{\ + for (int u=0; u<size; u++)\ + array[u] = 0;\ +} + +// Copy an 1D array +#define copyArray(array, copy, size)\ +{\ + for (int u=0; u<size; u++)\ + copy[u] = array[u];\ +} + +#endif diff --git a/src/sources/utils/io.h b/src/sources/utils/io.h deleted file mode 100644 index 9aa5899..0000000 --- a/src/sources/utils/io.h +++ /dev/null @@ -1,26 +0,0 @@ -#ifndef select_ioutils_H -#define select_ioutils_H - -// Fill an array with zeros -#define zeroArray(array, size)\ -{\ - for (int u=0; u<size; u++)\ - array[u] = 0;\ -} - -// Copy an 1D array -#define copyArray(array, copy, size)\ -{\ - for (int u=0; u<size; u++)\ - copy[u] = array[u];\ -} - -// Matrix Index ; TODO? ncol unused -#define mi(i,j,nrow,ncol)\ - j*nrow + i - -// Array Index ; TODO? d3 unused -#define ai(i,j,k,d1,d2,d3)\ - k*d1*d2 + j*d1 + i - -#endif diff --git a/src/sources/utils/tune_parallelisms.h b/src/sources/utils/tune_parallelisms.h deleted file mode 100644 index 33a5171..0000000 --- a/src/sources/utils/tune_parallelisms.h +++ /dev/null @@ -1,10 +0,0 @@ -#ifndef tune_parallelism_H -#define tune_parallelism_H - -// Number of OpenMP threads -#define OMP_NUM_THREADS 8 - -// CHUNK_SIZE = number of lambda values to be treated sequentially by a single core -#define CHUNK_SIZE 1 - -#endif -- 2.44.0