Skip to main content

Nelson-Siegel-Svensson Model

https://img.shields.io/pypi/v/nelson_siegel_svensson.svg https://github.com/luphord/nelson_siegel_svensson/actions/workflows/python-package.yml/badge.svg Documentation Status

Implementation of the Nelson-Siegel-Svensson interest rate curve model in Python.

from nelson_siegel_svensson import NelsonSiegelSvenssonCurve
import numpy as np
from matplotlib.pyplot import plot

y = NelsonSiegelSvenssonCurve(0.028, -0.03, -0.04, -0.015, 1.1, 4.0)
t = np.linspace(0, 20, 100)
plot(t, y(t))
docs/_static/an_example_nelson-siegel-svensson-curve.png

Features

  • Python implementation of the Nelson-Siegel curve (three factors)

  • Python implementation of the Nelson-Siegel-Svensson curve (four factors)

  • Methods for zero and forward rates (as vectorized functions of time points)

  • Methods for the factors (as vectorized function of time points)

  • Calibration based on ordinary least squares (OLS) for betas and nonlinear optimization for taus

  • Simple command line interface (CLI) for evaluating, calibrating and plotting curves

Calibration

In order to calibrate a curve to given data you can use the calibrate_ns_ols and calibrate_nss_ols functions in the calibrate module:

import numpy as np
from nelson_siegel_svensson.calibrate import calibrate_ns_ols

t = np.array([0.0, 0.5, 1.0, 2.0, 3.0, 4.0, 5.0, 10.0, 15.0, 20.0, 25.0, 30.0])
y = np.array([0.01, 0.011, 0.013, 0.016, 0.019, 0.021, 0.026, 0.03, 0.035, 0.037, 0.038, 0.04])

curve, status = calibrate_ns_ols(t, y, tau0=1.0)  # starting value of 1.0 for the optimization of tau
assert status.success
print(curve)

which gives the following output:

NelsonSiegelCurve(beta0=0.04201739383636799, beta1=-0.031829031569430594, beta2=-0.026797319779108236, tau=1.7170972656534174)
docs/_static/calibrated_nelson-siegel-curve.png

Command Line interface

nelson_siegel_svensson provides basic functionality using a command line interface (CLI):

Usage: nelson_siegel_svensson [OPTIONS] COMMAND [ARGS]...

Commandline interface for nelson_siegel_svensson.

Options:
--help  Show this message and exit.

Commands:
calibrate  Calibrate a curve to the given data points.
evaluate   Evaluate a curve at given points.
plot       Plot a curve at given points.

In order to calibrate a curve to given data points on the command line, try

nelson_siegel_svensson calibrate -t '[0.0, 0.5, 1.0, 2.0, 3.0, 4.0, 5.0, 10.0, 15.0, 20.0, 25.0, 30.0]' -y '[0.01, 0.011, 0.013, 0.016, 0.019, 0.021, 0.026, 0.03, 0.035, 0.037, 0.038, 0.04]' --nelson-siegel --initial-tau1 1.0

which gives

{"beta0": 0.042017393764903765, "beta1": -0.03182903146166806, "beta2": -0.026797320316066128, "tau": 1.717097232403383}

This curve can then be evaluated on the command line using

nelson_siegel_svensson evaluate -c '{"beta0": 0.042017393764903765, "beta1": -0.03182903146166806, "beta2": -0.026797320316066128, "tau": 1.717097232403383}' -t '[0, 1, 2, 3]'

resulting in

[0.010188362303235707, 0.012547870204470839, 0.01574855552855885, 0.01897955804146046]

And finally, the curve can be plotted with

nelson_siegel_svensson plot -o cli_plot_example.png -c '{"beta0": 0.042017393764903765, "beta1": -0.03182903146166806, "beta2": -0.026797320316066128, "tau": 1.717097232403383}'
docs/_static/cli_plot_example.png

Note that the quoting in the above commands prevents bash from evalutating the JSON-based parameters. Depending on your shell, you may require a different quoting mechanism.

Credits

Main developer is luphord.

This package was prepared with Cookiecutter and the audreyr/cookiecutter-pypackage project template.

History

0.5.0 (2022-11-13)

  • Drop support for Python 3.7

  • Add support Python 3.9 and 3.10

  • Reformat code with black

  • Upgrade dependencies

  • Upgrade development status to beta

0.4.3 (2022-03-15)

  • Migrate to GitHub Actions

0.4.2 (2020-02-04)

  • Support Python 3.8

  • Upgrade flake8 (test requirement) as prior version did not support Python 3.8

0.4.1 (2019-11-14)

  • Added return type annotations for core modules

  • Added some example notebooks

0.4.0 (2019-07-08)

  • Simple command line interface (CLI) supporting curve evaluation, calibration and plotting

  • Added more documentation

0.3.0 (2019-03-17)

  • Added type annotations

0.2.0 (2019-02-20)

  • Ordinary least squares based calibration of Nelson-Siegel-Svensson

  • Ordinary least squares based calibration of Nelson-Siegel

  • A little bit of usage documentation

0.1.0 (2019-02-13)

  • First release on PyPI.

Release files for nelson-siegel-svensson 0.5.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 nelson-siegel-svensson 0.5.0
File Size Uploaded
nelson_siegel_svensson-0.5.0.tar.gz 141.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for nelson-siegel-svensson 0.5.0
File Interpreter ABI Platform
nelson_siegel_svensson-0.5.0-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 151.5 kB

Release files / nelson_siegel_svensson-0.5.0.tar.gz

Download URL nelson_siegel_svensson-0.5.0.tar.gz
Size 141.6 kB
Tags Source
SHA-256 checksum
How to use checksums
cc1ed8ba0d12f236490da5bc5eadf846e0baeb92f4935223cc99dfbd44c018e2
BLAKE2b-256 checksum
How to use checksums
4918b0889a14db20f21f4164196661d8714e9f09797ab0e2788b550d7574cfc1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.8.13

Release files / nelson_siegel_svensson-0.5.0-py2.py3-none-any.whl

Download URL nelson_siegel_svensson-0.5.0-py2.py3-none-any.whl
Size 9.9 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
cd33414a5348e6640858de011b493ab06e4af25e8d572cec214ba63599dd04d2
BLAKE2b-256 checksum
How to use checksums
208e775d07576b015e79d7aae476cda14e98c1c562b4d4b4e20ff6a93b106e4e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.8.13

Release history Release notifications | RSS feed

This release

0.5.0 This release

2 release files

0.4.3

2 release files

0.4.2

2 release files

0.4.1

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.0

2 release files

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