-
Notifications
You must be signed in to change notification settings - Fork 7
/
runfile_localVol.py
133 lines (98 loc) · 4.84 KB
/
runfile_localVol.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# MIT License
# Copyright (c) 2020 Christa Cuchiero, Wahid Khosrawi, Josef Teichmann
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
"""runfile_localVol.py:
This file implements the the functionality for computing and plotting local-vol
implied volatilities for the sampled parameters given as input and options also
given as input. Starting point is the main function.
"""
import matplotlib.pyplot as plt
import numpy as np, tensorflow as tf
import os
from compute_pM import MC_pM_locvol
from finModels.helpers_finModels import fin_surface
import pickle
import helpers
# Env variables
HOSTNAME = os.uname()[1]
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
def makePrices(para_locVol, option, para_MC, just_ops = False):
'''Compute locVol prices for the given parameters. '''
version = para_locVol['version']
if not os.path.exists('caliRes/'+version):
os.mkdir('caliRes/'+version)
Batchsize_tf = tf.placeholder(dtype=tf.int32, shape=())
strikes_tf = [tf.placeholder(dtype=tf.float32, shape=(option.nK[i]) ) for i in range(option.nT) ]
pM_tf = MC_pM_locvol(para_locVol, option, para_MC, Batchsize_tf, strikes_tf)
if just_ops:
return(pM_tf, Batchsize_tf, strikes_tf)
N_mc_data = para_MC['N_mc_data']
N_mc_inner = para_MC['N_mc_inner']
helpers.init_folders_and_readme(mode='create_data', version=version)
print('I run this script on host: {}'.format(HOSTNAME), version)
# Check if Data version already exists
if os.path.isfile('caliRes/pM_'+version+'.pkl'):
if version != 'test':
raise Exception('Data file already exists. Give new name to store it.\n\n')
# Compute the number of MC rounds we need to get to Batchsize
N_MC_outer = int(np.trunc(N_mc_data/N_mc_inner))
D = dict(zip(strikes_tf, option.K) )
with tf.Session() as sess:
print('\nData computation progress:')
pM = sess.run(
pM_tf,
feed_dict={**D,Batchsize_tf: N_mc_inner}
)
prog = 100 /(N_MC_outer)
print('\rMC simulation {:7} | progress: {:10.4}% '.format(1,prog), end='')
for i in np.arange(2, N_MC_outer+1):
prog = 100*i /(N_MC_outer)
pM_aux = sess.run(pM_tf, feed_dict={**D,Batchsize_tf: N_mc_inner})
pM = [ pM[iteraux] + pM_aux[iteraux] for iteraux in range( len(pM) ) ]
print('\rMC simulation {:7} | progress: {:10.4}% '.format(i,prog), end='')
pM = [pM[iteraux] / N_MC_outer for iteraux in range( len(pM) ) ]
# Save the prices to disk for later calibration
with open('data/locVolPrices/pM_'+version+'.pkl', 'wb') as f:
pickle.dump(pM, f)
def plot_IV(para, option):
'''load computed prices and create fin_surf instance. Also do all needed conversions'''
log_m = option.log_m_list(para['S0'])
version = para['version']
with open('data/locVolPrices/pM_'+version+'.pkl', 'rb') as f:
pM = pickle.load(f)
# Constuct the object that handles conversion
data_handler = fin_surface(mats=option.T, strikes=option.K, spot=para['S0'], version = version)
data_handler.paralocVol = para
data_handler.feed_prices(prices= [p for p in pM] )
data_handler.convert(direction='price->iV')
for i in range(len(pM)):
plt.plot(log_m[i], data_handler.iV[i] )
if not os.path.exists('caliRes/'+version):
os.mkdir('caliRes/'+version)
plt.savefig('caliRes/'+version+'/plot_{}.png'.format(str(i+1).zfill(3)) )
plt.close()
return(data_handler)
def main(para_locVol, option, para_MC, compute_prices):
'''wrapper function. Depending on compute_prices data is computed or just loaded.'''
if compute_prices:
makePrices(para_locVol, option, para_MC)
finsurf = plot_IV(para_locVol, option)
return(finsurf)
if __name__ == "__main__":
pass