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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
|
#include <gsl/gsl_linalg.h>
#include <gsl/gsl_matrix.h>
#include <gsl/gsl_multimin.h>
#include <gsl/gsl_rng.h>
#include <gsl/gsl_sf.h>
#include <math.h>
#include <stdio.h>
#include "logistic.h"
#include "debug.h"
// I need to bundle all the data that goes to the function to optimze
// together.
typedef struct {
gsl_matrix_int *X;
gsl_vector_int *nlev;
gsl_vector *y;
gsl_matrix *Xc; // Continuous covariates matrix Nobs x Kc (NULL if not used).
double lambdaL1;
double lambdaL2;
} fix_parm_mixed_T;
double fLogit_mixed(gsl_vector *beta, gsl_matrix_int *X, gsl_vector_int *nlev,
gsl_matrix *Xc, gsl_vector *y, double lambdaL1,
double lambdaL2) {
int n = y->size;
int npar = beta->size;
double total = 0;
double aux = 0;
// Changed loop start at 1 instead of 0 to avoid regularization of
// beta_0*\/
// #pragma omp parallel for reduction (+:total)
for (int i = 1; i < npar; ++i)
total += beta->data[i] * beta->data[i];
total = (-total * lambdaL2 / 2);
// #pragma omp parallel for reduction (+:aux)
for (int i = 1; i < npar; ++i)
aux += (beta->data[i] > 0 ? beta->data[i] : -beta->data[i]);
total = total - aux * lambdaL1;
// #pragma omp parallel for schedule(static) shared(n,beta,X,nlev,y)
// #reduction (+:total)
for (int i = 0; i < n; ++i) {
double Xbetai = beta->data[0];
int iParm = 1;
for (size_t k = 0; k < X->size2; ++k) {
if (gsl_matrix_int_get(X, i, k) > 0)
Xbetai += beta->data[gsl_matrix_int_get(X, i, k) - 1 + iParm];
iParm += nlev->data[k] - 1;
}
for (size_t k = 0; k < (Xc->size2); ++k)
Xbetai += gsl_matrix_get(Xc, i, k) * beta->data[iParm++];
total += y->data[i] * Xbetai - gsl_sf_log_1plusx(gsl_sf_exp(Xbetai));
}
return -total;
}
void logistic_mixed_pred(gsl_vector *beta, // Vector of parameters
// length = 1 + Sum_k(C_k -1)
gsl_matrix_int *X, // Matrix Nobs x K
gsl_vector_int *nlev, // Vector with number categories
gsl_matrix *Xc, // Continuous covariates matrix:
// obs x Kc (NULL if not used).
gsl_vector *yhat) { // Vector of prob. predicted by
// the logistic
for (size_t i = 0; i < X->size1; ++i) {
double Xbetai = beta->data[0];
int iParm = 1;
for (size_t k = 0; k < X->size2; ++k) {
if (gsl_matrix_int_get(X, i, k) > 0)
Xbetai += beta->data[gsl_matrix_int_get(X, i, k) - 1 + iParm];
iParm += nlev->data[k] - 1;
}
// Adding the continuous.
for (size_t k = 0; k < (Xc->size2); ++k)
Xbetai += gsl_matrix_get(Xc, i, k) * beta->data[iParm++];
yhat->data[i] = 1 / (1 + gsl_sf_exp(-Xbetai));
}
}
// The gradient of f, df = (df/dx, df/dy).
void wgsl_mixed_optim_df(const gsl_vector *beta, void *params,
gsl_vector *out) {
fix_parm_mixed_T *p = (fix_parm_mixed_T *)params;
int n = p->y->size;
int K = p->X->size2;
int Kc = p->Xc->size2;
int npar = beta->size;
// Intitialize gradient out necessary?
for (int i = 0; i < npar; ++i)
out->data[i] = 0;
// Changed loop start at 1 instead of 0 to avoid regularization of beta 0.
for (int i = 1; i < npar; ++i)
out->data[i] = p->lambdaL2 * beta->data[i];
for (int i = 1; i < npar; ++i)
out->data[i] += p->lambdaL1 * ((beta->data[i] > 0) - (beta->data[i] < 0));
for (int i = 0; i < n; ++i) {
double pn = 0;
double Xbetai = beta->data[0];
int iParm = 1;
for (int k = 0; k < K; ++k) {
if (gsl_matrix_int_get(p->X, i, k) > 0)
Xbetai += beta->data[gsl_matrix_int_get(p->X, i, k) - 1 + iParm];
iParm += p->nlev->data[k] - 1;
}
// Adding the continuous.
for (int k = 0; k < Kc; ++k)
Xbetai += gsl_matrix_get(p->Xc, i, k) * beta->data[iParm++];
pn = -(p->y->data[i] - 1 / (1 + gsl_sf_exp(-Xbetai)));
out->data[0] += pn;
iParm = 1;
for (int k = 0; k < K; ++k) {
if (gsl_matrix_int_get(p->X, i, k) > 0)
out->data[gsl_matrix_int_get(p->X, i, k) - 1 + iParm] += pn;
iParm += p->nlev->data[k] - 1;
}
// Adding the continuous.
for (int k = 0; k < Kc; ++k) {
out->data[iParm++] += gsl_matrix_get(p->Xc, i, k) * pn;
}
}
}
// The Hessian of f.
void wgsl_mixed_optim_hessian(const gsl_vector *beta, void *params,
gsl_matrix *out) {
fix_parm_mixed_T *p = (fix_parm_mixed_T *)params;
int n = p->y->size;
int K = p->X->size2;
int Kc = p->Xc->size2;
int npar = beta->size;
gsl_vector *gn = gsl_vector_safe_alloc(npar); // gn
// Intitialize Hessian out necessary ???
gsl_matrix_set_zero(out);
/* Changed loop start at 1 instead of 0 to avoid regularization of beta 0*/
for (int i = 1; i < npar; ++i)
gsl_matrix_set(out, i, i, (p->lambdaL2)); // Double-check this.
// L1 penalty not working yet, as not differentiable, I may need to
// do coordinate descent (as in glm_net)
for (int i = 0; i < n; ++i) {
double pn = 0;
double aux = 0;
double Xbetai = beta->data[0];
int iParm1 = 1;
for (int k = 0; k < K; ++k) {
if (gsl_matrix_int_get(p->X, i, k) > 0)
Xbetai += beta->data[gsl_matrix_int_get(p->X, i, k) - 1 + iParm1];
iParm1 += p->nlev->data[k] - 1; //-1?
}
// Adding the continuous.
for (int k = 0; k < Kc; ++k)
Xbetai += gsl_matrix_get(p->Xc, i, k) * beta->data[iParm1++];
pn = 1 / (1 + gsl_sf_exp(-Xbetai));
// Add a protection for pn very close to 0 or 1?
aux = pn * (1 - pn);
// Calculate sub-gradient vector gn.
gsl_vector_set_zero(gn);
gn->data[0] = 1;
iParm1 = 1;
for (int k = 0; k < K; ++k) {
if (gsl_matrix_int_get(p->X, i, k) > 0)
gn->data[gsl_matrix_int_get(p->X, i, k) - 1 + iParm1] = 1;
iParm1 += p->nlev->data[k] - 1;
}
// Adding the continuous.
for (int k = 0; k < Kc; ++k) {
gn->data[iParm1++] = gsl_matrix_get(p->Xc, i, k);
}
for (int k1 = 0; k1 < npar; ++k1)
if (gn->data[k1] != 0)
for (int k2 = 0; k2 < npar; ++k2)
if (gn->data[k2] != 0)
*gsl_matrix_ptr(out, k1, k2) += (aux * gn->data[k1] * gn->data[k2]);
}
gsl_vector_free(gn);
}
double wgsl_mixed_optim_f(gsl_vector *v, void *params) {
fix_parm_mixed_T *p = (fix_parm_mixed_T *)params;
return fLogit_mixed(v, p->X, p->nlev, p->Xc, p->y, p->lambdaL1, p->lambdaL2);
}
// Compute both f and df together.
void wgsl_mixed_optim_fdf(gsl_vector *x, void *params, double *f,
gsl_vector *df) {
*f = wgsl_mixed_optim_f(x, params);
wgsl_mixed_optim_df(x, params, df);
}
// Xc is the matrix of continuous covariates, Nobs x Kc (NULL if not used).
int logistic_mixed_fit(gsl_vector *beta, gsl_matrix_int *X,
gsl_vector_int *nlev, gsl_matrix *Xc, gsl_vector *y,
double lambdaL1, double lambdaL2) {
// double mLogLik = 0;
fix_parm_mixed_T p;
int npar = beta->size;
int iter = 0;
double maxchange = 0;
// Intializing fix parameters.
p.X = X;
p.Xc = Xc;
p.nlev = nlev;
p.y = y;
p.lambdaL1 = lambdaL1;
p.lambdaL2 = lambdaL2;
// Initial fit.
// auto mLogLik = wgsl_mixed_optim_f(beta, &p);
gsl_matrix *myH = gsl_matrix_safe_alloc(npar, npar); // Hessian matrix.
gsl_vector *stBeta = gsl_vector_safe_alloc(npar); // Direction to move.
gsl_vector *myG = gsl_vector_safe_alloc(npar); // Gradient.
gsl_vector *tau = gsl_vector_safe_alloc(npar); // tau for QR.
for (iter = 0; iter < 100; iter++) {
wgsl_mixed_optim_hessian(beta, &p, myH); // Calculate Hessian.
wgsl_mixed_optim_df(beta, &p, myG); // Calculate Gradient.
gsl_linalg_QR_decomp(myH, tau); // Calculate next beta.
gsl_linalg_QR_solve(myH, tau, myG, stBeta);
gsl_vector_sub(beta, stBeta);
// Monitor convergence.
maxchange = 0;
for (int i = 0; i < npar; i++)
if (maxchange < fabs(stBeta->data[i]))
maxchange = fabs(stBeta->data[i]);
if (maxchange < 1E-4)
break;
}
// Final fit.
// mLogLik = wgsl_mixed_optim_f(beta, &p);
gsl_vector_free(tau);
gsl_vector_free(stBeta);
gsl_vector_free(myG);
gsl_matrix_free(myH);
return 0;
}
/***************/
/* Categorical */
/***************/
// I need to bundle all the data that goes to the function to optimze
// together.
typedef struct {
gsl_matrix_int *X;
gsl_vector_int *nlev;
gsl_vector *y;
double lambdaL1;
double lambdaL2;
} fix_parm_cat_T;
double fLogit_cat(gsl_vector *beta, gsl_matrix_int *X, gsl_vector_int *nlev,
gsl_vector *y, double lambdaL1, double lambdaL2) {
int n = y->size;
int npar = beta->size;
double total = 0;
double aux = 0;
// omp_set_num_threads(ompthr); /\* Changed loop start at 1 instead
// of 0 to avoid regularization of beta 0*\/ /\*#pragma omp parallel
// for reduction (+:total)*\/
for (int i = 1; i < npar; ++i)
total += beta->data[i] * beta->data[i];
total = (-total * lambdaL2 / 2);
// /\*#pragma omp parallel for reduction (+:aux)*\/
for (int i = 1; i < npar; ++i)
aux += (beta->data[i] > 0 ? beta->data[i] : -beta->data[i]);
total = total - aux * lambdaL1;
// #pragma omp parallel for schedule(static) shared(n,beta,X,nlev,y)
// #reduction (+:total)
for (int i = 0; i < n; ++i) {
double Xbetai = beta->data[0];
int iParm = 1;
for (size_t k = 0; k < X->size2; ++k) {
if (gsl_matrix_int_get(X, i, k) > 0)
Xbetai += beta->data[gsl_matrix_int_get(X, i, k) - 1 + iParm];
iParm += nlev->data[k] - 1;
}
total += y->data[i] * Xbetai - gsl_sf_log_1plusx(gsl_sf_exp(Xbetai));
}
return -total;
}
void logistic_cat_pred(gsl_vector *beta, // Vector of parameters
// length = 1 + Sum_k(C_k-1).
gsl_matrix_int *X, // Matrix Nobs x K
gsl_vector_int *nlev, // Vector with #categories
gsl_vector *yhat) { // Vector of prob. predicted by
// the logistic.
for (size_t i = 0; i < X->size1; ++i) {
double Xbetai = beta->data[0];
int iParm = 1;
for (size_t k = 0; k < X->size2; ++k) {
if (gsl_matrix_int_get(X, i, k) > 0)
Xbetai += beta->data[gsl_matrix_int_get(X, i, k) - 1 + iParm];
iParm += nlev->data[k] - 1;
}
yhat->data[i] = 1 / (1 + gsl_sf_exp(-Xbetai));
}
}
// The gradient of f, df = (df/dx, df/dy).
void wgsl_cat_optim_df(const gsl_vector *beta, void *params, gsl_vector *out) {
fix_parm_cat_T *p = (fix_parm_cat_T *)params;
int n = p->y->size;
int K = p->X->size2;
int npar = beta->size;
// Intitialize gradient out necessary?
for (int i = 0; i < npar; ++i)
out->data[i] = 0;
// Changed loop start at 1 instead of 0 to avoid regularization of beta 0.
for (int i = 1; i < npar; ++i)
out->data[i] = p->lambdaL2 * beta->data[i];
for (int i = 1; i < npar; ++i)
out->data[i] += p->lambdaL1 * ((beta->data[i] > 0) - (beta->data[i] < 0));
for (int i = 0; i < n; ++i) {
double pn = 0;
double Xbetai = beta->data[0];
int iParm = 1;
for (int k = 0; k < K; ++k) {
if (gsl_matrix_int_get(p->X, i, k) > 0)
Xbetai += beta->data[gsl_matrix_int_get(p->X, i, k) - 1 + iParm];
iParm += p->nlev->data[k] - 1;
}
pn = -(p->y->data[i] - 1 / (1 + gsl_sf_exp(-Xbetai)));
out->data[0] += pn;
iParm = 1;
for (int k = 0; k < K; ++k) {
if (gsl_matrix_int_get(p->X, i, k) > 0)
out->data[gsl_matrix_int_get(p->X, i, k) - 1 + iParm] += pn;
iParm += p->nlev->data[k] - 1;
}
}
}
// The Hessian of f.
void wgsl_cat_optim_hessian(const gsl_vector *beta, void *params,
gsl_matrix *out) {
fix_parm_cat_T *p = (fix_parm_cat_T *)params;
int n = p->y->size;
int K = p->X->size2;
int npar = beta->size;
// Intitialize Hessian out necessary.
gsl_matrix_set_zero(out);
// Changed loop start at 1 instead of 0 to avoid regularization of beta.
for (int i = 1; i < npar; ++i)
gsl_matrix_set(out, i, i, (p->lambdaL2)); // Double-check this.
// L1 penalty not working yet, as not differentiable, I may need to
// do coordinate descent (as in glm_net).
for (int i = 0; i < n; ++i) {
double pn = 0;
double aux = 0;
double Xbetai = beta->data[0];
int iParm2 = 1;
int iParm1 = 1;
for (int k = 0; k < K; ++k) {
if (gsl_matrix_int_get(p->X, i, k) > 0)
Xbetai += beta->data[gsl_matrix_int_get(p->X, i, k) - 1 + iParm1];
iParm1 += p->nlev->data[k] - 1; //-1?
}
pn = 1 / (1 + gsl_sf_exp(-Xbetai));
// Add a protection for pn very close to 0 or 1?
aux = pn * (1 - pn);
*gsl_matrix_ptr(out, 0, 0) += aux;
iParm2 = 1;
for (int k2 = 0; k2 < K; ++k2) {
if (gsl_matrix_int_get(p->X, i, k2) > 0)
*gsl_matrix_ptr(out, 0, gsl_matrix_int_get(p->X, i, k2) - 1 + iParm2) +=
aux;
iParm2 += p->nlev->data[k2] - 1; //-1?
}
iParm1 = 1;
for (int k1 = 0; k1 < K; ++k1) {
if (gsl_matrix_int_get(p->X, i, k1) > 0)
*gsl_matrix_ptr(out, gsl_matrix_int_get(p->X, i, k1) - 1 + iParm1, 0) +=
aux;
iParm2 = 1;
for (int k2 = 0; k2 < K; ++k2) {
if ((gsl_matrix_int_get(p->X, i, k1) > 0) &&
(gsl_matrix_int_get(p->X, i, k2) > 0))
*gsl_matrix_ptr(out, gsl_matrix_int_get(p->X, i, k1) - 1 + iParm1,
gsl_matrix_int_get(p->X, i, k2) - 1 + iParm2) += aux;
iParm2 += p->nlev->data[k2] - 1; //-1?
}
iParm1 += p->nlev->data[k1] - 1; //-1?
}
}
}
double wgsl_cat_optim_f(gsl_vector *v, void *params) {
double mLogLik = 0;
fix_parm_cat_T *p = (fix_parm_cat_T *)params;
mLogLik = fLogit_cat(v, p->X, p->nlev, p->y, p->lambdaL1, p->lambdaL2);
return mLogLik;
}
// Compute both f and df together.
void wgsl_cat_optim_fdf(gsl_vector *x, void *params, double *f,
gsl_vector *df) {
*f = wgsl_cat_optim_f(x, params);
wgsl_cat_optim_df(x, params, df);
}
int logistic_cat_fit(gsl_vector *beta, gsl_matrix_int *X, gsl_vector_int *nlev,
gsl_vector *y, double lambdaL1, double lambdaL2) {
// double mLogLik = 0;
fix_parm_cat_T p;
int npar = beta->size;
int iter = 0;
double maxchange = 0;
// Intializing fix parameters.
p.X = X;
p.nlev = nlev;
p.y = y;
p.lambdaL1 = lambdaL1;
p.lambdaL2 = lambdaL2;
#ifdef _RPR_DEBUG_
// Initial fit.
auto mLogLik = wgsl_cat_optim_f(beta, &p);
#endif
gsl_matrix *myH = gsl_matrix_safe_alloc(npar, npar); // Hessian matrix.
gsl_vector *stBeta = gsl_vector_safe_alloc(npar); // Direction to move.
gsl_vector *myG = gsl_vector_safe_alloc(npar); // Gradient.
gsl_vector *tau = gsl_vector_safe_alloc(npar); // tau for QR.
for (iter = 0; iter < 100; iter++) {
wgsl_cat_optim_hessian(beta, &p, myH); // Calculate Hessian.
wgsl_cat_optim_df(beta, &p, myG); // Calculate Gradient.
gsl_linalg_QR_decomp(myH, tau); // Calculate next beta.
gsl_linalg_QR_solve(myH, tau, myG, stBeta);
gsl_vector_sub(beta, stBeta);
// Monitor convergence.
maxchange = 0;
for (int i = 0; i < npar; i++)
if (maxchange < fabs(stBeta->data[i]))
maxchange = fabs(stBeta->data[i]);
#ifdef _RPR_DEBUG_
mLogLik = wgsl_cat_optim_f(beta, &p);
#endif
if (maxchange < 1E-4)
break;
}
// Final fit.
// mLogLik = wgsl_cat_optim_f(beta, &p);
gsl_vector_free(tau);
gsl_vector_free(stBeta);
gsl_vector_free(myG);
gsl_matrix_free(myH);
return 0;
}
/***************/
/* Continuous */
/***************/
// I need to bundle all the data that goes to the function to optimze
// together.
typedef struct {
gsl_matrix *Xc; // continuous covariates; Matrix Nobs x Kc
gsl_vector *y;
double lambdaL1;
double lambdaL2;
} fix_parm_cont_T;
double fLogit_cont(const gsl_vector *beta, const gsl_matrix *Xc, const gsl_vector *y,
double lambdaL1, double lambdaL2) {
int n = y->size;
int npar = beta->size;
double total = 0;
double aux = 0;
// omp_set_num_threads(ompthr); /\* Changed loop start at 1 instead
// of 0 to avoid regularization of beta_0*\/ /\*#pragma omp parallel
// for reduction (+:total)*\/
for (int i = 1; i < npar; ++i)
total += beta->data[i] * beta->data[i];
total = (-total * lambdaL2 / 2);
// /\*#pragma omp parallel for reduction (+:aux)*\/
for (int i = 1; i < npar; ++i)
aux += (beta->data[i] > 0 ? beta->data[i] : -beta->data[i]);
total = total - aux * lambdaL1;
// #pragma omp parallel for schedule(static) shared(n,beta,X,nlev,y)
// #reduction (+:total)
for (int i = 0; i < n; ++i) {
double Xbetai = beta->data[0];
int iParm = 1;
for (size_t k = 0; k < (Xc->size2); ++k)
Xbetai += gsl_matrix_get(Xc, i, k) * beta->data[iParm++];
total += y->data[i] * Xbetai - gsl_sf_log_1plusx(gsl_sf_exp(Xbetai));
}
return -total;
}
void logistic_cont_pred(gsl_vector *beta, // Vector of parameters
// length = 1 + Sum_k(C_k-1).
gsl_matrix *Xc, // Continuous covariates matrix,
// Nobs x Kc (NULL if not used).
gsl_vector *yhat) { // Vector of prob. predicted by
// the logistic.
for (size_t i = 0; i < Xc->size1; ++i) {
double Xbetai = beta->data[0];
int iParm = 1;
for (size_t k = 0; k < (Xc->size2); ++k)
Xbetai += gsl_matrix_get(Xc, i, k) * beta->data[iParm++];
yhat->data[i] = 1 / (1 + gsl_sf_exp(-Xbetai));
}
}
// The gradient of f, df = (df/dx, df/dy).
void wgsl_cont_optim_df(const gsl_vector *beta, const void *params, gsl_vector *out) {
fix_parm_cont_T *p = (fix_parm_cont_T *)params;
int n = p->y->size;
int Kc = p->Xc->size2;
int npar = beta->size;
// Intitialize gradient out necessary?
for (int i = 0; i < npar; ++i)
out->data[i] = 0;
// Changed loop start at 1 instead of 0 to avoid regularization of beta 0.
for (int i = 1; i < npar; ++i)
out->data[i] = p->lambdaL2 * beta->data[i];
for (int i = 1; i < npar; ++i)
out->data[i] += p->lambdaL1 * ((beta->data[i] > 0) - (beta->data[i] < 0));
for (int i = 0; i < n; ++i) {
double pn = 0;
double Xbetai = beta->data[0];
int iParm = 1;
for (int k = 0; k < Kc; ++k)
Xbetai += gsl_matrix_get(p->Xc, i, k) * beta->data[iParm++];
pn = -(p->y->data[i] - 1 / (1 + gsl_sf_exp(-Xbetai)));
out->data[0] += pn;
iParm = 1;
// Adding the continuous.
for (int k = 0; k < Kc; ++k) {
out->data[iParm++] += gsl_matrix_get(p->Xc, i, k) * pn;
}
}
}
// The Hessian of f.
void wgsl_cont_optim_hessian(const gsl_vector *beta, void *params,
gsl_matrix *out) {
fix_parm_cont_T *p = (fix_parm_cont_T *)params;
int n = p->y->size;
int Kc = p->Xc->size2;
int npar = beta->size;
gsl_vector *gn = gsl_vector_safe_alloc(npar); // gn.
// Intitialize Hessian out necessary ???
gsl_matrix_set_zero(out);
// Changed loop start at 1 instead of 0 to avoid regularization of
// beta 0.
for (int i = 1; i < npar; ++i)
gsl_matrix_set(out, i, i, (p->lambdaL2)); // Double-check this.
// L1 penalty not working yet, as not differentiable, I may need to
// do coordinate descent (as in glm_net).
for (int i = 0; i < n; ++i) {
double pn = 0;
double aux = 0;
double Xbetai = beta->data[0];
int iParm1 = 1;
for (int k = 0; k < Kc; ++k)
Xbetai += gsl_matrix_get(p->Xc, i, k) * beta->data[iParm1++];
pn = 1 / (1 + gsl_sf_exp(-Xbetai));
// Add a protection for pn very close to 0 or 1?
aux = pn * (1 - pn);
// Calculate sub-gradient vector gn.
gsl_vector_set_zero(gn);
gn->data[0] = 1;
iParm1 = 1;
for (int k = 0; k < Kc; ++k) {
gn->data[iParm1++] = gsl_matrix_get(p->Xc, i, k);
}
for (int k1 = 0; k1 < npar; ++k1)
if (gn->data[k1] != 0)
for (int k2 = 0; k2 < npar; ++k2)
if (gn->data[k2] != 0)
*gsl_matrix_ptr(out, k1, k2) += (aux * gn->data[k1] * gn->data[k2]);
}
gsl_vector_free(gn);
}
double wgsl_cont_optim_f(const gsl_vector *v, const void *params) {
double mLogLik = 0;
fix_parm_cont_T *p = (fix_parm_cont_T *)params;
mLogLik = fLogit_cont(v, p->Xc, p->y, p->lambdaL1, p->lambdaL2);
return mLogLik;
}
// Compute both f and df together.
void wgsl_cont_optim_fdf(const gsl_vector *x, const void *params, double *f,
gsl_vector *df) {
*f = wgsl_cont_optim_f(x, params);
wgsl_cont_optim_df(x, params, df);
}
int logistic_cont_fit(gsl_vector *beta,
gsl_matrix *Xc, // Continuous covariates matrix,
// Nobs x Kc (NULL if not used).
gsl_vector *y, double lambdaL1, double lambdaL2) {
fix_parm_cont_T p;
int npar = beta->size;
int iter = 0;
double maxchange = 0;
// Initializing fix parameters.
p.Xc = Xc;
p.y = y;
p.lambdaL1 = lambdaL1;
p.lambdaL2 = lambdaL2;
#ifdef _RPR_DEBUG_
// Initial fit.
auto mLogLik = wgsl_cont_optim_f(beta, &p);
#endif
gsl_matrix *myH = gsl_matrix_safe_alloc(npar, npar); // Hessian matrix.
gsl_vector *stBeta = gsl_vector_safe_alloc(npar); // Direction to move.
gsl_vector *myG = gsl_vector_safe_alloc(npar); // Gradient.
gsl_vector *tau = gsl_vector_safe_alloc(npar); // tau for QR.
for (iter = 0; iter < 100; iter++) {
wgsl_cont_optim_hessian(beta, &p, myH); // Calculate Hessian.
wgsl_cont_optim_df(beta, &p, myG); // Calculate Gradient.
gsl_linalg_QR_decomp(myH, tau); // Calculate next beta.
gsl_linalg_QR_solve(myH, tau, myG, stBeta);
gsl_vector_sub(beta, stBeta);
// Monitor convergence.
maxchange = 0;
for (int i = 0; i < npar; i++)
if (maxchange < fabs(stBeta->data[i]))
maxchange = fabs(stBeta->data[i]);
#ifdef _RPR_DEBUG_
mLogLik = wgsl_cont_optim_f(beta, &p);
#endif
if (maxchange < 1E-4)
break;
}
// Final fit.
// mLogLik = wgsl_cont_optim_f(beta, &p);
gsl_vector_free(tau);
gsl_vector_free(stBeta);
gsl_vector_free(myG);
gsl_matrix_free(myH);
return 0;
}
|