Using Numba for Performance Gains - A Finance Example¶

This post looks at how Numba can be used to speed up a finance example with minimal effort. The example looks at an application of Monte Carlo simulation for calculating a simplified version of Credit Valuation Adjustment on a portfolio. The first half explains the finance and setup and the second half show how we can speed it up and benefit from that speedup.

Credit Valuation Adjustment is a measure of the credit risk a bank has between its counterparties. Specifically it is the adjustment to the fair value price of derivative instruments to account for counterparty credit risk. This is an interesting problem to look at as a bank can have millions of credit instruments (referenced as deals in the code) outstanding with thousands of counterparties. There are many types of deals, each type having its own complexity in calculating price. Similarly, each counterparty has its own risks, default risk being the main concern with CVA. Monte Carlo is applied to estimate the future values of these instruments along with the probability of default from the counterparty.

The formula for CVA is as such,

$$CVA = (1-R)\sum_{i=0}^{n}q_i p_i$$

where $q$ is the default probability, $R$ the recovery rate and $p$ is the price of the instrument. To simplify the calculation, we can ignore the recovery rate and assume the default probability of the counterparty is limited to a hazard rate such that:

$$q_i = \int_{0}^{i} q(\lambda, t)dt$$

Only two types of deals are looked at, foreign exchange (forex) futures and swaps. The swaps are based on the forex price. Assuming a fixed interest rate, the forex and swaps are easy to price. In the first cell below, we generate a random sample of deals, assigning values and hazard rates based on the weights below.