Julia Community 🟣

Sebastian Schlenkrich
Sebastian Schlenkrich

Posted on

Introducing DiffFusion.jl

In this post we introduce the DiffFusion.jl framework for Monte Carlo simulation of financial risk factors to the Julia community.

Many risk management and pricing processes in finance require the simulation of risk factors like asset prices for equities, exchange rates or interest rates. Based on the simulated risk factors, scenario prices of financial instruments are derived. And the scenario prices then feed into pricing and risk measure calculation.

Relevant pricing measures are, for example, valuation adjustments (XVAs). Important risk measures are, for example, counterparty credit risk (CCR) exposures and Value-at-Risk (VaR). And besides the actual measure calculation it is important to understand the sensitivities of the measure with respect to the model inputs.

DiffFusion.jl aims at providing a modelling framework for the simulation of such risk factors, scenario-based financial instrument pricing and risk measure calculation in Julia. Risk factors are simulated using diffusion models. And we utilise Julia's Automatic Differentiation (AD) tools to efficiently calculate sensitivities.

Why Another Financial Modelling Library?

Libraries for financial instrument pricing and risk management are well established in the industry and in academia. Some notable open source examples are listed below.

  • QuantLib and Open Source Risk Engine (ORE). QuantLib is probably the most popular open source financial engineering library. It is around for more than 20 years. ORE is a risk management suite build on top of QuantLib and maintained by Acadia. QuantLib and ORE are written in C++.

  • OpenGamma Strata. Strata is the open source pricing and market risk library by OpenGamma. It is the successor of the OpenGamma Platform which was started around 10 years ago. Strata is written in Java.

  • finmath lib. finmath lib is a mathematical finance library also written in Java. It is developed and maintained by Christian Fries, LMU MΓΌnchen since 2008.

  • TF Quant Finance. TF Quant Finance is a relatively new mathematical finance library developed by Google. It is written in Python and heavily utilises TensorFlow.

In addition, there are several vendors offering proprietary solutions for risk management processes. And large financial institutions often have their in-house systems.

With DiffFusion.jl we want to take a fresh start regarding the design of a Monte Carlo based simulation framework. In particular, we want to make models and financial instruments composable with simple but clear interfaces. This should make the framework easily extendable.

Another motivation for DiffFusion.jl is computational performance. QuantLib has limitations when it comes to high-performance computing and parallelisation. Achieving good computational performance with Java-based libraries is also challenging. The TensorFlow-based TF Quant Finance library aims at high-performance computing. And as long as operations are represented within TensorFlow this is certainly true. But when complex control flow is required we are back in the Python world where computational performance easily deteriorates.

Julia is chosen as programming language because it aims at high-performance computing by design. The language allows for interactive programming similar to Python. And there is quite some effort and success to incorporate AD into to language without much additional coding for the user.

Simulation Framework

The basic model in DiffFusion.jl is a multi-variate generic stochastic process (Xt)\left(X_{t}\right) with Xt∈RnX_t \in \mathbb{R}^{n} and initial condition X0=0X_{0} = 0 . The elements of the process (Xt)\left(X_{t}\right) are grouped into models. That is, we decompose XtX_{t} into

(Xt1,…,XtN). \left(X_{t}^{1}, \ldots, X_{t}^{N}\right).

Each component model is described by the process (Xtk)\left(X_{t}^{k}\right) with Xtk∈RnkX_{t}^{k} \in \mathbb{R}^{n_{k}} , k=1,…,Nk=1,\ldots,N . And each component model is specified independently but with a common interface. This allows for a flexible combination of models.

The common structure of the component models is given by the dynamics

Xtk=Θk(s,t)+Hk(s,t)Xs+∫stΣk(u)⊀dWu X_{t}^{k}=\Theta^{k}\left(s,t\right)+H^{k}\left(s,t\right)X_{s}+\int_{s}^{t}\Sigma^{k}\left(u\right)^{\top}dW_{u}

