packages

interpolate::smooth::spline

The cubic smoothing spline: the g minimizing sum w_i (y_i - g(x_i))^2 + lam * integral g''^2, by Reinsch's algorithm (1967): with Q the second-difference and R the tridiagonal Gram matrices of the natural cubic spline, the second derivatives solve the pentadiagonal SPD system (R + lam Q^T W^-1 Q) gamma = Q^T y, and g = y - lam W^-1 Q gamma. Banded Cholesky, O(n). Without lam, generalized cross-validation (Craven and Wahba 1979) picks it; the trace of the hat matrix comes from the band of the inverse (Hutchinson and de Hoog 1985), O(n) per trial.

Functions

make_smoothing_splineSource

pub fn make_smoothing_spline<X: AsVector<f64>, Y: AsVector<f64>>(x: X, y: Y, lam: f64? = none, w: Vector<f64>? = none) throws (InterpError) -> PPoly

The cubic smoothing spline of (x, y) with weights w (default all

  1. and penalty lam on integral g''^2, a natural cubic PPoly with breakpoints at x. lam near 0 interpolates; a huge lam gives the weighted least-squares line. When lam is none, the value minimizing the GCV score n RSS / tr(I - A)^2 is found by a log-spaced scan and golden-section refinement (about 60 O(n) solves). x must rise strictly; at least 3 points.
nabi
use interpolate::Curve;
use core::array::Vector;

let x: Vector<f64> = [0, 1, 2, 3, 4];
let y: Vector<f64> = [0, 1, 2, 3, 4];                 // a line is not smoothed
let s = try interpolate::make_smoothing_spline(x, y, 10.0);
assert((s.at(2.5) - 2.5).abs() < 1e-12);
let g = try interpolate::make_smoothing_spline(x, [0.1, 0.9, 2.2, 2.8, 4.1]);
assert((g.at(2.0) - 2.0).abs() < 0.2);