Skip to main content

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.

Metadata

Release files for Dynamic-Nelson-Siegel-Svensson-Kalman-Filter 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for Dynamic-Nelson-Siegel-Svensson-Kalman-Filter 0.1.0
File Size Uploaded Provenance Actions
Dynamic_Nelson_Siegel_Svensson_Kalman_Filter-0.1.0.tar.gz 5.9 kB Without provenance Details for Dynamic_Nelson_Siegel_Svensson_Kalman_Filter-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for Dynamic-Nelson-Siegel-Svensson-Kalman-Filter 0.1.0
File Interpreter ABI Platform Actions
Dynamic_Nelson_Siegel_Svensson_Kalman_Filter-0.1.0-py3-none-any.whl Python 3 none any Details for Dynamic_Nelson_Siegel_Svensson_Kalman_Filter-0.1.0-py3-none-any.whl

Total release size: 13.8 kB

Release files / Dynamic_Nelson_Siegel_Svensson_Kalman_Filter-0.1.0.tar.gz

Download URL Dynamic_Nelson_Siegel_Svensson_Kalman_Filter-0.1.0.tar.gz
Size 5.9 kB
Tags Source
SHA-256 checksum
How to use checksums
e49784de87d6a91bbc57a392c3274918df9d7c3897592007f786698ae32a27b2
BLAKE2b-256 checksum
How to use checksums
fa4911e5744b1f008423d92aca44f260d3268dfe262bd4f4b33c9b3adeafd531
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/39.0.1 requests-toolbelt/0.9.1 tqdm/4.57.0 CPython/3.7.0

Release files / Dynamic_Nelson_Siegel_Svensson_Kalman_Filter-0.1.0-py3-none-any.whl

Download URL Dynamic_Nelson_Siegel_Svensson_Kalman_Filter-0.1.0-py3-none-any.whl
Size 7.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e2a1f9c84eb5046d0c23216981af488fad4f89e07f4186ebd1c070a84d086e1c
BLAKE2b-256 checksum
How to use checksums
fe7f146159ed3b1a3a6dc7af953f40b1d3b65d5be9e7300d5912db0b155873bd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/39.0.1 requests-toolbelt/0.9.1 tqdm/4.57.0 CPython/3.7.0

Security for Dynamic-Nelson-Siegel-Svensson-Kalman-Filter 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page