Implementation:Avhz RustQuant Exponential Interpolation
| Knowledge Sources | |
|---|---|
| Domains | Numerical_Methods, Mathematics |
| Last Updated | 2026-02-07 19:00 GMT |
Overview
Concrete implementation of exponential interpolation provided by the RustQuant library.
Description
The ExponentialInterpolator struct implements piecewise exponential interpolation between data points. Unlike linear interpolation which assumes a straight line between points, exponential interpolation assumes the underlying relationship is exponential, making it particularly well-suited for discount factors, survival probabilities, and other quantities that naturally exhibit exponential decay or growth over time.
The interpolation formula used is geometric (multiplicative) interpolation. For a query point X between known points (x_l, y_l) and (x_r, y_r), the interpolated value is computed as:
result = y_r^exp1 * y_l^exp2
where exp1 = (X - x_l) / (x_r - x_l) and exp2 = (x_r - X) / (x_r - x_l). This is equivalent to log-linear interpolation: taking the exponential of a linear interpolation in log-space.
The struct is generic over IndexType (implementing InterpolationIndex) and ValueType (implementing InterpolationValue + Float). The Float bound is required for the powf() operation used in exponential computation. This supports both numeric and date/time index types.
Construction: The new() method validates that xs and ys have equal length, returning RustQuantError::UnequalLength if not. The input pairs are automatically sorted by x-value.
Evaluation: The interpolate() method first checks that the point is within range, returning RustQuantError::OutsideOfRange if not. If the point exactly matches a known x-value (found via binary search), the corresponding y-value is returned directly. Otherwise, the enclosing interval is found using partition_point and the exponential formula is applied.
Usage
Use exponential interpolation when the underlying data exhibits exponential behavior, which is common in financial applications. It is the standard choice for interpolating discount factors along a yield curve, where the relationship between time and discount factor is inherently exponential. It also works well for survival probabilities in credit modeling and for any data where log-linear behavior is expected. The test suite demonstrates its use with both numeric x-values (producing exact results for power functions like 5^x) and date-indexed discount rates.
Code Reference
Source Location
- Repository: RustQuant
- File: crates/RustQuant_math/src/interpolation/exponential_interpolator.rs
- Lines: 1-211
Signature
pub struct ExponentialInterpolator<IndexType, ValueType>
where
IndexType: InterpolationIndex<DeltaDiv = ValueType> + Send + Sync,
ValueType: InterpolationValue + Send + Sync,
{
pub xs: Vec<IndexType>,
pub ys: Vec<ValueType>,
pub fitted: bool,
}
impl<IndexType, ValueType> ExponentialInterpolator<IndexType, ValueType>
where
IndexType: InterpolationIndex<DeltaDiv = ValueType> + Send + Sync,
ValueType: InterpolationValue + Send + Sync,
{
pub fn new(
xs: Vec<IndexType>,
ys: Vec<ValueType>,
) -> Result<ExponentialInterpolator<IndexType, ValueType>, RustQuantError>;
}
impl<IndexType, ValueType> Interpolator<IndexType, ValueType>
for ExponentialInterpolator<IndexType, ValueType>
where
IndexType: InterpolationIndex<DeltaDiv = ValueType> + Send + Sync,
ValueType: InterpolationValue + Float + Send + Sync,
{
fn fit(&mut self) -> Result<(), RustQuantError>;
fn range(&self) -> (IndexType, IndexType);
fn add_point(&mut self, point: (IndexType, ValueType));
fn interpolate(&self, point: IndexType) -> Result<ValueType, RustQuantError>;
}
Import
use RustQuant::math::interpolation::{ExponentialInterpolator, Interpolator};
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| xs | Vec<IndexType> |
Yes | X-axis values (index points); automatically sorted on construction |
| ys | Vec<ValueType> |
Yes | Y-axis values corresponding to each x-point; must be the same length as xs
|
Outputs
| Name | Type | Description |
|---|---|---|
| Result | Result<ExponentialInterpolator, RustQuantError> |
The constructed interpolator, or UnequalLength error
|
| interpolate() | Result<ValueType, RustQuantError> |
Interpolated value at the query point, or OutsideOfRange error
|
Usage Examples
use RustQuant::math::interpolation::{ExponentialInterpolator, Interpolator};
// Data points following y = 5^x
let xs = vec![1.0, 2.0, 3.0, 5.0];
let ys = vec![5.0, 25.0, 125.0, 3125.0];
let interpolator = ExponentialInterpolator::new(xs, ys).unwrap();
// Interpolate at x = 4.0 -> expects 625.0 (since 5^4 = 625)
let value = interpolator.interpolate(4.0).unwrap();
// value is approximately 625.0