GithubHelp home page GithubHelp logo

luphord / nelson_siegel_svensson Goto Github PK

View Code? Open in Web Editor NEW
107.0 1.0 40.0 235 KB

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

License: MIT License

Makefile 6.53% Python 93.47%
python3 interest-rates curve-fitting yield-curve finance

nelson_siegel_svensson's People

Contributors

dependabot[bot] avatar luphord avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

nelson_siegel_svensson's Issues

matplotlib as extra requirement

Hay,

First of all this package is very usefull, tnx!

@luphord did you consider add matplotlib as an extra requirement istead of a regular requirement?
It's required only by the example and this package is big.

In my case I use your package in alpine docker and the time to compile matplotlib is half of the build time.

Tnx!

Weighted Regression

Great package!

Just a question - do you correct for the fact that front end of the curve usually has much more data
than the far end of the curve? Something like Weighted Least Squares?

Cannot iterate over results of calibrate_nss_ols process to collect parameters

  • Nelson-Siegel-Svensson Model version:
  • Python version: 3.8.5
  • Operating System: windows

Description

My question or issue is with the calibrate_nss_ols or calibrate_ns_ols method. It would be great to be able to iterate through the results of the calibration.

As it stands, I cannot select the parameters of the calibrated nelson siegel or nelson siegel svensson model for later use. It appears the only way to use the parameters or results of the calibrated model is to manually copy them and paste them as follows:

NelsonSiegelCurve(beta0=0.04201739383636799, beta1=-0.031829031569430594, beta2=-0.
˓→026797319779108236, tau=1.7170972656534174)

This manual process appears inefficient to me. It would be great to have the results of the calibration in list format [] so that we can index or iterate through the results of the calibrate_nss_ols method to obtain and cycle through the parameters like so: beta0 = results[0], beta1 = results[1], ... ,tau2 = results[5]

What I Did

curve, status = calibrate_nss_ols(t, y)

curve variable above only returns an object that doesn't allow me to index or select and assign the parameters of the calibrated model to a variable. It would be fantastic if the results of the calibration was in a list format so that we can index through the parameters, select them and assign them to a variable for reuse...

Thank you so much for taking the time to read this and thank you for your work on this api!! Overall it's a great product and the world owes you a debt of gratitude for this work.

Paste the command(s) you ran and the output.
If there was a crash, please include the traceback here.

how to estimate tau?

Hi! I have read your documentation and some source code. It seems it can estimate betas given tau, but how can I get tau from the data?

Unexpected results for calibrate_nss_ols function

  • Nelson-Siegel-Svensson Model version: v0.4.2
  • Python version: 3.9.2
  • Operating System: debian 11

Description

unexpected results of calibrate_nss_ols calibration function

What I Did

Hi There,
first of all - thanks for this python implementation !
i have some excel data that i would like to compute the calibrate values for and i'm getting very different results from my excel.
 
