RustQuant_math/lib.rs
1// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2// MATHEMATICS MODULE
3// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4
5//! Mathematics related items.
6//!
7//! ### Optimization and Root Finding
8//!
9//! - [x] Gradient Descent
10//! - [x] Newton-Raphson
11//!
12//! Note: the reason you need to specify the lifetimes and use the type `Variable` is because the gradient descent optimiser uses the `RustQuant::autodiff` module to compute the gradients. This is a slight inconvenience, but the speed-up is enormous when working with functions with many inputs (when compared with using finite-difference quotients).
13//!
14//! ```rust
15//! use RustQuant::math::*;
16//! use RustQuant::autodiff::*;
17//!
18//! // Define the objective function.
19//! fn himmelblau<'v>(variables: &[Variable<'v>]) -> Variable<'v> {
20//! let x = variables[0];
21//! let y = variables[1];
22//!
23//! ((x.powf(2.0) + y - 11.0).powf(2.0) + (x + y.powf(2.0) - 7.0).powf(2.0))
24//! }
25//!
26//! // Create a new GradientDescent object with:
27//! // - Step size: 0.005
28//! // - Iterations: 10000
29//! // - Tolerance: sqrt(machine epsilon)
30//! let gd = GradientDescent::new(0.005, 10000, Some(std::f64::EPSILON.sqrt()));
31//!
32//! // Perform the optimisation with:
33//! // - Initial guess (10.0, 10.0),
34//! // - Verbose output.
35//! let result = gd.optimize(&himmelblau, &vec![10.0, 10.0], true);
36//!
37//! // Print the result.
38//! println!("{:?}", result.minimizer);
39//! ```
40//!
41//! ### Integration
42//!
43//! - Numerical Integration (needed for Heston model, for example):
44//! - [x] Tanh-Sinh (double exponential) quadrature
45//!
46//! ```rust
47//! use RustQuant::math::*;
48//!
49//! // Define a function to integrate: e^(sin(x))
50//! fn f(x: f64) -> f64 {
51//! (x.sin()).exp()
52//! }
53//!
54//! // Integrate from 0 to 5.
55//! let integral = integrate(f, 0.0, 5.0);
56//!
57//! // ~ 7.18911925
58//! println!("Integral = {}", integral);
59//! ```
60//!
61//! ### Risk-Reward Metrics
62//!
63//! - [x] Risk-Reward Measures (Sharpe, Treynor, Sortino, etc)
64
65/// Statistical distributions.
66pub mod distributions;
67pub use distributions::*;
68
69/// Numerical integration routines.
70/// The primary (useful) integrator is the Tanh-Sinh (double exponential) implementation.
71pub mod integration;
72pub use integration::*;
73
74/// Numerical optimization and root-finding routines.
75pub mod optimization;
76pub use optimization::*;
77
78/// Fast fourier transform.
79pub mod fft;
80pub use fft::*;
81
82/// Interpolation routines.
83pub mod interpolation;
84pub use interpolation::*;
85
86/// Simple risk/reward measures.
87pub mod risk_reward;
88pub use risk_reward::*;
89
90/// Root-finding routines.
91pub mod rootfinding;
92pub use rootfinding::*;
93
94/// Sequences of numbers and associated functions.
95pub mod sequences;
96pub use sequences::*;
97
98/// Statistic trait.
99pub mod statistic;
100pub use statistic::*;