Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Avhz RustQuant Linear Interpolation

From Leeroopedia


Knowledge Sources
Domains Numerical_Methods, Mathematics
Last Updated 2026-02-07 19:00 GMT

Overview

Concrete implementation of linear interpolation provided by the RustQuant library.

Description

The LinearInterpolator struct implements standard piecewise linear interpolation between data points. Given a set of known (x, y) pairs, it estimates intermediate values by drawing straight lines between consecutive points. This is the simplest and most commonly used interpolation method.

For a query point X between known points (x_l, y_l) and (x_r, y_r), the interpolated value is:

result = y_l + (y_r - y_l) * (X - x_l) / (x_r - x_l)

The struct is generic over IndexType (implementing InterpolationIndex) and ValueType (implementing InterpolationValue). This allows it to work with numeric types as well as date/time types from the time crate, making it directly usable for time-indexed financial data.

Construction: The new() method validates that xs and ys have equal length, returning RustQuantError::UnequalLength if not. Input pairs are automatically sorted by x-value.

Evaluation: The interpolate() method first checks the query point is within range, returning RustQuantError::OutsideOfRange if not. If the point exactly matches a known x-value (via binary search), the corresponding y-value is returned directly. Otherwise, the enclosing interval is located using partition_point and the linear formula is applied.

The struct implements the Interpolator trait, providing fit(), range(), add_point(), and interpolate(). The add_point() method inserts a new point in sorted order, maintaining the invariant that xs is always sorted.

Usage

Use linear interpolation as the default, general-purpose interpolation method. It is appropriate when simplicity and speed are priorities, when the data is dense enough that linear approximation is adequate, or when no particular functional form is assumed for the underlying data. In quantitative finance, it is commonly used for rate interpolation along yield curves (especially for short tenors or when combined with other smoothing techniques) and for general data lookup tables. The test suite demonstrates usage with both f64 indices and time::Date / time::OffsetDateTime indices.

Code Reference

Source Location

Signature

pub struct LinearInterpolator<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> LinearInterpolator<IndexType, ValueType>
where
    IndexType: InterpolationIndex<DeltaDiv = ValueType> + Send + Sync,
    ValueType: InterpolationValue + Send + Sync,
{
    pub fn new(
        xs: Vec<IndexType>,
        ys: Vec<ValueType>,
    ) -> Result<LinearInterpolator<IndexType, ValueType>, RustQuantError>;
}

impl<IndexType, ValueType> Interpolator<IndexType, ValueType>
    for LinearInterpolator<IndexType, ValueType>
where
    IndexType: InterpolationIndex<DeltaDiv = ValueType> + Send + Sync,
    ValueType: InterpolationValue + 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::{LinearInterpolator, 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<LinearInterpolator, 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::{LinearInterpolator, Interpolator};

// Define data points
let xs = vec![1.0, 2.0, 3.0, 4.0, 5.0];
let ys = vec![1.0, 2.0, 3.0, 4.0, 5.0];

// Construct and fit the interpolator
let mut interpolator = LinearInterpolator::new(xs, ys).unwrap();
let _ = interpolator.fit();

// Interpolate at x = 2.5 -> expects 2.5
let value = interpolator.interpolate(2.5).unwrap();
// value is 2.5

// Also works with date indices:
// let dates = vec![d_1m, d_2m];
// let rates = vec![0.9870, 0.9753];
// let interp = LinearInterpolator::new(dates, rates).unwrap();

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment