forked from gbm-developers/gbm3
-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathcoxph.cpp
288 lines (250 loc) · 5.98 KB
/
coxph.cpp
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
// GBM by Greg Ridgeway Copyright (C) 2003
#include "coxph.h"
CCoxPH::CCoxPH()
{
}
CCoxPH::~CCoxPH()
{
}
GBMRESULT CCoxPH::ComputeWorkingResponse
(
double *adT,
double *adDelta,
double *adOffset,
double *adF,
double *adZ,
double *adWeight,
bool *afInBag,
unsigned long nTrain,
int cIdxOff
)
{
unsigned long i = 0;
double dF = 0.0;
double dTot = 0.0;
double dRiskTot = 0.0;
vecdRiskTot.resize(nTrain);
dRiskTot = 0.0;
for(i=0; i<nTrain; i++)
{
if(afInBag[i])
{
dF = adF[i] + ((adOffset==NULL) ? 0.0 : adOffset[i]);
dRiskTot += adWeight[i]*exp(dF);
vecdRiskTot[i] = dRiskTot;
}
}
dTot = 0.0;
for(i=nTrain-1; i!=ULONG_MAX; i--) // i is unsigned so wraps to ULONG_MAX
{
if(afInBag[i])
{
if(adDelta[i]==1.0)
{
dTot += adWeight[i]/vecdRiskTot[i];
}
dF = adF[i] + ((adOffset==NULL) ? 0.0 : adOffset[i]);
adZ[i] = adDelta[i] - exp(dF)*dTot;
}
}
return GBM_OK;
}
GBMRESULT CCoxPH::InitF
(
double *adY,
double *adMisc,
double *adOffset,
double *adWeight,
double &dInitF,
unsigned long cLength
)
{
dInitF = 0.0;
return GBM_OK;
}
double CCoxPH::Deviance
(
double *adT,
double *adDelta,
double *adOffset,
double *adWeight,
double *adF,
unsigned long cLength,
int cIdxOff
)
{
unsigned long i=0;
double dL = 0.0;
double dF = 0.0;
double dW = 0.0;
double dTotalAtRisk = 0.0;
dTotalAtRisk = 0.0;
for(i=cIdxOff; i<cLength+cIdxOff; i++)
{
dF = adF[i] + ((adOffset==NULL) ? 0.0 : adOffset[i]);
dTotalAtRisk += adWeight[i]*exp(dF);
if(adDelta[i]==1.0)
{
dL += adWeight[i]*(dF - log(dTotalAtRisk));
dW += adWeight[i];
}
}
return -2*dL/dW;
}
GBMRESULT CCoxPH::FitBestConstant
(
double *adT,
double *adDelta,
double *adOffset,
double *adW,
double *adF,
double *adZ,
const std::vector<unsigned long>& aiNodeAssign,
unsigned long nTrain,
VEC_P_NODETERMINAL vecpTermNodes,
unsigned long cTermNodes,
unsigned long cMinObsInNode,
bool *afInBag,
double *adFadj,
int cIdxOff
)
{
GBMRESULT hr = GBM_OK;
double dF = 0.0;
double dRiskTot = 0.0;
unsigned long i = 0;
unsigned long k = 0;
unsigned long m = 0;
double dTemp = 0.0;
bool fTemp = false;
unsigned long K = 0;
veciK2Node.resize(cTermNodes);
veciNode2K.resize(cTermNodes);
for(i=0; i<cTermNodes; i++)
{
veciNode2K[i] = 0;
if(vecpTermNodes[i]->cN >= cMinObsInNode)
{
veciK2Node[K] = i;
veciNode2K[i] = K;
K++;
}
}
vecdP.resize(K);
matH.setactualsize(K-1);
vecdG.resize(K-1);
vecdG.assign(K-1,0.0);
// zero the Hessian
for(k=0; k<K-1; k++)
{
for(m=0; m<K-1; m++)
{
matH.setvalue(k,m,0.0);
}
}
// get the gradient & Hessian, Ridgeway (1999) pp. 100-101
// correction from Ridgeway (1999): fix terminal node K-1 prediction to 0.0
// for identifiability
dRiskTot = 0.0;
vecdP.assign(K,0.0);
for(i=0; i<nTrain; i++)
{
if(afInBag[i] && (vecpTermNodes[aiNodeAssign[i]]->cN >= cMinObsInNode))
{
dF = adF[i] + ((adOffset==NULL) ? 0.0 : adOffset[i]);
vecdP[veciNode2K[aiNodeAssign[i]]] += adW[i]*exp(dF);
dRiskTot += adW[i]*exp(dF);
if(adDelta[i]==1.0)
{
// compute g and H
for(k=0; k<K-1; k++)
{
vecdG[k] +=
adW[i]*((aiNodeAssign[i]==veciK2Node[k]) - vecdP[k]/dRiskTot);
matH.getvalue(k,k,dTemp,fTemp);
matH.setvalue(k,k,dTemp -
adW[i]*vecdP[k]/dRiskTot*(1-vecdP[k]/dRiskTot));
for(m=0; m<k; m++)
{
matH.getvalue(k,m,dTemp,fTemp);
dTemp += adW[i]*vecdP[k]/dRiskTot*vecdP[m]/dRiskTot;
matH.setvalue(k,m,dTemp);
matH.setvalue(m,k,dTemp);
}
}
}
}
}
/*
for(k=0; k<K-1; k++)
{
for(m=0; m<K-1; m++)
{
matH.getvalue(k,m,dTemp,fTemp);
Rprintf("%f ",dTemp);
}
Rprintf("\n");
}
*/
// one step to get leaf predictions
matH.invert();
for(k=0; k<cTermNodes; k++)
{
vecpTermNodes[k]->dPrediction = 0.0;
}
for(m=0; m<K-1; m++)
{
for(k=0; k<K-1; k++)
{
matH.getvalue(k,m,dTemp,fTemp);
if(!R_FINITE(dTemp)) // occurs if matH was not invertible
{
vecpTermNodes[veciK2Node[k]]->dPrediction = 0.0;
break;
}
else
{
vecpTermNodes[veciK2Node[k]]->dPrediction -= dTemp*vecdG[m];
}
}
}
// vecpTermNodes[veciK2Node[K-1]]->dPrediction = 0.0; // already set to 0.0
return hr;
}
double CCoxPH::BagImprovement
(
double *adT,
double *adDelta,
double *adOffset,
double *adWeight,
double *adF,
double *adFadj,
bool *afInBag,
double dStepSize,
unsigned long nTrain
)
{
double dReturnValue = 0.0;
double dNum = 0.0;
double dDen = 0.0;
double dF = 0.0;
double dW = 0.0;
unsigned long i = 0;
dNum = 0.0;
dDen = 0.0;
for(i=0; i<nTrain; i++)
{
if(!afInBag[i])
{
dNum += adWeight[i]*exp(dF + dStepSize*adFadj[i]);
dDen += adWeight[i]*exp(dF);
if(adDelta[i]==1.0)
{
dReturnValue +=
adWeight[i]*(dStepSize*adFadj[i] - log(dNum) + log(dDen));
dW += adWeight[i];
}
}
}
return dReturnValue/dW;
}