A comprehensive guide to building correct, performant, and maintainable financial systems using OCaml.
Quantitative finance sits at the intersection of mathematics, statistics, and software engineering. Most practitioners reach for Python for its rapid prototyping or C++ for raw speed — but OCaml offers something rare: a language that is simultaneously expressive, correct by construction, and fast enough for production trading systems.
This book teaches quantitative finance through the lens of OCaml. You will learn to price derivatives, manage risk, model credit, build trading algorithms, and design robust financial infrastructure — all while exploiting OCaml's type system to make whole classes of financial programming errors impossible at compile time.
What makes this book different:
- Every concept is accompanied by production-quality OCaml code
- Mathematical derivations are presented honestly, not buried in appendices
- We build reusable, well-typed libraries that accumulate across chapters
- Performance and correctness are treated as equal concerns
- Coverage extends to modern OCaml 5 features (domains, effects, OxCaml extensions)
The book is organized into seven parts that can be read sequentially or used as a reference:
| Part | Chapters | Topics |
|---|---|---|
| I | 1–4 | OCaml essentials, mathematics, probability |
| II | 5–8 | Fixed income, bonds, yield curves, rates derivatives |
| III | 9–14 | Equity markets, Black-Scholes, Monte Carlo, volatility |
| IV | 15–17 | Credit risk, CDOs, multi-asset models |
| V | 18–21 | Market risk, Greeks, XVA, portfolio optimization |
| VI | 22–25 | Algorithmic trading, execution, HFT infrastructure |
| VII | 26–31 | Advanced stochastic calculus, ML, regulatory, OxCaml |
Readers with OCaml experience may skim Chapters 1–2. Readers with finance experience may skim Chapters 5 and 9.
Part I: Foundations
Part II: Fixed Income and Interest Rates
Part III: Equity and Derivatives
Part IV: Credit and Multi-Asset
Part V: Risk Management
Part VI: Algorithmic Trading and Market Microstructure
Part VII: Advanced Topics
Appendices
Each chapter directory contains:
chXX-topic/
├── README.md ← chapter text
├── lib/ ← reusable library modules
├── examples/ ← worked examples
├── exercises/ ← practice problems
└── benchmarks/ ← performance experiments
# Install dependencies
opam install core owl zarith menhir ppx_deriving
# Build all examples
cd quantitative-finance-with-ocaml
dune build
# Run tests
dune test
Throughout this book:
- OCaml code is shown in syntax-highlighted blocks
- Mathematical formulas use standard notation: $S_t$ for asset price at time $t$, $\sigma$ for volatility, $r$ for risk-free rate
- Types are given in OCaml notation, e.g.,
float -> float -> float - Module paths are written
Module.function, e.g.,Black_scholes.price
Version 1.0 — February 2026. Corrections and contributions welcome via the project repository.