# Kolmogorov Backward Equations

##### Ashutosh Bharambe
using Flux, StochasticDiffEq
using NeuralNetDiffEq
using Plots
using CuArrays
using CUDAnative


## Introduction on Backward Kolmogorov Equations

The backward Kolmogorov Equation deals with a terminal condtion. The one dimensional backward kolmogorov equation that we are going to deal with is of the form :

$\frac{\partial p}{\partial t} = -\mu(x)\frac{\partial p}{\partial x} - \frac{1}{2}{\sigma^2}(x)\frac{\partial^2 p}{\partial x^2} ,\hspace{0.5cm} p(T , x) = \varphi(x)$

for all $t \in{ [0 , T] }$ and for all $x \in R^d$

#### The Black Scholes Model

The Black-Scholes Model governs the price evolution of the European put or call option. In the below equation V is the price of some derivative , S is the Stock Price , r is the risk free interest rate and σ the volatility of the stock returns. The payoff at a time T is known to us. And this makes it a terminal PDE. In case of an European put option the PDE is:

$\frac{\partial V}{\partial t} + rS\frac{\partial V}{\partial S} + \frac{1}{2}{\sigma^2}{S^2}\frac{\partial^2 V}{\partial S^2} -rV = 0 ,\hspace{0.5cm} V(T , S) = max\{\mathcal{K} - S , 0 \}$

for all $t \in{ [0 , T] }$ and for all $S \in R^d$

In order to make the above equation in the form of the Backward - Kolmogorov PDE we should substitute

$V(S , t) = e^{r(t-T)}p(S , t)$

and thus we get

$e^{r(t-T)}\frac{\partial p}{\partial t} + re^{r(t-T)}p(S , t) = -\mu(x)\frac{\partial p}{\partial x}e^{r(t-T)} - \frac{1}{2}{\sigma^2}(x)\frac{\partial^2 p}{\partial x^2}e^{r(t-T)} + re^{r(t-T)}p(S , t)$

And the terminal condition

$p(S , T) = max\{ \mathcal{K} - x , 0 \}$

We will train our model and the model itself will be the solution of the equation

## Defining the problem and the solver

We should start defining the terminal condition for our equation:

function phi(xi)
y = Float64[]
K = 100
for x in eachcol(xi)
val = max(K - maximum(x) , 0.00)
y = push!(y , val)
end
y = reshape(y , 1 , size(y)[1] )
return y
end

phi (generic function with 1 method)


Now we shall define the problem : We will define the σ and μ by comparing it to the orignal equation. The xspan is the span of initial stock prices.

d = 1
r = 0.04
sigma = 0.2
xspan = (80.00 , 115.0)
tspan = (0.0 , 1.0)
σ(du , u , p , t) = du .= sigma.*u
μ(du , u , p , t) = du .= r.*u
prob = KolmogorovPDEProblem(μ , σ , phi , xspan , tspan, d)

KolmogorovPDEProblem
timespan: (0.0, 1.0)xspan: (80.0, 115.0)μ
Main.##WeaveSandBox#253.μSigma
Main.##WeaveSandBox#253.σ


Now once we have defined our problem it is necessary to define the parameters for the solver.

sdealg = EM()
dt = 0.01
dx = 0.01
trajectories = 100000

100000


Now lets define our model m and the optimiser

m = Chain(Dense(d, 64, elu),Dense(64, 128, elu),Dense(128 , 16 , elu) , Dense(16 , 1))
use_gpu = false
if CUDAnative.functional() == true
m = fmap(CuArrays.cu , m)
use_gpu = true
end

Flux.Optimise.ADAM(0.0005, (0.9, 0.999), IdDict{Any,Any}())


And then finally call the solver

@time sol = solve(prob, NeuralNetDiffEq.NNKolmogorov(m, opt, sdealg, ensemblealg), verbose = true, dt = dt,
dx = dx , trajectories = trajectories , abstol=1e-6, maxiters = 1000 , use_gpu = use_gpu)

