I want to interpolate a yield curve using Nelson-Siegel model. I'm new to R language but decided to use the "Yield Curve" package (available here http://cran.r-project.org/web/packages/YieldCurve/index.html and documentation's link: http://cran.r-project.org/web/packages/YieldCurve/YieldCurve.pdf ) There are 2 problems I faced:
1) I don't understand why the first 2 parameters of NSrates in the 6th and 7th line of the following code(p.6 in documentation) took that format:
data(FedYieldCurve)
tau <- c(3, 6, 12, 60, 84, 120)
mediumTerm <- c(12,60,84)
NSParameters <- Nelson.Siegel(rate=FedYieldCurve[1:10,],
maturity=tau, MidTau=mediumTerm )
y <- NSrates(NSParameters[5,1:3],
NSParameters$lambda[5],tau)
plot(tau,FedYieldCurve[5,],main="Fitting Nelson-Siegel yield curve", type="o")
lines(tau,y, col=2)
legend("topleft",legend=c("observed yield curve","fitted yield curve"),
col=c(1,2),lty=1)
grid()
2) how to extract a certain yield from the curve? For example, If I have a maturity of 12 months, what is its related yield?.
Thank you a lot for taking time to answer my questions.
Look at the help page for NSrates
.
It takes 3 arguments:
betaCoeff: vector or matrix of the beta's coefficients.
lambdat: value of the estimated lambda
maturity: maturity of the yield curve of which want to return the interest rates.
For betaCeff
, the code you show uses NSParameters[5, 1:3]
, or
beta_0 beta_1 beta_2
5 13.7156 -1.468064 1.237194
which is a data.frame
. Although data.frame
is not explicitly stated as a possible input, the first line of the function converts it to a matrix
(assuming you're using the CRAN version)
if(is.vector(betaCoeff)) betaCoeff <- matrix( betaCoeff, 1, 3)
For lambdat
, the code you show uses NSParameters$lambda[5]
which is the numeric
value to use for the estimated lambda.
The final argument is maturity
. The code you showed uses tau
which is
> tau
[1] 3 6 12 60 84 120
Per the documentation, the function will
Return interest rates in matrix object with number of rows equal to nrow(betaCoeff) and number of columns equal to length(maturity).
In this case, you get
> y
3 6 12 60 84 120
[1,] 12.7394 13.05852 13.40245 13.6897 13.69721 13.70273
If you only want the 12 month maturity
, then pass only 12
to the maturity
argument.
> NSrates(NSParameters[5,1:3], NSParameters$lambda[5], 12)
12
[1,] 13.40245
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.