diff options
Diffstat (limited to 'python/calibrate_swaption.py')
| -rw-r--r-- | python/calibrate_swaption.py | 30 |
1 files changed, 17 insertions, 13 deletions
diff --git a/python/calibrate_swaption.py b/python/calibrate_swaption.py index 7a9611f7..b091980b 100644 --- a/python/calibrate_swaption.py +++ b/python/calibrate_swaption.py @@ -3,9 +3,8 @@ import argparse from analytics import Index, Swaption import datetime from db import dbengine -from joblib import Parallel, delayed -from pickle import loads, dumps - +from functools import partial +from multiprocessing import Pool serenitas_engine = dbengine('serenitasdb') def get_data(index, series, date = datetime.date.min): @@ -40,14 +39,18 @@ def get_data_latest(): finally: return df -def calib(d, option, index_type, series): - option.strike = d['strike'] - option.ref = d['ref'] +def calib(option, index_type, series, d): + option.strike = d[2] + option.ref = d[0] r = [] for pv_type in ['pv', 'pv_black']: for option_type in ['pay', 'rec']: - mid = (d['{}_bid'.format(option_type)] + d['{}_offer'.format(option_type)])/2 * 1e-4 - option.option_type = 'payer' if option_type == 'pay' else 'receiver' + if option_type == "pay": + mid = (d[3] + d[4]) / 2 * 1e-4 + option.option_type = 'payer' + else: + mid = (d[5] + d[6]) / 2 * 1e-4 + option.option_type = 'receiver' try: setattr(option, pv_type, mid) except ValueError as e: @@ -55,7 +58,7 @@ def calib(d, option, index_type, series): print(e) else: r.append(option.sigma) - return [d['quotedate'], index_type, series, option.exercise_date, d['strike']] + r + return [d[1], index_type, series, option.exercise_date, d[2]] + 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". @@ -69,10 +72,11 @@ 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) - r = Parallel(n_jobs=nproc)( - delayed(calib, check_pickle=False)(d, option, index_type, series) for d in - v[['ref', 'quotedate', 'strike', 'pay_bid', 'pay_offer', 'rec_bid', 'rec_offer']]. - to_dict(orient = 'records')) + mycalib = partial(calib, option, index_type, series) + 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) if __name__ == "__main__": |
