-
Notifications
You must be signed in to change notification settings - Fork 210
/
_coxph_loss.pyx
71 lines (59 loc) · 2.3 KB
/
_coxph_loss.pyx
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
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
cimport cython
from libc cimport math
import numpy as np
cimport numpy as cnp
cnp.import_array()
@cython.wraparound(False)
@cython.cdivision(True)
@cython.boundscheck(False)
def coxph_negative_gradient(cnp.npy_uint8[:] event,
cnp.npy_double[:] time,
cnp.npy_double[:] y_pred):
cdef cnp.npy_double s
cdef int i
cdef int j
cdef cnp.npy_intp n_samples = event.shape[0]
cdef cnp.ndarray[cnp.npy_double, ndim=1] gradient = cnp.PyArray_EMPTY(1, &n_samples, cnp.NPY_DOUBLE, 0)
cdef cnp.npy_double[:] exp_tsj = cnp.PyArray_ZEROS(1, &n_samples, cnp.NPY_DOUBLE, 0)
cdef cnp.npy_double[:] exp_pred = np.exp(y_pred)
with nogil:
for i in range(n_samples):
for j in range(n_samples):
if time[j] >= time[i]:
exp_tsj[i] += exp_pred[j]
for i in range(n_samples):
s = 0
for j in range(n_samples):
if event[j] and time[i] >= time[j]:
s += exp_pred[i] / exp_tsj[j]
gradient[i] = event[i] - s
return gradient
@cython.wraparound(False)
@cython.cdivision(True)
@cython.boundscheck(False)
def coxph_loss(cnp.npy_uint8[:] event,
cnp.npy_double[:] time,
cnp.npy_double[:] y_pred):
cdef cnp.npy_intp n_samples = event.shape[0]
cdef cnp.npy_double at_risk
cdef cnp.npy_double loss = 0
with nogil:
for i in range(n_samples):
at_risk = 0
for j in range(n_samples):
if time[j] >= time[i]:
at_risk += math.exp(y_pred[j])
loss += event[i] * (y_pred[i] - math.log(at_risk))
return - loss