# JSoC 2020 : Working with Differential Equations and Neural Networks

I was selected for JSoC 2020 for the project "* Neural Networks for solving differential equations*". It took me a while to understand the workflow before writing the proposal. My mentor

**Chris Rackauckas**guided me through the entire project.

I first started finishing my existing work on Kolmogorov Equations solver. And then moved on to the next, optimal stopping problem solver.

For understanding Optimal Stopping Problem, we can take an example of an American Option. An American option can be exercised before its maturity and thus we need to determine an optimal stopping time to exercise the American option given the discounted pay-off function at the maturity time T. Considering that we can model a stock price at any time t as a solution to following SDE :

We intend to calculate :

### Role of Neural networks:

We first discretise the above problem using Euler-Maruyama Scheme.

In order to calculate the above supremum we express this as:

where ,

Clearly the sum over all n of inline_formula not implemented is one. The second term in the above expression checks if it is already one for some k. We take inline_formula not implemented as our neural network with N-dimensional output and thus the problem reduces to a standard maximisation problem. Another important property of the model is that the last layer should always be a softmax function in order to satisfy the sum over n equals one condition.

### Defining the Problem and solver code:

We define this as a simple `SDEProblem`

based on the process the stock follows as mentioned in the first equation and pass the terminal condition as an additional argument.

First things first , Adding and pre-compiling necessary dependencies:

`using Pkg`

`Pkg.add("Flux")`

`Pkg.add("StochasticDiffEq")`

`Pkg.add("LinearAlgebra")`

`Pkg.add("NeuralNetDiffEq")`

`using Flux , StochasticDiffEq , LinearAlgebra`

`using NeuralNetDiffEq`

Defining a problem with inline_formula not implemented and inline_formula not implemented Considering N = 50, we define a dt. In the below test case we are considering an American Max Put Option wherein the discounted pay-off function is given as :

formula not implementedK is the strike price of the option and d is dimension of the option

`d = 1`

`r = 0.04f0`

`beta = 0.2f0`

`T = 1`

`u0 = 85.00`

`u0 = fill(u0 , d , 1)`

`sdealg = EM()`

`ensemblealg = EnsembleThreads()`

`f(du,u,p,t) = (du .= r*u)`

`sigma(du,u,p,t) = (du .= Diagonal(beta*u))`

`tspan = (0.0 , 1.0)`

`N = 50`

`dt = tspan[2]/49`

`K = 100.00`

`function g(t , x)`

` return exp(-r*t)*(max(K - maximum(x) , 0))`

`end`

`prob = SDEProblem(f , sigma , u0 , tspan ; g = g)`

`opt = Flux.ADAM(0.1)`

`m = Chain(Dense(d , 5, tanh), Dense(5, 16 , tanh) , Dense(16 , N ), softmax)`

`sol = solve(prob, NeuralNetDiffEq.NNStopping( m, opt , sdealg , ensemblealg), verbose = true, dt = dt,`

` abstol=1e-6, maxiters = 20 , trajectories = 500)`

We test the price against the Binomial Tree method, for different initial values we obtain :

The accuracy of above graph tend to improve on increasing the number of trajectories.

### Post Optimal Stopping Time Problem :

After this task was merged , I moved on to the following tasks :

Adding GPU and some extra features to the Kolmogorov Solver in order to improve the next task i.e. the tutorial.

Writing a tutorial for the NNKolmogorov Solver.

Implementing a deep primal-dual based algorithm to determine upper and lower bound to the existing BSDE solver. (Upcoming blog)

Writing a documentation for the completed algorithms.

Conclusively, it was a great learning and working experience and I look forward to learn and utilise this opportunity more!