-
Notifications
You must be signed in to change notification settings - Fork 2
/
glmnetMex.matlabR13.F
371 lines (317 loc) · 10.4 KB
/
glmnetMex.matlabR13.F
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
C glmnetMex.F
C
C Lasso and elastic-net regularized generalized linear models
C [a0,ca,ia,nin,rsq,alm,nlp,jerr] = ...
C glmnetMex(parm,x,y,jd,vp,ne,nx,nlam,flmin,ulam,thr,isd,w,ka)
C [a0,ca,ia,nin,dev,alm,nlp,jerr] = ...
C glmnetMex(parm,x,y,jd,vp,ne,nx,nlam,flmin,ulam,thr,isd,nc,maxit,kopt)
C
C Extremely efficient procedures for fitting the entire lasso or
C elastic-net regularization path for linear regression, logistic and
C multinomial regression models. The algorithm uses cyclical coordinate
C descent in a pathwise as described in the paper on the maintainer's
C website.
C
C NOTES: This is a MEX-file wrapper of GLMnet.f for MATLAB. Should be called
C only by glmnet.m. For details about input and output arguments, see
C GLMnet.f.
C
C LICENSE: GPL-2
C
C DATE: 13 Jul 2009
C
C AUTHORS:
C Algorithm designed by Jerome Friedman, Trevor Hastie and Rob Tibshirani
C Fortran code written by Jerome Friedman
C MATLAB wrapper written and maintained by Hui Jiang, jiangh@stanford.edu
C Department of Statistics, Stanford University, Stanford, California, USA.
C
C REFERENCES:
C Friedman, J., Hastie, T. and Tibshirani, R. (2009)
C Regularization Paths for Generalized Linear Models via Coordinate Descent.
C To appear, Journal of Statistical Software
C
C EXAMPLE:
C parm = 1.0;
C x = [1 1; 2 2; 3 3];
C y = [1 3 2]';
C jd = 0;
C vp = [1 1]';
C ne = 3;
C nx = 2;
C nlam = 100;
C flmin = 0.0001;
C ulam = 0;
C thr = 1.0e-4;
C isd = 1;
C w = [1 1 1]';
C ka = 2;
C [a0,ca,ia,nin,rsq,alm,nlp,jerr] = glmnetMex(parm,x,y,jd,vp,ne,nx,nlam,flmin,ulam,thr,isd,w,ka)
C
C DEVELOPMENT: 13 Jul 2009: Original version of glmnetMex.f written.
C
C-----------------------------------------------------------------------
subroutine mexFunction(nlhs, plhs, nrhs, prhs)
C-----------------------------------------------------------------------
C (pointer) Replace integer by integer*8 on the DEC Alpha
C 64-bit platform
integer plhs(*), prhs(*)
integer nlhs, nrhs
C-----------------------------------------------------------------------
C Input
real parm,flmin,thr
integer ka,no,ni,nc,ne,nx,nlam,isd,maxit,kopt
real, dimension (:), allocatable :: x,y,w,vp,ulam
integer, dimension (:), allocatable :: ix,jx,jd
C Output
integer lmu,nlp,jerr
real, dimension (:), allocatable :: a0,ca,alm,dev,rsq
integer, dimension (:), allocatable :: ia,nin
C Temporary
integer temp_pr
integer temp_m, temp_n
integer task
C Check for proper number of arguments.
if(nrhs .eq. 14 .and. nlhs .eq. 8) then
task = 1;
elseif(nrhs .eq. 15 .and. nlhs .eq. 8) then
task = 2;
else
call mexErrMsgTxt('Incorrect number of arguments.')
endif
C Get input
temp_pr = mxGetPr(prhs(1))
call getreal(temp_pr,parm,1)
temp_pr = mxGetPr(prhs(2))
no = mxGetM(prhs(2))
ni = mxGetN(prhs(2))
allocate(x(1:no*ni))
call getreal(temp_pr,x,no*ni)
temp_pr = mxGetPr(prhs(4))
temp_m = mxGetM(prhs(4))
temp_n = mxGetN(prhs(4))
allocate(jd(temp_m*temp_n))
call getinteger(temp_pr,jd,temp_m*temp_n)
temp_pr = mxGetPr(prhs(5))
allocate(vp(1:ni))
call getreal(temp_pr,vp,ni)
temp_pr = mxGetPr(prhs(6))
call getinteger(temp_pr,ne,1)
temp_pr = mxGetPr(prhs(7))
call getinteger(temp_pr,nx,1)
temp_pr = mxGetPr(prhs(8))
call getinteger(temp_pr,nlam,1)
temp_pr = mxGetPr(prhs(9))
call getreal(temp_pr,flmin,1)
temp_pr = mxGetPr(prhs(10))
temp_m = mxGetM(prhs(10))
temp_n = mxGetN(prhs(10))
allocate(ulam(1:temp_m * temp_n))
call getreal(temp_pr,ulam,temp_m * temp_n)
temp_pr = mxGetPr(prhs(11))
call getreal(temp_pr,thr,1)
temp_pr = mxGetPr(prhs(12))
call getinteger(temp_pr,isd,1)
if (task .eq. 1) then
temp_pr = mxGetPr(prhs(3))
allocate(y(1:no))
call getreal(temp_pr,y,no)
temp_pr = mxGetPr(prhs(13))
allocate(w(1:no))
call getreal(temp_pr,w,no)
temp_pr = mxGetPr(prhs(14))
call getinteger(temp_pr,ka,1)
elseif (task .eq. 2) then
temp_pr = mxGetPr(prhs(13))
call getinteger(temp_pr,nc,1)
temp_pr = mxGetPr(prhs(14))
call getinteger(temp_pr,maxit,1)
temp_pr = mxGetPr(prhs(15))
call getinteger(temp_pr,kopt,1)
temp_pr = mxGetPr(prhs(3))
allocate(y(1:no*(max(2,nc))))
call getreal(temp_pr,y,no*(max(2,nc)))
endif
C Allocate memory for output
allocate(ia(1:nx))
call zerointeger(ia,nx)
allocate(nin(1:nlam))
call zerointeger(nin,nlam)
allocate(alm(1:nlam))
call zeroreal(alm,nlam)
if (task .eq. 1) then
allocate(a0(1:nlam))
call zeroreal(a0,nlam)
allocate(ca(1:nx*nlam))
call zeroreal(ca,nx*nlam)
allocate(rsq(1:nlam))
call zeroreal(rsq,nlam)
elseif (task .eq. 2) then
allocate(a0(1:nc*nlam))
call zeroreal(a0,nc*nlam)
allocate(ca(1:nx*nc*nlam))
call zeroreal(ca,nx*nc*nlam)
allocate(dev(1:nlam))
call zeroreal(dev,nlam)
endif
C Call glmnet
lmu = 0
nlp = 0
jerr = 0
if (task .eq. 1) then
call elnet(ka,parm,no,ni,x,y,w,jd,vp,ne,nx,nlam
*,flmin,ulam,thr,isd,lmu,a0,ca,ia,nin,rsq,alm,nlp,jerr)
elseif (task .eq. 2) then
call lognet(parm,no,ni,nc,x,y,jd,vp,ne,nx,nlam,flmin
*,ulam,thr,isd,maxit,kopt,lmu,a0,ca,ia,nin,dev,alm,nlp,jerr)
endif
C Prepare output
plhs(3) = mxCreateDoubleMatrix(nx,1,0)
temp_pr = mxGetPr(plhs(3))
call putinteger(ia,temp_pr,nx)
plhs(4) = mxCreateDoubleMatrix(lmu,1,0)
temp_pr = mxGetPr(plhs(4))
call putinteger(nin,temp_pr,lmu)
plhs(6) = mxCreateDoubleMatrix(lmu,1,0)
temp_pr = mxGetPr(plhs(6))
call putreal(alm,temp_pr,lmu)
plhs(7) = mxCreateDoubleMatrix(1,1,0)
temp_pr = mxGetPr(plhs(7))
call putinteger(nlp,temp_pr,1)
plhs(8) = mxCreateDoubleMatrix(1,1,0)
temp_pr = mxGetPr(plhs(8))
call putinteger(jerr,temp_pr,1)
if (task .eq. 1) then
plhs(1) = mxCreateDoubleMatrix(lmu,1,0)
temp_pr = mxGetPr(plhs(1))
call putreal(a0,temp_pr,lmu)
plhs(2) = mxCreateDoubleMatrix(nx,lmu,0)
temp_pr = mxGetPr(plhs(2))
call putreal(ca,temp_pr,nx*lmu)
plhs(5) = mxCreateDoubleMatrix(lmu,1,0)
temp_pr = mxGetPr(plhs(5))
call putreal(rsq,temp_pr,lmu)
elseif (task .eq. 2) then
plhs(1) = mxCreateDoubleMatrix(nc,lmu,0)
temp_pr = mxGetPr(plhs(1))
call putreal(a0,temp_pr,nc*lmu)
plhs(2) = mxCreateDoubleMatrix(nx*nc,lmu,0)
temp_pr = mxGetPr(plhs(2))
call putreal(ca,temp_pr,nx*nc*lmu)
plhs(5) = mxCreateDoubleMatrix(lmu,1,0)
temp_pr = mxGetPr(plhs(5))
call putreal(dev,temp_pr,lmu)
endif
C Deallocate memory
deallocate(x)
deallocate(y)
deallocate(jd)
deallocate(vp)
deallocate(ulam)
deallocate(a0)
deallocate(ca)
deallocate(ia)
deallocate(nin)
deallocate(alm)
C For logistic elastic net
if (task .eq. 1) then
deallocate(w)
deallocate(rsq)
elseif (task .eq. 2) then
deallocate(dev)
endif
return
end
C End of subroutine mexFunction
subroutine real8toreal(x, y, size)
integer size
real*8 x(size)
real y(size)
do 10 i=1,size
y(i)= x(i)
10 continue
return
end
subroutine realtoreal8(x, y, size)
integer size
real x(size)
real*8 y(size)
do 20 i=1,size
y(i)= x(i)
20 continue
return
end
subroutine real8tointeger(x, y, size)
integer size
real*8 x(size)
integer y(size)
do 30 i=1,size
y(i)= x(i)
30 continue
return
end
subroutine integertoreal8(x, y, size)
integer size
integer x(size)
real*8 y(size)
do 40 i=1,size
y(i)= x(i)
40 continue
return
end
subroutine getreal(pr,x,size)
integer pr,size
real x(size)
real*8, dimension (:), allocatable :: temp
allocate(temp(1:size))
call mxCopyPtrToReal8(pr,temp,size)
call real8toreal(temp,x,size)
deallocate(temp)
return
end
subroutine getinteger(pr,x,size)
integer pr,size
integer x(size)
real*8, dimension (:), allocatable :: temp
allocate(temp(1:size))
call mxCopyPtrToReal8(pr,temp,size)
call real8tointeger(temp,x,size)
deallocate(temp)
return
end
subroutine putreal(x,pr,size)
integer pr,size
real x(size)
real*8, dimension (:), allocatable :: temp
allocate(temp(1:size))
call realtoreal8(x,temp,size)
call mxCopyReal8ToPtr(temp,pr,size)
deallocate(temp)
return
end
subroutine putinteger(x,pr,size)
integer pr,size
integer x(size)
real*8, dimension (:), allocatable :: temp
allocate(temp(1:size))
call integertoreal8(x,temp,size)
call mxCopyReal8ToPtr(temp,pr,size)
deallocate(temp)
return
end
subroutine zeroreal(x,size)
integer size
real x(size)
do 90 i=1,size
x(i) = 0
90 continue
return
end
subroutine zerointeger(x,size)
integer size
integer x(size)
do 100 i=1,size
x(i) = 0
100 continue
return
end