Current loss is: 135.46196
Current loss is: 137.49124
Current loss is: 141.95793
Current loss is: 140.52911
Current loss is: 136.69978
Current loss is: 134.2908
Current loss is: 134.53447
Current loss is: 136.14449
Current loss is: 137.06105
Current loss is: 136.43205
Current loss is: 134.86511
Current loss is: 133.49971
Current loss is: 133.11554
Current loss is: 133.66829
Current loss is: 134.38719
Current loss is: 134.35638
Current loss is: 133.4273
Current loss is: 132.45308
Current loss is: 132.04744
Current loss is: 132.16461
Current loss is: 132.3127
Current loss is: 132.06299
Current loss is: 131.53343
Current loss is: 131.0813
Current loss is: 131.02525
Current loss is: 131.1753
Current loss is: 131.10983
Current loss is: 130.72433
Current loss is: 130.29233
Current loss is: 130.0523
Current loss is: 129.94316
Current loss is: 129.74654
Current loss is: 129.41165
Current loss is: 129.11026
Current loss is: 129.00012
Current loss is: 128.9382
Current loss is: 128.71873
Current loss is: 128.4112
Current loss is: 128.2521
Current loss is: 128.19333
Current loss is: 128.0344
Current loss is: 127.79979
Current loss is: 127.67537
Current loss is: 127.58987
Current loss is: 127.40461
Current loss is: 127.21442
Current loss is: 127.084015
Current loss is: 126.949
Current loss is: 126.76815
Current loss is: 126.57664
Current loss is: 126.397255
Current loss is: 126.1867
Current loss is: 125.92802
Current loss is: 125.66324
Current loss is: 125.48338
Current loss is: 125.30214
Current loss is: 125.091644
Current loss is: 124.86625
Current loss is: 124.63377
Current loss is: 124.37106
Current loss is: 124.055595
Current loss is: 123.63267
Current loss is: 123.33332
Current loss is: 123.101234
Current loss is: 122.90485
Current loss is: 122.65503
Current loss is: 122.435974
Current loss is: 122.20913
Current loss is: 121.987045
Current loss is: 121.7673
Current loss is: 121.544815
Current loss is: 121.328186
Current loss is: 121.10686
Current loss is: 120.8911
Current loss is: 120.66781
Current loss is: 120.447136
Current loss is: 120.220634
Current loss is: 119.997475
Current loss is: 119.770775
Current loss is: 119.54528
Current loss is: 119.31562
Current loss is: 119.087395
Current loss is: 118.8568
Current loss is: 118.628044
Current loss is: 118.39653
Current loss is: 118.166466
Current loss is: 117.93384
Current loss is: 117.70254
Current loss is: 117.469215
Current loss is: 117.23637
Current loss is: 117.00278
Current loss is: 116.768295
Current loss is: 116.534515
Current loss is: 116.29969
Current loss is: 116.06608
Current loss is: 115.83305
Current loss is: 115.59946
Current loss is: 115.366104
Current loss is: 115.13276
Current loss is: 114.89947
Current loss is: 114.66724
Current loss is: 114.43578
Current loss is: 114.20465
Current loss is: 113.97449
Current loss is: 113.7464
Current loss is: 113.51902
Current loss is: 113.29145
Current loss is: 113.065254
Current loss is: 112.840904
Current loss is: 112.61809
Current loss is: 112.39686
Current loss is: 112.17747
Current loss is: 111.960175
Current loss is: 111.745186
Current loss is: 111.53384
Current loss is: 111.32973
Current loss is: 111.145996
Current loss is: 110.986916
Current loss is: 110.84565
Current loss is: 110.59211
Current loss is: 110.31119
Current loss is: 110.14149
Current loss is: 110.01411
Current loss is: 109.801704
Current loss is: 109.557915
Current loss is: 109.40648
Current loss is: 109.274765
Current loss is: 109.0666
Current loss is: 108.85927
Current loss is: 108.72027
Current loss is: 108.5858
Current loss is: 108.403015
Current loss is: 108.21706
Current loss is: 108.07753
Current loss is: 107.9555
Current loss is: 107.80652
Current loss is: 107.63943
Current loss is: 107.491104
Current loss is: 107.37092
Current loss is: 107.25782
Current loss is: 107.132164
Current loss is: 106.994705
Current loss is: 106.86023
Current loss is: 106.74038
Current loss is: 106.6353
Current loss is: 106.539116
Current loss is: 106.44675
Current loss is: 106.35506
Current loss is: 106.261765
Current loss is: 106.16632
Current loss is: 106.06836
Current loss is: 105.97111
Current loss is: 105.87713
Current loss is: 105.78918
Current loss is: 105.707695
Current loss is: 105.632126
Current loss is: 105.56147
Current loss is: 105.495125
Current loss is: 105.4332
Current loss is: 105.37706
Current loss is: 105.330894
Current loss is: 105.305336
Current loss is: 105.3265
Current loss is: 105.41958
Current loss is: 105.55027
Current loss is: 105.41724
Current loss is: 105.09852
Current loss is: 105.007324
Current loss is: 105.16417
Current loss is: 105.12575
Current loss is: 104.888535
Current loss is: 104.91153
Current loss is: 104.993996
Current loss is: 104.827225
Current loss is: 104.76332
Current loss is: 104.84082
Current loss is: 104.746284
Current loss is: 104.66749
Current loss is: 104.71807
Current loss is: 104.66227
Current loss is: 104.59215
Current loss is: 104.620346
Current loss is: 104.58313
Current loss is: 104.52376
Current loss is: 104.53624
Current loss is: 104.511154
Current loss is: 104.46132
Current loss is: 104.463066
Current loss is: 104.44778
Current loss is: 104.40657
Current loss is: 104.39997
Current loss is: 104.3913
Current loss is: 104.35821
Current loss is: 104.34529
Current loss is: 104.33979
Current loss is: 104.3144
Current loss is: 104.297745
Current loss is: 104.292694
Current loss is: 104.273865
Current loss is: 104.2558
Current loss is: 104.249504
Current loss is: 104.23571
Current loss is: 104.21793
Current loss is: 104.20972
Current loss is: 104.199486
Current loss is: 104.18345
Current loss is: 104.1734
Current loss is: 104.16531
Current loss is: 104.151985
Current loss is: 104.14074
Current loss is: 104.133286
Current loss is: 104.122826
Current loss is: 104.111435
Current loss is: 104.103485
Current loss is: 104.095276
Current loss is: 104.084946
Current loss is: 104.076294
Current loss is: 104.06917
Current loss is: 104.06057
Current loss is: 104.0518
Current loss is: 104.04477
Current loss is: 104.03767
Current loss is: 104.02961
Current loss is: 104.02234
Current loss is: 104.015976
Current loss is: 104.00906
Current loss is: 104.001945
Current loss is: 103.99564
Current loss is: 103.98964
Current loss is: 103.983185
Current loss is: 103.97689
Current loss is: 103.971146
Current loss is: 103.965454
Current loss is: 103.95953
Current loss is: 103.95382
Current loss is: 103.94849
Current loss is: 103.94316
Current loss is: 103.93771
Current loss is: 103.93248
Current loss is: 103.92752
Current loss is: 103.922585
Current loss is: 103.91761
Current loss is: 103.9128
Current loss is: 103.90823
Current loss is: 103.903725
Current loss is: 103.89923
Current loss is: 103.89486
Current loss is: 103.89067
Current loss is: 103.88655
Current loss is: 103.882454
Current loss is: 103.878456
Current loss is: 103.87459
Current loss is: 103.87079
Current loss is: 103.867004
Current loss is: 103.86329
Current loss is: 103.859634
Current loss is: 103.85609
Current loss is: 103.85257
Current loss is: 103.849075
Current loss is: 103.845634
Current loss is: 103.84228
Current loss is: 103.83896
Current loss is: 103.835686
Current loss is: 103.83246
Current loss is: 103.82928
Current loss is: 103.82616
Current loss is: 103.82309
Current loss is: 103.82006
Current loss is: 103.817055
Current loss is: 103.81412
Current loss is: 103.81124
Current loss is: 103.808395
Current loss is: 103.805595
Current loss is: 103.80285
Current loss is: 103.800156
Current loss is: 103.79753
Current loss is: 103.79494
Current loss is: 103.792404
Current loss is: 103.789925
Current loss is: 103.78749
Current loss is: 103.78511
Current loss is: 103.78279
Current loss is: 103.78051
Current loss is: 103.77829
Current loss is: 103.776085
Current loss is: 103.773964
Current loss is: 103.77186
Current loss is: 103.7698
Current loss is: 103.76778
Current loss is: 103.7658
Current loss is: 103.763824
Current loss is: 103.76192
Current loss is: 103.760025
Current loss is: 103.758156
Current loss is: 103.75632
Current loss is: 103.75449
Current loss is: 103.752716
Current loss is: 103.75095
Current loss is: 103.74921
Current loss is: 103.747505
Current loss is: 103.74582
Current loss is: 103.744156
Current loss is: 103.74253
Current loss is: 103.74092
Current loss is: 103.73933
Current loss is: 103.737755
Current loss is: 103.73624
Current loss is: 103.73471
Current loss is: 103.73321
Current loss is: 103.73175
Current loss is: 103.7303
Current loss is: 103.72887
Current loss is: 103.72747
Current loss is: 103.72607
Current loss is: 103.72471
Current loss is: 103.723366
Current loss is: 103.72204
Current loss is: 103.720726
Current loss is: 103.71944
Current loss is: 103.71817
Current loss is: 103.71692
Current loss is: 103.715675
Current loss is: 103.714455
Current loss is: 103.71326
Current loss is: 103.71207
Current loss is: 103.71091
Current loss is: 103.70975
Current loss is: 103.70861
Current loss is: 103.70749
Current loss is: 103.706375
Current loss is: 103.70528
Current loss is: 103.7042
Current loss is: 103.70315
Current loss is: 103.70209
Current loss is: 103.70106
Current loss is: 103.70005
Current loss is: 103.699036
Current loss is: 103.69804
Current loss is: 103.69706
Current loss is: 103.69609
Current loss is: 103.69514
Current loss is: 103.6942
Current loss is: 103.69326
Current loss is: 103.69236
Current loss is: 103.69146
Current loss is: 103.690575
Current loss is: 103.689705
Current loss is: 103.68889
Current loss is: 103.68812
Current loss is: 103.68745
Current loss is: 103.68697
Current loss is: 103.68689
Current loss is: 103.687645
Current loss is: 103.69008
Current loss is: 103.69635
Current loss is: 103.710045
Current loss is: 103.74145
Current loss is: 103.80018
Current loss is: 103.92171
Current loss is: 104.054695
Current loss is: 104.201225
Current loss is: 104.03506
Current loss is: 103.786995
Current loss is: 103.68329
Current loss is: 103.82442
Current loss is: 103.95312
Current loss is: 103.81608
Current loss is: 103.679184
Current loss is: 103.729095
Current loss is: 103.827286
Current loss is: 103.80062
Current loss is: 103.687935
Current loss is: 103.69279
Current loss is: 103.771385
Current loss is: 103.75233
Current loss is: 103.681366
Current loss is: 103.68122
Current loss is: 103.729416
Current loss is: 103.722595
Current loss is: 103.6735
Current loss is: 103.67788
Current loss is: 103.711395
Current loss is: 103.698296
Current loss is: 103.66752
Current loss is: 103.6726
Current loss is: 103.6931
Current loss is: 103.68626
Current loss is: 103.66488
Current loss is: 103.667305
Current loss is: 103.68153
Current loss is: 103.67624
Current loss is: 103.662254
Current loss is: 103.66325
Current loss is: 103.67226
Current loss is: 103.669914
Current loss is: 103.66019
Current loss is: 103.659836
Current loss is: 103.665955
Current loss is: 103.665
Current loss is: 103.65855
Current loss is: 103.656944
Current loss is: 103.660645
Current loss is: 103.6613
Current loss is: 103.65709
Current loss is: 103.65475
Current loss is: 103.65661
Current loss is: 103.657776
Current loss is: 103.655556
Current loss is: 103.65316
Current loss is: 103.653534
Current loss is: 103.65472
Current loss is: 103.65384
Current loss is: 103.65188
Current loss is: 103.65126
Current loss is: 103.65194
Current loss is: 103.651955
Current loss is: 103.65072
Current loss is: 103.64962
Current loss is: 103.6496
Current loss is: 103.64986
Current loss is: 103.649376
Current loss is: 103.6484
Current loss is: 103.64782
Current loss is: 103.64785
Current loss is: 103.647766
Current loss is: 103.64721
Current loss is: 103.64651
Current loss is: 103.64614
Current loss is: 103.64606
Current loss is: 103.645836
Current loss is: 103.64535
Current loss is: 103.644806
Current loss is: 103.6445
Current loss is: 103.64432
Current loss is: 103.64408
Current loss is: 103.64367
Current loss is: 103.64323
Current loss is: 103.64291
Current loss is: 103.6427
Current loss is: 103.64244
Current loss is: 103.6421
Current loss is: 103.64174
Current loss is: 103.64142
Current loss is: 103.64116
Current loss is: 103.64091
Current loss is: 103.640625
Current loss is: 103.6403
Current loss is: 103.639984
Current loss is: 103.63972
Current loss is: 103.639465
Current loss is: 103.639206
Current loss is: 103.63894
Current loss is: 103.63863
Current loss is: 103.63834
Current loss is: 103.63811
Current loss is: 103.637856
Current loss is: 103.63759
Current loss is: 103.63734
Current loss is: 103.63708
Current loss is: 103.63681
Current loss is: 103.63656
Current loss is: 103.636314
Current loss is: 103.63608
Current loss is: 103.635826
Current loss is: 103.63559
Current loss is: 103.63534
Current loss is: 103.63511
Current loss is: 103.63486
Current loss is: 103.634636
Current loss is: 103.6344
Current loss is: 103.63417
Current loss is: 103.63393
Current loss is: 103.63372
Current loss is: 103.63348
Current loss is: 103.63327
Current loss is: 103.63303
Current loss is: 103.63283
Current loss is: 103.63261
Current loss is: 103.632385
Current loss is: 103.632164
Current loss is: 103.63196
Current loss is: 103.631744
Current loss is: 103.63153
Current loss is: 103.63132
Current loss is: 103.63111
Current loss is: 103.630905
Current loss is: 103.63071
Current loss is: 103.6305
Current loss is: 103.630295
Current loss is: 103.6301
Current loss is: 103.629906
Current loss is: 103.62971
Current loss is: 103.62951
Current loss is: 103.629326
Current loss is: 103.62913
Current loss is: 103.62894
Current loss is: 103.62875
Current loss is: 103.628555
Current loss is: 103.62838
Current loss is: 103.62819
Current loss is: 103.628
Current loss is: 103.627815
Current loss is: 103.62764
Current loss is: 103.62746
Current loss is: 103.62728
Current loss is: 103.6271
Current loss is: 103.626915
Current loss is: 103.62674
Current loss is: 103.62656
Current loss is: 103.626396
Current loss is: 103.62622
Current loss is: 103.626045
Current loss is: 103.62588
Current loss is: 103.62572
Current loss is: 103.62555
Current loss is: 103.62539
Current loss is: 103.62521
Current loss is: 103.62506
Current loss is: 103.62488
Current loss is: 103.62472
Current loss is: 103.62456
Current loss is: 103.624405
Current loss is: 103.62424
Current loss is: 103.624084
Current loss is: 103.62394
Current loss is: 103.62378
Current loss is: 103.62363
Current loss is: 103.62349
Current loss is: 103.62334
Current loss is: 103.623215
Current loss is: 103.623085
Current loss is: 103.623
Current loss is: 103.62294
Current loss is: 103.622955
Current loss is: 103.62311
Current loss is: 103.62353
Current loss is: 103.6244
Current loss is: 103.62616
Current loss is: 103.62954
Current loss is: 103.636314
Current loss is: 103.64872
Current loss is: 103.67395
Current loss is: 103.71669
Current loss is: 103.80196
Current loss is: 103.91083
Current loss is: 104.08799
Current loss is: 104.11973
Current loss is: 104.06644
Current loss is: 103.78572
Current loss is: 103.62797
Current loss is: 103.69833
Current loss is: 103.8452
Current loss is: 103.89328
Current loss is: 103.742836
Current loss is: 103.62678
Current loss is: 103.65615
Current loss is: 103.75078
Current loss is: 103.77643
Current loss is: 103.68211
Current loss is: 103.620094
Current loss is: 103.6568
Current loss is: 103.707794
Current loss is: 103.6922
Current loss is: 103.63256
Current loss is: 103.624626
Current loss is: 103.664276
Current loss is: 103.67637
Current loss is: 103.64685
Current loss is: 103.6193
Current loss is: 103.63066
Current loss is: 103.654144
Current loss is: 103.6487
Current loss is: 103.626015
Current loss is: 103.618546
Current loss is: 103.63219
Current loss is: 103.64135
Current loss is: 103.63074
Current loss is: 103.61834
Current loss is: 103.62025
Current loss is: 103.629585
Current loss is: 103.63102
Current loss is: 103.622475
Current loss is: 103.616776
Current loss is: 103.61996
Current loss is: 103.625046
Current loss is: 103.62417
Current loss is: 103.618675
Current loss is: 103.61625
Current loss is: 103.618866
Current loss is: 103.6215
Current loss is: 103.62025
Current loss is: 103.61689
Current loss is: 103.61576
Current loss is: 103.61741
Current loss is: 103.6189
Current loss is: 103.618095
Current loss is: 103.61606
Current loss is: 103.615204
Current loss is: 103.616066
Current loss is: 103.617
Current loss is: 103.61668
Current loss is: 103.61547
Current loss is: 103.61474
Current loss is: 103.615036
Current loss is: 103.61564
Current loss is: 103.61564
Current loss is: 103.614975
Current loss is: 103.614365
Current loss is: 103.61428
Current loss is: 103.614586
Current loss is: 103.614746
Current loss is: 103.61448
Current loss is: 103.61403
Current loss is: 103.61377
Current loss is: 103.61381
Current loss is: 103.61394
Current loss is: 103.61391
Current loss is: 103.61368
Current loss is: 103.61341
Current loss is: 103.613266
Current loss is: 103.61326
Current loss is: 103.6133
Current loss is: 103.613235
Current loss is: 103.61307
Current loss is: 103.612885
Current loss is: 103.61277
Current loss is: 103.612724
Current loss is: 103.61272
Current loss is: 103.612656
Current loss is: 103.61255
Current loss is: 103.612404
Current loss is: 103.6123
Current loss is: 103.61223
Current loss is: 103.61219
Current loss is: 103.61213
Current loss is: 103.61206
Current loss is: 103.611946
Current loss is: 103.61185
Current loss is: 103.611755
Current loss is: 103.61169
Current loss is: 103.611626
Current loss is: 103.611565
Current loss is: 103.61148
Current loss is: 103.611404
Current loss is: 103.61133
Current loss is: 103.61124
Current loss is: 103.61117
Current loss is: 103.6111
Current loss is: 103.61104
Current loss is: 103.61097
Current loss is: 103.6109
Current loss is: 103.61082
Current loss is: 103.61075
Current loss is: 103.610664
Current loss is: 103.610596
Current loss is: 103.610535
Current loss is: 103.610466
Current loss is: 103.6104
Current loss is: 103.61033
Current loss is: 103.61027
Current loss is: 103.61018
Current loss is: 103.61013
Current loss is: 103.61005
Current loss is: 103.61
Current loss is: 103.60992
Current loss is: 103.609856
Current loss is: 103.60979
Current loss is: 103.609726
Current loss is: 103.60966
Current loss is: 103.6096
Current loss is: 103.60953
Current loss is: 103.60947
Current loss is: 103.6094
Current loss is: 103.60934
Current loss is: 103.609276
Current loss is: 103.6092
Current loss is: 103.609146
Current loss is: 103.60908
Current loss is: 103.60902
Current loss is: 103.608955
Current loss is: 103.608894
Current loss is: 103.60884
Current loss is: 103.60878
Current loss is: 103.60872
Current loss is: 103.60865
Current loss is: 103.60859
Current loss is: 103.60853
Current loss is: 103.608475
Current loss is: 103.60841
Current loss is: 103.60836
Current loss is: 103.608284
Current loss is: 103.60823
Current loss is: 103.60818
Current loss is: 103.60811
Current loss is: 103.60807
Current loss is: 103.60799
Current loss is: 103.60795
Current loss is: 103.60789
Current loss is: 103.60783
Current loss is: 103.607765
Current loss is: 103.60772
Current loss is: 103.607666
Current loss is: 103.607605
Current loss is: 103.607544
Current loss is: 103.6075
Current loss is: 103.60744
Current loss is: 103.607376
Current loss is: 103.60734
Current loss is: 103.60728
Current loss is: 103.60723
Current loss is: 103.60718
Current loss is: 103.60714
Current loss is: 103.60711
Current loss is: 103.60708
Current loss is: 103.607056
Current loss is: 103.60708
Current loss is: 103.60717
Current loss is: 103.607346
Current loss is: 103.60772
Current loss is: 103.60844
Current loss is: 103.60979
Current loss is: 103.61239
Current loss is: 103.61715
Current loss is: 103.626465
Current loss is: 103.643074
Current loss is: 103.67646
Current loss is: 103.73093
Current loss is: 103.83902
Current loss is: 103.96915
Current loss is: 104.18416
Current loss is: 104.21104
Current loss is: 104.15066
Current loss is: 103.81383
Current loss is: 103.61847
Current loss is: 103.68861
Current loss is: 103.8679
Current loss is: 103.95843
Current loss is: 103.79806
Current loss is: 103.63681
Current loss is: 103.621994
Current loss is: 103.72842
Current loss is: 103.79947
Current loss is: 103.71444
Current loss is: 103.61845
Current loss is: 103.62633
Current loss is: 103.69572
Current loss is: 103.712074
Current loss is: 103.64511
Current loss is: 103.605835
Current loss is: 103.635925
Current loss is: 103.67284
Current loss is: 103.662766
Current loss is: 103.619225
Current loss is: 103.607216
Current loss is: 103.632576
Current loss is: 103.64689
Current loss is: 103.63006
Current loss is: 103.60753
Current loss is: 103.611176
Current loss is: 103.62851
Current loss is: 103.62938
Current loss is: 103.61468
Current loss is: 103.60511
Current loss is: 103.611496
Current loss is: 103.62095
Current loss is: 103.61838
Current loss is: 103.60872
Current loss is: 103.604836
Current loss is: 103.609985
Current loss is: 103.61477
Current loss is: 103.61188
Current loss is: 103.60608
Current loss is: 103.60472
Current loss is: 103.608154
Current loss is: 103.61068
Current loss is: 103.6087
Current loss is: 103.60522
Current loss is: 103.604355
Current loss is: 103.606316
Current loss is: 103.60788
Current loss is: 103.60686
Current loss is: 103.604774
Current loss is: 103.60405
Current loss is: 103.60508
Current loss is: 103.60612
Current loss is: 103.60576
Current loss is: 103.60453
Current loss is: 103.60383
Current loss is: 103.604195
Current loss is: 103.6049
Current loss is: 103.60496
Current loss is: 103.604324
Current loss is: 103.603714
Current loss is: 103.60369
Current loss is: 103.60406
Current loss is: 103.60429
Current loss is: 103.604065
Current loss is: 103.60364
Current loss is: 103.60342
Current loss is: 103.60351
Current loss is: 103.60371
Current loss is: 103.60374
Current loss is: 103.603546
Current loss is: 103.60331
Current loss is: 103.60321
Current loss is: 103.60328
Current loss is: 103.60335
Current loss is: 103.60334
Current loss is: 103.60321
Current loss is: 103.60306
Current loss is: 103.603
Current loss is: 103.60303
Current loss is: 103.60306
Current loss is: 103.60303
Current loss is: 103.60295
Current loss is: 103.60286
Current loss is: 103.6028
Current loss is: 103.6028
Current loss is: 103.6028
Current loss is: 103.60277
Current loss is: 103.60273
Current loss is: 103.60266
Current loss is: 103.60261
Current loss is: 103.60258
Current loss is: 103.602554
Current loss is: 103.60254
Current loss is: 103.60251
Current loss is: 103.60247
Current loss is: 103.60242
Current loss is: 103.60238
Current loss is: 103.60235
Current loss is: 103.60234
Current loss is: 103.602295
Current loss is: 103.60228
Current loss is: 103.602234
Current loss is: 103.602196
Current loss is: 103.602165
Current loss is: 103.60213
Current loss is: 103.602104
Current loss is: 103.60207
Current loss is: 103.60205
Current loss is: 103.60203
Current loss is: 103.60199
Current loss is: 103.601944
Current loss is: 103.60192
Current loss is: 103.60189
Current loss is: 103.60186
Current loss is: 103.60184
Current loss is: 103.6018
Current loss is: 103.60177
Current loss is: 103.601746
Current loss is: 103.601715
Current loss is: 103.60168
Current loss is: 103.601654
Current loss is: 103.60163
Current loss is: 103.60161
Current loss is: 103.60158
Current loss is: 103.60154
Current loss is: 103.60151
Current loss is: 103.60148
Current loss is: 103.60147
Current loss is: 103.60144
Current loss is: 103.601395
Current loss is: 103.601364
Current loss is: 103.60135
Current loss is: 103.60131
Current loss is: 103.60129
Current loss is: 103.60126
Current loss is: 103.60122
Current loss is: 103.601204
Current loss is: 103.60118
Current loss is: 103.60116
Current loss is: 103.60112
Current loss is: 103.60109
Current loss is: 103.60107
Current loss is: 103.60104
Current loss is: 103.601006
Current loss is: 103.60099
Current loss is: 103.60096
Current loss is: 103.60094
Current loss is: 103.60091
Current loss is: 103.60089
Current loss is: 103.600845
Current loss is: 103.60084
Current loss is: 103.60081
Current loss is: 103.60078
Current loss is: 103.600746
Current loss is: 103.60073
Current loss is: 103.600685
Current loss is: 103.60068
Current loss is: 103.60064
Current loss is: 103.60062
Current loss is: 103.6006
Current loss is: 103.600555
Current loss is: 103.60054
Current loss is: 103.60052
Current loss is: 103.60049
Current loss is: 103.60046
Current loss is: 103.60045
Current loss is: 103.60042
Current loss is: 103.600395
Current loss is: 103.60038
Current loss is: 103.600334
Current loss is: 103.6003
Current loss is: 103.600296
Current loss is: 103.600266
Current loss is: 103.60025
Current loss is: 103.60023
Current loss is: 103.60022
Current loss is: 103.60019
Current loss is: 103.600174
Current loss is: 103.600174
Current loss is: 103.600174
Current loss is: 103.600204
Current loss is: 103.600266
Current loss is: 103.600365
Current loss is: 103.60058
Current loss is: 103.600945
Current loss is: 103.601585
Current loss is: 103.60271
Current loss is: 103.60466
Current loss is: 103.60823
Current loss is: 103.614296
Current loss is: 103.62568
Current loss is: 103.644516
Current loss is: 103.68064
Current loss is: 103.73505
Current loss is: 103.83854
Current loss is: 103.95373
Current loss is: 104.14281
Current loss is: 104.175385
Current loss is: 104.15587
Current loss is: 103.86905
Current loss is: 103.64777
Current loss is: 103.619255
Current loss is: 103.75986
Current loss is: 103.91917
Current loss is: 103.89172
Current loss is: 103.76932
Current loss is: 103.625916
Current loss is: 103.61207
Current loss is: 103.7019
Current loss is: 103.752556
Current loss is: 103.71366
Current loss is: 103.62436
Current loss is: 103.60493
Current loss is: 103.65741
Current loss is: 103.69347
Current loss is: 103.67363
Current loss is: 103.6193
Current loss is: 103.60065
Current loss is: 103.62717
Current loss is: 103.65266
Current loss is: 103.64506
Current loss is: 103.613144
Current loss is: 103.6001
Current loss is: 103.61525
Current loss is: 103.63118
Current loss is: 103.627144
Current loss is: 103.60844
Current loss is: 103.59945
Current loss is: 103.607056
Current loss is: 103.61754
Current loss is: 103.6174
Current loss is: 103.606834
Current loss is: 103.59948
Current loss is: 103.60209
Current loss is: 103.608765
Current loss is: 103.61043
Current loss is: 103.60508
Current loss is: 103.599754
Current loss is: 103.5997
Current loss is: 103.603485
Current loss is: 103.60588
Current loss is: 103.60403
Current loss is: 103.60045
Current loss is: 103.59884
Current loss is: 103.60023
Current loss is: 103.60232
Current loss is: 103.602455
Current loss is: 103.60073
Current loss is: 103.59898
Current loss is: 103.59885
Current loss is: 103.599976
Current loss is: 103.60084
Current loss is: 103.60054
Current loss is: 103.59943
Current loss is: 103.598625
Current loss is: 103.59872
Current loss is: 103.599365
Current loss is: 103.59975
Current loss is: 103.599495
Current loss is: 103.59888
Current loss is: 103.59848
Current loss is: 103.59852
Current loss is: 103.59885
Current loss is: 103.59907
Current loss is: 103.598946
Current loss is: 103.598625
Current loss is: 103.59836
89.860730 seconds (247.81 M allocations: 128.737 GiB, 7.60% gc time)
(Float32[87.59 94.19 … 108.81 96.43], Float32[12.965411 8.924145 … 3.489032
5 7.820206])


