CompositeCurve#
- class rateslib.curves.CompositeCurve(curves, id=NoInput.blank, _no_validation=False)#
Bases:
_BaseCurve
A dynamic composition of a sequence of other
_BaseCurve
.Note
Can only composite curves of the same type:
Curve
orLineCurve
. Other curve parameters such asmodifier
,calendar
andconvention
must also match.- Parameters:
Examples
Composite two
LineCurve
s. Here, simulating the effect of adding quarter-end turns to a cubic spline interpolator, which is otherwise difficult to mathematically derive.In [1]: from rateslib.curves import LineCurve, CompositeCurve In [2]: line_curve1 = LineCurve( ...: nodes={ ...: dt(2022, 1, 1): 2.5, ...: dt(2023, 1, 1): 3.5, ...: dt(2024, 1, 1): 3.0, ...: }, ...: t=[dt(2022, 1, 1), dt(2022, 1, 1), dt(2022, 1, 1), dt(2022, 1, 1), ...: dt(2023, 1, 1), ...: dt(2024, 1, 1), dt(2024, 1, 1), dt(2024, 1, 1), dt(2024, 1, 1)], ...: ) ...: In [3]: line_curve2 = LineCurve( ...: nodes={ ...: dt(2022, 1, 1): 0, ...: dt(2022, 3, 31): -0.2, ...: dt(2022, 4, 1): 0, ...: dt(2022, 6, 30): -0.2, ...: dt(2022, 7, 1): 0, ...: dt(2022, 9, 30): -0.2, ...: dt(2022, 10, 1): 0, ...: dt(2022, 12, 31): -0.2, ...: dt(2023, 1, 1): 0, ...: dt(2023, 3, 31): -0.2, ...: dt(2023, 4, 1): 0, ...: dt(2023, 6, 30): -0.2, ...: dt(2023, 7, 1): 0, ...: dt(2023, 9, 30): -0.2, ...: }, ...: interpolation="flat_forward", ...: ) ...: In [4]: curve = CompositeCurve([line_curve1, line_curve2]) In [5]: curve.plot("1d") Out[5]: (<Figure size 640x480 with 1 Axes>, <Axes: >, [<matplotlib.lines.Line2D at 0x115e3c410>])
(
Source code
,png
,hires.png
,pdf
)We can also composite DF based curves by using a fast approximation or an exact match.
In [6]: from rateslib.curves import Curve, CompositeCurve In [7]: curve1 = Curve( ...: nodes={ ...: dt(2022, 1, 1): 1.0, ...: dt(2023, 1, 1): 0.98, ...: dt(2024, 1, 1): 0.965, ...: dt(2025, 1, 1): 0.955 ...: }, ...: t=[dt(2023, 1, 1), dt(2023, 1, 1), dt(2023, 1, 1), dt(2023, 1, 1), ...: dt(2024, 1, 1), ...: dt(2025, 1, 1), dt(2025, 1, 1), dt(2025, 1, 1), dt(2025, 1, 1)], ...: ) ...: In [8]: curve2 =Curve( ...: nodes={ ...: dt(2022, 1, 1): 1.0, ...: dt(2022, 6, 30): 1.0, ...: dt(2022, 7, 1): 0.999992, ...: dt(2022, 12, 31): 0.999992, ...: dt(2023, 1, 1): 0.999984, ...: dt(2023, 6, 30): 0.999984, ...: dt(2023, 7, 1): 0.999976, ...: dt(2023, 12, 31): 0.999976, ...: dt(2024, 1, 1): 0.999968, ...: dt(2024, 6, 30): 0.999968, ...: dt(2024, 7, 1): 0.999960, ...: dt(2025, 1, 1): 0.999960, ...: }, ...: ) ...: In [9]: curve = CompositeCurve([curve1, curve2]) In [10]: curve.plot("1D", comparators=[curve1, curve2], labels=["Composite", "C1", "C2"]) Out[10]: (<Figure size 640x480 with 1 Axes>, <Axes: >, [<matplotlib.lines.Line2D at 0x11670ca50>, <matplotlib.lines.Line2D at 0x11670cb90>, <matplotlib.lines.Line2D at 0x11670ccd0>])
(
Source code
,png
,hires.png
,pdf
)Attributes Summary
Int in {0,1,2} describing the AD order associated with the Curve.
A str identifier to name the Curve used in
Solver
mappings.An instance of
_CurveInterpolator
.An instance of
_CurveMeta
.An instance of
_CurveNodes
.Methods Summary
copy
()Create an identical copy of the curve object.
index_value
(date, index_lag[, interpolation])Calculate the accrued value of the index from the
index_base
.plot
(tenor[, right, left, comparators, ...])Plot given forward tenor rates from the curve.
plot_index
([right, left, comparators, ...])Plot given index values on a Curve.
rate
(effective[, termination, modifier, ...])Calculate the rate on the Curve using DFs.
roll
(tenor[, id])Create a
RolledCurve
: translating the rate space of Self in time.shift
(spread[, id])Create a
ShiftedCurve
: moving Self vertically in rate space.translate
(start[, id])Create a
TranslatedCurve
: maintaining an identical rate space, but moving the initial node date forwards in time.Attributes Documentation
- ad#
Int in {0,1,2} describing the AD order associated with the Curve.
- interpolator#
An instance of
_CurveInterpolator
.
- meta#
- nodes#
An instance of
_CurveNodes
.
Methods Documentation
- copy()#
Create an identical copy of the curve object.
- Return type:
Self
- index_value(date, index_lag, interpolation='curve')#
Calculate the accrued value of the index from the
index_base
.This method will raise if performed on a ‘values’ type Curve.
- Parameters:
date (datetime) – The date for which the index value will be returned.
index_lag (int) – The number of months by which to lag the index when determining the value.
interpolation (str in {"curve", "monthly", "daily"}) – The method for returning the index value. Monthly returns the index value for the start of the month and daily returns a value based on the interpolation between nodes (which is recommended “linear_index) for
InflationCurve
.
- Return type:
Notes
The interpolation methods function as follows:
“curve”: will raise if the requested
index_lag
does not match the lag attributed to the Curve. In the case theindex_lag
matches, then the index value for any date is derived via the implied interpolation for the discount factors of the Curve.\[I_v(m) = \frac{I_b}{v(m)}\]“monthly”: For any date, m, uses the “curve” method having adjusted m in two ways. Firstly it deducts a number of months equal to \(L - L_c\), where L is the given
index_lag
and \(L_c\) is the index lag of the Curve. And the day of the month is set to 1.\[\begin{split}&I^{monthly}_v(m) = I_v(m_adj) \\ &\text{where,} \\ &m_adj = Date(Year(m), Month(m) - L + L_c, 1) \\\end{split}\]“daily”: For any date, m, with a given
index_lag
performs calendar day interpolation on surrounding “monthly” values.\[\begin{split}&I^{daily}_v(m) = I^{monthly}_v(m) + \frac{Day(m) - 1}{n} \left ( I^{monthly}_v(m_+) - I^{monthly}_v(m) \right ) \\ &\text{where,} \\ &m_+ = \text{Any date in the month following, }m &n = \text{Calendar days in, } Month(m)\end{split}\]
Examples
The SWESTR rate, for reference value date 6th Sep 2021, was published as 2.375% and the RFR index for that date was 100.73350964. Below we calculate the value that was published for the RFR index on 7th Sep 2021 by the Riksbank.
In [1]: index_curve = Curve( ...: nodes={ ...: dt(2021, 9, 6): 1.0, ...: dt(2021, 9, 7): 1 / (1 + 2.375/36000) ...: }, ...: index_base=100.73350964, ...: convention="Act360", ...: index_lag=0, ...: ) ...: In [2]: index_curve.rate(dt(2021, 9, 6), "1d") Out[2]: 2.3750000000015703 In [3]: index_curve.index_value(dt(2021, 9, 7), 0) Out[3]: 100.7401552534832
- plot(tenor, right=NoInput.blank, left=NoInput.blank, comparators=NoInput.blank, difference=False, labels=NoInput.blank)#
Plot given forward tenor rates from the curve. See notes.
- Parameters:
tenor (str) – The tenor of the forward rates to plot, e.g. “1D”, “3M”.
right (datetime or str, optional) – The right bound of the graph. If given as str should be a tenor format defining a point measured from the initial node date of the curve. Defaults to the final node of the curve minus the
tenor
.left (datetime or str, optional) – The left bound of the graph. If given as str should be a tenor format defining a point measured from the initial node date of the curve. Defaults to the initial node of the curve.
comparators (list[Curve]) – A list of curves which to include on the same plot as comparators.
difference (bool) – Whether to plot as comparator minus base curve or outright curve levels in plot. Default is False.
labels (list[str]) – A list of strings associated with the plot and comparators. Must be same length as number of plots.
- Returns:
(fig, ax, line)
- Return type:
Matplotlib.Figure, Matplotplib.Axes, Matplotlib.Lines2D
Notes
This function plots single-period, simple interest curve rates, which are defined as:
\[1 + r d = \frac{v_{start}}{v_{end}}\]where d is the day count fraction determined using the
convention
associated with the Curve.This function does not plot swap rates, which is impossible since the Curve object contains no information regarding the parameters of the ‘swap’ (e.g. its frequency or its convention etc.). If
tenors
longer than one year are sought results may start to deviate from those one might expect. See Issue 246.
- plot_index(right=NoInput.blank, left=NoInput.blank, comparators=NoInput.blank, difference=False, labels=NoInput.blank, interpolation='curve')#
Plot given index values on a Curve.
- Parameters:
right (datetime or str, optional) – The right bound of the graph. If given as str should be a tenor format defining a point measured from the initial node date of the curve. Defaults to the final node of the curve minus the
tenor
.left (datetime or str, optional) – The left bound of the graph. If given as str should be a tenor format defining a point measured from the initial node date of the curve. Defaults to the initial node of the curve.
comparators (list[Curve]) – A list of curves which to include on the same plot as comparators.
difference (bool) – Whether to plot as comparator minus base curve or outright curve levels in plot. Default is False.
labels (list[str]) – A list of strings associated with the plot and comparators. Must be same length as number of plots.
interpolation (str in {"curve", "daily", "monthly"}) – The type of index interpolation method to use.
- Returns:
(fig, ax, line)
- Return type:
Matplotlib.Figure, Matplotplib.Axes, Matplotlib.Lines2D
- rate(effective, termination=NoInput.blank, modifier=NoInput.inherit, float_spread=NoInput.blank, spread_compound_method=NoInput.blank)#
Calculate the rate on the Curve using DFs.
If rates are sought for dates prior to the initial node of the curve None will be returned.
- Parameters:
effective (datetime) – The start date of the period for which to calculate the rate.
termination (datetime or str) – The end date of the period for which to calculate the rate.
modifier (str, optional) – The day rule if determining the termination from tenor. If False is determined from the Curve modifier.
float_spread (float, optional) – A float spread can be added to the rate in certain cases.
spread_compound_method (str in {"none_simple", "isda_compounding"}) – The method if adding a float spread. If “none_simple” is used this results in an exact calculation. If “isda_compounding” or “isda_flat_compounding” is used this results in an approximation.
- Return type:
Notes
Calculating rates from a curve implies that the conventions attached to the specific index, e.g. USD SOFR, or GBP SONIA, are applicable and these should be set at initialisation of the
Curve
. Thus, the convention used to calculate therate
is taken from theCurve
from whichrate
is called.modifier
is only used if a tenor is given as the termination.Major indexes, such as legacy IBORs, and modern RFRs typically use a
convention
which is either “Act365F” or “Act360”. These conventions do not need additional parameters, such as the termination of a leg, the frequency or a leg or whether it is a stub to calculate a DCF.Adding Floating Spreads
An optimised method for adding floating spreads to a curve rate is provided. This is quite restrictive and mainly used internally to facilitate other parts of the library.
When
spread_compound_method
is “none_simple” the spread is a simple linear addition.When using “isda_compounding” or “isda_flat_compounding” the curve is assumed to be comprised of RFR rates and an approximation is used to derive to total rate.
Examples
In [1]: curve_act365f = Curve( ...: nodes={ ...: dt(2022, 1, 1): 1.0, ...: dt(2022, 2, 1): 0.98, ...: dt(2022, 3, 1): 0.978, ...: }, ...: convention='Act365F' ...: ) ...: In [2]: curve_act365f.rate(dt(2022, 2, 1), dt(2022, 3, 1)) Out[2]: 2.6657902424774402
Using a different convention will result in a different rate:
In [3]: curve_act360 = Curve( ...: nodes={ ...: dt(2022, 1, 1): 1.0, ...: dt(2022, 2, 1): 0.98, ...: dt(2022, 3, 1): 0.978, ...: }, ...: convention='Act360' ...: ) ...: In [4]: curve_act360.rate(dt(2022, 2, 1), dt(2022, 3, 1)) Out[4]: 2.6292725679229547
- roll(tenor, id=NoInput.blank)#
Create a
RolledCurve
: translating the rate space of Self in time.For examples see the documentation for
RolledCurve
.- Parameters:
tenor (datetime, str or int) – The measure of time by which to translate the curve through time.
id (str, optional) – Set the id of the returned curve.
- Return type:
- shift(spread, id=NoInput.blank)#
Create a
ShiftedCurve
: moving Self vertically in rate space.For examples see the documentation for
ShiftedCurve
.- Parameters:
- Return type:
- translate(start, id=NoInput.blank)#
Create a
TranslatedCurve
: maintaining an identical rate space, but moving the initial node date forwards in time.For examples see the documentation for
TranslatedCurve
.- Parameters:
start (datetime) – The new initial node date for the curve. Must be after the original initial node date.
id (str, optional) – Set the id of the returned curve.
- Return type: