A modular Monte Carlo engine for pricing one-factor options by simulating paths of an underlying stochastic differential equation (SDE) and aggregating payoffs. The code is organized around a clean set of interfaces (SDE / RNG / FDM / Pricer) and two coordinating patterns:
- Builder: assembles a consistent object network (model + discretization + RNG) from user/config choices.
- Mediator: drives the simulation loop (path generation) and notifies pricers with generated paths.
The design follows a “plug-and-play” architecture: you can add new SDEs, time-stepping schemes, RNGs, or option pricers without touching the main simulation loop.
At a high level the program does:
- Choose and configure an SDE (e.g., GBM / CEV).
- Choose a finite-difference time-marching scheme (Euler / Milstein / predictor-corrector variants) to discretize the SDE.
- Choose an RNG to generate standard normal variates.
- Run a Monte Carlo loop that generates full price paths.
- Feed each path into one or more pricers (European / barrier / Asian / etc.) and finalize statistics.
The key idea is that the Mediator owns the simulation loop and is completely decoupled from the specific models, schemes, and products via abstract interfaces.
MonteCarloPricer/
├── build/ # Build directory (generated after CMake)
├── include/
│ ├── FDMAbstract.hpp # Base FDM interface / abstractions
│ ├── FDMDerived.hpp # Concrete one-step schemes (Euler, Milstein, etc.)
│ ├── Interface.hpp # High-level wiring / application interface
│ ├── MCBuilder.hpp # Builder: constructs SDE + FDM + RNG configuration
│ ├── MCMediator.hpp # Mediator: simulation driver (path generation + notifications)
│ ├── OptionData.hpp # Option + market data container (S0, K, r, T, vol, etc.)
│ ├── PricerAbstract.hpp # Pricer interface / base class
│ ├── PricerDerived.hpp # Concrete pricers (European, Asian, Barrier, etc.)
│ ├── RNGAbstract.hpp # RNG interface / base class
│ ├── RNGDerived.hpp # Concrete RNGs (Box-Muller, Polar Marsaglia, etc.)
│ ├── SDEBase.hpp # SDE interface / base abstraction
│ ├── SDEConcrete.hpp # Concrete SDEs (GBM, CEV, etc.)
│ ├── Singleton.hpp # Utility singleton (if used for shared config/state)
│ └── StopWatch.hpp # Timing utility
├── src/
│ ├── Interface.cpp # Application wiring / glue implementation
│ ├── PricerDerived.cpp # Pricer implementations
│ ├── RNGDerived.cpp # RNG implementations
│ ├── SDEConcrete.cpp # SDE implementations
│ ├── SDEConcrete.cpp # SDE implementations
│ └── main.cpp # Program entry point
- A C++ compiler with C++17 support (Clang or GCC recommended)
- CMake (3.14+)
# from repo root
mkdir -p build
cmake -S . -B build
cmake --build build -jThe executable will be created in build/.
./build/MonteCarloPricerConcrete models in SDEConcrete.* implement an SDE interface (drift/diffusion, initial condition, expiry). Typical implementations include:
-
Geometric Brownian Motion (GBM):
$$dS_t = \mu S_t,dt + \sigma S_t,dW_t$$ -
CEV:
$$dS_t = \mu S_t,dt + \sigma S_t^\beta,dW_t$$
One-step schemes in FDMDerived.hpp advance the process, e.g.:
- Euler–Maruyama
- Milstein
- (Optional) Predictor–Corrector variants
The discretization does not generate randomness itself; it consumes Wiener increments / normal variates provided by the RNG module.
Concrete generators in RNGDerived.* typically produce standard normal
- Box–Muller
- Polar Marsaglia
Concrete pricers in PricerDerived.* consume generated paths and compute discounted payoffs, e.g.:
-
European payoff using terminal value
$S_T$ - Barrier / Asian style logic
- Create a new class in
SDEConcrete.hpp/.cppimplementing the SDE interface. - Register it in the builder (or factory selection) so it can be constructed from config.
- Implement a new derived class in
FDMDerived.hpp(or split into.cppif desired). - Ensure it conforms to the FDM interface and consumes RNG-provided increments.
- Implement a derived class in
PricerDerived.hpp/.cpp. - Plug it into the mediator subscription/wiring (one mediator run can feed multiple pricers).
- Glasserman, P. Monte Carlo Methods in Financial Engineering (2004)
- Kloeden, P. & Platen, E. Numerical Solution of Stochastic Differential Equations (1995)
- Duffy & Kienitz, Monte Carlo engine design patterns and architecture (2009)