From: Benjamin Auder Date: Fri, 16 Dec 2016 15:48:27 +0000 (+0100) Subject: finished translation MATLAB --> R,C ; start debugging... X-Git-Url: https://git.auder.net/images/assets/doc/%7B%7B%20path%28%27fos_user_registration_register%27%29%20%7D%7D?a=commitdiff_plain;h=8e92c49c15bdacebf46190e7c8279bd227873928;p=valse.git finished translation MATLAB --> R,C ; start debugging... --- 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 ba4ef92..0000000 Binary files a/src/sources/.constructionModelesLassoRank.c.swp and /dev/null differ 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