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 B Spline Interpolation

From Leeroopedia


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

Overview

Concrete implementation of B-Spline interpolation provided by the RustQuant library.

Description

The BSplineInterpolator struct implements B-Spline curve evaluation using the Cox-de Boor recursive algorithm. B-Splines are piecewise polynomial curves defined by a set of knots (breakpoints), control points (coefficients), and a degree parameter. They provide smooth interpolation with local control -- modifying one control point only affects the curve in the vicinity of that point.

The struct is generic over two type parameters: IndexType (which must implement InterpolationIndex) and ValueType (which must implement InterpolationValue). This allows the interpolator to work with numeric types (e.g., f64) as well as date/time types (e.g., time::OffsetDateTime), enabling its use for time-indexed financial curves.

Construction: The new() method validates that the number of knots equals control_points.len() + degree + 1 (a fundamental B-Spline requirement), returning a RustQuantError::BSplineInvalidParameters error if violated. The knots are automatically sorted on construction.

Evaluation: The interpolate() method first checks that the query point lies within the valid range [knots[degree], knots[n - degree - 1]], returning RustQuantError::BSplineOutsideOfRange if not. It then computes the B-Spline value by summing control_points[i] * cox_de_boor(point, i, degree) over all control points. The cox_de_boor() method implements the standard recursive Cox-de Boor basis function evaluation with proper handling of zero-width knot spans.

The struct implements the Interpolator trait, providing fit(), range(), add_point(), and interpolate(). The fit() method simply marks the interpolator as fitted. The add_point() method inserts a new knot in sorted order and appends a control point.

Usage

Use B-Spline interpolation when you need smooth, locally controlled curves of arbitrary degree. This is particularly useful for constructing yield curves, volatility surfaces, or any financial term structure where smoothness and local adjustability are important. The support for date-indexed knots makes it directly applicable to time series interpolation in quantitative finance.

Code Reference

Source Location

Signature

pub struct BSplineInterpolator<IndexType, ValueType>
where
    IndexType: InterpolationIndex<DeltaDiv = ValueType> + Send + Sync,
    ValueType: InterpolationValue + Send + Sync,
{
    pub knots: Vec<IndexType>,
    pub control_points: Vec<ValueType>,
    pub degree: usize,
    pub fitted: bool,
}

impl<IndexType, ValueType> BSplineInterpolator<IndexType, ValueType>
where
    IndexType: InterpolationIndex<DeltaDiv = ValueType> + Send + Sync,
    ValueType: InterpolationValue + Send + Sync,
{
    pub fn new(
        knots: Vec<IndexType>,
        control_points: Vec<ValueType>,
        degree: usize,
    ) -> Result<BSplineInterpolator<IndexType, ValueType>, RustQuantError>;
}

impl<IndexType, ValueType> Interpolator<IndexType, ValueType>
    for BSplineInterpolator<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::{BSplineInterpolator, Interpolator};

I/O Contract

Inputs

Name Type Required Description
knots Vec<IndexType> Yes Knot vector defining the breakpoints of the B-Spline; must have length control_points.len() + degree + 1
control_points Vec<ValueType> Yes Control point values (coefficients) for the B-Spline
degree usize Yes Polynomial degree of the B-Spline (e.g., 2 for quadratic, 3 for cubic)

Outputs

Name Type Description
Result Result<BSplineInterpolator, RustQuantError> The constructed interpolator, or an error if parameters are inconsistent
interpolate() Result<ValueType, RustQuantError> Interpolated value at the query point, or an error if the point is outside the valid range

Usage Examples

use RustQuant::math::interpolation::{BSplineInterpolator, Interpolator};

// Define knots and control points for a degree-2 B-Spline
let knots = vec![0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0];
let control_points = vec![-1.0, 2.0, 0.0, -1.0];

// Construct the B-Spline interpolator (7 knots = 4 control points + degree 2 + 1)
let mut interpolator = BSplineInterpolator::new(knots, control_points, 2).unwrap();
let _ = interpolator.fit();

// Interpolate at x = 2.5
let value = interpolator.interpolate(2.5).unwrap();
// value is approximately 1.375

Related Pages

Page Connections

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