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
|
Series | list[int]
|
The known business days sequence. |
required |
known_rates
|
Series | list[float]
|
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 example:
Flat forward interpolation example:
>>> fforward = Interpolator("flat_forward", known_bdays, known_rates)
>>> fforward(45)
0.04833068080970859
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
|
The interest rate interpolated by the specified method for the given number of business days. |
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/252
is the time in years for the interpolated point.bday
is the number of business days for the interpolated point (input to this method).k
is the index of the current known point.timeₖ = bdayₖ/252
is the time in years of pointk
.rateₖ
is the interest rate (decimal) at pointk
.j
is the index of the previous known point (k - 1
).timeⱼ = bdayⱼ/252
is 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(bday)
Finds the appropriate interpolation point and returns the interest rate interpolated by the specified method from that point.
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
|
The interest rate interpolated by the specified method for the given number of business days. |