aboutsummaryrefslogtreecommitdiffstats
path: root/python
diff options
context:
space:
mode:
Diffstat (limited to 'python')
-rw-r--r--python/calibrate_swaption.py24
1 files changed, 13 insertions, 11 deletions
diff --git a/python/calibrate_swaption.py b/python/calibrate_swaption.py
index b091980b..c768ea8d 100644
--- a/python/calibrate_swaption.py
+++ b/python/calibrate_swaption.py
@@ -5,6 +5,7 @@ import datetime
from db import dbengine
from functools import partial
from multiprocessing import Pool
+from itertools import repeat
serenitas_engine = dbengine('serenitasdb')
def get_data(index, series, date = datetime.date.min):
@@ -39,17 +40,17 @@ def get_data_latest():
finally:
return df
-def calib(option, index_type, series, d):
- option.strike = d[2]
- option.ref = d[0]
+def calib(option, ref, strike, pay_bid, pay_offer, rec_bid, rec_offer):
+ option.ref = ref
+ option.strike = strike
r = []
for pv_type in ['pv', 'pv_black']:
for option_type in ['pay', 'rec']:
if option_type == "pay":
- mid = (d[3] + d[4]) / 2 * 1e-4
+ mid = (pay_bid + pay_offer) / 2 * 1e-4
option.option_type = 'payer'
else:
- mid = (d[5] + d[6]) / 2 * 1e-4
+ mid = (rec_bid + rec_offer) / 2 * 1e-4
option.option_type = 'receiver'
try:
setattr(option, pv_type, mid)
@@ -58,7 +59,7 @@ def calib(option, index_type, series, d):
print(e)
else:
r.append(option.sigma)
- return [d[1], index_type, series, option.exercise_date, d[2]] + r
+ return [strike] + r
def calibrate(index_type=None, series=None, date=None, nproc=4, latest=False):
sql_str = ("INSERT INTO swaption_calib VALUES({}) ON CONFLICT DO NOTHING".
@@ -72,12 +73,13 @@ def calibrate(index_type=None, series=None, date=None, nproc=4, latest=False):
trade_date, expiry, index_type, series = k
index = Index.from_name(index_type, series, "5yr", trade_date)
option = Swaption(index, expiry.date(), 100)
- mycalib = partial(calib, option, index_type, series)
+ mycalib = partial(calib, option)
with Pool(nproc) as p:
- r = p.map(mycalib, v[['ref', 'quotedate', 'strike', 'pay_bid',
- 'pay_offer', 'rec_bid', 'rec_offer']].
- itertuples(index=False, name=None))
- serenitas_engine.execute(sql_str, r)
+ r = p.starmap(mycalib, v[['ref', 'strike', 'pay_bid',
+ 'pay_offer', 'rec_bid', 'rec_offer']].
+ itertuples(index=False, name=None))
+ to_insert = [[a, index_type, series, expiry] + b for a, b in zip(v.quotedate.tolist(), r)]
+ serenitas_engine.execute(sql_str, to_insert)
if __name__ == "__main__":
parser = argparse.ArgumentParser()