RustQuant
Rust library for quantitative finance tools.
Contact: rustquantcontact@gmail.com
Latest additions:
- 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.
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.
Table of Contents
- Automatic Differentiation
- Option Pricers
- Stochastic Processes and Short Rate Models
- Bonds
- Distributions
- Mathematics
- Helper Functions and Macros
- How-tos
- References
Automatic Differentiation
Currently only gradients can be computed. Suggestions on how to extend the functionality to Hessian matrices are definitely welcome.
- [x] 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$
Option Pricers
The stochastic process generators can be used to price path-dependent options via Monte-Carlo.
- Monte Carlo pricing:
- [x] Lookback
- [ ] Asian
- [ ] Chooser
- [ ] Barrier
Stochastic Processes and Short Rate Models
The following is a list of stochastic processes that can be generated.
- [x] Brownian Motion
- [x] Geometric Brownian Motion
- $dXt = \mu Xt dt + \sigma Xt dWt$
- Models: Black-Scholes (1973), Rendleman-Bartter (1980)
- [x] Cox-Ingersoll-Ross (1985)
- $dXt = (\theta - \alpha Xt)dt + \sqrt{rt} \sigma dWt$
- [x] Ornstein-Uhlenbeck process
- $dXt = \theta(\mu - Xt)dt + \sigma dW_t$
- Models: Vasicek (1977)
- [ ] Ho-Lee (1986)
- $dXt = \thetat dt + \sigma dW_t$
- [ ] Hull-White (1990)
- $dXt = (\theta - \alpha Xt)dt + \sigmat dWt$
- [ ] Black-Derman-Toy (1990)
- $d\ln(X) = \left[ \thetat + \frac{\sigmat'}{\sigmat}\ln(X) \right]dt + \sigmat dW_t$
- $d\ln(X) = \thetat dt + \sigma dWt$
- [ ] Merton's model (1973)
- $Xt = X0 + at + \sigma W_t^$
- $dXt = adt + \sigma dWt^$
Bonds
Most will follow the notation and formulas in John C. Hull's Options, Futures, and Other Derivatives.
- Prices:
- [X] The Vasicek Model
- [x] The Cox, Ingersoll, and Ross Model
- [ ] The Rendleman and Bartter Model
- [ ] The Ho–Lee Model
- [ ] The Hull–White (One-Factor) Model
- [ ] The Black–Derman–Toy Model
- [ ] The Black–Karasinski Model
- [ ] Duration
- [ ] Convexity
Distributions
Probability density/mass functions, distribution functions, characteristic functions, etc.
- [x] Gaussian
- [x] Bernoulli
- [x] Binomial
- [x] Poisson
- [x] Uniform (discrete & continuous)
- [ ] Chi-Squared
- [ ] Gamma
- [ ] Exponential
Mathematics
- Numerical Integration (needed for Heston model, for example):
- [x] Tanh-Sinh (double exponential) quadrature
- [x] Composite Midpoint Rule
- [x] Composite Trapezoidal Rule
- [x] Composite Simpson's 3/8 Rule
- [x] Risk-Reward Measures (Sharpe, Treynor, Sortino, etc)
- [x] Newton-Raphson
- [x] Standard Normal Distribution (Distribution/Density functions, and generation of variates)
- [ ] Interpolation
Helper Functions and Macros
A collection of utility functions and macros.
- [x] Plot a vector.
- [x] Write vector to file.
- [x] Cumulative sum of vector.
- [x] Linearly spaced sequence.
- [x]
assert_approx_equal!
How-tos
Compute gradients:
```rust
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]));
}
```
Compute integrals:
```rust
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);
}
```
Price options:
```rust
use RustQuant::options::*;
fn main() {
let VanillaOption = EuropeanOption {
initialprice: 100.0,
strikeprice: 110.0,
riskfreerate: 0.05,
volatility: 0.2,
dividendrate: 0.02,
timeto_maturity: 0.5,
};
let prices = VanillaOption.price();
println!("Call price = {}", prices.0);
println!("Put price = {}", prices.1);
}
```
Generate stochastic processes:
```rust
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.trajectories);
}
```
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