How google/tf-quant-finance works
Project Type and Purpose
• Type: Open-source, modular Python library built on TensorFlow.
• Purpose: Provide three tiers of functionality for quantitative finance:
– Foundational (core math, random, optimization)
– Mid-level (PDE/ODE solvers, Ito processes, diffusion generators)
– Pricing (specific models—Black-Scholes, Hull-White, Heston, SABR, rate-curve builders, calibration)
• Packaging and tooling: Bazel BUILD files, pip packaging via setup.py, CI/CD (ci_build).
File Structure
• Top-level directories:
– tf_quant_finance/ (library code)
– api_docs/ (generated markdown documentation)
– tf_quant_finance/examples/ (demos & notebooks)
– tf_quant_finance/experimental/ (experimental modules & pricing platform framework)
– ci_build/ (Docker/Cloud Build configs)
– third_party/sobol_data/ (quasi-random seeds)
• Build tooling: Bazel BUILD files peppered in modules; WORKSPACE; setup.py; build_pip_pkg.sh.
• Core dependencies: TensorFlow (>=2.7), TensorFlow Probability, NumPy, attrs, dataclasses.
README for Additional Insights
• Project is arranged in three tiers (foundational → mid → pricing).
• Examples & notebooks illustrate usage at each tier.
• Development uses Bazel for builds/tests, pip for distribution.
• CI configured via ci_build/ Dockerfile and Cloud Build YAML.
Main Components & Their Relationships
a. Core/Foundation Layer
– Modules: tf_quant_finance/math, tf_quant_finance/utils, tf_quant_finance/types, tf_quant_finance/datetime.
– Functions: random number generation, integration, interpolation, root search, dataclass utilities, date conventions.
b. Mid-Level Layer
– Modules: tf_quant_finance/models (Ito processes, SDE sampling), tf_quant_finance/rates (curve builders, swaps analytics), tf_quant_finance/math/pde.
– Leverages core for numerical routines and TensorFlow‘s autodiff.
c. Pricing/High-Level Layer
– Modules: tf_quant_finance/black_scholes, plus experimental subpackages for instruments & platform.
– Provides model-specific pricing, calibrations, payoffs.
d. Examples & Demos
– Jupyter notebooks under tf_quant_finance/examples/jupyter_notebooks.
– Dockerized microservices for data generation, downloaders, pricers under tf_quant_finance/examples/demos.
e. Documentation
– Static API docs in api_docs/.
f. Build & CI/CD
– Bazel build system (BUILD, WORKSPACE).
– Packaging scripts (build_pip_pkg.sh, setup.py).
– CI config (ci_build/Dockerfile, ci_build/cloudbuild.yaml).
g. External Dependencies & Services
– TensorFlow, TensorFlow Probability, NumPy.
– GitHub for source; PyPI for distribution; Google Cloud Storage for build status badges.
Architectural Patterns & Design Principles
• Layered Architecture: clear separation Foundation → Mid-Level → Pricing.
• Modularization: each financial model or method lives in its own package/subdirectory with its own BUILD file and tests.
• Dependency Injection (via TensorFlow ops and Python imports) to maintain loose coupling.
• Automated Build/Test: Bazel orchestrates compilation and unit tests; CI/CD triggers on Git.
• Extensibility: Experimental folder for WIP features and pricing platform framework.
• Math
• Utils & Types
• Datetime
• third_party/sobol_data
Middle row (Mid-Level – blue)
• Models (Ito, Heston, SABR…)
• Rates (curve builders, analytics)
• PDE/ODE Solvers
Top row (Pricing – purple)
• Black-Scholes
• Experimental Pricing Platform & Instruments
Side blocks
• API Docs (orange) → overlays all modules
Tooling/CI (gray)
• Bazel Build
• Packaging (pip wheel)
• CI/CD (Docker, Cloud Build)
External (red/cloud)
• TensorFlow, TFP, NumPy
• GitHub, PyPI, GCS (build badges)