aboutsummaryrefslogtreecommitdiffstats
path: root/python/calibrate_swaption.py
diff options
context:
space:
mode:
Diffstat (limited to 'python/calibrate_swaption.py')
-rw-r--r--python/calibrate_swaption.py16
1 files changed, 12 insertions, 4 deletions
diff --git a/python/calibrate_swaption.py b/python/calibrate_swaption.py
index 0bcfb8f8..51e99a5a 100644
--- a/python/calibrate_swaption.py
+++ b/python/calibrate_swaption.py
@@ -3,9 +3,12 @@ import argparse
from analytics import Index, Swaption
import datetime
from db import dbengine
+from contextlib import contextmanager
+from itertools import starmap
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):
@@ -61,6 +64,10 @@ def calib(option, ref, strike, pay_bid, pay_offer, rec_bid, rec_offer):
r.append(option.sigma)
return [strike] + r
+@contextmanager
+def MaybePool(nproc):
+ yield Pool(nproc) if nproc > 1 else None
+
def calibrate(index_type=None, series=None, date=None, nproc=4, latest=False):
sql_str = ("INSERT INTO swaption_calib VALUES({}) ON CONFLICT DO NOTHING".
format(",".join(["%s"] * 9)))
@@ -69,16 +76,17 @@ def calibrate(index_type=None, series=None, date=None, nproc=4, latest=False):
else:
data = get_data(index_type, series, date)
- with Pool(nproc) as pool:
+ with MaybePool(nproc) as pool:
+ pstarmap = pool.starmap if pool else starmap
for k, v in data.groupby([data['quotedate'].dt.date, 'index', 'series']):
trade_date, index_type, series = k
index = Index.from_name(index_type, series, "5yr", trade_date)
for expiry, df in v.groupby(['expiry']):
option = Swaption(index, expiry.date(), 100)
mycalib = partial(calib, option)
- r = pool.starmap(mycalib, df[['ref', 'strike', 'pay_bid',
- 'pay_offer', 'rec_bid', 'rec_offer']].
- itertuples(index=False, name=None))
+ r = pstarmap(mycalib, df[['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(df.quotedate.tolist(), r)]
serenitas_engine.execute(sql_str, to_insert)