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:

PartChaptersTopics
I1–4OCaml essentials, mathematics, probability
II5–8Fixed income, bonds, yield curves, rates derivatives
III9–14Equity markets, Black-Scholes, Monte Carlo, volatility
IV15–17Credit risk, CDOs, multi-asset models
V18–21Market risk, Greeks, XVA, portfolio optimization
VI22–25Algorithmic trading, execution, HFT infrastructure
VII26–30Advanced 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

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


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.