CIR Curve¶
quantflow.rates.cir.CIRCurve
pydantic-model
¶
Bases: YieldCurve
Yield curve derived from the Cox-Ingersoll-Ross short-rate model.
The CIR model describes the short rate as a mean-reverting square-root diffusion:
The model admits a closed-form discount factor; see discount_factor.
Throughout, the auxiliary quantities are:
Fields:
-
ref_date(datetime) -
curve_type(Literal['cir_curve']) -
rate(DecimalNumber) -
kappa(DecimalNumber) -
theta(DecimalNumber) -
sigma(DecimalNumber)
calibrator
¶
Return a CIRCurveCalibration wrapping this curve.
instantaneous_forward_rate
¶
Calculate the instantaneous forward rate for the CIR model.
The forward rate is:
where:
Source code in quantflow/rates/cir.py
discount_factor
¶
Calculate the discount factor using the CIR closed-form solution.
where \(A(\tau)\) and \(B(\tau)\) are the affine_coefficients.
Source code in quantflow/rates/cir.py
affine_coefficients
¶
Return the affine coefficients \(A(\tau)\) and \(B(\tau)\) of the log discount factor.
where
Source code in quantflow/rates/cir.py
jacobian
¶
Analytical Jacobian of discount factors w.r.t. \([r_0, \kappa, \theta, \sigma]\). Returns shape (len(ttm), 4).
Source code in quantflow/rates/cir.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.cir.CIRCurveCalibration
pydantic-model
¶
Bases: YieldCurveCalibration[CIRCurve]
Calibration wrapper for a CIR yield curve.
Fields:
-
yield_curve(Y)
filtered_short_rate
property
¶
Unscented-Kalman-filtered short rate at each observation date.
Populated by [calibrate_historical_rates][quantflow.rates.cir.CIRCurveCalibration.filtered_short_rate.calibrate_historical_rates]; accessing it before a historical fit raises an error.
get_params
¶
set_params
¶
Source code in quantflow/rates/cir.py
get_bounds
¶
calibrate
¶
Fit the CIR curve to continuously compounded rates via least squares.
The Feller condition is enforced by reparametrising \(\sigma\) as
Since CIR requires non-negative rates, any negative input rates are floored to a small positive value before fitting.
| PARAMETER | DESCRIPTION |
|---|---|
ttm
|
Times to maturity in years.
TYPE:
|
rates
|
Continuously compounded rates, same length as ttm.
TYPE:
|
Source code in quantflow/rates/cir.py
calibrate_historical_rates
¶
Fit CIR by maximum likelihood with an unscented Kalman filter.
The short rate \(r_t\) is the latent state of a [CIRStateSpaceModel][quantflow.rates.cir.CIRCurveCalibration.CIRStateSpaceModel]. Its exact transition has a linear conditional mean but a state-dependent conditional variance, so the panel is filtered with the UnscentedKalmanFilter rather than the exact linear filter used for Vasicek.
The negative log-likelihood is minimised over \((\kappa, \theta, \rho, h)\), where \(h\) is the observation noise standard deviation and the Feller condition is enforced by reparametrising \(\sigma = \rho \sqrt{2\kappa\theta}\) with \(\rho \in (0, 1)\). The curve's rate is then set to the final filtered short rate.
| PARAMETER | DESCRIPTION |
|---|---|
ttm
|
Times to maturity in years, shape (n,).
TYPE:
|
rates
|
Continuously compounded rates, shape (T, n) (time by maturity).
TYPE:
|
dt
|
Per-step time increments in years, shape (T-1,); assumed uniform.
TYPE:
|
Source code in quantflow/rates/cir.py
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 | |
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.cir.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:
|