Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def df_to_zero_curve(rates, settlement_date, daycounter=Actual365Fixed()):
""" Converts a pandas data frame into a QL zero curve. """
dates = [pydate_to_qldate(dt) for dt in rates.index]
dates.insert(0, pydate_to_qldate(settlement_date))
# arbitrarily extend the curve a few years to provide flat
# extrapolation
dates.append(dates[-1] + 365 * 2)
values = rates.values.tolist()
values.insert(0, values[0])
values.append(values[-1])
return ZeroCurve(dates, values, daycounter)
offset = 366
todays_date = today() - offset
settlement_date = todays_date + 2
settings.evaluation_date = todays_date
# options parameters
option_type = Put
underlying = 36
strike = 40
dividend_yield = 0.00
risk_free_rate = 0.06
volatility = 0.20
maturity = settlement_date + 363
daycounter = Actual365Fixed()
underlyingH = SimpleQuote(underlying)
# bootstrap the yield/dividend/vol curves
flat_term_structure = FlatForward(
reference_date=settlement_date,
forward=risk_free_rate,
daycounter=daycounter
)
flat_dividend_ts = FlatForward(
reference_date=settlement_date,
forward=dividend_yield,
daycounter=daycounter
)
reference_date=settlement_date,
forward=0.06,
daycounter=Actual365Fixed()
)
# option parameters
exercise = AmericanExercise(
earliest_exercise_date=settlement_date,
latest_exercise_date=Date(17, May, 1999)
)
payoff = PlainVanillaPayoff(Put, 40.0)
# market data
underlying = SimpleQuote(36.0)
volatility = BlackConstantVol(todays_date, TARGET(), 0.20,
Actual365Fixed())
dividend_yield = FlatForward(
reference_date=settlement_date,
forward=0.00,
daycounter=Actual365Fixed()
)
# report
header = '%19s' % 'method' + ' |' + \
' |'.join(['%17s' % tag for tag in ['value',
'estimated error',
'actual error']])
print()
print(header)
print('-' * len(header))
refValue = None
def dfToZeroCurve(df_rates, dtSettlement, daycounter=Actual365Fixed()):
"""
Convert a panda data frame into a QL zero curve
"""
dates = [dateToQLDate(dt) for dt in df_rates.index]
dates.insert(0, dateToQLDate(dtSettlement))
dates.append(dates[-1] + 365 * 2)
vx = list(df_rates.values)
vx.insert(0, vx[0])
vx.append(vx[-1])
return ZeroCurve(dates, vx, daycounter)
)
cds_1y = CreditDefaultSwap(
SELLER, nominal, quoted_spreads[2], cds_schedule, Following,
Actual365Fixed()
)
cds_schedule = Schedule(
todays_date, maturities[3], Period(Quarterly), calendar,
termination_date_convention=Unadjusted,
date_generation_rule=TwentiethIMM
)
cds_2y = CreditDefaultSwap(
SELLER, nominal, quoted_spreads[3], cds_schedule, Following,
Actual365Fixed()
)
cds_3m.set_pricing_engine(engine);
cds_6m.set_pricing_engine(engine);
cds_1y.set_pricing_engine(engine);
cds_2y.set_pricing_engine(engine);
print("Repricing of quoted CDSs employed for calibration: ")
print("3M fair spread: {}".format(cds_3m.fair_spread))
print(" NPV: ", cds_3m.net_present_value)
print(" default leg: ", cds_3m.default_leg_npv)
print(" coupon leg: ", cds_3m.coupon_leg_npv)
print("6M fair spread: {}".format(cds_6m.fair_spread))
print(" NPV: ", cds_6m.net_present_value)
print(" default leg: ", cds_6m.default_leg_npv)
tenors = [Period(i, Months) for i in [3, 6, 12, 24]]
maturities = [
calendar.adjust(todays_date + tenors[i], Following) for i in range(4)
]
instruments = []
for i in range(4):
helper = SpreadCdsHelper(
quoted_spreads[i], tenors[i], 0, calendar, Quarterly,
Following, TwentiethIMM, Actual365Fixed(), recovery_rate, ts_curve
)
instruments.append(helper)
# Bootstrap hazard rates
hazard_rate_structure = PiecewiseDefaultCurve.from_reference_date(
ProbabilityTrait.HazardRate, Interpolator.BackwardFlat, todays_date, instruments, Actual365Fixed()
)
#vector > hr_curve_data = hazardRateStructure->nodes();
#cout << "Calibrated hazard rate values: " << endl ;
#for (Size i=0; i
if __name__ == '__main__':
#*********************
#*** MARKET DATA ***
#*********************
calendar = TARGET()
todays_date = Date(15, May, 2007)
# must be a business day
todays_date = calendar.adjust(todays_date)
Settings.instance().evaluation_date = todays_date
# dummy curve
ts_curve = FlatForward(
reference_date=todays_date, forward=0.01, daycounter=Actual365Fixed()
)
# In Lehmans Brothers "guide to exotic credit derivatives"
# p. 32 there's a simple case, zero flat curve with a flat CDS
# curve with constant market spreads of 150 bp and RR = 50%
# corresponds to a flat 3% hazard rate. The implied 1-year
# survival probability is 97.04% and the 2-years is 94.18%
# market
recovery_rate = 0.5
quoted_spreads = [0.0150, 0.0150, 0.0150, 0.0150 ]
tenors = [Period(i, Months) for i in [3, 6, 12, 24]]
maturities = [
calendar.adjust(todays_date + tenors[i], Following) for i in range(4)
]
instruments = []
# option parameters
exercise = AmericanExercise(
earliest_exercise_date=settlement_date,
latest_exercise_date=Date(17, May, 1999)
)
payoff = PlainVanillaPayoff(Put, 40.0)
# market data
underlying = SimpleQuote(36.0)
volatility = BlackConstantVol(todays_date, TARGET(), 0.20,
Actual365Fixed())
dividend_yield = FlatForward(
reference_date=settlement_date,
forward=0.00,
daycounter=Actual365Fixed()
)
# report
header = '%19s' % 'method' + ' |' + \
' |'.join(['%17s' % tag for tag in ['value',
'estimated error',
'actual error']])
print()
print(header)
print('-' * len(header))
refValue = None
def report(method, x, dx=None):
e = '%.4f' % abs(x - refValue)
x = '%.5f' % x
# curve with constant market spreads of 150 bp and RR = 50%
# corresponds to a flat 3% hazard rate. The implied 1-year
# survival probability is 97.04% and the 2-years is 94.18%
# market
recovery_rate = 0.5
quoted_spreads = [0.0150, 0.0150, 0.0150, 0.0150 ]
tenors = [Period(i, Months) for i in [3, 6, 12, 24]]
maturities = [
calendar.adjust(todays_date + tenors[i], Following) for i in range(4)
]
instruments = []
for i in range(4):
helper = SpreadCdsHelper(
quoted_spreads[i], tenors[i], 0, calendar, Quarterly,
Following, TwentiethIMM, Actual365Fixed(), recovery_rate, ts_curve
)
instruments.append(helper)
# Bootstrap hazard rates
hazard_rate_structure = PiecewiseDefaultCurve.from_reference_date(
ProbabilityTrait.HazardRate, Interpolator.BackwardFlat, todays_date, instruments, Actual365Fixed()
)
#vector > hr_curve_data = hazardRateStructure->nodes();
#cout << "Calibrated hazard rate values: " << endl ;
#for (Size i=0; i