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 Numerical Integration

From Leeroopedia


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

Overview

Concrete tool for numerical integration of functions over finite intervals provided by the RustQuant library.

Description

The integration module provides numerical quadrature using the Tanh-Sinh (double exponential) method. This quadrature scheme transforms the integral over [-1, +1] into an integral over the real line using hyperbolic trigonometric substitutions, making it highly effective for integrands with endpoint singularities.

The core public function is integrate(f, a, b), which computes the definite integral of a function f from a to b. Internally, it performs a linear change of variables to map [a, b] to [-1, +1], then applies the Tanh-Sinh quadrature.

The approximation formula is:

integral from -1 to +1 of f(x) dx is approximately the sum of w_k * f(x_k)

where the abscissae (nodes) are:

x_k = tanh(pi/2 * sinh(kh))

and the weights are:

w_k = (h * pi/2) * cosh(kh) / cosh^2(pi/2 * sinh(kh))

The module uses precomputed arrays of 100 abscissae and 100 weights (sourced from Casio Keisan). Non-finite function values are treated as zero. The internal tanhsinh function iterates over all 100 nodes to compute the weighted sum.

Usage

Use this function whenever you need to numerically evaluate a definite integral over a finite interval. In quantitative finance, numerical integration is essential for option pricing (e.g., Heston model), computing distribution functions, evaluating expected values, and pricing exotic derivatives where closed-form solutions do not exist.

Code Reference

Source Location

Signature

/// Integrates a function from `a` to `b`.
/// Uses the Tanh-Sinh quadrature over [-1, +1]
/// and then transforms to an integral over [a, b].
pub fn integrate<F>(f: F, a: f64, b: f64) -> f64
where
    F: Fn(f64) -> f64;

/// Abscissae: the 100 precomputed nodes for sum evaluation.
pub const ABSCISSAE: [f64; 100];

/// Weights: the 100 precomputed weights for sum evaluation.
pub const WEIGHTS: [f64; 100];

Import

use RustQuant::math::integrate;

I/O Contract

Inputs

Name Type Required Description
f F: Fn(f64) -> f64 Yes The function to integrate.
a f64 Yes The lower bound of integration.
b f64 Yes The upper bound of integration.

Outputs

Name Type Description
result f64 The approximate value of the definite integral of f from a to b.

Usage Examples

use RustQuant::math::integrate;

// Integrate e^(sin(x)) from 0 to 5
fn f(x: f64) -> f64 {
    (x.sin()).exp()
}

let integral = integrate(f, 0.0, 5.0);
// Result: approximately 7.1891

// Using a closure
let result = integrate(|x| x * x, 0.0, 1.0);
// Result: approximately 0.3333 (integral of x^2 from 0 to 1)

// Integrating a Gaussian PDF manually
use std::f64::consts::PI;
let gaussian_integral = integrate(
    |x| (-(x * x) / 2.0).exp() / (2.0 * PI).sqrt(),
    -5.0,
    5.0,
);
// Result: approximately 1.0

Related Pages

Page Connections

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