File: cox.py

package info (click to toggle)
python-scipy 0.6.0-12
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 32,016 kB
  • ctags: 46,675
  • sloc: cpp: 124,854; ansic: 110,614; python: 108,664; fortran: 76,260; objc: 424; makefile: 384; sh: 10
file content (209 lines) | stat: -rw-r--r-- 6,651 bytes parent folder | download
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
import shutil
import tempfile

import numpy as N

from scipy.sandbox.models import survival, model

class discrete:

    """
    A simple little class for working with discrete random vectors.
    """

    def __init__(self, x, w=None):
        self.x = N.squeeze(x)
        if self.x.shape == ():
            self.x = N.array([self.x])
        self.n = self.x.shape[0]
        if w is None:
            w = N.ones(self.n, N.float64) 
        else:
            if w.shape[0] != self.n:
                raise ValueError, 'incompatible shape for weights w'
            if N.any(N.less(w, 0)):
                raise ValueError, 'weights should be non-negative'
        self.w = w / w.sum()

    def mean(self, f=None):
        if f is None:
            fx = self.x
        else:
            fx = f(self.x)
        return (fx * self.w).sum()

    def cov(self):
        mu = self.mean()
        dx = self.x - N.multiply.outer(mu, self.x.shape[1])
        return N.dot(dx, N.transpose(dx))

class observation(survival.right_censored):

    def __getitem__(self, item):
        if self.namespace is not None:
            return self.namespace[item]
        else:
            return getattr(self, item)

    def __init__(self, time, delta, namespace=None):
        self.namespace = namespace
        survival.right_censored.__init__(self, time, delta)

    def __call__(self, formula, time=None, **extra):
        return formula(namespace=self, time=time, **extra)

class coxph(model.likelihood_model):

    def __init__(self, subjects, formula, time_dependent=False):
        self.subjects, self.formula = subjects, formula
        self.time_dependent = time_dependent
        self.initialize(self.subjects)

    def initialize(self, subjects):

        self.failures = {}
        for i in range(len(subjects)):
            s = subjects[i]
            if s.delta:
                if not self.failures.has_key(s.time):
                    self.failures[s.time] = [i]
                else:
                    self.failures[s.time].append(i)
        
        self.failure_times = self.failures.keys()
        self.failure_times.sort()

    def cache(self):
        if self.time_dependent:
            self.cachedir = tempfile.mkdtemp()

        self.design = {}
        self.risk = {}
        first = True
        
        for t in self.failures.keys():
            if self.time_dependent:
                d = N.array([s(self.formula, time=t)
                             for s in self.subjects]).astype('<f8')
                dshape = d.shape
                dfile = file(tempfile.mkstemp(dir=self.cachedir)[1], 'w')
                d.tofile(dfile)
                dfile.close()
                del(d)
                self.design[t] = N.memmap(dfile.name,
                                          dtype=N.dtype('<f8'),
                                          shape=dshape)
            elif first:
                d = N.array([s(self.formula, time=t)
                             for s in self.subjects]).astype(N.float64)
                self.design[t] = d
            else:
                self.design[t] = d
            self.risk[t] = N.compress([s.atrisk(t) for s in self.subjects],
                                      N.arange(self.design[t].shape[0]),axis=-1)
    def __del__(self):
        shutil.rmtree(self.cachedir, ignore_errors=True)

    def logL(self, b, ties='breslow'):

        logL = 0
        for t in self.failures.keys():
            fail = self.failures[t]
            d = len(fail)
            risk = self.risk[t]
            Zb = N.dot(self.design[t], b)

            logL += Zb[fail].sum()

            if ties == 'breslow':
                s = N.exp(Zb[risk]).sum()
                logL -= N.log(N.exp(Zb[risk]).sum()) * d
            elif ties == 'efron':
                s = N.exp(Zb[risk]).sum()
                r = N.exp(Zb[fail]).sum()
                for j in range(d):
                    logL -= N.log(s - j * r / d)
            elif ties == 'cox':
                raise NotImplementedError, 'Cox tie breaking method not implemented'
            else:
                raise NotImplementedError, 'tie breaking method not recognized'
        return logL

    def score(self, b, ties='breslow'):

        score = 0
        for t in self.failures.keys():
            fail = self.failures[t]
            d = len(fail)
            risk = self.risk[t]
            Z = self.design[t]

            score += Z[fail].sum()

            if ties == 'breslow':
                w = N.exp(N.dot(Z, b))
                rv = discrete(Z[risk], w=w[risk])
                score -= rv.mean() * d
            elif ties == 'efron':
                w = N.exp(N.dot(Z, b))
                score += Z[fail].sum()
                for j in range(d):
                    efron_w = w
                    efron_w[fail] -= i * w[fail] / d
                    rv = discrete(Z[risk], w=efron_w[risk])
                    score -= rv.mean()
            elif ties == 'cox':
                raise NotImplementedError, 'Cox tie breaking method not implemented'
            else:
                raise NotImplementedError, 'tie breaking method not recognized'
        return N.array([score])

    def information(self, b, ties='breslow'):

        info = 0
        score = 0
        for t in self.failures.keys():
            fail = self.failures[t]
            d = len(fail)
            risk = self.risk[t]
            Z = self.design[t]

            if ties == 'breslow':
                w = N.exp(N.dot(Z, b))
                rv = discrete(Z[risk], w=w[risk])
                info += rv.cov()
            elif ties == 'efron':
                w = N.exp(N.dot(Z, b))
                score += Z[fail].sum()
                for j in range(d):
                    efron_w = w
                    efron_w[fail] -= i * w[fail] / d
                    rv = discrete(Z[risk], w=efron_w[risk])
                    info += rv.cov()
            elif ties == 'cox':
                raise NotImplementedError, 'Cox tie breaking method not implemented'
            else:
                raise NotImplementedError, 'tie breaking method not recognized'
        return score

if __name__ == '__main__':
    import numpy.random as R
    n = 100
    X = N.array([0]*n + [1]*n)
    b = 0.4
    lin = 1 + b*X
    Y = R.standard_exponential((2*n,)) / lin
    delta = R.binomial(1, 0.9, size=(2*n,))

    subjects = [observation(Y[i], delta[i]) for i in range(2*n)]
    for i in range(2*n):
        subjects[i].X = X[i]

    import scipy.sandbox.models.formula as F
    x = F.quantitative('X')
    f = F.formula(x)

    c = coxph(subjects, f)

    c.cache()
#    c.newton([0.4])