|
From: Ngonidzashe F. <ngo...@gm...> - 2020-11-30 18:42:41
|
Hi QuantLib users,
I was looking at a CDS illustration done by Luigu below;
import QuantLib as ql
calendar = ql.TARGET()
todaysDate = ql.Date(15, ql.May, 2007)
ql.Settings.instance().evaluationDate = todaysDate
risk_free_rate = ql.YieldTermStructureHandle(ql.FlatForward(todaysDate, 0.01,
ql.Actual365Fixed()))
# ### CDS parameters
recovery_rate = 0.5
quoted_spreads = [0.0150, 0.0150, 0.0150, 0.0150]
tenors = [ql.Period(3, ql.Months), ql.Period(6, ql.Months), ql.Period(1, ql.
Years), ql.Period(2, ql.Years)]
maturities = [calendar.adjust(todaysDate + x, ql.Following) for x in tenors]
instruments = [
ql.SpreadCdsHelper(
ql.QuoteHandle(ql.SimpleQuote(s)),
tenor,
0,
calendar,
ql.Quarterly,
ql.Following,
ql.DateGeneration.TwentiethIMM,
ql.Actual365Fixed(),
recovery_rate,
risk_free_rate,
)
for s, tenor in zip(quoted_spreads, tenors)
]
hazard_curve = ql.PiecewiseFlatHazardRate(todaysDate, instruments, ql.
Actual365Fixed())
print("Calibrated hazard rate values: ")
for x in hazard_curve.nodes():
print("hazard rate on %s is %.7f" % x)
print("Some survival probability values: ")
print(
"1Y survival probability: %.4g, \n\t\texpected %.4g"
% (hazard_curve.survivalProbability(todaysDate + ql.Period("1Y")), 0.9704)
)
print(
"2Y survival probability: %.4g, \n\t\texpected %.4g"
% (hazard_curve.survivalProbability(todaysDate + ql.Period("2Y")), 0.9418)
)
# ### Reprice instruments
nominal = 1000000.0
probability = ql.DefaultProbabilityTermStructureHandle(hazard_curve)
# We'll create a cds for every maturity:
all_cds = []
for maturity, s in zip(maturities, quoted_spreads):
schedule = ql.Schedule(
todaysDate,
maturity,
ql.Period(ql.Quarterly),
calendar,
ql.Following,
ql.Unadjusted,
ql.DateGeneration.TwentiethIMM,
False,
)
cds = ql.CreditDefaultSwap(ql.Protection.Seller, nominal, s, schedule, ql.
Following, ql.Actual365Fixed())
engine = ql.MidPointCdsEngine(probability, recovery_rate, risk_free_rate)
cds.setPricingEngine(engine)
all_cds.append(cds)
print("Repricing of quoted CDSs employed for calibration: ")
for cds, tenor in zip(all_cds, tenors):
print("%s fair spread: %.7g" % (tenor, cds.fairSpread()))
print(" NPV: %g" % cds.NPV())
print(" default leg: %.7g" % cds.defaultLegNPV())
print(" coupon leg: %.7g" % cds.couponLegNPV())
This illustration already has CDS spreads already defined in the
quoted_spread object such that if you try to determine cds.fairSpread() you
will get what is already in the quotated_spread.
My questions are as follow;
1. If the quoted rates are not defined as in the above code, where do I
adjust to solve for the CDS spread required to have the Expected PV of the
default leg equal to the Expected PV of the coupon leg?
2. If it's possible, how do I print the cashflows of both the default leg
and the coupon leg?
3. Given the CDS spread (or the quoted spread), where do I adjust in the
code to get the value of the CDS at a given valuation date.
Regards,
Ngoni
|