diff options
| -rw-r--r-- | python/analytics/option.py | 25 | ||||
| -rw-r--r-- | python/calibrate_swaption.py | 23 |
2 files changed, 22 insertions, 26 deletions
diff --git a/python/analytics/option.py b/python/analytics/option.py index 23680b5b..e7c87fbb 100644 --- a/python/analytics/option.py +++ b/python/analytics/option.py @@ -12,11 +12,6 @@ from .utils import GHquad, build_table from .index import g, ForwardIndex, Index, engine from yieldcurve import roll_yc from pandas.tseries.offsets import BDay -try: - import cPickle as pickle -except ImportError: - import pickle -from pickle import dumps from functools import wraps from pyisda.curve import SpreadCurve @@ -28,7 +23,8 @@ from scipy.interpolate import SmoothBivariateSpline from matplotlib import cm from mpl_toolkits.mplot3d import Axes3D import matplotlib.pyplot as plt -from joblib import Parallel, delayed +from multiprocessing import Pool +from functools import partial from itertools import chain def calib(S0, fp, exercise_date, exercise_date_settle, @@ -381,7 +377,7 @@ class Swaption(BlackSwaption): def handle(x): self.sigma = x - return self.pv - val + return self._direction * (self.pv - val) eta = 1.1 a = self.sigma while True: @@ -483,15 +479,14 @@ class VolatilitySurface(ForwardIndex): quotes = quotes.assign(mid = quotes[['pay_bid','pay_offer']].mean(1) * 1e-4) else: quotes = quotes.assign(mid = quotes[['rec_bid','rec_offer']].mean(1) * 1e-4) - for expiry, df in quotes.groupby(['expiry']): - atm_strike = ATMstrike(self._index, expiry.date()) - option = swaption_class(self._index, expiry.date(), 100, option_type) + with Pool(4) as p: + for expiry, df in quotes.groupby(['expiry']): + atm_strike = ATMstrike(self._index, expiry.date()) + option = swaption_class(self._index, expiry.date(), 100, option_type) - T.append(option.T * np.ones(df.shape[0])) - moneyness.append(df.strike.values / atm_strike) - r.append(Parallel(n_jobs=4)( - delayed(compute_vol)(option, strike, mid) for strike, mid in - df[['strike', 'mid']].values)) + T.append(option.T * np.ones(df.shape[0])) + moneyness.append(df.strike.values / atm_strike) + r.append(p.starmap(partial(compute_vol, option), df[['strike', 'mid']].values)) r = np.fromiter(chain(*r), np.float, quotes.shape[0]) f = SmoothBivariateSpline(np.hstack(T), np.hstack(moneyness), r) self._surfaces[surface_id] = f diff --git a/python/calibrate_swaption.py b/python/calibrate_swaption.py index c768ea8d..eda9dcc4 100644 --- a/python/calibrate_swaption.py +++ b/python/calibrate_swaption.py @@ -69,17 +69,18 @@ def calibrate(index_type=None, series=None, date=None, nproc=4, latest=False): else: data = get_data(index_type, series, date) - for k, v in data.groupby([data['quotedate'].dt.date, 'expiry','index', 'series']): - 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) - with Pool(nproc) as p: - 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) + with Pool(nproc) as pool: + for k, v in data.groupby([data['quotedate'].dt.date, 'expiry','index', 'series']): + 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) + + r = pool.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() |
