Quantitative Finance with OCaml
A comprehensive guide to building correct, performant, and maintainable financial systems using OCaml.
About This Book
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)
How to Read This Book
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–30 | Advanced stochastic calculus, ML, systems design, capstone |
Readers with OCaml experience may skim Chapters 1–2. Readers with finance experience may skim Chapters 5 and 9.
Table of Contents
Part I: Foundations
- Chapter 1 — Why OCaml for Quantitative Finance?
- Chapter 2 — OCaml Essentials for Finance
- Chapter 3 — Mathematical Foundations
- Chapter 4 — Probability and Statistics
Part II: Fixed Income and Interest Rates
- Chapter 5 — Time Value of Money
- Chapter 6 — Bonds and Fixed Income Instruments
- Chapter 7 — The Yield Curve
- Chapter 8 — Interest Rate Derivatives
Part III: Equity and Derivatives
- Chapter 9 — Equity Markets and Instruments
- Chapter 10 — The Black-Scholes Framework
- Chapter 11 — Numerical Methods for Option Pricing
- Chapter 12 — Monte Carlo Methods
- Chapter 13 — Volatility
- Chapter 14 — Exotic Options
Part IV: Credit and Multi-Asset
- Chapter 15 — Credit Risk and Credit Derivatives
- Chapter 16 — Portfolio Credit Derivatives
- Chapter 17 — Multi-Asset Models and Correlation
Part V: Risk Management
- Chapter 18 — Market Risk
- Chapter 19 — Greeks and Hedging
- Chapter 20 — Counterparty Credit Risk
- Chapter 21 — Portfolio Risk and Optimization
Part VI: Algorithmic Trading and Market Microstructure
- Chapter 22 — Market Microstructure
- Chapter 23 — Execution Algorithms
- Chapter 24 — Quantitative Trading Strategies
- Chapter 25 — High-Performance Trading Infrastructure
Part VII: Advanced Topics
- Chapter 26 — Stochastic Calculus and Advanced Pricing
- Chapter 27 — Machine Learning in Quantitative Finance
- Chapter 28 — Regulatory and Accounting Frameworks
- Chapter 29 — Systems Design for Quant Finance
- Chapter 30 — Capstone: A Complete Trading System
Appendices
- Appendix A — OCaml Quick Reference for Finance
- Appendix B — Mathematical Reference
- Appendix C — Financial Glossary
- Appendix D — Further Reading and Resources
- Appendix E — Setting Up the Development Environment
Companion Code
Each chapter directory contains:
chXX-topic/
├── README.md ← chapter text
├── lib/ ← reusable library modules
├── examples/ ← worked examples
├── exercises/ ← practice problems
└── benchmarks/ ← performance experiments
Building the Examples
# Install dependencies
opam install core owl zarith menhir ppx_deriving
# Build all examples
cd quantitative-finance-with-ocaml
dune build
# Run tests
dune test
A Note on Notation
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
Second edition in preparation. Corrections and suggestions welcome.