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.py31
1 files changed, 19 insertions, 12 deletions
diff --git a/python/calibrate_swaption.py b/python/calibrate_swaption.py
index 63a44213..ce145224 100644
--- a/python/calibrate_swaption.py
+++ b/python/calibrate_swaption.py
@@ -1,39 +1,43 @@
import pandas as pd
-from analytics import Index, Swaption
+from analytics import CreditIndex, 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):
- df = pd.read_sql_query("SELECT * from swaption_ref_quotes JOIN swaption_quotes " \
- "USING (ref_id) WHERE index=%s and series=%s " \
+ df = pd.read_sql_query("SELECT * from swaption_ref_quotes JOIN swaption_quotes "
+ "USING (ref_id) WHERE index=%s and series=%s "
"and quotedate >=%s ORDER BY quotedate",
serenitas_engine,
- params=(index, series, date), parse_dates=['quotedate', 'expiry'])
- df.loc[(df.quote_source == "GS") & (df['index'] =="HY"),
+ params=(index, series, date),
+ parse_dates=['quotedate', 'expiry'])
+ df.loc[(df.quote_source == "GS") & (df['index'] == "HY"),
["pay_bid", "pay_offer", "rec_bid", "rec_offer"]] *= 100
df.quotedate = df.quotedate.dt.tz_convert('America/New_York')
return df
+
def get_data_latest():
- df = pd.read_sql_query("SELECT quotedate, index, series, expiry, ref, quote_source, "
- "swaption_quotes.* FROM swaption_ref_quotes " \
- "JOIN swaption_quotes USING (ref_id) " \
- "LEFT JOIN swaption_calib USING (quote_id) " \
+ df = pd.read_sql_query("SELECT quotedate, index, series, expiry, ref, "
+ "quote_source, swaption_quotes.* "
+ "FROM swaption_ref_quotes "
+ "JOIN swaption_quotes USING (ref_id) "
+ "LEFT JOIN swaption_calib USING (quote_id) "
"WHERE swaption_calib.quote_id is NULL",
serenitas_engine,
parse_dates=['quotedate', 'expiry'])
df.loc[(df.quote_source == "GS") & (df['index'] == "HY"),
- ["pay_bid", "pay_offer", "rec_bid", "rec_offer"]] *=100
+ ["pay_bid", "pay_offer", "rec_bid", "rec_offer"]] *= 100
df.quotedate = df.quotedate.dt.tz_convert('America/New_York')
return df
+
def calib(option, ref, strike, pay_bid, pay_offer, rec_bid, rec_offer):
option.ref = ref
option.strike = strike
@@ -55,10 +59,12 @@ def calib(option, ref, strike, pay_bid, pay_offer, rec_bid, rec_offer):
r.append(option.sigma)
return 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"] * 5)))
@@ -71,7 +77,7 @@ def calibrate(index_type=None, series=None, date=None, nproc=4, latest=False):
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)
+ index = CreditIndex(index_type, series, "5yr", trade_date)
for expiry, df in v.groupby(['expiry']):
option = Swaption(index, expiry.date(), 100)
mycalib = partial(calib, option)
@@ -81,6 +87,7 @@ def calibrate(index_type=None, series=None, date=None, nproc=4, latest=False):
to_insert = [[a] + b for a, b in zip(df.quote_id, r)]
serenitas_engine.execute(sql_str, to_insert)
+
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser()