import bondPricing as bp
import numpy as np
import matplotlib.pyplot as plt
%load_ext autoreload
%autoreload 2
The default intensity $\lambda$ is related to the spread and the recovery rate by the relationship: $\lambda = \frac{Spread}{1-RecoveryRate}$
The Survival Probability P(t) and Default Probability PD(t) verify by: $ P(t) = 1 - PD(t) = e^{\int_{0}^{t}-\lambda (t) * dt}$
The Discount Factor DF(t) is given by: $ DF(t) = e^{\int_{0}^{t}-r(t) * dt}$
The price of the bond is then the sum of 3 terms:
Assuming constant spread and interest yields the result: $$ Price = \frac{Coupon}{r+\lambda}.(1-e^{-(r+\lambda).T}) + \frac{recoveryRate.\lambda}{r+\lambda}.(1-e^{-(r+\lambda).T}) + Notional . P(T) . DF(T)$$
T = 10
C = 0.05
r = np.linspace(0, 0.2, 50)
spread = 0.01
rr = 0.4
price = 100 * bp.bond_price(maturity=T, coupon=C, interest_rate = r, spread=spread, recovery_rate=rr)
cr01 = bp.bond_cr01(maturity=T, coupon=C, interest_rate = r, spread=spread, recovery_rate=rr)
ir01 = bp.bond_ir01(maturity=T, coupon=C, interest_rate = r, spread=spread, recovery_rate=rr)
cr_gamma = bp.bond_cr01(maturity=T, coupon=C, interest_rate = r, spread=spread, recovery_rate=rr, delta=False)
ir_gamma = bp.bond_ir01(maturity=T, coupon=C, interest_rate = r, spread=spread, recovery_rate=rr, delta=False)
plt.plot(100*r, price)
plt.title("Bond Price as a function of the interest rate")
plt.xlabel("Interest Rate [%]")
plt.ylabel("Bond Price");
$\ 1^{st}$ and $\ 2^{nd}$ order sensitivities are obtained using finite difference method
plt.plot(100*r, cr01, label="CR01")
plt.plot(100*r, ir01, label="IR01")
plt.title("Bond sensitivities as a function of the interest rate")
plt.xlabel("Interest Rate[%]")
plt.ylabel("Bond sensi")
plt.legend();
plt.plot(100*r, cr_gamma, label="CR Gamma")
plt.plot(100*r, ir_gamma, label="IR Gamma")
plt.title("Bond sensitivities as a function of the interest rate")
plt.xlabel("Interest Rate[%]")
plt.ylabel("Bond 2nd order sensi")
plt.legend();
Credit spreads are implied using the bond_price function and a vectorized Newton-Raphson method.
rr_array = np.linspace(0, 0.6, 30)
spread_95 = bp.bond_spread(maturity= T,coupon=C,interest_rate=C,price=95,recovery_rate=rr_array)
spread_75 = bp.bond_spread(maturity= T,coupon=C,interest_rate=C,price=75,recovery_rate=rr_array)
plt.plot(100*rr_array, 10_000 * spread_95)
plt.title("Implied spread for a pricer of 95")
plt.xlabel("Recovery Rate [%]")
plt.ylabel("Bond Credit Spread [bp]");
plt.plot(100*rr_array, 10_000 * spread_75)
plt.title("Implied spread for a pricer of 75")
plt.xlabel("Recovery Rate [%]")
plt.ylabel("Bond Credit Spread [bp]");