## Analyzing the solution

Now let us find a Monte-Carlo Solution and plot the both:

monte_carlo_sol = []
x_out = collect(85:2.00:110.00)
for x in x_out
u₀= [x]
g_val(du , u , p , t) = du .= 0.2.*u
f_val(du , u , p , t) = du .= 0.04.*u
dt = 0.01
tspan = (0.0,1.0)
prob = SDEProblem(f_val,g_val,u₀,tspan)
output_func(sol,i) = (sol[end],false)
ensembleprob_val = EnsembleProblem(prob , output_func = output_func )
s = reduce(hcat , sim_val.u)
mean_phi = sum(phi(s))/length(phi(s))
global monte_carlo_sol = push!(monte_carlo_sol , mean_phi)
end


##Plotting the Solutions We should reshape the inputs and outputs to make it compatible with our model. This is the most important part. The algorithm gives a distributed function over all initial prices in the xspan.

x_model = reshape(x_out, 1 , size(x_out)[1])
if use_gpu == true
m = fmap(cpu , m)
end
y_out = m(x_model)
y_out = reshape(y_out , 13 , 1)

13×1 Array{Float32,2}:
14.819762
13.37625
12.0128765
10.726072
9.55843
8.512779
7.55458
6.689971
5.9263196
5.2351093
4.597478
4.000861
3.4366512


And now finally we can plot the solutions

plot(x_out , y_out , lw = 3 ,  xaxis="Initial Stock Price", yaxis="Payoff" , label = "NNKolmogorov")
plot!(x_out , monte_carlo_sol , lw = 3 ,  xaxis="Initial Stock Price", yaxis="Payoff" ,label = "Monte Carlo Solutions")