Skip to main content

Module solvers

Module solvers 

Source
Expand description

Shared 1-D root-finding for the whole library, one algorithm per file.

Every iterative solve in the pricers routes through here — the BAW critical-exercise boundary (Newton-Raphson), implied volatility (safeguarded Newton), the inverse normal CDF polish (Halley) — so the numerical machinery lives in one maintainable place. Companion modules: fd_solvers for the finite- difference linear kernels and optimization for multi-dimensional model calibration.

Two ways to call a solver:

Direct, when the method is fixed at the call site — Solver1d carries the convergence settings and each method is a call on it:

use rustyqlib::core::solvers::Solver1d;
let root = Solver1d::default()
    .newton_raphson(|x| x * x - 2.0, |x| 2.0 * x, 1.0);
assert!(root.converged && (root.x - 2.0_f64.sqrt()).abs() < 1e-12);

Pluggable, when the method should be swappable — describe the problem once and pick the algorithm with Method; derivatives the problem does not supply are replaced by central finite differences, so every method runs on every problem (bracketed methods do require a bracket):

use rustyqlib::core::solvers::{Method, Problem, Solver1d};
let f = |x: f64| x * x - 2.0;
let problem = Problem::new(&f, 1.0).with_bracket(0.0, 2.0);
for method in [
    Method::Bisection,
    Method::NewtonRaphson,
    Method::Secant,
    Method::Halley,
    Method::NewtonSafeguarded,
] {
    let root = Solver1d::default().solve(method, &problem).unwrap();
    assert!((root.x - 2.0_f64.sqrt()).abs() < 1e-9, "{method:?}");
}

Convergence is on the residual: a solve is converged when |f(x)| <= tol. The bracketed methods additionally stop when the bracket collapses to machine width. Non-bracketed methods never error: they return a Root whose converged flag says whether the tolerance was met, leaving the retry/fallback policy to the caller.

Re-exports§

pub use solver_1d::Method;
pub use solver_1d::Problem;
pub use solver_1d::Root;
pub use solver_1d::Solver1d;

Modules§

bisection
Bisection: derivative-free and unconditionally convergent on a sign-changing bracket, at a linear rate.
halley
Halley’s method: cubic convergence using the first and second derivatives. Used to polish the inverse normal CDF (inv_norm_cdf).
newton_raphson
Newton-Raphson: quadratic convergence near the root, given an analytic derivative. Used by the Barone-Adesi-Whaley critical-boundary solve.
newton_safeguarded
Newton safeguarded by a bisection bracket: globally convergent where raw Newton diverges (flat tails, distant starts). Used by the implied volatility solver.
secant
Secant method: Newton’s convergence class without a derivative, approximating the slope from the last two iterates.
solver_1d
The shared solver types and the pluggable method dispatch.