From 8e92c49c15bdacebf46190e7c8279bd227873928 Mon Sep 17 00:00:00 2001 From: Benjamin Auder 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 #include -#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 #include -#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 #include -#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 #include -#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 #include -#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 zcmeHO+lw4o8Ly4HX4GUwSp-?+WMX!zx@)FpqQa7$3B=81hwaIXlM6{^XNNOYr@K#2 z-8@y*$!=JD5`2+`MMYLod=L`YmqkHgA66L=Uj+BX3;qehiY`8QU4P%HOILS#k_bMC zREJ;p)VY1%_dDPDPStlRyNg#>*Vt1Fiv~U)HH-`Uo7X;n=wHU;v0?Dc6N<_o@aex8 z3nioQ0nH4j?|F_Aag;bi?kB=Ylrd*LYZWJb|%G?wKtwo;jMbhC(5^eHuZ^3=g z&`o{AK*PZO7|7D@{8LAa&zxM+Oa1ulG4}C~UAbS6rmkV2VW454VW454VW454VW454 zVc`Fafiym3d41pEklfYr%?W2bA z3a}152E20wWx#iUtAGO>27dh!!}u<+4V(s^0RHh|!}u-mW8mAsYd{Zp24KLSK4cib z1AYU%3d{jNoH2~ofwRCo@C5MQVZ-=4@Jrz9KmtU759|TQf%iUW7=Hxb0p14Q0=@}s z12=#Zz@HyQ8{kzy0M7wSz&!9M@QV)^#?OF#;12LD;5A?f*ubZNM}U96-!Q%kaNs;J z3;Z6*lb--@0@`_NG*oT0ltbBxhY_w0_fts7TbsC(=vwJ3+1qQrCsUe;R7T;q6!Whh9xv;(jrccBgZm*gCg%fqI)8 z(mYk4P|Jp^posp}=Gn3X8A?0gx1@ay)j7+SeceuZW|INX{!}iHe6n(eobF{Z3moSZ z=I+3t2{}&b9F>Yw7@gqr0DpCyu8IN{MmJd$+HAZNJI(Nt2G$f_ib`de36d?l%$#Vp zvceWEYMA?8;ZEa?+HIon%u+sSHJa?0mIIq-OL|Gr@CUa#rX}Tze3GxYB8wu!xO9kK z)XI|!an?f?3fAMw6YPtbV0amfxeZT>Ips3dqU!x(t`Nh75B0h{^HVeFb0s{B#Yzr&D$JS= zh^P>?lZbRD%z~Y?uLSoJbA4@N=i-&Moy*T|o;!2)5(Pd@`6$Mr#Bt#LslqK(0{a-EOr94_$jB=39ay`S-|JhBqQmKj-3 zyBe`u>RS3S9BS0GaBs&s%fkd-1S?c1_wOiSV_h?~e>dL0gPCyqoc!4B%WhW@Ujlw4 z6Pt;Y+I8VD%;n51Oq$of{Fe2Cxd(-H+Xd+3I13B;??_&DONu~XHPq$h1Myq=Kr++= zowO$Om@dBF>!cRVwCGY~tVEzqESKcN$c7OU9GoJ9F;wisb}(10Z!HSze<(5(3zpI< z<{5~E+hr*hbRG6s@fZhbq@#QC4w?; zix`(-BH;|aoB8{9DQMBW>z98=BOvVjC*2@n{mNw>0CQcKiQo$dfyvO!rVxhM6hxlv zN(ABD)?(G!FpK_VbPEw=0pnR3DVglwtu>S3s2Kw8L^J(1Gfij&;1WP;8}o)#->g$Ne6-N&5&v z=hkw5^zB!@Kk@UB{l~S({hd~%;s=z{-bR~fzQx021d)(-ct4+^@eww(+f3^Z)>RZo zN?W5ndG%sB5#+qXNP1XO2fvkW8ldR%`q_0vvMA6i7$Nw6Jy@J6tWbf+QW?5FUax1f zw9nV}_=W!LsB#&V3;olzf?nb~z>bgL=l90yX749?Y}nc2hMEfL`9H(+_TzXar|19j zeE&yyzJCLF9f*PFfiu8KfB{E(yEW0cCiDfX&oyk_kcox7Smf?=)6!-094yvb(mK+n=B>w7PI#eqjnWSJ_a^dpM3SLhQI99xL6 zm-Y)_JhgGgP1=K!Xo;DWD zP+58QS1T$`&}t>f5b4E%Ii5V@BVW#2lJ|FXdftALbRLY)xXPDHacD)d mbl>r%RdvJyGqcPdVp#=RI3@T|i}KH!d3nw2@;3Ql=)VEdATI>~ 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 #include // 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 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 #include +#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; itau)) { 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 #include #include -#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 +#include #include -#include "omp_num_threads.h" +#include +#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 #include -#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 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