Implementation:Avhz RustQuant Interpolator Trait
| Knowledge Sources | |
|---|---|
| Domains | Numerical_Methods, Mathematics |
| Last Updated | 2026-02-07 19:00 GMT |
Overview
Concrete trait definitions and type infrastructure for interpolation provided by the RustQuant library.
Description
The interpolation module (mod.rs) defines the core traits and type machinery that all interpolation implementations in RustQuant are built upon. It establishes three key traits and provides blanket implementations for a wide range of numeric and date/time types.
InterpolationValue Trait
InterpolationValue defines the requirements for types that can serve as interpolated values (the y-axis). It requires num::Num + AddAssign + Debug + Copy + Clone + Sized. A blanket implementation is provided, so any type satisfying these bounds automatically implements the trait. This covers all standard numeric types (f32, f64, integers, rust_decimal::Decimal).
InterpolationIndex Trait
InterpolationIndex defines the requirements for types that can serve as index values (the x-axis). It requires Sub<Self> + PartialOrd + Copy + Clone + Sized + Display and introduces two associated types:
Delta-- the type of the difference between two index values (e.g.,time::Durationfor date types).DeltaDiv-- the type produced when dividing two deltas (must implementInterpolationValue).
This design elegantly handles the case where subtracting two dates produces a Duration, and dividing two durations produces an f64. Implementations are provided via the impl_interpolation_index! macro for:
- All signed integer types (
i8throughi128,isize) - All unsigned integer types (
u8throughu128,usize) - Floating point types (
f32,f64) - Date/time types:
time::Date,time::Time,time::OffsetDateTime,time::PrimitiveDateTime(all withDelta = time::Duration,DeltaDiv = f64) rust_decimal::Decimal
Interpolator Trait
Interpolator<IndexType, ValueType> is the main trait implemented by all interpolation models. It requires Send + Sync (making interpolators safe to share across threads) and defines four methods:
fit(&mut self) -> Result<(), RustQuantError>-- fit the interpolator to its data.interpolate(&self, point: IndexType) -> Result<ValueType, RustQuantError>-- compute the interpolated value at a given point.range(&self) -> (IndexType, IndexType)-- return the valid interpolation range.add_point(&mut self, point: (IndexType, ValueType))-- add a new data point to the interpolator.
The module also re-exports the three concrete interpolator implementations: LinearInterpolator, ExponentialInterpolator, and BSplineInterpolator.
Usage
Use the Interpolator trait when writing generic code that should work with any interpolation method. The trait-based design allows algorithms to accept dyn Interpolator or be generic over T: Interpolator, enabling runtime or compile-time selection of the interpolation strategy. The InterpolationIndex and InterpolationValue traits ensure type safety while supporting a broad range of numeric and temporal index types commonly used in quantitative finance.
Code Reference
Source Location
- Repository: RustQuant
- File: crates/RustQuant_math/src/interpolation/mod.rs
- Lines: 1-114
Signature
pub trait InterpolationValue:
num::Num + AddAssign + std::fmt::Debug + Copy + Clone + Sized
{
}
pub trait InterpolationIndex:
Sub<Self, Output = Self::Delta> + PartialOrd + Copy + Clone + Sized + std::fmt::Display
{
type Delta: Div<Self::Delta, Output = Self::DeltaDiv>
+ Mul<Self::DeltaDiv, Output = Self::Delta>;
type DeltaDiv: InterpolationValue;
}
pub trait Interpolator<IndexType, ValueType>: Send + Sync
where
IndexType: InterpolationIndex,
ValueType: InterpolationValue,
{
fn fit(&mut self) -> Result<(), RustQuantError>;
fn interpolate(&self, point: IndexType) -> Result<ValueType, RustQuantError>;
fn range(&self) -> (IndexType, IndexType);
fn add_point(&mut self, point: (IndexType, ValueType));
}
Import
use RustQuant::math::interpolation::{
Interpolator, InterpolationIndex, InterpolationValue,
};
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| IndexType | Generic type parameter | Yes | The x-axis type; must implement InterpolationIndex (e.g., f64, time::Date)
|
| ValueType | Generic type parameter | Yes | The y-axis type; must implement InterpolationValue (e.g., f64)
|
| point (for interpolate) | IndexType |
Yes | The x-value at which to evaluate the interpolation |
| point (for add_point) | (IndexType, ValueType) |
Yes | A new data point to add to the interpolator |
Outputs
| Name | Type | Description |
|---|---|---|
| fit() | Result<(), RustQuantError> |
Success or an error if fitting fails (e.g., unequal input lengths) |
| interpolate() | Result<ValueType, RustQuantError> |
The interpolated value at the query point, or an error if unfitted or out of range |
| range() | (IndexType, IndexType) |
The minimum and maximum x-values defining the valid interpolation domain |
Usage Examples
use RustQuant::math::interpolation::{
LinearInterpolator, ExponentialInterpolator, BSplineInterpolator, Interpolator,
};
// All interpolators implement the Interpolator trait.
// Example with LinearInterpolator:
let xs = vec![1.0, 2.0, 3.0, 4.0, 5.0];
let ys = vec![1.0, 4.0, 9.0, 16.0, 25.0];
let mut interp = LinearInterpolator::new(xs, ys).unwrap();
interp.fit().unwrap();
let value = interp.interpolate(2.5).unwrap();
let (lo, hi) = interp.range(); // (1.0, 5.0)
// Add a new data point dynamically
interp.add_point((6.0, 36.0));
// Works with date indices too:
// use time::macros::date;
// let dates = vec![date!(2024-01-01), date!(2024-07-01)];
// let rates = vec![0.05, 0.055];
// let interp = LinearInterpolator::new(dates, rates).unwrap();