| Crates.io | fast-sde |
| lib.rs | fast-sde |
| version | 0.1.0 |
| created_at | 2025-09-21 02:54:54.819961+00 |
| updated_at | 2025-09-21 02:54:54.819961+00 |
| description | High-performance Monte Carlo simulation for stochastic differential equations in quantitative finance |
| homepage | https://github.com/R0B11N/fast-sde |
| repository | https://github.com/R0B11N/fast-sde |
| max_upload_size | |
| id | 1848371 |
| size | 196,829 |
A high-performance Rust library for Monte Carlo simulation of Stochastic Differential Equations (SDEs) for quantitative finance applications, including option pricing and risk management.
fast-sde/
├─ Cargo.toml
├─ src/
│ ├─ lib.rs
│ ├─ rng.rs
│ ├─ math_utils.rs
│ ├─ models/
│ │ ├─ mod.rs
│ │ ├─ model.rs # Abstract SDE model trait
│ │ ├─ gbm.rs # Black-Scholes / GBM exact step
│ │ ├─ heston.rs # Heston model
│ │ ├─ sabr.rs # SABR model
│ │ ├─ merton.rs # Merton jump-diffusion
│ │ └─ ou_process.rs # Ornstein-Uhlenbeck process
│ ├─ solvers/
│ │ ├─ mod.rs
│ │ ├─ euler_maruyama.rs # Euler-Maruyama scheme
│ │ ├─ milstein.rs # Milstein scheme
│ │ └─ srk.rs # Stochastic Runge-Kutta (order 1) scheme
│ ├─ mc/
│ │ ├─ mod.rs
│ │ ├─ mc_engine.rs # Monte Carlo engine (paths, payoff)
│ │ └─ payoffs.rs # Payoff functions
│ └─ analytics/
│ ├─ mod.rs
│ └─ bs_analytic.rs # Black-Scholes closed-form & Greeks
├─ tests/
│ ├─ integration_test.rs
│ ├─ solver_convergence_test.rs
│ └─ greeks_test.rs
├─ examples/
│ └─ demo.rs # Demonstrates usage and benchmarks
└─ README.md
For a generic SDE (dX_t = a(X_t, t) dt + b(X_t, t) dW_t), the Euler–Maruyama scheme is given by:
[X_{n+1} = X_n + a(X_n, t_n) \Delta t + b(X_n, t_n) \Delta W_n]
where (\Delta W_n \sim N(0, \Delta t)).
For a scalar SDE, the Milstein scheme provides higher accuracy:
[X_{n+1} = X_n + a \Delta t + b \Delta W + \frac{1}{2} b b' ((\Delta W)^2 - \Delta t)]
where (b' = \frac{\partial}{\partial x} b(x, t)).
A simple Heun-like strong order 1.0 SRK variant is implemented (details in src/solvers/srk.rs).
SDE:
[dS_t = \mu S_t dt + \sigma S_t dW_t]
The exact solution is used for stepping where possible.
SDE System:
[\begin{cases} dS_t = \mu S_t dt + \sqrt{V_t} S_t dW_t^{(1)} \ dV_t = \kappa (\theta - V_t) dt + \xi \sqrt{V_t} dW_t^{(2)} \end{cases}]
with correlation (\rho) between (dW_t^{(1)}) and (dW_t^{(2)}). Full truncation Euler is used for (V_t).
Implemented for (\beta=1) (lognormal case).
Combines GBM with Poisson jumps. Jump sizes are log-normally distributed.
Generic MC Loop: Parallelized paths using rayon.
Payoff Functions: European call/put are implemented.
Variance Reduction: Implemented using Antithetic Variates and Control Variates (for GBM). For the control variate, the optimal coefficient (b) is estimated as:
[b = \frac{\text{Cov}(Y, X)}{\text{Var}(X)}]
where (Y) is the Monte Carlo estimator (payoff) and (X) is the control variate (e.g., terminal asset price with known expectation).
Deterministic Seeding: Per-path and per-thread Random Number Generators (RNGs) are deterministically seeded (using cfg.seed + i as u64) to ensure reproducible benchmarks.
All tests can be run from the fast-sde directory using cargo test.
tests/integration_test.rs)This test compares the Monte Carlo price of a European call option under the Black-Scholes model against its known analytical solution. It asserts that the relative error is less than 0.01 (1%). This test also includes an assertion that the variance reduction factor (VRF) achieved by the control variate is greater than 1.2, demonstrating its effectiveness.
tests/solver_convergence_test.rs)These tests verify the weak convergence of the Euler-Maruyama, Milstein, and SRK schemes when applied to the Ornstein-Uhlenbeck (OU) process. For each solver, multiple simulations are run with decreasing step sizes. The tests assert that the absolute error (between the simulated mean and the exact mean of the OU process) generally decreases as the number of steps increases, and that the final absolute error falls below specific thresholds:
0.150.10.05tests/greeks_test.rs)This test validates the Monte Carlo Delta calculated using the pathwise derivative method for a European call option under Black-Scholes against the analytical Black-Scholes Delta. It asserts that the relative error is less than 0.01 (1%).
A warning regarding nalgebra v0.27.1's future incompatibility with Rust is noted, originating from statrs. The nalgebra version is explicitly pinned to 0.27 in Cargo.toml to maintain compatibility with statrs. This does not affect current functionality and is being tracked for resolution in future updates, pending statrs dependency updates.
To build the project, navigate to the fast-sde directory and run:
cargo build --release
To run the tests:
cargo test
To run the example demo:
cargo run --example demo --release
Running fast-sde Monte Carlo Demo
MC Price: 8.433100875569536 (59274.2039 ms)
Analytic Price: 8.433318690109608
Absolute Error (Price): 0.00021781454007197
Relative Error (Price): 0.000025827855921941808
MC Delta (Pathwise): 0.5596206111408688 (60093.255900000004 ms)
Analytic Delta: 0.5596176923702425
Absolute Error (Delta): 0.000002918770626303413
Relative Error (Delta): 0.0000052156510884083295
To generate plots for convergence or path simulations, you can output data from the Rust application (e.g., to CSV files) and then use external plotting libraries like matplotlib in Python or Gnuplot.
cust or accel crates) for accelerated Monte Carlo simulations.This project is licensed under either of
at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.