Nelson Siegel Curve¶
quantflow.rates.nelson_siegel.NelsonSiegel
pydantic-model
¶
Bases: YieldCurve
Class representing a Nelson-Siegel yield curve
The Nelson-Siegel model is a popular parametric model for fitting the term structure of interest rates. It is defined by the following formula for the instantaneous forward rate:
where \(\tau\) is the time to maturity, \(\beta_1\) is the level parameter, \(\beta_2\) is the slope parameter, \(\beta_3\) is the curvature parameter and \(\lambda\) is the decay factor.
Fields:
-
ref_date(datetime) -
curve_type(Literal['nelson_siegel']) -
beta1(Decimal) -
beta2(Decimal) -
beta3(Decimal) -
lambda_(Decimal)
calibrator
¶
Return a NelsonSiegelCalibration wrapping this curve.
instanteous_forward_rate
¶
Source code in quantflow/rates/nelson_siegel.py
discount_factor
¶
Calculate the discount factor for a given time to maturity.
The discount factor is calculated using the formula:
Source code in quantflow/rates/nelson_siegel.py
jacobian
¶
Analytical Jacobian of discount factors w.r.t. params.
Params order: \([\beta_1, \beta_2, \beta_3, \lambda]\). Shape: (len(ttm), 4).
Source code in quantflow/rates/nelson_siegel.py
calibrate
classmethod
¶
Fit a Nelson-Siegel curve to observed continuously compounded rates.
Uses a profile OLS approach: for each candidate \(\lambda\) the betas are solved exactly via least squares, so only a 1-D scalar minimisation over \(\lambda\) is needed.
Observations whose rates deviate by more than 3 robust standard deviations (MAD-scaled) from the median are excluded before fitting, making the result robust to a small number of bad parity observations.
| PARAMETER | DESCRIPTION |
|---|---|
ttm
|
times to maturity in years (1-D, length >= 3)
TYPE:
|
rates
|
observed continuously compounded rates, same length as ttm
TYPE:
|
lambda_bounds
|
search bounds for the decay parameter \(\lambda\)
TYPE:
|
Source code in quantflow/rates/nelson_siegel.py
continuously_compounded_rate
¶
Calculate the continuously compounded rate for a given time to maturity.
The continuously compounded rate is related to the discount factor by the following formula:
where \(D(\tau)\) is the discount factor for a given time to maturity \(\tau\).
Accepts a scalar float or a float array. Returns a scalar float for scalar input and a numpy float array for array input.
| PARAMETER | DESCRIPTION |
|---|---|
ttm
|
Time to maturity in years
TYPE:
|
Source code in quantflow/rates/yield_curve.py
plot
¶
Plot the continuously compounded rate vs time to maturity.
Requires plotly to be installed.
| PARAMETER | DESCRIPTION |
|---|---|
ttm_max
|
Maximum time to maturity in years
TYPE:
|
n
|
Number of points to evaluate
TYPE:
|
Source code in quantflow/rates/yield_curve.py
register_curve_types
classmethod
¶
Register a yield curve subclass for deserialization.
Source code in quantflow/rates/yield_curve.py
curve_types
classmethod
¶
get_curve_class
classmethod
¶
quantflow.rates.nelson_siegel.NelsonSiegelCalibration
pydantic-model
¶
Bases: YieldCurveCalibration[NelsonSiegel]
Calibration wrapper for a Nelson-Siegel yield curve.
Fields:
-
yield_curve(Y) -
beta_bounds(tuple[float, float]) -
lambda_bounds(tuple[float, float])
lambda_bounds
pydantic-field
¶
Lower and upper bounds for the decay parameter
get_params
¶
set_params
¶
Source code in quantflow/rates/nelson_siegel.py
get_bounds
¶
calibrate
¶
Fit the curve using the fast profile-OLS solver.
Drop times to maturity <= 1 day (if any) before fitting, as these are often dominated by noise and can cause instability in the fit.
| PARAMETER | DESCRIPTION |
|---|---|
ttm
|
Times to maturity in years.
TYPE:
|
target
|
Target discount factors, same length as ttm.
TYPE:
|