Commit | Line | Data |
---|---|---|
8e92c49c | 1 | #include <stdlib.h> |
1d3c1faa | 2 | #include <omp.h> |
8e92c49c BA |
3 | #include "EMGLLF.h" |
4 | #include "utils.h" | |
1d3c1faa BA |
5 | |
6 | // Main job on raw inputs (after transformation from mxArray) | |
09ab3c16 | 7 | void selectiontotale_core( |
8e92c49c BA |
8 | // IN parameters |
9 | const double* phiInit, // parametre initial de moyenne renormalisé | |
10 | const double* rhoInit, // parametre initial de variance renormalisé | |
11 | const double* piInit,// parametre initial des proportions | |
12 | const double* gamInit, // paramètre initial des probabilités a posteriori de chaque échantillon | |
13 | int mini, // nombre minimal d'itérations dans lambdaIndex'algorithme EM | |
14 | int maxi, // nombre maximal d'itérations dans lambdaIndex'algorithme EM | |
15 | double gamma, // valeur de gamma : puissance des proportions dans la pénalisation pour un Lasso adaptatif | |
16 | const double* glambda, // valeur des paramètres de régularisation du Lasso | |
17 | const double* X,// régresseurs | |
18 | const double* Y,// réponse | |
19 | double seuil, // seuil pour prendre en compte une variable | |
20 | double tau, // seuil pour accepter la convergence | |
1d3c1faa | 21 | // OUT parameters (all pointers, to be modified) |
8e92c49c BA |
22 | int* A1, // matrice des coefficients des parametres selectionnes |
23 | int* A2, // matrice des coefficients des parametres non selectionnes | |
24 | double* Rho,// estimateur ainsi calculé par le Lasso | |
25 | double* Pi,// estimateur ainsi calculé par le Lasso | |
1d3c1faa | 26 | // additional size parameters |
8e92c49c BA |
27 | int n,// taille de lambdaIndex'echantillon |
28 | int p,// nombre de covariables | |
29 | int m,// taille de Y (multivarié) | |
30 | int k,// nombre de composantes | |
31 | int L) // taille de glambda | |
1d3c1faa BA |
32 | { |
33 | // Fill outputs with zeros: they might not be assigned | |
34 | for (int u=0; u<p*(m+1)*L; u++) | |
35 | { | |
36 | A1[u] = 0; | |
37 | A2[u] = 0; | |
38 | } | |
39 | for (int u=0; u<m*m*k*L; u++) | |
40 | Rho[u] = 0.0; | |
41 | for (int u=0; u<k*L; u++) | |
42 | Pi[u] = 0.0; | |
8e92c49c | 43 | |
1d3c1faa | 44 | //initiate parallel section |
8e92c49c | 45 | int lambdaIndex; |
1d3c1faa BA |
46 | omp_set_num_threads(OMP_NUM_THREADS); |
47 | #pragma omp parallel default(shared) private(lambdaIndex) | |
48 | { | |
49 | #pragma omp for schedule(dynamic,CHUNK_SIZE) nowait | |
50 | for (lambdaIndex=0; lambdaIndex<L; lambdaIndex++) | |
51 | { | |
52 | //allocate output variables | |
8e92c49c BA |
53 | double* phi = (double*)malloc(p*m*k*sizeof(double)); |
54 | double* rho = (double*)malloc(m*m*k*sizeof(double)); | |
55 | double* pi = (double*)malloc(k*sizeof(double)); | |
56 | double* LLF = (double*)malloc(maxi*sizeof(double)); | |
57 | double* S = (double*)malloc(p*m*k*sizeof(double)); | |
1d3c1faa BA |
58 | EMGLLF(phiInit,rhoInit,piInit,gamInit,mini,maxi,gamma,glambda[lambdaIndex],X,Y,tau, |
59 | phi,rho,pi,LLF,S, | |
60 | n,p,m,k); | |
61 | free(LLF); | |
62 | free(S); | |
8e92c49c | 63 | |
1d3c1faa | 64 | //Si un des coefficients est supérieur au seuil, on garde cette variable |
8e92c49c BA |
65 | int* selectedVariables = (int*)calloc(p*m,sizeof(int)); |
66 | int* discardedVariables = (int*)calloc(p*m,sizeof(int)); | |
1d3c1faa | 67 | int atLeastOneSelectedVariable = 0; |
8e92c49c | 68 | for (int j=0; j<p; j++) |
1d3c1faa | 69 | { |
8e92c49c BA |
70 | int cpt = 0; |
71 | int cpt2 = 0; | |
72 | for (int jj=0; jj<m; jj++) | |
1d3c1faa | 73 | { |
8e92c49c BA |
74 | double maxPhi = 0.0; |
75 | for (int r=0; r<k; r++) | |
1d3c1faa | 76 | { |
8e92c49c BA |
77 | if (fabs(phi[ai(j,jj,r,p,m,k)]) > maxPhi) |
78 | maxPhi = fabs(phi[ai(j,jj,r,p,m,k)]); | |
1d3c1faa BA |
79 | } |
80 | if (maxPhi > seuil) | |
81 | { | |
8e92c49c | 82 | selectedVariables[mi(j,cpt,p,m)] = jj+1; |
1d3c1faa BA |
83 | atLeastOneSelectedVariable = 1; |
84 | cpt++; | |
85 | } | |
86 | else | |
87 | { | |
8e92c49c | 88 | discardedVariables[mi(j,cpt2,p,m)] = jj+1; |
1d3c1faa BA |
89 | cpt2++; |
90 | } | |
91 | } | |
92 | } | |
93 | free(phi); | |
8e92c49c | 94 | |
1d3c1faa BA |
95 | if (atLeastOneSelectedVariable) |
96 | { | |
8e92c49c BA |
97 | int* vec = (int*)malloc(p*sizeof(int)); |
98 | int vecSize = 0; | |
99 | for (int j=0; j<p; j++) | |
1d3c1faa | 100 | { |
8e92c49c | 101 | if (selectedVariables[mi(j,0,p,m)] != 0) |
1d3c1faa BA |
102 | vec[vecSize++] = j; |
103 | } | |
104 | ||
105 | //A1 | |
8e92c49c BA |
106 | for (int j=0; j<p; j++) |
107 | A1[ai(j,0,lambdaIndex,p,m+1,L)] = (j < vecSize ? vec[j]+1 : 0); | |
108 | for (int j=0; j<vecSize; j++) | |
1d3c1faa | 109 | { |
8e92c49c BA |
110 | for (int jj=1; jj<=m; jj++) |
111 | A1[ai(j,jj,lambdaIndex,p,m+1,L)] = selectedVariables[mi(vec[j],jj-1,p,m)]; | |
1d3c1faa BA |
112 | } |
113 | //A2 | |
8e92c49c BA |
114 | for (int j=0; j<p; j++) |
115 | A2[ai(j,0,lambdaIndex,p,m+1,L)] = j+1; | |
116 | for (int j=0; j<p; j++) | |
1d3c1faa | 117 | { |
8e92c49c BA |
118 | for (int jj=1; jj<=m; jj++) |
119 | A2[ai(j,jj,lambdaIndex,p,m+1,L)] = discardedVariables[mi(j,jj-1,p,m)]; | |
1d3c1faa BA |
120 | } |
121 | //Rho | |
8e92c49c | 122 | for (int j=0; j<m; j++) |
1d3c1faa | 123 | { |
8e92c49c | 124 | for (int jj=0; jj<m; jj++) |
1d3c1faa | 125 | { |
8e92c49c BA |
126 | for (int r=0; r<k; r++) |
127 | Rho[ai4(j,jj,r,lambdaIndex,m,m,k,L)] = rho[ai(j,jj,r,m,m,k)]; | |
1d3c1faa BA |
128 | } |
129 | } | |
130 | //Pi | |
8e92c49c BA |
131 | for (int r=0; r<k; r++) |
132 | Pi[mi(r,lambdaIndex,k,L)] = pi[r]; | |
1d3c1faa BA |
133 | free(vec); |
134 | } | |
8e92c49c | 135 | |
1d3c1faa BA |
136 | free(selectedVariables); |
137 | free(discardedVariables); | |
138 | free(rho); | |
139 | free(pi); | |
140 | } | |
141 | } | |
142 | } |