Interpolator
Interpolator(method, known_bdays, known_rates, extrapolate=False)
Interpolator class for interest rate interpolation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
method
|
Literal['flat_forward', 'linear']
|
The interpolation method to use. |
required |
known_bdays
|
ArrayLike
|
The known business days sequence. |
required |
known_rates
|
ArrayLike
|
The known interest rates sequence. |
required |
extrapolate
|
bool
|
If True, extrapolates beyond known business days using the last available rate. Defaults to False, returning NaN for out-of-range values. |
False
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If known_bdays and known_rates do not have the same length. |
ValueError
|
If the interpolation method is not recognized |
Note
- This class uses a 252 business days per year convention.
- Instances of this class are immutable. To modify the interpolation settings, create a new instance.
Examples:
>>> from pyield import Interpolator
>>> known_bdays = [30, 60, 90]
>>> known_rates = [0.045, 0.05, 0.055]
Linear interpolation:
Flat forward interpolation:
>>> fforward = Interpolator("flat_forward", known_bdays, known_rates)
>>> fforward(45)
0.04833068080970859
Array interpolation (polars shows 6 decimal places by default):
>>> fforward([15, 45, 75, 100])
shape: (4,)
Series: 'interpolated_rate' [f64]
[
0.045
0.048331
0.052997
null
]
If extrapolation is enabled, the last known rate is used:
>>> fforward_extrap = Interpolator(
... "flat_forward", known_bdays, known_rates, extrapolate=True
... )
>>> print(fforward_extrap(100))
0.055
Source code in pyield/interpolator.py
__call__(bday)
Allows the instance to be called as a function to perform interpolation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bday
|
int
|
Number of business days for which the interest rate is to be calculated. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
float |
float | Series
|
The interest rate interpolated by the specified method for the given number of business days. If the input is out of range and extrapolation is disabled, returns float("nan"). |
Source code in pyield/interpolator.py
__len__()
__repr__()
flat_forward(bday, k)
Performs the interest rate interpolation using the flat forward method.
This method calculates the interpolated interest rate for a given
number of business days (bday) using the flat forward methodology,
based on two known points: the current point (k) and the previous point (j).
Assuming interest rates are in decimal form, the interpolated rate is calculated. Time is measured in years based on a 252-business-day year.
The interpolated rate is given by the formula:
Where the factors used in the formula are defined as:
fⱼ = (1 + rateⱼ)^timeⱼis the compounding factor at pointj.fₖ = (1 + rateₖ)^timeₖis the compounding factor at pointk.fₜ = (time - timeⱼ)/(timeₖ - timeⱼ)is the time factor.
And the variables are defined as:
time = bday/252is the time in years for the interpolated point.bdayis the number of business days for the interpolated point (input to this method).kis the index of the current known point.timeₖ = bdayₖ/252is the time in years of pointk.rateₖis the interest rate (decimal) at pointk.jis the index of the previous known point (k - 1).timeⱼ = bdayⱼ/252is the time in years of pointj.rateⱼis the interest rate (decimal) at pointj.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bday
|
int
|
Number of bus. days for which the rate is to be interpolated. |
required |
k
|
int
|
The index in the known_bdays and known_rates arrays such that
known_bdays[k-1] < bday < known_bdays[k]. This |
required |
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
The interpolated interest rate in decimal form. |
Source code in pyield/interpolator.py
interpolate(bdays)
Interpolates rates for given business day(s).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bdays
|
int | ArrayLike
|
int or ArrayLike - Business day(s) for interpolation |
required |
Returns:
| Type | Description |
|---|---|
float | Series
|
float or pl.Series - Interpolated rate(s) |
Source code in pyield/interpolator.py
linear(bday, k)
Performs the interest rate interpolation using the linear method.
The interpolated rate is given by the formula: y = y1 + (x - x1) * (y2 - y1) / (x2 - x1)
Where: - (x, y) is the point to be interpolated (bday, interpolated_rate). - (x1, y1) is the previous known point (bday_j, rate_j). - (x2, y2) is the next known point (bday_k, rate_k).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bday
|
int
|
Number of bus. days for which the rate is to be interpolated. |
required |
k
|
int
|
The index such that known_bdays[k-1] < bday < known_bdays[k]. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
The interpolated interest rate in decimal form. |