-
Notifications
You must be signed in to change notification settings - Fork 3
/
ppde_BlackScholes_lookback.py
156 lines (130 loc) · 5.2 KB
/
ppde_BlackScholes_lookback.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
import torch
import torch.nn as nn
import numpy as np
import argparse
import tqdm
import os
import math
import matplotlib.pyplot as plt
from lib.bsde import PPDE_BlackScholes as PPDE
from lib.options import Lookback
def sample_x0(batch_size, dim, device):
sigma = 0.3
mu = 0.08
tau = 0.1
z = torch.randn(batch_size, dim, device=device)
x0 = torch.exp((mu-0.5*sigma**2)*tau + 0.3*math.sqrt(tau)*z) # lognormal
return x0
def write(msg, logfile, pbar):
pbar.write(msg)
with open(logfile, "a") as f:
f.write(msg)
f.write("\n")
def train(T,
n_steps,
d,
mu,
sigma,
depth,
rnn_hidden,
ffn_hidden,
max_updates,
batch_size,
lag,
base_dir,
device,
method
):
logfile = os.path.join(base_dir, "log.txt")
ts = torch.linspace(0,T,n_steps+1, device=device)
lookback = Lookback()
ppde = PPDE(d, mu, sigma, depth, rnn_hidden, ffn_hidden)
ppde.to(device)
optimizer = torch.optim.RMSprop(ppde.parameters(), lr=0.0005)
scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size=5000, gamma=0.2)
pbar = tqdm.tqdm(total=max_updates)
losses = []
for idx in range(max_updates):
optimizer.zero_grad()
x0 = sample_x0(batch_size, d, device)
if method=="bsde":
loss, _, _ = ppde.fbsdeint(ts=ts, x0=x0, option=lookback, lag=lag)
else:
loss, _, _ = ppde.conditional_expectation(ts=ts, x0=x0, option=lookback, lag=lag)
loss.backward()
optimizer.step()
scheduler.step()
losses.append(loss.detach().cpu().item())
# testing
if (idx+1) % 10 == 0:
with torch.no_grad():
x0 = torch.ones(5000,d,device=device) # we do monte carlo
loss, Y, payoff = ppde.fbsdeint(ts=ts,x0=x0,option=lookback,lag=lag)
payoff = torch.exp(-mu*ts[-1])*payoff.mean()
pbar.update(10)
write("loss={:.4f}, Monte Carlo price={:.4f}, predicted={:.4f}".format(loss.item(),payoff.item(), Y[0,0,0].item()),logfile,pbar)
result = {"state":ppde.state_dict(),
"loss":losses}
torch.save(result, os.path.join(base_dir, "result.pth.tar"))
# evaluation
x0 = torch.ones(1,d,device=device)#sample_x0(1, d, device)
with torch.no_grad():
x, _ = ppde.sdeint(ts=ts, x0=x0)
fig, ax = plt.subplots()
ax.plot(ts.cpu().numpy(), x[0,:,0].cpu().numpy())
ax.set_ylabel(r"$X(t)$")
fig.savefig(os.path.join(base_dir, "path_eval.pdf"))
pred, mc_pred = [], []
for idx, t in enumerate(ts[::lag]):
pred.append(ppde.eval(ts=ts, x=x[:,:(idx*lag)+1,:], lag=lag).detach())
mc_pred.append(ppde.eval_mc(ts=ts, x=x[:,:(idx*lag)+1,:], lag=lag, option=lookback, mc_samples=10000))
pred = torch.cat(pred, 0).view(-1).cpu().numpy()
mc_pred = torch.cat(mc_pred, 0).view(-1).cpu().numpy()
fig, ax = plt.subplots()
ax.plot(ts[::lag].cpu().numpy(), pred, '--', label="LSTM + BSDE + sign")
ax.plot(ts[::lag].cpu().numpy(), mc_pred, '-', label="MC")
ax.set_ylabel(r"$v(t,X_t)$")
ax.legend()
fig.savefig(os.path.join(base_dir, "BS_lookback_LSTM_sol.pdf"))
print("THE END")
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('--base_dir', default='./numerical_results/', type=str)
parser.add_argument('--device', default=0, type=int)
parser.add_argument('--use_cuda', action='store_true', default=True)
parser.add_argument('--seed', default=1, type=int)
parser.add_argument('--batch_size', default=500, type=int)
parser.add_argument('--d', default=4, type=int)
parser.add_argument('--max_updates', default=5000, type=int)
parser.add_argument('--ffn_hidden', default=[20,20], nargs="+", type=int)
parser.add_argument('--rnn_hidden', default=20, type=int)
parser.add_argument('--depth', default=3, type=int)
parser.add_argument('--T', default=1., type=float)
parser.add_argument('--n_steps', default=100, type=int, help="number of steps in time discrretisation")
parser.add_argument('--lag', default=10, type=int, help="lag in fine time discretisation to create coarse time discretisation")
parser.add_argument('--mu', default=0.05, type=float, help="risk free rate")
parser.add_argument('--sigma', default=0.3, type=float, help="risk free rate")
parser.add_argument('--method', default="bsde", type=str, help="learning method", choices=["bsde","orthogonal"])
args = parser.parse_args()
if torch.cuda.is_available() and args.use_cuda:
device = "cuda:{}".format(args.device)
else:
device="cpu"
results_path = os.path.join(args.base_dir, "BS", args.method)
if not os.path.exists(results_path):
os.makedirs(results_path)
train(T=args.T,
n_steps=args.n_steps,
d=args.d,
mu=args.mu,
sigma=args.sigma,
depth=args.depth,
rnn_hidden=args.rnn_hidden,
ffn_hidden=args.ffn_hidden,
max_updates=args.max_updates,
batch_size=args.batch_size,
lag=args.lag,
base_dir=results_path,
device=device,
method=args.method
)