Skip to main content

Implementation of the Dynamic-Nelson-Siegel-Svensson models with Kalman FilterFitting and Forecasting the Yield Curve.

Project description

Dynamic-Nelson-Siegel-Svensson Models

https://img.shields.io/pypi/v/Dynamic_Nelson_Siegel_Svensson_Kalman_Filter.svg

This package implements the Dynamic Nelson-Siegel-Svensson models with Kalman filter in Python.

  • Free software: MIT license

  • Python 3.7 or later supported

Features

  • Python implementation of the Dynamic Nelson-Siegel curve (three factors) with Kalman filter

  • Python implementation of the Dynamic Nelson-Siegel-Svensson curve (four factors) with Kalman filter

  • Forecasting the yield curve is available

  • Log-likelihood is available to use optimize.minimize

Fitting the Dynamic-Nelson-Siegel

To fitting the yield curve, we must set (param,Y,lik,frct,ahead,mty,model)

param: initial parameters vector of Dynamic-Nelson-Siegel models obtained by OLS in the two-step approach =

([

The log of lambdas,

the square root of sample covariance diagonal matrix of VAR(1) residuals (H),

the VAR(1) matrix (phi),

the betas averages (mu), and

the cholesky deconposition of the VAR(1) estimated innovations covariance matrix (Q)

]);

Y: pandas data frame;

lik: log-likelihood;

frct: forecasts;

ahead: months (years) ahead forecasts;

mty: maturities in months (years);

model: ‘NS’ for the Dynamic Nelson-Siegel model or ‘S’ for the Dynamic Nelson-Siegel-Svensson model;

from Dynamic_Nelson_Siegel_Svensson_Kalman_Filter import kalman
import numpy as np
import pandas as pd

url = 'https://www.dropbox.com/s/inpnlugzkddp42q/bonds.csv?dl=1' # US Yield Curve 1972 - 2000
df = pd.read_csv(url,sep=';',index_col=0)

frct = False
ahead = 0
lik = True
mty =  np.array([3,6,9,12,15,18,21,24,30,36,48,60,72,84,96,108,120]) # maturities in months
model = 'NS'

param = np.array([-2.798522, # log of 0.0609
0.14170940,0.07289485,0.11492339,0.11120008,0.09055795,0.07672075,0.07222108,0.07076431,0.07012891,0.07267366,0.10624206,0.09029621,0.10374527,0.09801215,0.09122014,0.11794190,0.13354418, # H
0.99010443,0.02496842,-0.002294319, # phi
-0.02812401,0.94256154, 0.028699387, # phi
0.05178493,0.01247332, 0.788078795, # phi
8.345444,-1.572442,0.2029919,  # mu
0.3408764,-0.07882772,-0.21351036, # Q
0.62661018,-0.00425989, # Q
1.08802059]) # Q

kalman(param = param,Y = df, lik = lik, frct = frct, ahead = ahead, mty = mty, model = model)

which gives the following output

-2281.5860793152

Forecasting the Dynamic-Nelson-Siegel

Kalman function also provides forecasts. We set 12 months ahead, and we define param, Y, mty, and model as above.

frst = True
lik = False
ahead = 12

kalman(param = param,Y = df, lik = lik, frct = frct, ahead = ahead, mty = mty, model = model)

which returns (a_tt,a_t,P_tt,P_t,v2,v1,Yf)

a_tt: the a posteriori state estimate,

a_t: the a priori state estimate,

P_tt: the a posteriori estimate covariance matrix,

P_t: the a posteriori estimate covariance matrix,

v2: filtered yields,

v1: filtered erros, and

Yf: forecasts.

Fitting and Forecasting the Dynamic-Nelson-Siegel-Svensson

We can replicate the steps above but using the following initial parameters.

model = 'S'

param = np.array([-2.798522,-3.55908713, # lambdas
0.14170940,0.07289485,0.11492339,0.11120008,0.09055795,0.07672075,0.07222108,0.07076431,0.07012891,0.07267366,0.10624206,0.09029621,0.10374527,0.09801215,0.09122014,0.11794190,0.13354418, # H
0.99010443,0.02496842,-0.002294319,0.0000, # phi
-0.02812401,0.94256154, 0.028699387,0.000, # phi
0.05178493,0.01247332, 0.788078795,0.0000, # phi
0.00000000,0.00000000,0.0000000000,0.6000, # phi
8.345444,-1.572442,0.2029919,2.96696726, # mu
0.3408764,-0.07882772,-0.21351036,0.1, # Q
0.62661018,-0.00425989,0.2, # Q
1.08802059,0.3, # Q
0.4]) # Q

optimize.minimize

from scipy import optimize

optimize.minimize(fun = kalman, x0 = param, args = (df,lik,frct,ahead,mty,model),method = 'L-BFGS-B', bounds = None,options={'disp':True})

which gives the following output for the Dynamic-Nelson-Siegel model

 ...
 CONVERGENCE: REL_REDUCTION_OF_F_<=_FACTR*EPSMCH
 fun: -3184.551029683603
 ...
 success: True
 x: array([-2.55419095,  0.2682528 ,  0.07550792,  0.09029396,  0.10450556,
 0.09915572,  0.08648558,  0.07862346,  0.07209206,  0.07267585,
 0.07909973,  0.10295337,  0.09260472,  0.10041753,  0.1117615 ,
 0.10696698,  0.15069579,  0.17277911,  0.99437232,  0.02861622,
-0.02214779, -0.02886483,  0.93904762,  0.03963317,  0.02540488,
 0.02288304,  0.84151308,  7.99042415, -1.46634099, -0.43379175,
 0.30276472, -0.02340832,  0.0488872 ,  0.61858155,  0.01026079,
 0.89411573])

Credits

Main developer is Werley Cordeiro.

History

0.1.0 (2021-02-25)

  • First release on PyPI.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

Built Distribution

File details

Details for the file Dynamic_Nelson_Siegel_Svensson_Kalman_Filter-0.1.0.tar.gz.

File metadata

File hashes

Hashes for Dynamic_Nelson_Siegel_Svensson_Kalman_Filter-0.1.0.tar.gz
Algorithm Hash digest
SHA256 e49784de87d6a91bbc57a392c3274918df9d7c3897592007f786698ae32a27b2
MD5 3a42779aefef8320dd8e9c90072eb0ec
BLAKE2b-256 fa4911e5744b1f008423d92aca44f260d3268dfe262bd4f4b33c9b3adeafd531

See more details on using hashes here.

File details

Details for the file Dynamic_Nelson_Siegel_Svensson_Kalman_Filter-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for Dynamic_Nelson_Siegel_Svensson_Kalman_Filter-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e2a1f9c84eb5046d0c23216981af488fad4f89e07f4186ebd1c070a84d086e1c
MD5 e6b66608da524aede06ae80f94933068
BLAKE2b-256 fe7f146159ed3b1a3a6dc7af953f40b1d3b65d5be9e7300d5912db0b155873bd

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page