Technical resources

How to get started
Avera AI
    Introduction

    This library provides high-performance components leveraging the hardware acceleration support and automatic differentiation of TensorFlow. The library will provide TensorFlow support for foundational mathematical methods, mid-level methods, and specific pricing models. The coverage is being expanded over the next few months. The library is structured along three tiers:

    1. Foundational methods. Core mathematical methods - optimisation, interpolation, root finders, linear algebra, random and quasi-random number generation, etc.
    2. Mid-level methods. ODE & PDE solvers, Ito process framework, Diffusion Path Generators, Copula samplers etc.
    3. Pricing methods and other quant finance specific utilities. Specific Pricing models (e.g Local Vol (LV), Stochastic Vol (SV), Stochastic Local Vol (SLV), Hull-White (HW)) and their calibration. Rate curve building, payoff descriptions and schedule generation.

    We aim for the library components to be easily accessible at each level. Each layer will be accompanied by many examples which can be run independently of higher level components.

    Installation

    The easiest way to get started with the library is via the pip package.

    Note that library requires Python 3 and Tensorflow >= 2.1.

    First please install the most recent version of TensorFlow by following the TensorFlow installation instructions. For example, you could install TensorFlow using

    Shell
    pip3 install —upgrade tensorflow

    Then run

    Shell
    pip3 install —upgrade tf-quant-finance

    You maybe also have to use the option —user .

    TensorFlow training

    If you are not familiar with TensorFlow, a good place to get started is with the following self-study introduction to TensorFlow notebooks:

    Development roadmap

    We are working on expanding the coverage of the library. Areas under active development are:

    • Ito Processes: Framework for defining Ito processes. Includes methods for sampling paths from a process and for solving the associated backward Kolmogorov equation.
    • Implementation of the following specific processes/models:
      • Brownian Motion
      • Geometric Brownian Motion
      • Ornstein-Uhlenbeck
      • Single factor Hull White model
      • Heston model
      • Local volatility model
      • Quadratic Local Vol model
      • SABR model
    • Copulas: Support for defining and sampling from copulas.
    • SABR model calibration:
      • Dupire local vol calibration.
      • SABR model calibration.
    • Rate curve fitting: Hagan-West algorithm for yield curve bootstrapping and the Monotone Convex interpolation scheme.
    • Support for dates, day-count conventions, holidays, etc.
    Examples

    See tf_quant_finance/examples/ for end-to-end examples. It includes tutorial notebooks such as:

    The above links will open Jupyter Notebooks in Colab.

    Contributing

    We’re eager to collaborate with you! See CONTRIBUTING.md for a guide on how to contribute. This project adheres to TensorFlow’s code of conduct. By participating, you are expected to uphold this code.

    Development

    This section is meant for developers who want to contribute code to the library. If you are only interested in using the library, please follow the instructions in the Installation section.

    Development dependencies

    This library has the following dependencies:

    • Bazel Python 3 (Bazel uses Python 3 by default)
    • TensorFlow nightly build (most functions should work with TensorFlow 2.1)
    • TensorFlow Probability nightly build
    • Numpy version 1.16 or higher
    • Attrs

    This library requires the Bazel build system. Please follow the Bazel installation instructions for your platform. You can install TensorFlow and related dependencies using the pip3 install command:

    Shell
    pip3 install —upgrade tf-nightly tfp-nightly numpy==1.16.0 attrs
    Commonly used commands

    Clone the GitHub repository:

    Shell
    git clone https://github.com/google/tf-quant-finance.git

    After you run

    Shell
    cd tf_quant_finance

    you can execute tests using the bazel test command. For example,

    Shell
    bazel test tf_quant_finance/math/random_ops/sobol:sobol_test

    will run tests in sobol_test.py .

    Tests will be run using the Python version 3. Please make sure that you can run import tensorflow in the Python 3 shell, otherwise tests might fail.

    Docker images

    The official TF Quant Finance Docker images are located in the gcr.io container repository.

    Images are tagged using the GitHub release version. Images contain all development dependencies. See Dockerfile for details.

    You can pull the latest Docker image using

    Shell
    sudo docker pull gcr.io/tf-quant-finance-images/tf-quant-finance

    To start a TF Quant Finance container, use the following command:

    Shell
    sudo docker run -it gcr.io/tf-quant-finance-images/tf-quant-finance
    Building a custom pip package

    The following commands will build custom pip package from source and install it:

    Shell
    # sudo apt-get install bazel git python python-pip rsync # For Ubuntu. git clonehttps://github.com/google/tf-quant-finance.git cd tf-quant-finance bazel build :build_pip_pkg ./bazel-bin/build_pip_pkg artifacts pip install —user —upgrade artifacts/*.whl
    Community
    Disclaimers

    This is not an officially supported Google product. This library is under active development. Interfaces may change at any time.

    License

    This library is licensed under the Apache 2 license (see LICENSE). This library uses Sobol primitive polynomials and initial direction numbers which are licensed under the BSD license.

    Black-Scholes

    View source

    TensorFlow Quantitative Finance volatility surfaces and vanilla options.

    Modules

    approximations module: Approximations to the black scholes formula.

    Classes
    class ImpliedVolMethod: Implied volatility methods.
    Functions

    binary_price(...): Computes the Black Scholes price for a batch of binary call or put options.

    brownian_bridge_double(...): Computes probability of not touching the barriers for a 1D Brownian Bridge.

    brownian_bridge_single(...): Computes proba of not touching the barrier for a 1D Brownian Bridge.

    implied_vol(...): Finds the implied volatilities of options under the Black Scholes model.

    implied_vol_approx(...): Approximates the implied vol using the Stefanica-Radiocic algorithm.

    implied_vol_newton(...): Computes implied volatilities from given call or put option prices.

    option_price(...): Computes the Black Scholes price for a batch of call or put options.

    option_price_binomial(...): Computes the BS price for a batch of European or American options.

    Experimental

    View source

    Experimental modules.

    Modules

    dates module: Date-related utilities.

    instruments: module: Instruments.

    io module: Utilities to serialize and deserialize dictionaries of numpy arrays.

    lsm_algorithm module: LSM algorithm methods.

    Math

    View source

    TensorFlow Quantitative Finance general math functions.

    Modules

    integration module: Numerical integration methods.

    interpolation module: Ops related to interpolation.

    optimizer module: Optimization methods.

    pde module: PDE solver methods.

    piecewise module: Piecewise utility functions.

    random module: Ops related to random or quasi random sampling.

    root_search module: Root search functions.

    segment_ops module: Element wise ops acting on segments of arrays.

    Functions

    diff(...): Computes the difference between elements of an array at a regular interval.

    fwd_gradient(...): Computes forward mode gradient.

    gradients(...): Computes the gradients of func_or_y wrt to *xs.

    make_val_and_grad_fn(...): Function decorator to compute both function value and gradient.

    value_and_gradient(...): Computes f(*xs) and its gradients wrt to *xs.

    Models

    View source

    TensorFlow Quantitative Finance tools to build Diffusion Models.

    Modules

    euler_sampling module: The Euler sampling method for ito processes.

    hull_white module: TensorFlow Quantitative Finance tools to build Hull White type models.

    Classes

    class GenericItoProcess: Generic Ito process defined from a drift and volatility function.

    class GeometricBrownianMotion: Geometric Brownian Motion.

    class HestonModel: Heston Model with piecewise constant parameters.

    class ItoProcess: Interface for specifying Ito processes.

    class JoinedItoProcess: Join of Ito Processes with specified time dependent correlations.

    class MultivariateGeometricBrownianMotion: Multivariate Geometric Brownian Motion.

    Rates

    View source

    Functions to handle rates.

    Modules

    cashflows module: Collection of functions to compute properties of cashflows.

    constant_fwd module: Constant forward interpolation.

    forwards module: Collection of functions to compute properties of cashflows.

    hagan_west module: Hagan West algorithm for rate interpolation and bootstrapping.

    Classes

    class SwapCurveBuilderResult: SwapCurveBuilderResult(times, rates, discount_factors, initial_rates, converged, failed, iterations, objective_value)

    Functions

    swap_curve_bootstrap(...): Constructs the zero swap curve using bootstrap method.

    swap_curve_fit(...): Constructs the zero swap curve using optimization.

    Example Notebooks

    Examples demonstrating usage of TFF methods.

    In order to start the examples on Google Colab, please follow the link "Run in Google Colab" provided with the examples. To run the code without issues, connect to a GPU runtime (Runtime -> Change runtime type -> Choose GPU in Hardware accelerator section).

    AI Platform Notebooks instance was used to create the precomputed results (VM with 8 vCPUs and TESLA v100 GPU).

    List of examples:

    TensorFlow training

    If you are not familiar with TensorFlow, a good place to get started is with the following self-study introduction to TensorFlow notebooks: