Physics-Informed Neural Networks for Option Pricing
Dr. Yuchen Dong, MathWorks
The financial industry is increasingly using machine learning and AI to enhance its methodologies. Among these advancements, physics-informed neural networks (PINNs) offer a way to incorporate domain-specific knowledge into neural network training, making them effective for solving partial differential equations (PDEs) in finance.
This presentation demonstrates how PINNs can be applied to the Black-Scholes model for option pricing. Using MATLAB® and Deep Learning Toolbox™, we will show how to set up neural networks interactively and customize loss functions to address financial problems with greater precision.
Published: 22 Oct 2024
YUCHEN DONG: So today, I would like to talk about the physics-informed neural networks for option pricing. And my name is Yuchen. I'm a senior application engineer in computational finance at MathWorks.
So in today's presentation, I will first briefly introduce what is physics-informed neural networks. And in the second part, I will show how to implement PINNs for option pricing easily in MATLAB.
Compared with the classic pricing methodology, such as Monte Carlo simulations. When simulating the asset price, the classic method are time-consuming and computationally expensive. But since the training period is less than the classic Monte Carlo simulations, so it will shorten your runtime. But PINNs also can be very efficient on inverse problem to infer some unknown parameters which can help you to build the parameter estimation such as the volatility terms in Black-Scholes formula.
PINNs can also provide an alternative approach to price an exotic option or find more complicated options such as the option with some default risk.
For physics-informed neural network, this is a neural network that incorporates physics laws such as heat transfer, conservation laws, or fluid mechanism into the neural network structure and the training process. So for example, you can train a neural network that outputs a solution to a mathematical partial differential equations, and you can use this neural network as the numerical solution unto this equation. And PINN have already been developed as an alternative method to the classic neural network PDE solvers.
So for example, we usually sample x in the domain and a time t as the input or PINNs. And after training, the trained neural network can be regarded as the numerical solution to our PDE. And you can think of this trained neural network as the composition of different functions with respect to x and time t.
So in finance, Black-Scholes equation is a PDE governing the price evolution of derivatives. In the following example, we can show how to implement PINNs on pricing a European call option. In this PDE, the parameter V describes the value of options. S is the underlying assets. And in our case, I use the stock price as the underlying asset. Sigma is the volatility term of the return.
And r is the risk-free interest rate. K here represents the payoff function for a European call option. The PDE here is a very basic linear example. This method can also be extended to other type of exotic option or more complicated financial instruments valuation. And even if they have-- the PDE is a nonlinear form-- so if they have a PDE-related model, so then we can use PINNs to solve them.
To customize the loss function-- and the loss function can be defined by the mean square errors, and you can also use different other error matrix. The loss function is defined by the PDE equation with its initial and boundary conditions. Our goal of training is to find the coefficients of parameters in the neural network such that this neural network satisfies this PDE.
These derivative terms, partial V, partial T, partial second order derivative with respect to the underlying asset here and the first order derivative term, these three terms in PDE can apply the auto-differentiation tools in our Deep Learning Toolbox to compute the gradients for custom training loops. It is particularly useful to creating and training complex neural networks, deep learning models without needing to compute the gradient manually for optimization.
These tools and functions are available in our Deep Learning Toolbox. And in this case, our Black-Scholes PDE, partial V, partial T, the first-order derivative with respect to time t, and the first or second-order derivative with respect to the underlying assets. And we can use auto-differentiation and combine with other parameters, such as our loss function.
So here are several key steps of PINNs for option price. First is to generate the training data. The model depends on the underlying assets, which is the stock price in our example, and the time t, which represents the maturity time. As is shown on the right-hand side here, this plot has 5,000 gray points, which is sampling from the uniform distribution in our domain.
The orange, red, and the blue dots are described with a boundary condition, for example 1,000 data on each boundary. You can also allocate these points within the equal distance, and it will also another option for choice of the training data.
Next is to define the neural networks. You can use the predefined layers for neural networks in MATLAB or customize the layers for the neural network architecture. And the neural network here in the PINNs, you can think of as the numerical solutions to Black-Scholes PDE, which represents the option price.
To customize the loss function-- as mentioned before, the loss function consists of three parts, PDE, initial condition and boundary condition. The derivative can be implemented by the auto-differentiation tools in Deep Learning Toolbox.
The training procedure-- during the training, we can monitor the training progress, and we can also select a different optimizer, such as atom optimizer, LFBGF, and other algorithm options. We finally evaluate the model accuracy with a closed solution from the Black-Scholes equation. So we compare the numerical solution and the closed-form solution. So in a real case, once we get the numerical solution, you can also compare with other pricing vendors.
Let's switch to MATLAB. So this is a live script on PINNs for option pricing. You can find the MATLAB code from the GitHub. So we begin with the setting up for the option. So we can set up the strike price K, interest rate, and the sigma, the volatility terms. So you can also derive these parameters from some online data sources using the data free toolbox.
So we assume that S, the start price, the maximum, simulation is 500. You can also change the range of the asset price and the range of the time for the maturity if you have different maturity times.
So now we sample the data points. As mentioned, the data points including the domain and the three boundary conditions. And in the gray points, we use the uniform distribution to sample these data points in the range of the stock from 0 to 500 and the maturity time between 0 and 1. We also sampled the boundary point-- sampled data points on each boundary.
Next year, we set up the neural network with some predefined fully connected layers and some ReLU activation functions. And you can also choose different layers or activation functions in this model. Sometimes the neural network structure is complicated to visualize, but we have a Deep Network Designer to easily realize these tools, these layers. So in the left-hand side, the Layer Library, we predefine some input layers, fully connected layer, convolution layers.
So if you want to construct a neural network using these predefined layers, you can easily grab this layer into the window and connect with each layer. Once you click this layer, you can also set up the parameters, such as weights, output size dimensions in each layer so that you can build your neural network architecture.
If you already built the neural network architecture from different programming language using PyTorch or TensorFlow, you can also import your Python code into this Network Designer, and it will automatically convert it into the MATLAB code. So we can import the traced PyTorch model in here and the TensorFlow models using this button.
To train this model, we set the number of epochs, 5,000. And the initial rate is 5 times 10 to the negative 5. And these two parameters can also change as your design. You can also use our tool Experiment Manager app for the hyperparameter tuning to change the different settings of the training programs to get more better performance results. And this can also be applied in parallel computing to save your runtime.
To avoid a training date time-- so I just loaded the trained and neural networks PINNs matrix so that we can easily to find the modal evaluation.
So we compare the closer form and the prediction solution, so for maturity equal to three months, half a year, six months, nine months, and one year. So there are a little deviation, but the train of the numerical solution and the analytic solution I keep the same. A little deviation will happen as the T, the maturity goes further so that this neural network, this model can also be improved by different approach.
So the first approach, we can use the different layers in our neural network model. For example, in our neural network model, I use the fully connected layer and the ReLU layers as activation function. So you can change the ReLU layer as the tangent hyperbolic layers as the active function. Or you can change another choice.
So another way is to change the loss function. So in the loss function here, we customize the loss function based on our PDE information. So we use the DL gradient. This is a key function for the auto-differentiation to compute the derivatives in the PDE terms.
We can also regularize regularization, the loss function. We can also use the smooth L1 loss function to customize the loss function and to do the training process to make a better performance. So there is a different-- there are different approaches to improve our results.
And in this example, we show the workflow to use PINNs for option pricing. In the real case, if you have more complicated financial instruments, it can be described by a PDE form. So we can follow this workflow to construct your PINN solutions for the valuations.
So in our PINNs workflow-- so we first build a neural network. In the loss function, we use the auto-differentiation tools for Deep Learning Toolbox. And we also priced it using the predefined fully connected layer and ReLU activation layer. You can also customize the deep learning layers into the MATLAB neural networks architecture. During the training procedure, we can customize the loss function. Based on the PDE form, you can use the auto-differentiation tool, DL gradient function, to construct derivatives in the PDE form.
During the training, we can also monitor the training progress to see what's the mean square loss, what's the loss for each components of the loss function. You can customize the arrow matrix to do the training.
PINNs for option pricing is a new approach for option evaluation. So it can be extended into different types of financial instruments if it can be described by a PDE form. And it will help you to shorten the runtime. It has less training time than the classic Monte Carlo simulations.
Yeah, a more AI-related example in MATLAB, we have transformer layers, transformer layers for building a time series model. And we also have the Reinforcement Learning for Portfolio managers to compare to use multiagent reinforcement learning methodology to do the portfolio optimization.
And also for the language model, we can use the Text Analytics tool to research on the formal FOMC minutes, Federal Reserve minutes notes. And we have also more examples in AI application in computational finance. You can use this link to navigate you to these examples. OK, thank you for your time.