diff options
| -rw-r--r-- | python/test_cds.py | 42 | ||||
| -rw-r--r-- | python/test_option.py | 77 | ||||
| -rw-r--r-- | python/tests/test_cds.py | 2 | ||||
| -rw-r--r-- | python/tests/test_swaption.py | 5 |
4 files changed, 4 insertions, 122 deletions
diff --git a/python/test_cds.py b/python/test_cds.py deleted file mode 100644 index 2d4ef978..00000000 --- a/python/test_cds.py +++ /dev/null @@ -1,42 +0,0 @@ -""" Unittests for the CDS related classes. """ -from quantlib.settings import Settings -from quantlib.quotes import SimpleQuote -from quantlib.termstructures.yields.api import FlatForward -from quantlib.termstructures.credit.api import SpreadCdsHelper, UpfrontCdsHelper, PiecewiseDefaultCurve -from quantlib.time.api import (UnitedStates, Date, Actual365Fixed, Following, Quarterly, - TwentiethIMM, March, Period) -from yieldcurve import YC -import pdb - -def create_helper(): - - calendar = UnitedStates() - - todays_date = Date(31, March, 2014) - - Settings.instance().evaluation_date = todays_date - recovery_rate = 0.4 - upfronts = [0.01, 0.02, 0.04, 0.06] - - - tenors = [Period(str(i) +"Yr") for i in [2, 3, 5, 7]] - tenors = Period("2Yr") - - ts_curve = YC() - - helpers = UpfrontCdsHelper(0.05, 0.05, tenors, 0, calendar, Quarterly, - Following, TwentiethIMM, Actual365Fixed(), - recovery_rate, ts_curve) - pdb.set_trace() - #trait = ['HazardRate', 'DefaultDensity', 'SurvivalProbability']: - #interpolator = ['Linear', 'LogLinear', 'BackwardFlat']: - curve = PiecewiseDefaultCurve('HazardRate','BackwardFlat', - reference_date=todays_date, - helpers=[helpers], daycounter=Actual365Fixed()) - print(curve.survival_probability(Date(10, 3, 2015))) - return curve - -if __name__=="__main__": - curve = create_helper() - prob = curve.survival_probability(Date(10, 3, 2017)) - pdb.set_trace() diff --git a/python/test_option.py b/python/test_option.py deleted file mode 100644 index daa52d81..00000000 --- a/python/test_option.py +++ /dev/null @@ -1,77 +0,0 @@ -from pyisda.utils import build_yc - -from quantlib.settings import Settings -from quantlib.time.api import ( - Date, today, Period, Years, WeekendsOnly, Quarterly, CDS, Actual360, - ModifiedFollowing, Actual365Fixed, Months) -from quantlib.quotes import SimpleQuote -from quantlib.termstructures.credit.api import ( - SpreadCdsHelper, Interpolator, ProbabilityTrait, PiecewiseDefaultCurve ) -from quantlib.termstructures.yields.api import YieldTermStructure, DepositRateHelper -from quantlib.pricingengines.credit.isda_cds_engine import IsdaCdsEngine -import datetime -import math -import numpy as np -import pandas as pd -from pandas.tseries.offsets import BusinessDay as BDay -from db import dbengine -from yieldcurve import YC -from swaption import Index - -def get_spread(index, series): - engine = dbengine("serenitasdb") - df = pd.read_sql("SELECT date, tenor, closespread FROM index_quotes WHERE " \ - "index = %(index)s and series = %(series)s", - engine, - params = {"index": index, "series": series}, - parse_dates=['date'], - index_col = ['date', 'tenor']) - df = df.unstack('tenor') - df.columns = ['spread_' + c for c in df.columns.levels[1]] - return df[['spread_3yr', 'spread_5yr', 'spread_7yr', 'spread_10yr']] - -def test_sc(df): - settings = Settings() - settings.evaluation_date = today() - tenors = [Period(3, Years), Period(5, Years), Period(7, Years), Period(10, Years)] - quotes = [] - for i in range(4): - quotes.append(SimpleQuote()) - yts = YieldTermStructure() - helpers = [SpreadCdsHelper(q, t, 1, WeekendsOnly(), Quarterly, - ModifiedFollowing, CDS, Actual360(), 0.4, - discount_curve = yts, - lastperiod = Actual360(True)) - for q, t in zip(quotes, tenors)] - pdc = PiecewiseDefaultCurve(ProbabilityTrait.SurvivalProbability, - Interpolator.LogLinear, - 0, WeekendsOnly(), helpers, Actual365Fixed()) - ts = YC() - yts.link_to(ts) - for r in df.itertuples(index=True, name = None): - settings.evaluation_date = Date.from_datetime(r[0]) - ts = YC() - yts.link_to(ts) - for i in range(4): - quotes[i].value = r[i+1] *1e-4 - print(pdc.dates) - print(pdc.data) - - -if __name__ == "__main__": - settings = Settings() - trade_date = datetime.date.today() - settings.evaluation_date = today() - start_date = datetime.date(2016, 6, 20) - term_date = datetime.date(2021, 6, 20) - recovery = 0.4 - fixed_rate = 0.01 - ig26 = Index(start_date, term_date, recovery, fixed_rate) - yc = build_yc(trade_date, True) - exercise_date = datetime.date(2016, 10, 19) - exercise_date_settle = pd.Timestamp(exercise_date + 3* BDay()).date() - spread = np.array([65, 70, 72.5, 75, 77.5, 80, 85, 90, 95, 100, 110, 120, 130, 140]) - tailprob = np.array([64.2, 51.7, 45.8, 40.2, 35, 30.4, 23.2, 18, 14.3, 11.6, 7.9, 5.6, 4.1, 3]) - cumprob = 1-tailprob/100 - hist = cumprob/spread - hist = np.hstack((0, hist)) diff --git a/python/tests/test_cds.py b/python/tests/test_cds.py index 28c22cc2..26ec809e 100644 --- a/python/tests/test_cds.py +++ b/python/tests/test_cds.py @@ -6,7 +6,7 @@ from quantlib.time.api import Date import sys sys.path.append('..') -from swaption import Index +from analytics import Index from yieldcurve import YC, ql_to_jp class TestUpfront(unittest.TestCase): diff --git a/python/tests/test_swaption.py b/python/tests/test_swaption.py index 1ff2f5ec..10c07249 100644 --- a/python/tests/test_swaption.py +++ b/python/tests/test_swaption.py @@ -7,7 +7,8 @@ import numpy as np import sys sys.path.append('..') -from swaption import Index, g +from analytics import Index +from analytics.option import g class TestStrike(unittest.TestCase): index = Index.from_name("ig", 26, "5yr", @@ -19,7 +20,7 @@ class TestStrike(unittest.TestCase): def test_strike(self): """ check if strike price is the same as computing the pv with the expected forward yield curve.""" - strike = g(self.index, self.index.spread, self.exercise_date) + strike = g(self.index, self.index.spread, self.exercise_date, False) * self.index.notional old_yc = self.index._yc self.index.trade_date = self.exercise_date self.index._yc = old_yc.expected_forward_curve(self.exercise_date) |
