Rust library for quantitative finance tools.
π― 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.
- 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.
- Automatic Differentiation
- Option Pricers
- Stochastic Processes and Short Rate Models
- Bonds
- Distributions
- Mathematics
- Helper Functions and Macros
- How-tos
- References
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$
- i.e for functions
- 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$
- i.e. for functions
-
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 #:
- Lookback
- Asian
- Chooser
- Barrier
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)$
- 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
Probability density/mass functions, distribution functions, characteristic functions, etc.
- Gaussian
- Bernoulli
- Binomial
- Poisson
- Uniform (discrete & continuous)
- Chi-Squared
- Gamma
- Exponential
- 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
A collection of utility functions and macros.
- Plot a vector.
- Write vector to file.
- Cumulative sum of vector.
- Linearly spaced sequence.
-
assert_approx_equal!
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.
π I would greatly appreciate contributions so it can get to the v1.0.0
mark ASAP.
You can download data from Yahoo! Finance into a Polars DataFrame
.
use RustQuant::data::*;
use time::macros::date;
fn main() {
// New YahooFinanceData instance.
// By default, date range is: 1970-01-01 to present.
let mut yfd = YahooFinanceData::new("AAPL".to_string());
// Can specify custom dates (optional).
yfd.set_start_date(time::macros::datetime!(2019 - 01 - 01 0:00 UTC));
yfd.set_end_date(time::macros::datetime!(2020 - 01 - 01 0:00 UTC));
// Download the historical data.
yfd.get_price_history();
println!("Apple's quotes: {:?}", yfd.price_history)
}
Apple's quotes: Some(shape: (252, 7)
ββββββββββββββ¬ββββββββββββ¬ββββββββββββ¬ββββββββββββ¬ββββββββββββ¬βββββββββββββ¬ββββββββββββ
β date β open β high β low β close β volume β adjusted β
β --- β --- β --- β --- β --- β --- β --- β
β date β f64 β f64 β f64 β f64 β f64 β f64 β
ββββββββββββββͺββββββββββββͺββββββββββββͺββββββββββββͺββββββββββββͺβββββββββββββͺββββββββββββ‘
β 2019-01-02 β 38.7225 β 39.712502 β 38.557499 β 39.48 β 1.481588e8 β 37.994499 β
β 2019-01-03 β 35.994999 β 36.43 β 35.5 β 35.547501 β 3.652488e8 β 34.209969 β
β 2019-01-04 β 36.1325 β 37.137501 β 35.950001 β 37.064999 β 2.344284e8 β 35.670372 β
β 2019-01-07 β 37.174999 β 37.2075 β 36.474998 β 36.982498 β 2.191112e8 β 35.590965 β
β β¦ β β¦ β β¦ β β¦ β β¦ β β¦ β β¦ β
β 2019-12-26 β 71.205002 β 72.495003 β 71.175003 β 72.477501 β 9.31212e7 β 70.798401 β
β 2019-12-27 β 72.779999 β 73.4925 β 72.029999 β 72.449997 β 1.46266e8 β 70.771545 β
β 2019-12-30 β 72.364998 β 73.172501 β 71.305 β 72.879997 β 1.441144e8 β 71.191582 β
β 2019-12-31 β 72.482498 β 73.419998 β 72.379997 β 73.412498 β 1.008056e8 β 71.711739 β
ββββββββββββββ΄ββββββββββββ΄ββββββββββββ΄ββββββββββββ΄ββββββββββββ΄βββββββββββββ΄ββββββββββββ)
use RustQuant::data::*;
fn main() {
// New `Data` instance.
let mut data = Data::new(
format: DataFormat::CSV, // Can also be JSON or PARQUET.
path: String::from("./file/path/read.csv")
)
// Read from the given file.
data.read().unwrap();
// New path to write the data to.
data.path = String::from("./file/path/write.csv")
data.write().unwrap();
println!("{:?}", data.data)
}
use RustQuant::autodiff::*;
fn main() {
// Create a new Tape.
let t = Tape::new();
// Assign variables.
let x = t.var(0.5);
let y = t.var(4.2);
// Define a function.
let z = x * y + x.sin();
// Accumulate the gradient.
let grad = z.accumulate();
println!("Function = {}", z);
println!("Gradient = {:?}", grad.wrt([x, y]));
}
use RustQuant::math::*;
fn main() {
// Define a function to integrate: e^(sin(x))
fn f(x: f64) -> f64 {
(x.sin()).exp()
}
// Integrate from 0 to 5.
let integral = integrate(f, 0.0, 5.0);
// ~ 7.18911925
println!("Integral = {}", integral);
}
use RustQuant::options::*;
fn main() {
let VanillaOption = EuropeanOption {
initial_price: 100.0,
strike_price: 110.0,
risk_free_rate: 0.05,
volatility: 0.2,
dividend_rate: 0.02,
time_to_maturity: 0.5,
};
let prices = VanillaOption.price();
println!("Call price = {}", prices.0);
println!("Put price = {}", prices.1);
}
use RustQuant::stochastics::*;
fn main() {
// Create new GBM with mu and sigma.
let gbm = GeometricBrownianMotion::new(0.05, 0.9);
// Generate path using Euler-Maruyama scheme.
// Parameters: x_0, t_0, t_n, n, sims, parallel.
let output = (&gbm).euler_maruyama(10.0, 0.0, 0.5, 10, 1, false);
println!("GBM = {:?}", output.paths);
}
- 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 # Formulas
- Antoine Savine - Modern Computational Finance: AAD and Parallel Simulations