Such a dynamic corresponds to a multi-variate Ornstein-Uhlenbeck (OU) processes. The OU process setting covers a wide range of practical models. In DiffFusion.jl, we currently implement

  • multi-factor HJM interest rate models,
  • log-normal models for exchange rates and equities,
  • Dodgson-Kainth and Jarrow-Yildirim models for inflation, and
  • single and multi-factor models for commodity futures.

Extensions to local and stochastic volatility models are on our roadmap. This will be implemented by letting the functions Θk\Theta^{k} , HkH^{k} and Σk\Sigma^{k} depend on the state XsX_s .

With the chosen component model design the composite model for (Xt)\left(X_{t}\right) also follows a multi-variate OU process. This allows for efficient and bias-free simulation.

Generic State Variables

It may seem that choosing the model based on a generic state variable process (Xt)\left(X_{t}\right) with initial condition X0=0X_{0}=0 is a pure choice of convention.

Alternatively, interest rate processes may be formulated in terms of a short rate, exchange rate processes may be formulated in terms of the actual rate and equity processes may be formulated in terms of the share price.

The generic state variable process is in fact a deliberate choice. This formulation yields that the model functions Θk\Theta^{k} , HkH^{k} and Σk\Sigma^{k} depend only on volatility and mean reversion parameters. In particular, initial curves for interest rates, exchange rates and equity prices do not enter the model simulation.

The decoupling of model simulation from initial curves contributes to the flexibility of the framework. Moreover, it proves advantageous for Delta sensitivity calculation.

It may not seem obvious, how exchange rates or equity prices can be formulated in terms of a generic state variable. We give a derivation of the representation in the documentation of the DiffFusion.jl package.

Exploiting Sparsity

The composite OU process of the combination of component models is simulated as

Xt=Θ(s,t)+H(s,t)Xs+∫stΣ(u)⊀dWu. X_{t}=\Theta\left(s,t\right)+H\left(s,t\right)X_{s}+\int_{s}^{t}\Sigma\left(u\right)^{\top}dW_{u}.

Here, H(s,t)H\left(s,t\right) is an nΓ—nn\times n matrix and Ξ£(u)\Sigma\left(u\right) is an nΓ—mn\times m matrix with mβ‰ˆnm\approx n .

A brute-force implementation of the composite model functions HH and Ξ£\Sigma via dense matrices would be inefficient in terms of storage and linear algebra operations. The corresponding matrices are sparse and of block-diagonal form. This property needs to be exploited for a computationally efficient implementation.

Julia has the SparseArrays package as part of its standard library. Sparse matrices are stored in Compressed Sparse Column (CSC) format. This matrix representation is used to efficiently implement the matrix-vector multiplications while keeping the simulation framework generic.

Financial Instrument Pricing

Risk factor simulation is only the first step of financial risk measure calculation. From a computational effort perspective, the most challenging part is calculating scenario prices of financial instruments.

For this step we choose to vectorise operations along simulated paths. This aims at exploiting efficient implementations of BLAS routines.

In order to further speed up calculations we may use parallelisation across instrument legs and simulation time points.

Current State and Next Steps

Recently, we released DiffFusion.jl v0.3.0. At this point, risk factor simulation for cross-asset models is implemented. Financial instrument pricing (including AD sensitivities) for linear rates, FX and Equity instruments is also available.

The latest release also includes interest rate option pricing. Relevant instruments are caplets/floorlets on forward- and backward-looking rates, European swaptions and Bermudan swaptions utilizing American Monte Carlo methods. We also added methods to calibrate our multi-factor rates models.

An example notebook that illustrates the simulation framework by means of interest rate derivatives is available here. The notebook can be run locally or online via mybinder.org.

We plan to continuously grow the simulation framework for additional functionality. Forthcoming additions will be

  • simulation of collateral accounts,
  • functionality for XVA risk measure calculation,
  • inflation-linked coupons, and
  • FX and equity options as well as corresponding model calibration methods.

Getting Involved

If you find the DffFusion.jl framework interesting and want to get involved please reach out. We welcome feedback, criticism and, of course, contributions to the code base.

Top comments (0)