Skip to content

Commit

Permalink
calibrated dnn to mkt prices, performance plots
Browse files Browse the repository at this point in the history
  • Loading branch information
frankieycy committed Aug 9, 2024
1 parent 5236b28 commit 7465d70
Show file tree
Hide file tree
Showing 8 changed files with 142 additions and 33 deletions.
98 changes: 65 additions & 33 deletions dnn_pricing/dnnMktCalibrate.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,52 +3,84 @@
from sklearn.metrics import mean_squared_error
from dnnOptionPricer import *

S0 = 431.3399963378906
r = 0.02672004830382966
q = 0.02
data_folder = "test-0718/"

dynamics = ["GBM", "HES", "MER"]
n_inputs = {"GBM": 5, "HES": 9, "MER": 8}
put_call = ["put", "call"]
model_dim = [120,120,120,120,120,1]
model_file = "model_500ep|120,120,120,120,120,1|l,l,l,l,l,i.pt"
option_data = "../bkts_data/option_chain_SPY_2021-07-16.csv"
args = {
"S0": 431.3399963378906,
"r": 0.02672004830382966,
"q": 0.02,
"M_range": [0.8, 1.2],
"dynamics": ["GBM", "HES", "MER"],
"n_inputs": {"GBM": 5, "HES": 9, "MER": 8},
"put_call": ["put", "call"],
"model_dim": [120,120,120,120,120,1],
"model_file": "model_500ep|120,120,120,120,120,1|l,l,l,l,l,i.pt",
"option_data": "../bkts_data/option_chain_SPY_2021-07-16.csv",
"init_vals": {"GBM": [0.2], "HES": [0.2,1,0.04,0.5,-0.5], "MER": [0.2,1,0.1,0.2]}
}

def main():
dyn = "HES"
pc = "put"
data = pd.read_csv(option_data)
output_cols = [
"Contract Name",
"Type",
"Put/Call",
"Strike",
"Maturity (Year)",
"Market Price",
"DNN Price (Nelder-Mead)",
"DNN Price (Powell)",
"DNN Price (COBYLA)",
"Implied Vol",
"Delta",
"Gamma",
"Vega",
"Rho",
"Theta"
]

def calibrate_dnn(dyn, pc):
print("calibrating %s %s" % (dyn, pc))
data = pd.read_csv(args["option_data"])
data = data[data["Put/Call"]==pc.capitalize()]
K = data["Strike"].values
M = args["S0"]/K
data = data[(M >= args["M_range"][0]) & (M <= args["M_range"][1])]
K = data["Strike"].values
T = data["Maturity (Year)"].values
V = data["Market Price"].values
n = len(data)
model_path = "result/%s trained networks/%s/%s" % (dyn, pc, model_file)
model = MLP(n_inputs[dyn], model_dim)
print("mkt data to calibrate:")
print(data.head(10))
MTtensor = torch.t(Tensor([args["S0"]/K,T]))
model_path = "result/%s trained networks/%s/%s" % (dyn, pc, args["model_file"])
model = MLP(args["n_inputs"][dyn], args["model_dim"])
model.load(model_path)
def dnn_pricer(sig,kappa,theta,zeta,rho):
inputs = torch.cat((torch.t(Tensor([S0/K,T])), Tensor([[r,q,sig,kappa,theta,zeta,rho]]*n)), dim=1)
def dnn_pricer(x):
inputs = torch.cat((MTtensor, Tensor([np.concatenate(([args["r"],args["q"]],x))]*n)), dim=1)
return K * model(inputs).detach().numpy().flatten()
def objective(x):
return np.sqrt(mean_squared_error(dnn_pricer(*x), V))
init_vals = [0.2,1,0.04,0.5,-0.5]
print("initial objective", objective(init_vals))
# bounds = [(0,2), (0,100), (0,4), (0,2), (-1,1)]
# opt_res = spo.minimize(objective, x0=init_vals, bounds=bounds, method="Newton-CG")
for m in ["Nelder-Mead","Powell","COBYLA"]:
print("using %s" % m)
return np.sqrt(mean_squared_error(dnn_pricer(x), V))
init_vals = args["init_vals"][dyn]
print("initial objective:", objective(init_vals))
opt_log = dict()
for m in ["Nelder-Mead", "Powell", "COBYLA"]:
print("using method %s" % m)
opt_res = spo.minimize(objective, x0=init_vals, method=m)
params = opt_res.x
print(params, objective(params))
print(dnn_pricer(*params), V)
data["DNN Price (%s)" % m] = dnn_pricer(params)
opt_log[m] = {
"opt_res": opt_res,
"params": params
}
print("params:", params)
print("objective:", objective(params))
data = data[output_cols]
data.to_csv(data_folder + "dnn_%s_%s.csv" % (dyn, pc), index=False)
print()

