-
Notifications
You must be signed in to change notification settings - Fork 131
/
equation.py
239 lines (198 loc) · 9.78 KB
/
equation.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
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
import numpy as np
import tensorflow as tf
class Equation(object):
"""Base class for defining PDE related function."""
def __init__(self, eqn_config):
self.dim = eqn_config.dim
self.total_time = eqn_config.total_time
self.num_time_interval = eqn_config.num_time_interval
self.delta_t = self.total_time / self.num_time_interval
self.sqrt_delta_t = np.sqrt(self.delta_t)
self.y_init = None
def sample(self, num_sample):
"""Sample forward SDE."""
raise NotImplementedError
def f_tf(self, t, x, y, z):
"""Generator function in the PDE."""
raise NotImplementedError
def g_tf(self, t, x):
"""Terminal condition of the PDE."""
raise NotImplementedError
class HJBLQ(Equation):
"""HJB equation in PNAS paper doi.org/10.1073/pnas.1718942115"""
def __init__(self, eqn_config):
super(HJBLQ, self).__init__(eqn_config)
self.x_init = np.zeros(self.dim)
self.sigma = np.sqrt(2.0)
self.lambd = 1.0
def sample(self, num_sample):
dw_sample = np.random.normal(size=[num_sample, self.dim, self.num_time_interval]) * self.sqrt_delta_t
x_sample = np.zeros([num_sample, self.dim, self.num_time_interval + 1])
x_sample[:, :, 0] = np.ones([num_sample, self.dim]) * self.x_init
for i in range(self.num_time_interval):
x_sample[:, :, i + 1] = x_sample[:, :, i] + self.sigma * dw_sample[:, :, i]
return dw_sample, x_sample
def f_tf(self, t, x, y, z):
return -self.lambd * tf.reduce_sum(tf.square(z), 1, keepdims=True) / 2
def g_tf(self, t, x):
return tf.math.log((1 + tf.reduce_sum(tf.square(x), 1, keepdims=True)) / 2)
class AllenCahn(Equation):
"""Allen-Cahn equation in PNAS paper doi.org/10.1073/pnas.1718942115"""
def __init__(self, eqn_config):
super(AllenCahn, self).__init__(eqn_config)
self.x_init = np.zeros(self.dim)
self.sigma = np.sqrt(2.0)
def sample(self, num_sample):
dw_sample = np.random.normal(size=[num_sample, self.dim, self.num_time_interval]) * self.sqrt_delta_t
x_sample = np.zeros([num_sample, self.dim, self.num_time_interval + 1])
x_sample[:, :, 0] = np.ones([num_sample, self.dim]) * self.x_init
for i in range(self.num_time_interval):
x_sample[:, :, i + 1] = x_sample[:, :, i] + self.sigma * dw_sample[:, :, i]
return dw_sample, x_sample
def f_tf(self, t, x, y, z):
return y - tf.pow(y, 3)
def g_tf(self, t, x):
return 0.5 / (1 + 0.2 * tf.reduce_sum(tf.square(x), 1, keepdims=True))
class PricingDefaultRisk(Equation):
"""
Nonlinear Black-Scholes equation with default risk in PNAS paper
doi.org/10.1073/pnas.1718942115
"""
def __init__(self, eqn_config):
super(PricingDefaultRisk, self).__init__(eqn_config)
self.x_init = np.ones(self.dim) * 100.0
self.sigma = 0.2
self.rate = 0.02 # interest rate R
self.delta = 2.0 / 3
self.gammah = 0.2
self.gammal = 0.02
self.mu_bar = 0.02
self.vh = 50.0
self.vl = 70.0
self.slope = (self.gammah - self.gammal) / (self.vh - self.vl)
def sample(self, num_sample):
dw_sample = np.random.normal(size=[num_sample, self.dim, self.num_time_interval]) * self.sqrt_delta_t
x_sample = np.zeros([num_sample, self.dim, self.num_time_interval + 1])
x_sample[:, :, 0] = np.ones([num_sample, self.dim]) * self.x_init
for i in range(self.num_time_interval):
x_sample[:, :, i + 1] = (1 + self.mu_bar * self.delta_t) * x_sample[:, :, i] + (
self.sigma * x_sample[:, :, i] * dw_sample[:, :, i])
return dw_sample, x_sample
def f_tf(self, t, x, y, z):
piecewise_linear = tf.nn.relu(
tf.nn.relu(y - self.vh) * self.slope + self.gammah - self.gammal) + self.gammal
return (-(1 - self.delta) * piecewise_linear - self.rate) * y
def g_tf(self, t, x):
return tf.reduce_min(x, 1, keepdims=True)
class PricingDiffRate(Equation):
"""
Nonlinear Black-Scholes equation with different interest rates for borrowing and lending
in Section 4.4 of Comm. Math. Stat. paper doi.org/10.1007/s40304-017-0117-6
"""
def __init__(self, eqn_config):
super(PricingDiffRate, self).__init__(eqn_config)
self.x_init = np.ones(self.dim) * 100
self.sigma = 0.2
self.mu_bar = 0.06
self.rl = 0.04
self.rb = 0.06
self.alpha = 1.0 / self.dim
def sample(self, num_sample):
dw_sample = np.random.normal(size=[num_sample, self.dim, self.num_time_interval]) * self.sqrt_delta_t
x_sample = np.zeros([num_sample, self.dim, self.num_time_interval + 1])
x_sample[:, :, 0] = np.ones([num_sample, self.dim]) * self.x_init
factor = np.exp((self.mu_bar-(self.sigma**2)/2)*self.delta_t)
for i in range(self.num_time_interval):
x_sample[:, :, i + 1] = (factor * np.exp(self.sigma * dw_sample[:, :, i])) * x_sample[:, :, i]
return dw_sample, x_sample
def f_tf(self, t, x, y, z):
temp = tf.reduce_sum(z, 1, keepdims=True) / self.sigma
return -self.rl * y - (self.mu_bar - self.rl) * temp + (
(self.rb - self.rl) * tf.maximum(temp - y, 0))
def g_tf(self, t, x):
temp = tf.reduce_max(x, 1, keepdims=True)
return tf.maximum(temp - 120, 0) - 2 * tf.maximum(temp - 150, 0)
class BurgersType(Equation):
"""
Multidimensional Burgers-type PDE in Section 4.5 of Comm. Math. Stat. paper
doi.org/10.1007/s40304-017-0117-6
"""
def __init__(self, eqn_config):
super(BurgersType, self).__init__(eqn_config)
self.x_init = np.zeros(self.dim)
self.y_init = 1 - 1.0 / (1 + np.exp(0 + np.sum(self.x_init) / self.dim))
self.sigma = self.dim + 0.0
def sample(self, num_sample):
dw_sample = np.random.normal(size=[num_sample, self.dim, self.num_time_interval]) * self.sqrt_delta_t
x_sample = np.zeros([num_sample, self.dim, self.num_time_interval + 1])
x_sample[:, :, 0] = np.ones([num_sample, self.dim]) * self.x_init
for i in range(self.num_time_interval):
x_sample[:, :, i + 1] = x_sample[:, :, i] + self.sigma * dw_sample[:, :, i]
return dw_sample, x_sample
def f_tf(self, t, x, y, z):
return (y - (2 + self.dim) / 2.0 / self.dim) * tf.reduce_sum(z, 1, keepdims=True)
def g_tf(self, t, x):
return 1 - 1.0 / (1 + tf.exp(t + tf.reduce_sum(x, 1, keepdims=True) / self.dim))
class QuadraticGradient(Equation):
"""
An example PDE with quadratically growing derivatives in Section 4.6 of Comm. Math. Stat. paper
doi.org/10.1007/s40304-017-0117-6
"""
def __init__(self, eqn_config):
super(QuadraticGradient, self).__init__(eqn_config)
self.alpha = 0.4
self.x_init = np.zeros(self.dim)
base = self.total_time + np.sum(np.square(self.x_init) / self.dim)
self.y_init = np.sin(np.power(base, self.alpha))
def sample(self, num_sample):
dw_sample = np.random.normal(size=[num_sample, self.dim, self.num_time_interval]) * self.sqrt_delta_t
x_sample = np.zeros([num_sample, self.dim, self.num_time_interval + 1])
x_sample[:, :, 0] = np.ones([num_sample, self.dim]) * self.x_init
for i in range(self.num_time_interval):
x_sample[:, :, i + 1] = x_sample[:, :, i] + dw_sample[:, :, i]
return dw_sample, x_sample
def f_tf(self, t, x, y, z):
x_square = tf.reduce_sum(tf.square(x), 1, keepdims=True)
base = self.total_time - t + x_square / self.dim
base_alpha = tf.pow(base, self.alpha)
derivative = self.alpha * tf.pow(base, self.alpha - 1) * tf.cos(base_alpha)
term1 = tf.reduce_sum(tf.square(z), 1, keepdims=True)
term2 = -4.0 * (derivative ** 2) * x_square / (self.dim ** 2)
term3 = derivative
term4 = -0.5 * (
2.0 * derivative + 4.0 / (self.dim ** 2) * x_square * self.alpha * (
(self.alpha - 1) * tf.pow(base, self.alpha - 2) * tf.cos(base_alpha) - (
self.alpha * tf.pow(base, 2 * self.alpha - 2) * tf.sin(base_alpha)
)
)
)
return term1 + term2 + term3 + term4
def g_tf(self, t, x):
return tf.sin(
tf.pow(tf.reduce_sum(tf.square(x), 1, keepdims=True) / self.dim, self.alpha))
class ReactionDiffusion(Equation):
"""
Time-dependent reaction-diffusion-type example PDE in Section 4.7 of Comm. Math. Stat. paper
doi.org/10.1007/s40304-017-0117-6
"""
def __init__(self, eqn_config):
super(ReactionDiffusion, self).__init__(eqn_config)
self._kappa = 0.6
self.lambd = 1 / np.sqrt(self.dim)
self.x_init = np.zeros(self.dim)
self.y_init = 1 + self._kappa + np.sin(self.lambd * np.sum(self.x_init)) * np.exp(
-self.lambd * self.lambd * self.dim * self.total_time / 2)
def sample(self, num_sample):
dw_sample = np.random.normal(size=[num_sample, self.dim, self.num_time_interval]) * self.sqrt_delta_t
x_sample = np.zeros([num_sample, self.dim, self.num_time_interval + 1])
x_sample[:, :, 0] = np.ones([num_sample, self.dim]) * self.x_init
for i in range(self.num_time_interval):
x_sample[:, :, i + 1] = x_sample[:, :, i] + dw_sample[:, :, i]
return dw_sample, x_sample
def f_tf(self, t, x, y, z):
exp_term = tf.exp((self.lambd ** 2) * self.dim * (t - self.total_time) / 2)
sin_term = tf.sin(self.lambd * tf.reduce_sum(x, 1, keepdims=True))
temp = y - self._kappa - 1 - sin_term * exp_term
return tf.minimum(tf.constant(1.0, dtype=tf.float64), tf.square(temp))
def g_tf(self, t, x):
return 1 + self._kappa + tf.sin(self.lambd * tf.reduce_sum(x, 1, keepdims=True))