forked from WinVector/RcppDynProg
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogistic_regress.cpp
More file actions
435 lines (405 loc) · 12.6 KB
/
logistic_regress.cpp
File metadata and controls
435 lines (405 loc) · 12.6 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
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
#include <RcppArmadillo.h>
#include <math.h>
// [[Rcpp::depends(RcppArmadillo)]]
using Rcpp::NumericVector;
#include "input_summary.h"
double logit(const double x) {
return log(x/(1.0-x));
}
double sigmoid(const double x) {
return 1/(1+exp(-x));
}
NumericVector logistic_solve1_worker(const NumericVector &x, const NumericVector &y,
const NumericVector &w,
const NumericVector &initial_link,
const int i, const int j,
const int skip) {
const int vlen = x.length();
if((i<0) || (j>=vlen) || (vlen!=y.length()) || (vlen!=w.length()) || (vlen!=initial_link.length())) {
throw std::range_error("Inadmissible value");
}
// init return structure
NumericVector coef = NumericVector(2);
// look for corner cases
const input_summary isum = input_summary(x, y, w, i, j, skip);
// no-data cases
if(isum.k_points<=0L) {
coef(0) = 0;
coef(1) = 0;
return coef;
}
if(!isum.y_varies()) {
// y-pure
if(isum.saw_y_pos) {
coef(0) = std::numeric_limits<double>::max();
coef(1) = 0;
} else {
coef(0) = -std::numeric_limits<double>::max();
coef(1) = 0;
}
return(coef);
}
// we now know y varies
if(!isum.x_varies()) {
// x not varying case
coef(0) = logit(isum.total_wy/isum.total_w);
coef(1) = 0;
return(coef);
}
if(isum.seperable()) {
// check for seperable data cases, x able to perfectly sort y
if(isum.min_x_pos>isum.max_x_neg) {
// Note: no solution possible in this case, just returning info
coef(0) = logit(isum.total_wy/isum.total_w);
coef(1) = std::numeric_limits<double>::max();
return(coef);
} else {
// Note: no solution possible in this case, just returning info
coef(0) = logit(isum.total_wy/isum.total_w);
coef(1) = -std::numeric_limits<double>::max();
return(coef);
}
}
// Set up structers for IRwLS
// https://www.cs.purdue.edu/homes/zhang923/notes/irwls.pdf
arma::colvec link(j-i+1, arma::fill::zeros);
arma::colvec preds(j-i+1, arma::fill::zeros);
arma::colvec z(j-i+1, arma::fill::zeros);
arma::colvec wadj(j-i+1, arma::fill::ones);
for(int k=i; k<=j; ++k) {
if(k!=skip) {
link(k-i) = initial_link(k);
}
}
// Iteratively re-weighted least squares solution
double c0 = 0.0;
double c1 = 0.0;
double diff = 1.0;
for(int rep = 0; (rep<20) && (diff>1.e-6); ++rep) {
// build composite weights and z-target
for(int k=i; k<=j; ++k) {
if(k!=skip) {
preds(k-i) = sigmoid(link(k-i));
z(k-i) = link(k-i) + (y(k) - preds(k-i))/(preds(k-i)*(1.0-preds(k-i)));
wadj(k-i) = preds(k-i)*(1.0-preds(k-i))*w(k);
}
}
// build linear fitting data
double xx_0_0 = 0;
double xx_1_0 = 0;
double xx_0_1 = 0;
double xx_1_1 = 0;
double sy_0 = 0;
double xy_1 = 0;
double sum_w = 0;
for(int k=i; k<=j; ++k) {
if(k!=skip) {
xx_0_0 = xx_0_0 + wadj(k-i)*1.0;
xx_1_0 = xx_1_0 + wadj(k-i)*x(k);
xx_0_1 = xx_0_1 + wadj(k-i)*x(k);
xx_1_1 = xx_1_1 + wadj(k-i)*x(k)*x(k);
sy_0 = sy_0 + wadj(k-i)*z(k-i);
xy_1 = xy_1 + wadj(k-i)*x(k)*z(k-i);
sum_w = sum_w + wadj(k-i);
}
}
// fit linear model
c0 = 0.0;
c1 = 0.0;
if(sum_w>0.0) {
const double det = xx_0_0*xx_1_1 - xx_0_1*xx_1_0;
if(det!=0) {
// solve linear system and form estimate
c0 = (xx_1_1*sy_0 - xx_0_1*xy_1)/det;
c1 = (-xx_1_0*sy_0 + xx_0_0*xy_1)/det;
} else {
c0 = sy_0/sum_w;
c1 = 0.0;
}
}
// build next link
diff = 0.0;
for(int k=i; k<=j; ++k) {
if(k!=skip) {
const double nvi = c0 + c1*x(k);
const double diffi = nvi - link(k-i);
diff = diff + diffi*diffi;
link(k-i) = nvi;
}
}
}
coef(0) = c0;
coef(1) = c1;
return coef;
}
//' logistic_fit
//'
//' Calculate y ~ sigmoid(a + b x) using iteratively re-weighted least squares.
//' Zero indexed.
//'
//' @param x NumericVector, expanatory variable.
//' @param y NumericVector, 0/1 values to fit.
//' @param w NumericVector, weights (required, positive).
//' @param initial_link, initial link estimates (required, all zeroes is a good start).
//' @param i integer, first index (inclusive).
//' @param j integer, last index (inclusive).
//' @param skip integer, index to skip (-1 to not skip).
//' @return vector of a and b.
//'
//' @keywords internal
//'
//' @examples
//'
//' set.seed(5)
//' d <- data.frame(
//' x = rnorm(10),
//' y = sample(c(0,1), 10, replace = TRUE)
//' )
//' weights <- runif(nrow(d))
//' m <- glm(y~x, data = d, family = binomial, weights = weights)
//' coef(m)
//' logistic_solve1(d$x, d$y, weights, rep(0.0, nrow(d)), 0, nrow(d)-1, -1)
//'
//' @export
// [[Rcpp::export]]
NumericVector logistic_solve1(NumericVector x, NumericVector y,
NumericVector w,
NumericVector initial_link,
const int i, const int j,
const int skip) {
return logistic_solve1_worker(x, y,
w,
initial_link,
i, j,
skip);
}
NumericVector xlogistic_fits_worker(const NumericVector &x, const NumericVector &y,
const NumericVector &w,
const int i, const int j) {
const int vlen = x.length();
if((i<0) || (j>=vlen) || (vlen!=y.length()) || (vlen!=w.length())) {
throw std::range_error("Inadmissible value");
} const int n = j-i+1;
Rcpp::NumericVector final_links(n);
for(int k=0; k<n; ++k) {
final_links(k) = 0.0;
}
// look for corner cases
if(n<=2) {
// for out of sample- force links to zero unless we have more than 2 points
return final_links;
}
// look for corner cases
const input_summary isum = input_summary(x, y, w, i, j, -1);
if(!isum.saw_data()) {
return final_links;
}
if(!isum.y_varies()) {
// y-pure constant
if(isum.saw_y_pos) {
for(int k=0; k<n; ++k) {
final_links(k) = std::numeric_limits<double>::max();
}
} else {
for(int k=0; k<n; ++k) {
final_links(k) = -std::numeric_limits<double>::max();
}
}
return final_links;
}
// we now know y varies
if(!isum.x_varies()) {
const double lk = logit(isum.total_wy/isum.total_w);
for(int k=0; k<n; ++k) {
final_links(k) = lk;
}
return final_links;
}
// we now know x varies
if(isum.seperable()) {
// check for seperable data cases, x able to perfectly sort y
// NOTE: in this case this estimate is optimistic, as we guess the seperation point
for(int k=0; k<n; ++k) {
if(y(i+k)>=0.5) {
final_links(k) = std::numeric_limits<double>::max();
} else {
final_links(k) = -std::numeric_limits<double>::max();
}
}
return final_links;
}
// now on to general case
const int nx = x.length();
Rcpp::NumericVector initial_link(nx);
for(int k = 0; k<nx; ++k) {
initial_link(i) = 0.0;
}
// solve whole system to get a good start for hold-out systems.
Rcpp::NumericVector coefs = logistic_solve1_worker(x, y,
w,
initial_link,
i, j,
-1);
for(int k=i; k<=j; ++k) {
initial_link(k) = coefs(0) + coefs(1)*x(k);
}
// solve hold-out systems
for(int k=i; k<=j; ++k) {
Rcpp::NumericVector coefsi = logistic_solve1_worker(x, y,
w,
initial_link,
i, j,
k);
final_links(k-i) = coefsi(0) + coefsi(1)*x(k-i);
}
return final_links;
}
//' Out of sample logistic predictions (in link space).
//'
//' 1-hold out logistic regression predections.
//' Zero indexed.
//'
//' @param x NumericVector, expanatory variable.
//' @param y NumericVector, 0/1 values to fit.
//' @param w NumericVector, weights (required, positive).
//' @param i integer, first index (inclusive).
//' @param j integer, last index (inclusive).
//' @return vector of predictions for interval.
//'
//' @keywords internal
//'
//' @examples
//'
//' set.seed(5)
//' d <- data.frame(x = rnorm(10))
//' d$y <- d$x + rnorm(nrow(d))>0
//' weights <- runif(nrow(d))
//' m <- glm(y~x, data = d, family = binomial, weights = weights)
//' d$pred1 <- predict(m, newdata = d, type = "link")
//' d$pred2 <- xlogistic_fits(d$x, d$y, weights, 0, nrow(d)-1)
//' d <- d[order(d$x), , drop = FALSE]
//' print(d)
//'
//' @export
// [[Rcpp::export]]
NumericVector xlogistic_fits(NumericVector x, NumericVector y,
NumericVector w,
const int i, const int j) {
return xlogistic_fits_worker(x, y,
w,
i, j);
}
NumericVector logistic_fits_worker(const NumericVector &x, const NumericVector &y,
const NumericVector &w,
const int i, const int j) {
const int vlen = x.length();
if((i<0) || (j>=vlen) || (vlen!=y.length()) || (vlen!=w.length())) {
throw std::range_error("Inadmissible value");
}
const int n = j-i+1;
// initialize return structure
Rcpp::NumericVector final_links(n);
for(int k=0; k<n; ++k) {
final_links(k) = 0.0;
}
// look for corner cases
if(n<=1) {
if(n==1) {
if(y(0)>0.5) {
final_links(0) = std::numeric_limits<double>::max();
} else {
final_links(0) = -std::numeric_limits<double>::max();
}
}
return final_links;
}
// look for more corner cases
const input_summary isum = input_summary(x, y, w, i, j, -1);
if(!isum.saw_data()) {
return final_links;
}
if(!isum.y_varies()) {
// y-pure constant
if(isum.saw_y_pos>=0.5) {
for(int k=0; k<n; ++k) {
final_links(k) = std::numeric_limits<double>::max();
}
} else {
for(int k=0; k<n; ++k) {
final_links(k) = -std::numeric_limits<double>::max();
}
}
return final_links;
}
// we now know y varies
if(!isum.x_varies()) {
const double lk = logit(isum.total_wy/isum.total_w);
for(int k=0; k<n; ++k) {
final_links(k) = lk;
}
return final_links;
}
// we now know x varies
if(isum.seperable()) {
// check for seperable data cases, x able to perfectly sort y
for(int k=0; k<n; ++k) {
if(y(i+k)>0.5) {
final_links(k) = std::numeric_limits<double>::max();
} else {
final_links(k) = -std::numeric_limits<double>::max();
}
}
return final_links;
}
// on to general case
const int nx = x.length();
Rcpp::NumericVector initial_link(nx);
for(int k = 0; k<nx; ++k) {
initial_link(i) = 0.0;
}
// solve whole system
Rcpp::NumericVector coefs = logistic_solve1_worker(x, y,
w,
initial_link,
i, j,
-1);
for(int k=i; k<=j; ++k) {
final_links(k-i) = coefs(0) + coefs(1)*x(k-i);
}
return final_links;
}
//' In sample logistic predictions (in link space).
//'
//' logistic regression predictions.
//' Zero indexed.
//'
//' @param x NumericVector, expanatory variable.
//' @param y NumericVector, 0/1 values to fit.
//' @param w NumericVector, weights (required, positive).
//' @param i integer, first index (inclusive).
//' @param j integer, last index (inclusive).
//' @return vector of predictions for interval.
//'
//' @keywords internal
//'
//' @examples
//'
//' set.seed(5)
//' d <- data.frame(x = rnorm(10))
//' d$y <- d$x + rnorm(nrow(d))>0
//' weights <- runif(nrow(d))
//' m <- glm(y~x, data = d, family = binomial, weights = weights)
//' d$pred1 <- predict(m, newdata = d, type = "link")
//' d$pred2 <- logistic_fits(d$x, d$y, weights, 0, nrow(d)-1)
//' d <- d[order(d$x), , drop = FALSE]
//' print(d)
//'
//' @export
// [[Rcpp::export]]
NumericVector logistic_fits(NumericVector x, NumericVector y,
NumericVector w,
const int i, const int j) {
return logistic_fits_worker(x, y,
w,
i, j);
}