# for dyn in dynamics:
# for pc in put_call:
# print("calibrating %s %s" % (dyn, pc))
# model_path = "result/%s trained networks/%s/%s" % (dyn, pc, model_file)
# model = MLP(n_inputs[dyn], model_dim)
# model.load(model_path)
# pass
def main():
for dyn in args["dynamics"]:
for pc in args["put_call"]:
calibrate_dnn(dyn, pc)

if __name__=="__main__":
main()
77 changes: 77 additions & 0 deletions dnn_pricing/result/mkt calibration/dnnMktCalibrate.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
Gbm put calibration under different minimization methods

initial objective: 6.868051664134034
using method Nelder-Mead
params: [0.21546875]
objective: 6.731850438345304
using method Powell
params: [0.2154891]
objective: 6.731850399083168
using method COBYLA
params: [0.21558086]
objective: 6.731853992997851

Gbm call calibration under different minimization methods

initial objective: 5.072589837756422
using method Nelder-Mead
params: [0.16367188]
objective: 3.8627662241380367
using method Powell
params: [0.16366962]
objective: 3.862765750379424
using method COBYLA
params: [0.16356719]
objective: 3.8627765314537537

Merton put calibration under different minimization methods

initial objective: 10.36094942879816
using method Nelder-Mead
params: [0.17151546 1.12247462 0.10837124 0.05494519]
objective: 6.4849991226099135
using method Powell
params: [ 0.05557684 0.64628819 -0.03051313 0.30891579]
objective: 5.629234594537098
using method COBYLA
params: [ 0.05326636 0.65409727 -0.03171914 0.30828395]
objective: 5.629511560710035

Merton call calibration under different minimization methods

initial objective: 14.128172520193203
using method Nelder-Mead
params: [ 0.08454932 0.68137934 -0.14803701 0.13071758]
objective: 3.342730860007632
using method Powell
params: [ 0.06462324 0.33818109 -0.21205753 0.2500078 ]
objective: 3.5026590186496485
using method COBYLA
params: [ 0.07838252 0.90503712 -0.1276526 0.11451638]
objective: 3.3674879165319225

Heston put calibration under different minimization methods

initial objective: 7.527090924736272
using method Nelder-Mead
params: [ 0.14294561 1.84320996 0.07317877 -0.02692959 -0.40631107]
objective: 6.2212245738875955
using method Powell
params: [ 0.04538429 2.27430948 0.08126959 0.13789064 -0.91094551]
objective: 6.128816126889603
using method COBYLA
params: [ 0.02980197 2.18469218 0.08623377 0.26658284 -0.44829848]
objective: 6.213661333153228

Heston call calibration under different minimization methods

initial objective: 4.013464521974254
using method Nelder-Mead
params: [ 0.14836916 0.66304691 0.0680975 0.48761702 -0.65135713]
objective: 3.313023758828946
using method Powell
params: [ 0.15182468 1.12138606 0.04178745 0.20569562 -1.18580852]
objective: 3.209692306412288
using method COBYLA
params: [ 0.14916193 0.83663081 0.06301192 0.58304154 -0.54309511]
objective: 3.328468441305359
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 7465d70

Please sign in to comment.