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
$\left(X_{t}\right)$
with
$X_t \in \mathbb{R}^{n}$
and initial condition
$X_{0} = 0$
. The elements of the process
$\left(X_{t}\right)$
are grouped into *models*. That is, we decompose
$X_{t}$
into

Each component model is described by the process $\left(X_{t}^{k}\right)$ with $X_{t}^{k} \in \mathbb{R}^{n_{k}}$ , $k=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

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 $\Theta^{k}$ , $H^{k}$ and $\Sigma^{k}$ depend on the state $X_s$ .

With the chosen component model design the composite model for $\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 $\left(X_{t}\right)$ with initial condition $X_{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 $\Theta^{k}$ , $H^{k}$ and $\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

Here, $H\left(s,t\right)$ is an $n\times n$ matrix and $\Sigma\left(u\right)$ is an $n\times m$ matrix with $m\approx n$ .

A brute-force implementation of the composite model functions $H$ 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)