-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathlr.h
366 lines (305 loc) · 10.1 KB
/
lr.h
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
#include <bits/stdc++.h>
#include "util.hpp"
#include "matrix_methods.h"
#define D_matrix std::vector<vector<double> >
using namespace std;
bool debug = true;
std::ostream & operator<<(std::ostream &stream,std::vector<double> v)
{
for(int l = 0;l<v.size();l++)
{
stream<<v[l]<<' ';
}
return stream;
}
double sigmoid(double x) {
double e = 2.718281828;
return 1.0 / (1.0 + pow(e, -x));
}
D_matrix _multiply(D_matrix &m1, D_matrix &m2){
D_matrix ans;
for(int i = 0; i<m1.size(); ++i){
std::vector<double>line(m2[0].size(),0);
ans.push_back(line);
}
if(m1[0].size()!=m2.size()){
cout<<"cannot _multiply\n";
return ans;
}
for(size_t i = 0; i<m1.size(); ++i){
for(size_t j = 0; j<m2[0].size(); ++j){
for(size_t k = 0; k<m1[0].size(); ++k){
ans[i][j] = ans[i][j] + m1[i][k]*m2[k][j];
}
}
}
return ans;
}
double linear_predictor(std::vector<double>&model, std::vector<double>&data){
double s = 0.0;
for(int i = 0; i<model.size(); ++i)s = s + model[i]*data[i];
return s;
}
double predict(std::vector<double>&model, std::vector<double>&data){
double s = 0.0;
for(int i = 0; i<model.size(); ++i)s = s + model[i]*data[i];
return sigmoid(s);
}
D_matrix transpose(D_matrix &mm){
D_matrix m(mm[0].size(),std::vector<double>(mm.size(),-1));
for(int i = 0;i<mm.size(); ++i){
for(int j = 0;j<mm[0].size();++j){
m[j][i] = mm[i][j];
}
}
return m;
}
void printD(D_matrix mm){
for(size_t i = 0; i<mm.size(); ++i){
for(size_t j = 0; j<mm[0].size(); ++j){
cout<<mm[i][j]<<" ";
}
cout<<"\n";
}
return;
}
std::vector<double> glm_newton_raphson(std::vector<std::vector<double> >& x, std::vector<double>& y, double _gamma, int mx_iters, bool &is_singularity_error, bool &is_nan_error, double &ret_error, int &exit_iteration_no) {
srand(time(0)); double epsilon = 1e-6;
double gamma = _gamma;
double error = 0.0;
int max_iters = mx_iters;
int iter = 0;
std::vector<double> weight_old(x[0].size());
for (size_t i=0; i<weight_old.size(); ++i) {
double mxx = -10000000000.0;
for(size_t j = 0; j<x.size(); ++j){
mxx = max(mxx,x[j][i]);
}
weight_old[i] = 1.0/mxx;
}
double prev_error = 1e18;
D_matrix A = x; D_matrix A_T = transpose(A);
D_matrix A_minus_y(A.size(),std::vector<double>(1,0));
std::vector<double>B_proxy(A.size(),0);
D_matrix A_T_B(A_T.size(),std::vector<double>(B_proxy.size(),0));
while(true){
double error = 0.0;
for(size_t i = 0; i<A.size(); ++i){
double z_i = 0.0;
for(size_t j = 0; j<A[0].size(); ++j){
z_i = z_i + A[i][j]*weight_old[j];
}
double alph_i = sigmoid(z_i);
error = error + (y[i]-alph_i)*(y[i]-alph_i);
B_proxy[i] = alph_i*(1.0-alph_i);
A_minus_y[i][0] = alph_i-y[i];
}
error /= x.size();
ret_error = error;
if (fabs(error-prev_error) < epsilon) {
break;
}
prev_error = error;
for(size_t i = 0; i<A_T_B.size(); ++i){
for(size_t j = 0; j<A_T_B[0].size(); ++j){
A_T_B[i][j] = A_T[i][j]*B_proxy[j];
}
}
D_matrix toinv = _multiply(A_T_B,A);
bool sing = false;
bool nan = false;
D_matrix hinv = inverse(toinv,(int)toinv.size(),sing,nan);
if(sing || nan){
is_singularity_error = sing;
is_nan_error = nan;
ret_error = prev_error;
return weight_old;
}
D_matrix gradient = _multiply(A_T,A_minus_y);
D_matrix grad_mul_hinv = _multiply(hinv,gradient);
for(size_t j = 0; j<weight_old.size(); ++j){
weight_old[j] = weight_old[j] - gamma*grad_mul_hinv[j][0];
}
iter += 1;
exit_iteration_no = iter;
if (iter >= max_iters) {
break;
}
prev_error = error;
ret_error = prev_error;
}
return weight_old;
}
/*
* Logistic regression model optimization using IRLS algorithm
* code is written following
* http://114.70.193.188/BML/slides_freda/lec7.pdf
* and
* https://github.com/SurajGupta/r-source/blob/master/src/library/stats/R/glm.R
* used function interface can be reduced to smaller interface
*/
std::vector<double> glm_irls(std::vector<std::vector<double> >& x, std::vector<double>& y, double _gamma, int mx_iters, bool &is_singularity_error, bool &is_nan_error, double &ret_error, int &exit_iteration_no) {
double epsilon = 1e-6;
int max_iters = mx_iters;
int iter = 0;
// initializing needed data structure early
// maybe helpful in reducing allocation time cost
// weight vector to return
std::vector<double> weight(x[0].size(),1);
// weight matrix of shape (feature count,1) considering offset coefficient as feauture also
// this one will be deduced and assigned at each iteration
D_matrix w(x[0].size(),std::vector<double>(1,1));
// to store the good observation/sample from given x at each iteration
D_matrix X(x.size(),std::vector<double>(x[0].size(),0));
// to store the linear predictor \sum{x*w} at each iteration
D_matrix eta(x.size(),std::vector<double>(1,0));
// to store sigmoid(eta) at each iteration
D_matrix mu(x.size(),std::vector<double>(1,0));
// to store mu*(1-mu) of the good observation at each iteration
// it was supposed to be diagonal matrix of (# of good observation, # of good observation)
// made as (#of input observation, 1) to store only the diagonal element
D_matrix S(x.size(),std::vector<double>(1,0));
// to store fisher score(!)
// z = eta + (y-mu)/(mu*(1-mu) + 1e-305) of the good observations at each iteration
D_matrix z(x.size(),std::vector<double>(1,0));
// to store S*X, resultant shape (# of good observation, # of feature)
// considering offset coefficient as a feature also
D_matrix S_X(x.size(),std::vector<double>(x[0].size(),0));
// to store S*X, resultant shape (# of good observation, 1) at each iteration
D_matrix S_z(x.size(),std::vector<double>(1,0));
// in the iteration loop, weight will be deduced at end
// loop will begin with assumption of mu and eta
// this initialization is done following R function of binomial$initialize
// can be seen using following R code
// > f = binomial()
// > f = binomial(logit)
// > f$initialize
// basically mu is 1/4 if y is 0 else 3/2
for(size_t i=0;i<mu.size();i++)
{
mu[i][0] = (y[i] + 0.5)/2;
eta[i][0] = log(mu[i][0]/(1-mu[i][0]));
}
// initial value of square error which will be returned
double prev_error = 1e18;
// this loop may be broken by four condition
// iteration > max_iteration
// change in error < epsilon(1e-6)
// matrix inversion is not possible
// no good observation is not found
while(true){
// clearing vectors for new iteration
X.clear();
S.clear();
S_X.clear();
z.clear();
S_z.clear();
double error = 0.0;
// good observation means observation with mu*(1-mu) or variance(!) > 0
// 1e-305 is taken as epsilon
int num_of_good_observations = 0;
for(size_t i = 0; i<x.size(); ++i){
double g_i = mu[i][0]*(1.0-mu[i][0]);
// taking only the good observations
if (g_i > 1e-305) {
num_of_good_observations++;
X.push_back(x[i]);
z.push_back(std::vector<double>(1, eta[i][0] + (y[i]-mu[i][0])/(g_i+1e-305)));
S.push_back(std::vector<double>(1,g_i));
}
error = error + (y[i]-mu[i][0])*(y[i]-mu[i][0]);
}
// if no good observation is found model fitting is exited
// following https://github.com/SurajGupta/r-source/blob/a28e609e72ed7c47f6ddfbb86c85279a0750f0b7/src/library/stats/R/glm.R#L231
if (num_of_good_observations == 0) {
std::cerr << "No good observation is found" << std::endl;
// weight got at previous iteration which is deduced from some good observation will be returned
// this may be wrong because weight which produce no good observation is suspicious
break;
}
// checking average error
// only normal way of model fitting finishing
error /= x.size();
ret_error = error;
if (fabs(error-prev_error) < epsilon) {
break;
}
prev_error = error;
// transpose for calculating hessian = X_T * S * X
D_matrix X_T = transpose(X);
// using simple loop instead of _multiply because S is supposed to be a diagonal matrix
// reducing number of multiplication and needed memory
for(size_t i=0;i<S.size();i++)
{
S_X.push_back(std::vector<double>(X[0].size(),0));
for(size_t j=0;j<X[0].size();j++)
{
S_X[i][j] = S[i][0]*X[i][j];
}
}
D_matrix hessian = _multiply(X_T, S_X);
// to extract information that if hessian inversing cannot be done
bool sing = false;
bool nan = false;
D_matrix hessian_inv = inverse(hessian,(int)hessian.size(),sing,nan);
if(sing || nan){
is_singularity_error = sing;
is_nan_error = nan;
ret_error = prev_error;
break;
}
// to calculate X_T * S * z
// using simple loop instead of _multiply because S is supposed to be a diagonal matrix
// reducing number of multiplication
for(size_t i=0;i<z.size();i++)
{
S_z.push_back(std::vector<double>(1,S[i][0]*z[i][0]));
}
D_matrix X_T_S_z = _multiply(X_T,S_z);
// according to http://114.70.193.188/BML/slides_freda/lec7.pdf
// updated w = (X_T * S * X)^-1 * X_T * S * z
w = _multiply(hessian_inv,X_T_S_z);
iter += 1;
exit_iteration_no = iter;
if (iter >= max_iters) {
break;
}
prev_error = error;
ret_error = prev_error;
// assigning newly got w to variable which will be returned
for(size_t i=0;i<weight.size();i++)
{
weight[i]=w[i][0];
}
// calculating eta and mu for next iteration using updated w
for(size_t i = 0; i<x.size(); ++i){
eta[i][0] = 0;
for(size_t j = 0; j<x[0].size(); ++j){
eta[i][0] += x[i][j]*w[j][0];
}
mu[i][0] = sigmoid(eta[i][0]);
}
}
return weight;
}
std::vector<double> linear_regression(std::vector<std::vector<double> >& x, std::vector<double>& y) {
std::vector< double > weight(x[0].size());
for(int i = 0; i<(int)weight.size(); ++i){
weight[i] = 0.0;
}
D_matrix X = x; D_matrix X_T = transpose(X);
D_matrix _y = from_vector_to_D(y);
D_matrix A = _multiply(X_T,X);
bool sing = false;
bool nan = false;
D_matrix A_inv = inverse(A,(int)A.size(),sing,nan);
if(sing){
return weight;
}
D_matrix B = _multiply(A_inv,X_T);
D_matrix W = _multiply(B,_y);
for(int i = 0; i<(int)W.size(); ++i){
weight[i] = W[i][0];
}
return weight;
}