Nelson Siegel Curve¶
quantflow.rates.nelson_siegel.NelsonSiegelCurve
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_curve']) -
beta1(Decimal) -
beta2(Decimal) -
beta3(Decimal) -
lambda_(Decimal)
calibrator
¶
Return a NelsonSiegelCalibration wrapping this curve.
instantaneous_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
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
rates
¶
Calculate zero rates compounded at the given frequency.
The continuously compounded rate \(r_c(\tau)\) is converted to a rate compounded \(m\) times per year via:
When frequency=0 the result is continuously compounded (same as
continuously_compounded_rate).
| PARAMETER | DESCRIPTION |
|---|---|
ttm
|
Time to maturity in years
TYPE:
|
frequency
|
Compounding periods per year (e.g. 2 for semi-annual). Pass 0 for continuously compounded.
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.
The registry key is the curve_type discriminator value rather than
the class name, so the two can be named independently.
Source code in quantflow/rates/yield_curve.py
curve_types
classmethod
¶
get_curve_class
classmethod
¶
quantflow.rates.nelson_siegel.NelsonSiegelCalibration
pydantic-model
¶
Bases: YieldCurveCalibration[NelsonSiegelCurve]
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.
Drops times to maturity below 1 day, which are often dominated by noise.
| PARAMETER | DESCRIPTION |
|---|---|
ttm
|
Times to maturity in years.
TYPE:
|
rates
|
Continuously compounded rates, same length as ttm.
TYPE:
|
Source code in quantflow/rates/nelson_siegel.py
prepare
¶
Hook called before optimisation with the observation times to maturity.
By default it does nothing. Curves whose parameters depend on the observation grid, such as interpolated curves, use it to seed their nodes.
| PARAMETER | DESCRIPTION |
|---|---|
ttm
|
Observation times to maturity in years
TYPE:
|
Source code in quantflow/rates/calibration.py
calibrate_df
¶
Fit the yield curve to target discount factors.
Converts discount factors to continuously compounded rates then calls calibrate.
| PARAMETER | DESCRIPTION |
|---|---|
ttm
|
Times to maturity in years.
TYPE:
|
target
|
Target discount factors, same length as ttm.
TYPE:
|
Source code in quantflow/rates/calibration.py
calibrate_historical_rates_dataframe
¶
Fit the yield curve from a historical panel of rates.
Tenor column labels are parsed into times to maturity, per-step
time increments are inferred from the DatetimeIndex (irregular
spacing supported), and rates are converted to continuously
compounded if a finite frequency is supplied. The actual fit
is delegated to [calibrate_historical_rates][quantflow.rates.nelson_siegel.calibrate_historical_rates],
which subclasses override.
| PARAMETER | DESCRIPTION |
|---|---|
rates
|
Historical zero rates with a DatetimeIndex and tenor column labels parsed by [ccy.Period][ccy.dates.period.Period] (e.g.
TYPE:
|
frequency
|
Compounding periods per year of the input rates.
TYPE:
|
Source code in quantflow/rates/calibration.py
calibrate_historical_rates
¶
Model-specific hook for historical rate calibration.
Default implementation raises NotImplementedError. Subclasses with a stochastic short-rate dynamic override this method.
| PARAMETER | DESCRIPTION |
|---|---|
ttm
|
Times to maturity in years.
TYPE:
|
rates
|
Continuously compounded rates, same shape as ttm.
TYPE:
|
dt
|
Time increments between observations, same length as rates.
TYPE:
|