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:
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.
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
Shellpip3 install —upgrade tensorflow
Then run
Shellpip3 install —upgrade tf-quant-finance
You maybe also have to use the option —user .
If you are not familiar with TensorFlow, a good place to get started is with the following self-study introduction to TensorFlow notebooks:
We are working on expanding the coverage of the library. Areas under active development are:
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.
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.
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.
This library has the following dependencies:
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:
Shellpip3 install —upgrade tf-nightly tfp-nightly numpy==1.16.0 attrs
Clone the GitHub repository:
Shellgit clone https://github.com/google/tf-quant-finance.git
After you run
Shellcd tf_quant_finance
you can execute tests using the bazel test command. For example,
Shellbazel 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.
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
Shellsudo docker pull gcr.io/tf-quant-finance-images/tf-quant-finance
To start a TF Quant Finance container, use the following command:
Shellsudo docker run -it gcr.io/tf-quant-finance-images/tf-quant-finance
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
This is not an officially supported Google product. This library is under active development. Interfaces may change at any time.
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.
TensorFlow Quantitative Finance volatility surfaces and vanilla options.
approximations module: Approximations to the black scholes formula.
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 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.
TensorFlow Quantitative Finance general math functions.
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.
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.
TensorFlow Quantitative Finance tools to build Diffusion Models.
euler_sampling module: The Euler sampling method for ito processes.
hull_white module: TensorFlow Quantitative Finance tools to build Hull White type models.
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.
Functions to handle rates.
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.
class SwapCurveBuilderResult: SwapCurveBuilderResult(times, rates, discount_factors, initial_rates, converged, failed, iterations, objective_value)
swap_curve_bootstrap(...): Constructs the zero swap curve using bootstrap method.
swap_curve_fit(...): Constructs the zero swap curve using optimization.
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:
If you are not familiar with TensorFlow, a good place to get started is with the following self-study introduction to TensorFlow notebooks: