-
Notifications
You must be signed in to change notification settings - Fork 2
/
DGP.py
146 lines (101 loc) · 4.53 KB
/
DGP.py
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
import torch
import matplotlib.pyplot as plt
#from lifelines import KaplanMeierFitter
from utils import *
#from Clayton import Clayton
def risk_1(x, coeff):
return torch.square(torch.matmul(x, coeff))/x.shape[1]
def risk_poly(x, coeff):
#return torch.matmul(x, torch.ones((10,)))/5
#return (torch.matmul(x,coeff)/3)**4
return 0.3*torch.matmul(torch.sigmoid(x), coeff)**2
#return torch.sin(torch.matmul(x, coeff))
#return (torch.matmul(x, coeff) + torch.matmul(x**2, coeff) + torch.matmul((x**3)+(x**4)+(x**5)+(x**6), coeff))*0.2
#return (torch.matmul(x, coeff) + torch.matmul(x**2, coeff) + torch.matmul((x**3)+(x**4), coeff))*0.1
#def risk_m(x, coeff):
# return torch.square(torch.matmul(x, coeff))
def risk_m(x, coeff):
return torch.square(torch.matmul(x, coeff))/10
def risk_2(x, coeff):
return 2.5*torch.sum(torch.sigmoid(x*coeff), dim=1)/x.shape[1]
class Exp_linear:
def __init__(self, bh, nf) -> None:
self.nf = nf
self.bh = torch.tensor([bh]).type(torch.float32)
self.coeff = torch.rand((nf,))
def hazard(self, t, x):
return self.bh * torch.exp(torch.matmul(x, self.coeff))
def cum_hazard(self, t, x):
return self.hazard(t, x) * t
def survival(self, t, x):
return torch.exp(-self.cum_hazard(t, x))
def CDF(self, t, x):
return 1.0 - self.survival(t, x)
def PDF(self, t, x):
return self.survival(t,x)*self.hazard(t,x)
def rvs(self, x, u):
return -LOG(u)/self.hazard(t=None, x=x)
class EXP_nonlinear:
def __init__(self, bh, nf, risk_function) -> None:
self.nf = nf
self.bh = torch.tensor([bh]).type(torch.float32)
self.coeff = torch.rand((nf,))
self.risk_function = risk_function
def hazard(self, t, x):
return self.bh * torch.exp(self.risk_function(x, self.coeff ))
def cum_hazard(self, t, x):
return self.hazard(t, x) * t
def survival(self, t, x):
return torch.exp(-self.cum_hazard(t, x))
def CDF(self, t, x):
return 1.0 - self.survival(t, x)
def PDF(self, t, x):
return self.survival(t,x)*self.hazard(t,x)
def rvs(self, x, u):
return -LOG(u)/self.hazard(t=None, x=x)
class Weibull_linear:
def __init__(self, nf, alpha, gamma, device):
#torch.manual_seed(0)
self.nf = nf
self.alpha = torch.tensor([alpha], device=device).type(torch.float32)
self.gamma = torch.tensor([gamma], device=device).type(torch.float32)
self.coeff = torch.rand((nf,), device=device).type(torch.float32)#.clamp(0.1,1.0)
def PDF(self ,t ,x):
return self.hazard(t, x) * self.survival(t,x)
def CDF(self ,t ,x):
return 1 - self.survival(t,x)
def survival(self ,t ,x):
return torch.exp(-self.cum_hazard(t,x))
def hazard(self, t, x):
return ((self.gamma/self.alpha)*((t/self.alpha)**(self.gamma-1))) * torch.exp(torch.matmul(x, self.coeff))
def cum_hazard(self, t, x):
return ((t/self.alpha)**self.gamma) * torch.exp(torch.matmul(x, self.coeff))
def rvs(self, x, u):
return ((-LOG(u)/torch.exp(torch.matmul(x, self.coeff)))**(1/self.gamma))*self.alpha
class Weibull_nonlinear:
#torch.manual_seed(0)
def __init__(self, nf, alpha, gamma, risk_function, device):
#torch.manual_seed(0)
self.nf = nf
self.alpha = torch.tensor([alpha],device=device).type(torch.float32)
self.gamma = torch.tensor([gamma], device=device).type(torch.float32)
self.coeff = torch.rand((nf,),device=device)
self.risk_function = risk_function
def PDF(self ,t ,x):
return self.hazard(t, x) * self.survival(t, x)
def CDF(self ,t ,x):
return 1 - self.survival(t, x)
def survival(self ,t ,x):
return torch.exp(-self.cum_hazard(t, x))
def hazard(self, t, x):
return ((self.gamma/self.alpha)*((t/self.alpha)**(self.gamma-1))) * torch.exp(self.risk_function(x,self.coeff))
def cum_hazard(self, t, x):
return ((t/self.alpha)**self.gamma) * torch.exp(self.risk_function(x,self.coeff))
def rvs(self, x, u):
return ((-LOG(u)/torch.exp(self.risk_function(x, self.coeff)))**(1/self.gamma))*self.alpha
if __name__ == "__main__":
dgp1 =Weibull_linear(2, 14, 3)
dgp1.coeff = torch.tensor([0.3990, 0.5167])
x = torch.rand((1000,2))
t = dgp1.rvs(x, torch.rand((1000,)))
print(torch.min(t), torch.max(t))