Bond Caculate and Term Structure-债券计算及期限结构
R Markdown
This is an R Markdown document. \(\color{#8D0012}{Markdown}\) is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
Bond price,ytm,duration and convexity
Description
Basic yield, pricing, duration and convexity calculations. These functions perform simple present value calculations assuming that all periods between payments are the same length. Unlike bond functions in Excel, for example, settlement and maturity dates are not used. By default, duration is Macaulay duration.
Usage
- bondpv(coupon, mat, yield, principal, freq)
- bondyield(price, coupon, mat, principal, freq)
- duration(price, coupon, mat, principal, freq, modified)
- convexity(price, coupon, mat, principal, freq)
Arguments
- coupon: annual coupon
- mat: maturity in years
- yield: annual yield to maturity. If freq > 1, the yield is freq times the per period yield.
- principal: maturity payment of the bond, in addition to the final coupon. Default value is $1,000. If the instrument is an annuity, set principal to zero.
- freq: number of payments per year.
- price: price of the bond
- modified: If true, compute modified duration, otherwise compute Macaulay duration. FALSE by default.
Value
Return price, yield, duration and convexity. The bond price mathematical formula is: \[P = \sum_{t=1}^{t} \frac{C_{t-1}} {(1+ytm)^{t-1}} + \frac{C_{t}+P_{0}} {(1+ytm)^{t}}\]
The bond duartion formula is: \[MacD = \sum_{t=1}^{t}t*\frac{CF_{t}} {(1+y)^{t}}\frac{1}{P}\]
The modified duration formula is: \[ModD = -\frac{1}{P} \frac{\mathrm{d} P}{\mathrm{d} y} = \frac{1}{P} \frac{1}{1+y} \sum_{n=1}^{t} \frac{t*CF_{t}} {(1+y)^{t}}= \frac{MacD}{1+y}\]
The convexity formula is: \[Conv = \frac{1}{P} \frac{\mathrm{d}^2 P}{\mathrm{d} y^2} = \frac{1}{P}\frac{1}{(1+y)^2}\sum_{n=1}^{t} \frac{t(t+1)CF_{t}} {(1+y)^{t}}\]
The change of bond price can be caculated by the formula with ModD and Conv approximately: \[\frac{\Delta P}{P} \approx -ModD*{\Delta y} + \frac{1}{2} Conv*{\Delta y^2}\]
In fixed income practice, effective duration and effective convexity are used to describe the interest risk more often: \[EffD = \frac{1}{P} \frac{P_--P_+}{y_+-y_-} = \frac{P_--P_+}{2\Delta y P}\], \[EffConv = \frac{D_--D_+}{2 \Delta y} = \frac{P_--P_+-2P}{2 (\Delta y)^2 P}\]
Examples of bond caculate
library(derivmkts)
coupon <- 6; mat <- 3.65; freq <- 2; principal <- 100; yield <- 0.045;
price <- bondpv(coupon, mat, yield, principal, freq); price## [1] 104.8077
bondyield(coupon, mat, price=price, principal, freq) ## [1] 0.045
duration(price, coupon, mat, principal, freq, modified=FALSE) ## [1] 3.216609
duration(price, coupon, mat, principal, freq, modified=TRUE) ## [1] 3.145828
convexity(price, coupon, mat, principal, freq) ## [1] 11.95165
In this example, the bond price is 104.81, and the modified duration is 3.15.
Term Structure Modeling
The functions in fBonds package are:
- NelsonSiegel Nelson-Siegel Term Structure,
- Svensson Nelson-Siegel-Svensson Term Structure.
Usage
- NelsonSiegel(rate, maturity, doplot = TRUE)
- Svensson(rate, maturity, doplot = TRUE)
Arguments
- doplot: a logical. Should a plot be displayed?
- maturity: a numeric vector of maturities on an annual scale.
- rate: a numeric vector of forward rates.
Value
a list object with entries returned from the optimization function nlminb.
Examples of term structure
| Maturity | Yield |
|---|---|
| 0.083 | 0.04984 |
| 0.167 | 0.05283 |
| 0.250 | 0.05549 |
| 0.333 | 0.05777 |
| 0.417 | 0.05961 |
| 0.500 | 0.06102 |
| 0.583 | 0.06216 |
| 0.667 | 0.06314 |
| 0.750 | 0.06403 |
| 0.833 | 0.06488 |
par(mar=c(5,4,2,2))
plot(dt,pch=16,col=sample(rainbow(6),nrow(dt),replace = T))
grid()Figure 1: Dot plot of bond data
par(mar=c(5,4,2,2))
NelsonSiegel(Yield, Maturity,doplot=T)Figure 2: Plot of NelsonSiegel Term Structure
Figure 2: Plot of NelsonSiegel Term Structure
## $par
## beta0 beta1 beta2 tau1
## 8.885514e-02 -3.671098e-02 -1.737138e-09 1.025116e+00
##
## $objective
## [1] 0.0001602499
##
## $convergence
## [1] 0
##
## $iterations
## [1] 25
##
## $evaluations
## function gradient
## 37 125
##
## $message
## [1] "relative convergence (4)"
par(mar=c(5,4,2,2))
Svensson(Yield, Maturity,doplot=T)Figure 3: Plot of Nelson-Siegel-Svensson Term Structure
Figure 3: Plot of Nelson-Siegel-Svensson Term Structure
Figure 3: Plot of Nelson-Siegel-Svensson Term Structure
## $par
## beta0 beta1 beta2 beta3 tau1
## 0.0677180818 -0.0202951750 0.0003743188 0.0568608140 0.5829797701
## tau2
## 21.0000767906
##
## $objective
## [1] 0.0290922
##
## $convergence
## [1] 1
##
## $iterations
## [1] 61
##
## $evaluations
## function gradient
## 132 503
##
## $message
## [1] "false convergence (8)"