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 Newton Raphson

From Leeroopedia


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

Overview

Concrete implementation of the Newton-Raphson root-finding algorithm provided by the RustQuant library.

Description

The NewtonRaphson struct implements the Newton-Raphson method, a classical root-finding algorithm that uses both the function value and its derivative to achieve quadratic convergence near the root. Unlike the Bisection and Brent solvers in this library, Newton-Raphson requires the caller to supply an explicit derivative function.

The implementation is a safe Newton-Raphson variant that maintains a bracketing interval and falls back to bisection when the Newton step would move outside the bracket or is not converging fast enough. Specifically, in solve_impl(), the search is first oriented so that f(xl) < 0. At each iteration, three checks are performed: (1) whether the Newton step lands outside the bracket [xl, xh], (2) whether the Newton step lands outside the bracket from the other direction, and (3) whether the current step is not decreasing fast enough (i.e., |2 * f(root)| > |dxold * f'(root)|). If any of these conditions hold, the algorithm falls back to a bisection step. Otherwise, it takes a standard Newton step x -= f(x)/f'(x). The loop terminates when |dx| < accuracy.

The solve() method handles the bracket-search phase identically to the Bisection and Brent solvers: starting from the guess, it expands the interval by a growth factor of 1.6 with a flip-flop alternation until a sign change is found. Once bracketed, it delegates to solve_impl().

The struct is generic over two function types: F: Fn(f64) -> f64 for the objective function and G: Fn(f64) -> f64 for its derivative.

Usage

Use Newton-Raphson when the derivative of the objective function is readily available and you want the fastest convergence. The safe-guarded implementation means it will still converge even when the Newton step would otherwise diverge, making it suitable for production use in quantitative finance applications such as implied volatility computation where the vega (derivative of price with respect to volatility) is known analytically.

Code Reference

Source Location

Signature

pub struct NewtonRaphson<F, G>
where
    F: Fn(f64) -> f64,
    G: Fn(f64) -> f64,
{
    function: F,
    derivative: G,
    guess: f64,
    data: RootfinderData,
}

impl<F, G> NewtonRaphson<F, G>
where
    F: Fn(f64) -> f64,
    G: Fn(f64) -> f64,
{
    pub fn new(function: F, derivative: G, guess: f64, data: RootfinderData) -> Self;
}

impl<F, G> Rootfinder<F> for NewtonRaphson<F, G>
where
    F: Fn(f64) -> f64,
    G: Fn(f64) -> f64,
{
    fn value(&self, x: f64) -> f64;
    fn derivative(&self, x: f64) -> f64;
    fn solve_impl(&mut self) -> f64;
    fn solve(&mut self) -> f64;
}

Import

use RustQuant::math::rootfinding::{NewtonRaphson, RootfinderData, Rootfinder};

I/O Contract

Inputs

Name Type Required Description
function F: Fn(f64) -> f64 Yes The objective function whose root is sought
derivative G: Fn(f64) -> f64 Yes The derivative of the objective function
guess f64 Yes Initial guess for the root location
data RootfinderData Yes Configuration containing accuracy, step size, bounds, and interval enforcement flag

Outputs

Name Type Description
root f64 The computed root of the objective function, or 0.0 if maximum iterations exceeded

Usage Examples

use RustQuant::math::rootfinding::{NewtonRaphson, RootfinderData, Rootfinder};
use std::f64::consts::SQRT_2;

// Define the objective function: f(x) = x^2 - 2
let f = |x: f64| x.powi(2) - 2.0;

// Define the derivative: f'(x) = 2x
let df = |x: f64| 2.0 * x;

// Create root-finder configuration:
//   accuracy = 1e-15, step_size = 1e-5,
//   lower_bound = 0.0, upper_bound = 2.0,
//   interval_enforced = true
let data = RootfinderData::new(1e-15, 1e-5, 0.0, 2.0, true);

// Construct the Newton-Raphson solver with initial guess = 1.0
let mut solver = NewtonRaphson::new(f, df, 1.0, data);
let root = solver.solve();

// root is approximately sqrt(2)
assert!((root - SQRT_2).abs() < 1e-15);

Related Pages

Page Connections

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