this is when i run in the code:
calibrate_nss_ols(np.array([1, 2, 5, 10, 25]), np.array([0.0039, 0.0061, 0.0166, 0.0258, 0.0332]) , tau0=[1.0, 1.0])
and i got this response:
(NelsonSiegelSvenssonCurve(beta0=0.037538814593271755, beta1=-0.03687865378245708, beta2=194192398920.83823, beta3=-194192398920.88785, tau1=1.000036239805082, tau2=1.0000362398052216)‏

however, in my excel sheet i use the same times (the time is in years of course) and the same yields as percentage (0.39%, 0.61%, 1.66%, 2.58%, 3.32%) and by curve factors are the following:

beta0=0.04
beta1=-0.03
beta2=-0.02
beta3=-0.02
tau0=2.10
tau0=1.04

i have test this a couple of times and i can't seem to find the problem,
Thank you very much for any help!

Negative values of tau

  • Nelson-Siegel-Svensson Model version:
  • Python version: 3.8
  • Operating System: Windows

Description

Tau gets negative in optimization-> produces massive values for factors, least squares can't handle
maturities=np.array([ 0.25 0.5 1. 2. 3. 5. 10. 30. ])
yields = np.array([ 7.80846154, 8.16153846, 8.54207692, 9.44315385, 9.78792308,
10.31846154, 10.77930769, 10.92284615])

I recommend supplying bounds in calibrate_ns_ols:
opt_res = minimize(errorfn_ns_ols, x0=tau0, args=(t, y), bounds=((0.01,None),))

Fixing Tau during OLS calibration

Is there a simple way to calibrate the beta parameters while fixing the taus to some constant? I may not want to calibrate the taus and just calibrate the betas

Thanks!

can't import nelson_siegel_svensson

  • Nelson-Siegel-Svensson Model version: 0.3.0
  • Python version: 3.7.3
  • Operating System: Windows 10

Description

I am having a problem importing nelson_siegel_svensson.
I am getting the following error: ImportError: cannot import name 'NelsonSiegelSvenssonCurve' from 'nelson_siegel_svensson' (C:\Users\user\Documents\Visual Studio 2017\Projects\corp_bond_yield_compare\nelson_siegel_svensson.py)

What I Did

I installed the module using pip as suggested tried to run the demo code you gave and the result was as the above

Error in calibrate_nss_ols() parameter output comparing with Fed Data

  • Nelson-Siegel-Svensson Model version:v0.4.2
  • Python version:3.7.6
  • Operating System: Macbook, Jupyter Notebook

Description

Input: different tenors of US zero-coupon bond yield from Bloomberg
I am trying to use calibrate_nss_ols() model to get the 6 parameters, and then I compare the parameters published by Fed
Fed data source: https://www.federalreserve.gov/data/yield-curve-tables/feds200628_1.html
But the result I get from the NSS model is a bit different from what Fed actually published

Describe what you were trying to get done.
Tell us what happened, what went wrong, and what you expected to happen.

What I Did

I input about 10 points on the US zero-coupon curve from Bloomberg
For example on 08/09/2015:
calibrate_nss_ols(t= np.array([0.25, 0.5, 1,2,3, 5,7, 10, 20,30]),
y = np.array([0.158, 0.224, 0.349, 0.719, 1.051, 1.552, 1.914, 2.216, 2.811, 3.182]))

For example on 27/10/2010:
calibrate_nss_ols(t= np.array([0.25, 0.5, 1,2,3, 5,7, 10, 20,30]),
y = np.array([0.114, 0.194, 0.231, 0.445, 0.673, 1.364, 2.143, 2.929, 4.197, 4.589]))

However, our result is a bit different from what Fed published: example on 2 dates:
image

Also, I tried to test the NSS parameters output from 2010 to today, by inputing the 10 points on zero coupon yield, most of the output parameters does not really match the parameters Fed published...

May I know what do you think could cause the error?

Thank you very much for your time and help!

Bests regards

Paste the command(s) you ran and the output.
If there was a crash, please include the traceback here.

Problems fitting the current Bund yield curve

  • Nelson-Siegel-Svensson Model version: 0.5.0
  • Python version: 3.10.3
  • Operating System: Anaconda on Windows 11 x64

Description

I'm trying to fit the current Bund yield curve. The model is having issues fitting it, and is very susceptible to the initial outliers. Even removing the firs couple of outliers in the beginning of the curve, it still fails.

What I Did

Here's the current yields tuple array:
[(217, 3.7) (177, 3.67) (65, 3.61) (247, 3.6) (156, 3.52) (127, 3.5) (338, 3.5) (309, 3.43) (373, 3.4) (428, 3.38) (519, 3.28) (493, 3.27) (548, 3.21) (610, 3.17) (85, 3.09) (674, 3.09) (708, 3.04) (730, 3.01) (5330, 3.0) (5745, 3.0) (4834, 2.98) (4600, 2.92) (858, 2.91) (4234, 2.9) (912, 2.89) (3919, 2.82) (1094, 2.81) (1039, 2.79) (3596, 2.77) (3415, 2.75) (3415, 2.74) (1283, 2.73) (1223, 2.73) (1362, 2.72) (3231, 2.72) (3049, 2.69) (2592, 2.69) (2227, 2.69) (1465, 2.68) (2642, 2.67) (2865, 2.67) (1646, 2.67) (2500, 2.67) (2684, 2.66) (1496, 2.66) (1404, 2.66) (2500, 2.66) (1546, 2.66) (2865, 2.65) (1588, 2.65) (2135, 2.65) (1862, 2.64) (2277, 2.63) (2319, 2.63) (1728, 2.62) (1954, 2.61) (1770, 2.61) (1465, 2.6)]


t = np.array(x['t'])
y = np.array(x['y'])

print(*zip(t,y))

curve, status = calibrate_ns_ols(t, y, tau0=60) # starting value of 1.0 will make a much worse fit.
assert status.success
print(status)
i = np.linspace(min(t), max(t), 100)
x_t = dt.date.today() + np.array([dt.timedelta(days=int(d)) for d in i]) 

plt.subplots(figsize=(10,4))

plt.plot(x_t, curve(i))
plt.scatter(x['Expiry'],y)
plt.show()
curve

The curve shows as: NelsonSiegelCurve(beta0=2.5846778100250116, beta1=-0.0699579846346146, beta2=3.514586203990244, tau=91.21267697168356)

Removing the first few outliers, using only the following data: (217, 3.7) (177, 3.67) (247, 3.6) (156, 3.52) (127, 3.5) (338, 3.5) (309, 3.43) (373, 3.4) (428, 3.38) (519, 3.28) (493, 3.27) (548, 3.21) (610, 3.17) (674, 3.09) (708, 3.04) (730, 3.01) (5330, 3.0) (5745, 3.0) (4834, 2.98) (4600, 2.92) (858, 2.91) (4234, 2.9) (912, 2.89) (3919, 2.82) (1094, 2.81) (1039, 2.79) (3596, 2.77) (3415, 2.75) (3415, 2.74) (1283, 2.73) (1223, 2.73) (1362, 2.72) (3231, 2.72) (3049, 2.69) (2592, 2.69) (2227, 2.69) (1465, 2.68) (2642, 2.67) (2865, 2.67) (1646, 2.67) (2500, 2.67) (2684, 2.66) (1496, 2.66) (1404, 2.66) (2500, 2.66) (1546, 2.66) (2865, 2.65) (1588, 2.65) (2135, 2.65) (1862, 2.64) (2277, 2.63) (2319, 2.63) (1728, 2.62) (1954, 2.61) (1770, 2.61) (1465, 2.6)

The NSS Curve is still not fitted and results in NelsonSiegelCurve(beta0=2.5938433203011453, beta1=-4.606659531771271, beta2=9.466695852698784, tau=60.88625970741441)

The NSS model fits even worse.

Now, the curves are what they are (and I have not shown you the other EU contries!), but some noise is inevitable and the NSS model and it seems very affected by outliers in the first few points.

I'd expect it to fit a much closer curve and to ignore the outliers in a much more robust way

Is the input yield zero-coupon yield?

A question that is not clear in the documentation but very important is whether the input yield is zero-coupon yield or yield of coupon-bearing bonds when calibrating the NS model.

Calibrating NS parameters for multiple term structures together

  • Nelson-Siegel-Svensson Model version: 0.4.2
  • Python version: 3.7.8
  • Operating System: Mac OS 11.2.3

Description

I was wondering if it is possible to calibrate the decay parameter lambda for multiple term structures like in this paper:
image

I have term structure data for multiple days and would like to find the optimal lambda and beta parameters considering all term structures together.

many thanks!

Implementation of the Nelson-Siegel-Svensson interest rate curve model

  • Nelson-Siegel-Svensson Model version:
    from nelson_siegel_svensson import NelsonSiegelSvenssonCurve
    from nelson_siegel_svensson.calibrate import calibrate_ns_ols
  • Python version:
  • Operating System:

Description

Hi,

I have a task: Nelson-Siegel-Svensson model to Fit the yield curve each day using the 6 benchmark yields for the October data.
(Only need 2-year, 3-year, 5-year, 7-year, 10-year, and 30-year maturities)
Importing the data and plot that is no problem.

I found some codes for Nelson-Siegel-Svensson Model, now I am trying to calibrate.

What I Did

So far The code is:

Importrates = quandl.get("USTREASURY/YIELD", start_date="2019-10-01", end_date="2019-11-30")
Tyields = Importrates.drop(['1 MO', '2 MO', '3 MO', '6 MO', '1 YR', '20 YR'], axis=1)

t = np.array([2.0, 3.0, 5.0, 7.0, 10.0, 30.0])
y = (Tyields)

curve, status = calibrate_ns_ols(t, y, tau0=1) #NS model calibrate

Also tried this:
t = ([2.0, 3.0, 5.0, 7.0, 10.0, 30.0])
y = (Tyields)

As I understand t = maturities and y = yields. Now I`m trying to get the yields from the table, but getting errors :
Mismatching shapes of time and values
or
'list' object has no attribute 'shape' ,

Anyway I know I am doing something wrong.
Can please someone explain or show the code how it can be done from that table for 6 maturities?

thank you in advance!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.