Download this file

383 lines (284 with data), 9.2 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <mex.h>
#include <math.h>
#include "matrix.h"
#include "sfa.h"
/*
Files contained in this header file sfa.h:
1. Algorithms for solving the linear system A A^T z0 = Av (see the description of A from the following context)
void Thomas(double *zMax, double *z0,
double * Av, int nn)
void Rose(double *zMax, double *z0,
double * Av, int nn)
int supportSet(double *x, double *v, double *z,
double *g, int * S, double lambda, int nn)
void dualityGap(double *gap, double *z,
double *g, double *s, double *Av,
double lambda, int nn)
void dualityGap2(double *gap, double *z,
double *g, double *s, double *Av,
double lambda, int nn)
2. The Subgraident Finding Algorithm (SFA) for solving problem (4) (refer to the description of the problem for detail)
int sfa(double *x, double *gap,
double *z, double *z0, double * v, double * Av,
double lambda, int nn, int maxStep,
double *s, double *g,
double tol, int tau, int flag)
int sfa_special(double *x, double *gap,
double *z, double * v, double * Av,
double lambda, int nn, int maxStep,
double *s, double *g,
double tol, int tau)
int sfa_one(double *x, double *gap,
double *z, double * v, double * Av,
double lambda, int nn, int maxStep,
double *s, double *g,
double tol, int tau)
*/
/*
In this file, we solve the Fused Lasso Signal Approximator (FLSA) problem:
min_x 1/2 \|x-v\|^2 + lambda1 * \|x\|_1 + lambda2 * \|A x\|_1, (1)
It can be shown that, if x* is the solution to
min_x 1/2 \|x-v\|^2 + lambda2 \|A x\|_1, (2)
then
x**= sgn(x*) max(|x*|-lambda_1, 0) (3)
is the solution to (1).
By some derivation (see the description in sfa.h), (2) can be solved by
x*= v - A^T z*,
where z* is the optimal solution to
min_z 1/2 z^T A AT z - < z, A v>,
subject to \|z\|_{infty} \leq lambda2 (4)
*/
/*
In flsa, we solve (1) corresponding to a given (lambda1, lambda2)
void flsa(double *x, double *z, double *gap,
double * v, double *z0,
double lambda1, double lambda2, int n,
int maxStep, double tol, int flag)
Output parameters:
x: the solution to problem (1)
z: the solution to problem (4)
infor: the information about running the subgradient finding algorithm
infor[0] = gap: the computed gap (either the duality gap
or the summation of the absolute change of the adjacent solutions)
infor[1] = steps: the number of iterations
infor[2] = lambad2_max: the maximal value of lambda2_max
infor[3] = numS: the number of elements in the support set
Input parameters:
v: the input vector to be projected
z0: a guess of the solution of z
lambad1: the regularization parameter
labmda2: the regularization parameter
n: the length of v and x
maxStep: the maximal allowed iteration steps
tol: the tolerance parameter
tau: the program sfa is checked every tau iterations for termination
flag: the flag for initialization and deciding calling sfa
switch ( flag )
1-4, 11-14: sfa
switch ( flag )
case 1, 2, 3, or 4:
z0 is a "good" starting point
(such as the warm-start of the previous solution,
or the user want to test the performance of this starting point;
the starting point shall be further projected to the L_{infty} ball,
to make sure that it is feasible)
case 11, 12, 13, or 14: z0 is a "random" guess, and thus not used
(we shall initialize z as follows:
if lambda2 >= 0.5 * lambda_2^max, we initialize the solution of the linear system;
if lambda2 < 0.5 * lambda_2^max, we initialize with zero
this solution is projected to the L_{infty} ball)
switch( flag )
5, 15: sfa_special
switch( flag )
5: z0 is a good starting point
15: z0 is a bad starting point, use the solution of the linear system
switch( flag )
6, 16: sfa_one
switch( flag )
6: z0 is a good starting point
16: z0 is a bad starting point, use the solution of the linear system
Revision made on October 31, 2009.
The input variable z0 is not modified after calling sfa. For this sake, we allocate a new variable zz to replace z0.
*/
void flsa(double *x, double *z, double *infor,
double * v, double *z0,
double lambda1, double lambda2, int n,
int maxStep, double tol, int tau, int flag){
int i, nn=n-1, m;
double zMax, temp;
double *Av, *g, *s;
int iterStep, numS;
double gap;
double *zz; /*to replace z0, so that z0 shall not revised after */
Av=(double *) malloc(sizeof(double)*nn);
/*
Compute Av= A*v (n=4, nn=3)
A= [ -1 1 0 0;
0 -1 1 0;
0 0 -1 1]
*/
for (i=0;i<nn; i++)
Av[i]=v[i+1]-v[i];
/*
Sovlve the linear system via Thomas's algorithm or Rose's algorithm
B * z0 = Av
*/
Thomas(&zMax, z, Av, nn);
/*
Rose(&zMax, z, Av, nn);
*/
/*
printf("\n zMax=%2.5f\n",zMax);
*/
/*
We consider two cases:
1) lambda2 >= zMax, which leads to a solution with same entry values
2) lambda2 < zMax, which needs to first run sfa, and then perform soft thresholding
*/
/*
First case: lambda2 >= zMax
*/
if (lambda2 >= zMax){
temp=0;
m=n%5;
if (m!=0){
for (i=0;i<m;i++)
temp+=v[i];
}
for (i=m;i<n;i+=5){
temp += v[i] + v[i+1] + v[i+2] + v[i+3] + v[i+4];
}
temp/=n;
/* temp is the mean value of v*/
/*
soft thresholding by lambda1
*/
if (temp> lambda1)
temp= temp-lambda1;
else
if (temp < -lambda1)
temp= temp+lambda1;
else
temp=0;
m=n%7;
if (m!=0){
for (i=0;i<m;i++)
x[i]=temp;
}
for (i=m;i<n;i+=7){
x[i] =temp;
x[i+1] =temp;
x[i+2] =temp;
x[i+3] =temp;
x[i+4] =temp;
x[i+5] =temp;
x[i+6] =temp;
}
gap=0;
free(Av);
infor[0]= gap;
infor[1]= 0;
infor[2]=zMax;
infor[3]=0;
return;
}
/*
Second case: lambda2 < zMax
We need to call sfa for computing x, and then do soft thresholding
Before calling sfa, we need to allocate memory for g and s,
and initialize z and z0.
*/
/*
Allocate memory for g and s
*/
g =(double *) malloc(sizeof(double)*nn),
s =(double *) malloc(sizeof(double)*nn);
m=flag /10;
/*
If m=0, then this shows that, z0 is a "good" starting point. (m=1-6)
Otherwise (m=11-16), we shall set z as either the solution to the linear system.
or the zero point
*/
if (m==0){
for (i=0;i<nn;i++){
if (z0[i] > lambda2)
z[i]=lambda2;
else
if (z0[i]<-lambda2)
z[i]=-lambda2;
else
z[i]=z0[i];
}
}
else{
if (lambda2 >= 0.5 * zMax){
for (i=0;i<nn;i++){
if (z[i] > lambda2)
z[i]=lambda2;
else
if (z[i]<-lambda2)
z[i]=-lambda2;
}
}
else{
for (i=0;i<nn;i++)
z[i]=0;
}
}
flag=flag %10; /*
flag is now in [1:6]
for sfa, i.e., flag in [1:4], we need initialize z0 with zero
*/
if (flag>=1 && flag<=4){
zz =(double *) malloc(sizeof(double)*nn);
for (i=0;i<nn;i++)
zz[i]=0;
}
/*
call sfa, sfa_one, or sfa_special to compute z, for finding the subgradient
and x
*/
if (flag==6)
iterStep=sfa_one(x, &gap, &numS,
z, v, Av,
lambda2, nn, maxStep,
s, g,
tol, tau);
else
if (flag==5)
iterStep=sfa_special(x, &gap, &numS,
z, v, Av,
lambda2, nn, maxStep,
s, g,
tol, tau);
else{
iterStep=sfa(x, &gap, &numS,
z, zz, v, Av,
lambda2, nn, maxStep,
s, g,
tol,tau, flag);
free (zz);
/*free the variable zz*/
}
/*
soft thresholding by lambda1
*/
for(i=0;i<n;i++)
if (x[i] > lambda1)
x[i]-=lambda1;
else
if (x[i]<-lambda1)
x[i]+=lambda1;
else
x[i]=0;
free(Av);
free(g);
free(s);
infor[0]=gap;
infor[1]=iterStep;
infor[2]=zMax;
infor[3]=numS;
}