## KRD

Function KRD within
Bond with keys Bond KRD keys returns the Key Rate Duration(s) (KRD) of the referenced bond(s) as of a specified array of future times T₁, T₂, ..., Tₙ
KRD is defined and implemented outside of QuantLib as follows:

Let P be the bond's dirty price as of the bond's settlement date
KRD is an array of n numbers D₁, D₂, ..., Dₙ, such that Dᵢ represents the sensitivity of P upon changing the zero rate rᵢ corresponding to maturity Tᵢ

More specifically:
Dᵢ = (Pᵢ⁻ - Pᵢ⁺)/(2ΔP)
, where
Δ = User-specified amount by which each interest rate described below is shifted both downwards and upwards. For example Δ = 0.01 means an initial rate of 4% is shifted downwards to 3% and upwards to 5%
P = Dirty price that corresponds to the given
clean price.
In order to explain Pᵢ⁻ and Pᵢ⁺, let:
y = Bond's continuously compounded yield implied by the given clean price.
YC = Yield curve built out of zero rates r₁, r₂, ..., rₙ, of which the corresponding equivalent continuously compounded rates are all equal to y
Due to the way the bond's continuously compounded yield y is defined, P must then equal the bond's NPV calculated off the yield curve YC
Now Pᵢ⁻ and Pᵢ⁺ are defined as follows:
Pᵢ⁻ = Dirty bond price calculated using a yield curve YCᵢ⁻, which differs from YC in that it is built out of the zero rates r₁, r₂, ..., rᵢ-Δ, ..., rₙ
Pᵢ⁺ = Dirty bond price calculated using a yield curve YCᵢ⁺, which differs from YC in that it is built out of the zero rates r₁, r₂, ..., rᵢ+Δ, ..., rₙ

Note all yield curves are built using linear interpolation and flat extrapolation with respect to the input zero rates.
This has the consequense that the curves YCᵢ⁻ and YCᵢ⁺ will differ from each other only in the time interval (Tᵢ₋₁,Tᵢ₊₁)
This in turn implies that Pᵢ⁻ - Pᵢ⁺ will receive non-zero contributions only from bond cash flows that occur in the interval (Tᵢ₋₁,Tᵢ₊₁)
The conclusion is that the iᵗʰ KRD Dᵢ = (Pᵢ⁻ - Pᵢ⁺)/(2ΔP) is determined only by the cash flows occurring in the interval (Tᵢ₋₁,Tᵢ₊₁)

PRECAUTION: This function treats the referenced bond as if it were a fixed rate bond even if this is not the case!
All index-linked cash flows are treated as fixed cash flows that pay the amount forecasted by the corresponding curve.