SHOGUN  v3.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
epsgLasso.h
Go to the documentation of this file.
1 /* This program is free software: you can redistribute it and/or modify
2  * it under the terms of the GNU General Public License as published by
3  * the Free Software Foundation, either version 3 of the License, or
4  * (at your option) any later version.
5  *
6  * This program is distributed in the hope that it will be useful,
7  * but WITHOUT ANY WARRANTY; without even the implied warranty of
8  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9  * GNU General Public License for more details.
10  *
11  * You should have received a copy of the GNU General Public License
12  * along with this program. If not, see <http://www.gnu.org/licenses/>.
13  *
14  * Copyright (C) 2009 - 2012 Jun Liu and Jieping Ye
15  */
16 
17 #ifndef EPSGLASSO_SLEP
18 #define EPSGLASSO_SLEP
19 
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <time.h>
23 #include <math.h>
24 #include <shogun/lib/slep/q1/epph.h> /* This is the head file that contains the implementation of the used functions*/
25 
26 
27 /*
28  Projection for sgLasso
29 
30  min 1/2 \|X - V\|_F^2 + \lambda_1 \|X\|_1 + \lambda_2 \|X\|_{p,1}
31 
32  Written by Jun Liu, January 15, 2010
33  For any problem, please contact: j.liu@asu.edu
34 
35  */
36 
37 void epsgLasso(double *X, double * normx, double * V, int k, int n, double lambda1, double lambda2, int pflag){
38  int i, j, *iter_step, nn=n*k, m;
39  double *v, *x;
40  double normValue,c0=0, c;
41 
42  v=(double *)malloc(sizeof(double)*n);
43  x=(double *)malloc(sizeof(double)*n);
44  iter_step=(int *)malloc(sizeof(int)*2);
45 
46  /*
47  initialize normx
48  */
49  normx[0]=normx[1]=0;
50 
51 
52  /*
53  X and V are k x n matrices in matlab, stored in column priority manner
54  x corresponds a row of X
55 
56  pflag=2: p=2
57  pflag=0: p=inf
58  */
59 
60  /*
61  soft thresholding
62  by lambda1
63 
64  the results are stored in X
65  */
66  for (i=0;i<nn;i++){
67  if (V[i]< -lambda1)
68  X[i]=V[i] + lambda1;
69  else
70  if (V[i]> lambda1)
71  X[i]=V[i] - lambda1;
72  else
73  X[i]=0;
74  }
75 
76  /*
77  Shrinkage or Truncating
78  by lambda2
79  */
80  if (pflag==2){
81  for(i=0; i<k; i++){
82 
83  /*
84  process the i-th row, and store it in v
85  */
86  normValue=0;
87 
88  m=n%5;
89  for(j=0;j<m;j++){
90  v[j]=X[i + j*k];
91  }
92  for(j=m;j<n;j+=5){
93  v[j ]=X[i + j*k];
94  v[j+1]=X[i + (j+1)*k ];
95  v[j+2]=X[i + (j+2)*k];
96  v[j+3]=X[i + (j+3)*k];
97  v[j+4]=X[i + (j+4)*k];
98  }
99 
100  m=n%5;
101  for(j=0;j<m;j++){
102  normValue+=v[j]*v[j];
103  }
104  for(j=m;j<n;j+=5){
105  normValue+=v[j]*v[j]+
106  v[j+1]*v[j+1]+
107  v[j+2]*v[j+2]+
108  v[j+3]*v[j+3]+
109  v[j+4]*v[j+4];
110  }
111 
112  /*
113  for(j=0; j<n; j++){
114  v[j]=X[i + j*k];
115 
116  normValue+=v[j]*v[j];
117  }
118  */
119 
120  normValue=sqrt(normValue);
121 
122  if (normValue<= lambda2){
123  for(j=0; j<n; j++)
124  X[i + j*k]=0;
125 
126  /*normx needs not to be updated*/
127  }
128  else{
129 
130  normx[1]+=normValue-lambda2;
131  /*update normx[1]*/
132 
133  normValue=(normValue-lambda2)/normValue;
134 
135  m=n%5;
136  for(j=0;j<m;j++){
137  X[i + j*k]*=normValue;
138  normx[0]+=fabs(X[i + j*k]);
139  }
140  for(j=m; j<n;j+=5){
141  X[i + j*k]*=normValue;
142  X[i + (j+1)*k]*=normValue;
143  X[i + (j+2)*k]*=normValue;
144  X[i + (j+3)*k]*=normValue;
145  X[i + (j+4)*k]*=normValue;
146 
147  normx[0]+=fabs(X[i + j*k])+
148  fabs(X[i + (j+1)*k])+
149  fabs(X[i + (j+2)*k])+
150  fabs(X[i + (j+3)*k])+
151  fabs(X[i + (j+4)*k]);
152  }
153 
154  /*
155  for(j=0; j<n; j++)
156  X[i + j*k]*=normValue;
157  */
158  }
159  }
160  }
161  else{
162  for(i=0; i<k; i++){
163 
164  /*
165  process the i-th row, and store it in v
166  */
167  normValue=0;
168  for(j=0; j<n; j++){
169  v[j]=X[i + j*k];
170 
171  normValue+=fabs(v[j]);
172  }
173 
174  if (normValue<= lambda2){
175  for(j=0; j<n; j++)
176  X[i + j*k]=0;
177  }
178  else{
179  eplb(x, &c, iter_step, v, n, lambda2, c0);
180 
181  for(j=0; j<n; j++){
182  if (X[i + j*k] > c)
183  X[i + j*k]=c;
184  else
185  if (X[i + j*k]<-c)
186  X[i + j*k]=-c;
187  }
188  }
189  }
190  }
191 
192 
193  free(v);
194  free(x);
195  free(iter_step);
196 }
197 #endif /* ----- #ifndef EPSGLASSO_SLEP ----- */
198 

SHOGUN Machine Learning Toolbox - Documentation