Rust library for quantitative finance tools.
:dart: I want to hit a stable and legitimate v1.0.0
by the end of 2023, so any and all feedback, suggestions, or contributions are strongly welcomed!
Contact: rustquantcontact@gmail.com
Disclaimer: This is currently a free-time project and not a professional financial software library. Nothing in this library should be taken as financial advice, and I do not recommend you to use it for trading or making financial decisions.
:newspaper: Latest features
- Gradient descent optimizer for functions $f: \mathbb{R}^n \rightarrow \mathbb{R}$.
- Download time series data from Yahoo! Finance.
- Read (write) from (to)
.csv
,.json
, and.parquet
files, using PolarsDataFrames
. - Arithmetic Brownian Motion generator.
- Gamma, exponential, and chi-squared distributions.
- Forward start option pricer (Rubinstein 1990 formula).
- Gap option and cash-or-nothing option pricers (currently adding more binary options).
- Asian option pricer (closed-form solution for continuous geometric average).
- Heston Model option pricer (uses the tanh-sinh quadrature numerical integrator).
- Tanh-sinh (double exponential) quadrature for evaluating integrals.
- Plus other basic numerical integrators (midpoint, trapezoid, Simpson's 3/8).
- Characteristic functions and density functions for common distributions:
- Gaussian, Bernoulli, Binomial, Poisson, Uniform, Chi-Squared, Gamma, and Exponential.
Table of Contents
- Automatic Differentiation
- Option Pricers
- Stochastic Processes and Short Rate Models
- Bonds
- Distributions
- Mathematics
- Helper Functions and Macros
- How-tos
- References
:link: Automatic Differentiation
Currently only gradients can be computed. Suggestions on how to extend the functionality to Hessian matrices are definitely welcome.
- Reverse (Adjoint) Mode
- Implementation via Operator and Function Overloading.
- Useful when number of outputs is smaller than number of inputs.
- i.e for functions $f:\mathbb{R}^n \rightarrow \mathbb{R}^m$, where $m \ll n$
- Forward (Tangent) Mode
- Implementation via Dual Numbers.
- Useful when number of outputs is larger than number of inputs.
- i.e. for functions $f:\mathbb{R}^n \rightarrow \mathbb{R}^m$, where $m \gg n$
:money_with_wings: Option Pricers
-
Closed-form price solutions:
- Heston Model
- Barrier
- European
- Greeks/Sensitivities
- Lookback
- Asian: Continuous Geometric Average
- Forward Start
- Basket
- Rainbow
- American
-
Lattice models:
- Binomial Tree (Cox-Ross-Rubinstein)
The stochastic process generators can be used to price path-dependent options via Monte-Carlo.
- Monte Carlo pricing:
- Lookback
- Asian
- Chooser
- Barrier
:chart_with_upwards_trend: Stochastic Processes and Short Rate Models
The following is a list of stochastic processes that can be generated.
- Brownian Motion
- Arithmetic Brownian Motion
- $dX(t) = \mu dt + \sigma dW(t)$
- Geometric Brownian Motion
- $dX(t) = \mu X(t) dt + \sigma X(t) dW(t)$
- Models: Black-Scholes (1973), Rendleman-Bartter (1980)
- Cox-Ingersoll-Ross (1985)
- $dX(t) = \left[ \theta - \alpha X(t) \right] dt + \sigma \sqrt{r_t} dW(t)$
- Ornstein-Uhlenbeck process
- $dX(t) = \theta \left[ \mu - X(t) \right] dt + \sigma dW(t)$
- Models: Vasicek (1977)
- Ho-Lee (1986)
- $dX(t) = \theta(t) dt + \sigma dW(t)$
- Hull-White (1990)
- $dX(t) = \left[ \theta(t) - \alpha X(t) \right]dt + \sigma dW(t)$
- Extended Vasicek (1990)
- $dX(t) = \left[ \theta(t) - \alpha(t) X(t) \right] dt + \sigma dW(t)$
- Black-Derman-Toy (1990)
- $d\ln[X(t)] = \left[ \theta(t) + \frac{\sigma'(t)}{\sigma(t)}\ln[X(t)] \right]dt + \sigma_t dW(t)$
- $d\ln[X(t)] = \theta(t) dt + \sigma dW(t)$
- Merton's model (1973)
- $dX(t) = adt + \sigma dW^*(t)$
:chart_with_downwards_trend: Bonds
- Prices:
- The Vasicek Model
- The Cox, Ingersoll, and Ross Model
- The Hull–White (One-Factor) Model
- The Rendleman and Bartter Model
- The Ho–Lee Model
- The Black–Derman–Toy Model
- The Black–Karasinski Model
- Duration
- Convexity
:bar_chart: Distributions
Probability density/mass functions, distribution functions, characteristic functions, etc.
- Gaussian
- Bernoulli
- Binomial
- Poisson
- Uniform (discrete & continuous)
- Chi-Squared
- Gamma
- Exponential
:triangular_ruler: Mathematics
- Optimisation:
- Gradient Descent
- Bisection
- Newton
- Secant
- Numerical Integration (needed for Heston model, for example):
- Tanh-Sinh (double exponential) quadrature
- Composite Midpoint Rule
- Composite Trapezoidal Rule
- Composite Simpson's 3/8 Rule
- Risk-Reward Measures (Sharpe, Treynor, Sortino, etc)
- Newton-Raphson
- Standard Normal Distribution (Distribution/Density functions, and generation of variates)
- Interpolation
:handshake: Helper Functions and Macros
A collection of utility functions and macros.
- Plot a vector.
- Write vector to file.
- Cumulative sum of vector.
- Linearly spaced sequence.
-
assert_approx_equal!
:heavy_check_mark: How-tos
I would not recommend using RustQuant within any other libraries for some time, as it will most likely go through many breaking changes as I learn more Rust and settle on a decent structure for the library.
:pray: I would greatly appreciate contributions so it can get to the v1.0.0
mark ASAP.
Download data from Yahoo! Finance:
You can download data from Yahoo! Finance into a Polars DataFrame
.
use *;
use date;
Read/write data:
use *;
Compute gradients:
use *;
Compute integrals:
use *;
Gradient Descent:
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).
use GradientDescent;
// Define the objective function.
Price options:
use *;
Generate stochastic processes:
use *;
:book: References:
- John C. Hull - Options, Futures, and Other Derivatives
- Damiano Brigo & Fabio Mercurio - Interest Rate Models - Theory and Practice (With Smile, Inflation and Credit)
- Paul Glasserman - Monte Carlo Methods in Financial Engineering
- Andreas Griewank & Andrea Walther - Evaluating Derivatives - Principles and Techniques of Algorithmic Differentiation
- Steven E. Shreve - Stochastic Calculus for Finance II: Continuous-Time Models
- Espen Gaarder Haug - Option Pricing Formulas
- Antoine Savine - Modern Computational Finance: AAD and Parallel Simulations