Python code: Estimation of Dynamic Nelson-Siegel model

This post introduces a Python library for estimation of the dynamic Nelson-Siegel (DNS).



Estimation and Forecast of Dynamic Nelson-Siegel model



I found the following useful Python library for estimating and forecasting the DNS or Svensson (DNSS) model.


In the Jupyter notebook, this library can be installed by running the following command.

1
!pip install Dynamic-Nelson-Siegel-Svensson-Kalman-Filter
cs



Correlated-factor DNS model


This Python DNS library use the specification of correlated-factor DNS model instead of the independent-factor model more generally. The correlated-factor DNS model can be expressed as the state space representation which consists of both measurement and state equation as follows.

\begin{align} y_t &= B(\boldsymbol{\tau}) X_t + \epsilon_t \\ X_t - \mu &= A (X_{t-1}-\mu) + \eta_t \end{align}
\[ B(\boldsymbol{\tau}) = \begin{bmatrix} 1 & \displaystyle \frac{1-e^{- \tau_1 \lambda }}{\tau_1 \lambda } & \displaystyle \frac{1-e^{- \tau_1 \lambda }}{\tau_1 \lambda }-e^{- \tau_1 \lambda } \\ 1 & \displaystyle \frac{1-e^{- \tau_2 \lambda }}{\tau_2 \lambda } & \displaystyle \frac{1-e^{- \tau_2 \lambda }}{\tau_2 \lambda }-e^{- \tau_2 \lambda } \\ ⋮&⋮&⋮\\ 1 & \displaystyle \frac{1-e^{- \tau_N \lambda }}{\tau_N \lambda } & \displaystyle \frac{1-e^{- \tau_N \lambda }}{\tau_N \lambda }-e^{- \tau_N \lambda } \end{bmatrix}, \] \[ y_t = \begin{bmatrix} y _{t} ( \tau _{1} )\\y _{t} ( \tau _{2} )\\ ⋮ \\y _{t} ( \tau _{N} )\end{bmatrix}, \epsilon_t = \begin{bmatrix} \epsilon_{t} ( \tau _{1} )\\\epsilon_{t} ( \tau _{2} )\\ ⋮ \\\epsilon_{t} ( \tau _{N} )\end{bmatrix}, \eta_{t} = \begin{bmatrix} \eta_{t}(L) \\ \eta_{t}(S)\\ \eta_{t}(C) \end{bmatrix}, \] \[ X_t = \begin{bmatrix} L_t \\ S_t \\ C_t \end{bmatrix}, \mu = \begin{bmatrix} \mu_L \\ \mu_S \\ \mu_C \end{bmatrix}, A = \begin{bmatrix} a_{11} & a_{12} & a_{13} \\ a_{21} & a_{22} & a_{23} \\ a_{31} & a_{32} & a_{33} \end{bmatrix} \]
where \(y_{t}(\tau)\) is continuously compounded spot rates of maturity \(\tau\) at time \(t\). \(L_t, S_t, C_t \) are level, slope, curvature factors respectively and its unconditional means and autoregressive coefficients are denoted as \( \mu_L, \mu_S, \mu_C \) and \( \phi_L, \phi_S, \phi_C \) sequentially. \( \lambda \) is an exponential time decay parameter. \( \epsilon_{t} \) follows a multivariate normal distribution with only diagonal variances, in other words, a diagonal covariance matrix.

Taking correlated-factors into account, \( \eta_{t} \) has a non-diagonal variance-covariance matrix \(Q=q \times q^{'}\). \[ q = \begin{bmatrix} q_{11} & q_{12} & q_{13} \\ 0 & q_{22} & q_{23} \\ 0 & 0 & q_{33} \end{bmatrix} \]

Python Jupyter Notebook Code


From the web page above, sample data can be obtained, which is the Diebold paper's monthly U.S yield curve dataset.
Python package: Estimation of Dynamic Nelson-Siegel model

Initial guesses for parameters are prepared and this is a rounded version of initial guesses which are provided by the above web page. I redefine an objective function (objf) based on kalman() function in this library to reduce the number of arguments to be passed. I also define a callback function (callbackF) which prints function evaluations periodically to check the progress of this optimization.

The dynamic Nelson-Siegel-Svensson (DNSS) model can also be estimated by setting model = 'S'.

Python package: Estimation of Dynamic Nelson-Siegel model

Optimization is done by using optimize.minimize() function of scipy library with 'L-BFGS-B' algorithm. Of course, there are many alternative optimization algorithm such as 'Nelder-Mead', 'BFGS', and 'CG' to name a few. The callback function prints the number of iteration and the function evaluations.
Python package: Estimation of Dynamic Nelson-Siegel model

After many iterations, we can get the following results of estimation.
Python package: Estimation of Dynamic Nelson-Siegel model

Parameter estimates are stored at x of the resulted object (dns_opt.x).
Python package: Estimation of Dynamic Nelson-Siegel model


Concluding Remarks


This post gives a guided-tour of how to use a Python library for estimation of dynamic Nelson-Siegel model. This also can be useful when the dynamic Svensson model needs to be estimated. \(\blacksquare\)


No comments:

Post a Comment