Forecasting Yield Curves using Dynamic Nelson-Siegel model with R code
[This article was first published on K & L Fintech Modeling, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
This post explains how to forecast yield curves using Dynamic Nelson-Siegel model given information of estimated parameters. Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Forecasting of Dynamic Nelson-Siegel model
We estimated parameters of dynamic Nelson-Seigel model in the previous post below.
Using estimated parameters in the previous post, let’s forecast yield curves.
Forecast
Forecasting equations of DNS model (\(h=1,…,H\)) consist of the state and measurement equations as follows.
\[\begin{align} X_{t+h} &= \psi_0 + \psi_1 X_{t+h-1} \\ y_{t+h} (\boldsymbol{\tau}) &= B(\boldsymbol{\tau}) X_{t+h} \end{align}\]
Here, \(X_t (h=1)\) is the filtered estimate of a state vector at time \(T\) (end of sample).
Estimated Parameters
From the previous post, we can get the following the filtered state estimate and estimated parameters.
\[\begin{align} \lambda &= 0.4336145 \\ A &= \begin{bmatrix} 0.9909972 & 0 & 0 \\ 0 & 0.9614324 & 0 \\ 0 & 0 & 0.9027326 \end{bmatrix} \\ \mu_X &= \begin{bmatrix} 0.0334947 \\ -0.0114954 \\ -0.0066945 \end{bmatrix} \\ X &= \begin{bmatrix} 0.01743225 \\ -0.002418071 \\ -0.0040720452 \end{bmatrix} \end{align}\]
R code
The R code below implements the above forecasting procedure using estimated parameters of DNS model. Forecasts are performed during the 120-month horizon.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 | #========================================================# # Quantitative ALM, Financial Econometrics & Derivatives # ML/DL using R, Python, Tensorflow by Sang-Heon Lee # # https://kiandlee.blogspot.com #——————————————————–# # Forecasting in Dynamic Nelson-Siegel model #========================================================# graphics.off() # clear all graphs rm(list = ls()) # remove all files from your workspace # DNS factor loading matrix NS.B <– function(lambda, tau) { col1 <– rep.int(1,length(tau)) col2 <– (1–exp(–lambda*tau))/(lambda*tau) col3 <– col2–exp(–lambda*tau) return(cbind(col1,col2,col3)) } #————————— # estimated parameters #————————— # AR matrix A <– diag(c(0.9909972, 0.9614324, 0.9027326)) # mean vector MU <– c(0.0334947, –0.0114954, –0.0066945) lambda <– 0.4336145 Phi0 <– (diag(3)–A)%*%MU Phi1 <– A # factor loading matrix v.mat <– c(0.25, 0.5, 0.75, 1, 1.5, 2, 2.5, 3, 4, 5, 7, 10, 20) nmat <– length(v.mat) B <– NS.B(lambda,v.mat) #—————————————– # filtered state estimate at time T #—————————————– # final state prevX <– c(0.01743225, –0.002418071, –0.0040720452) #—————————————– # forecasting H-horison #—————————————– # forecasting horizon nhor <– 120 # 5-year # placeholder for predicted states # and yield curve forecasts Xf <– matrix(NA, nhor, 3) yf <– matrix(NA, nhor, nmat) # dynamic forecasting for(h in 1:nhor) { # prediction Xhat <– Phi0+Phi1%*%prevX # forecast yield curve y_fit <– B%*%Xhat # use forecasted state at h # as previous state at h+1 prevX <– Xhat # save Xf[h,] <– Xhat yf[h,] <– y_fit } yf Xf | cs |
Running this R code produces the following output table of factors and yields forecast.
Factor forecasts are done recursively according to its vector autoregressive process. The following figure draws a conbined graph for the historical factor estimates and their forecasts.
Yields forecasts are calculated based on factor forecasts using the measurement equation. The following figure draws a conbined graph for the historical yield curves and their forecasts.
Concluding Remarks
This post shows how to forecast yield curves using DNS model based on its estimated parameters. For those who are not familiar with this procedure, I hope this will help. \(\blacksquare\)
To leave a comment for the author, please follow the link and comment on their blog: K & L Fintech Modeling.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.