Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP]Debug errors in Kolmogorov Equations model #179

Merged
merged 3 commits into from
Jun 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ version = "0.7.0"
AlgebraicMultigrid = "2169fc97-5a83-5252-b627-83903c6c433c"
ArbNumerics = "7e558dbc-694d-5a72-987c-6f4ebed21442"
BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf"
CUDA = "052768ef-5323-5732-b1bb-66c8b64840ba"
CUDAnative = "be33ccc6-a3ff-5ff2-a52e-74243cff1e17"
Cairo = "159f3aea-2a34-519c-b102-8c37f9878175"
CuArrays = "3a865a2d-5b23-5a0f-bc46-62713ec82fae"
Expand Down Expand Up @@ -78,7 +79,6 @@ MCMCChains = "3.0, 4.0"
Measurements = "2.1"
ModelingToolkit = "0.9, 0.10, 1.0, 2.0, 3.0"
NLsolve = "4.2"
NeuralNetDiffEq = "1.5"
Optim = "0.19, 0.20, 0.21"
OrdinaryDiffEq = "5.23"
ParameterizedFunctions = "4.2, 5.0"
Expand Down
21 changes: 15 additions & 6 deletions tutorials/models/08-kolmogorov_equations.jmd
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ author: Ashutosh Bharambe
using Flux, StochasticDiffEq
using NeuralNetDiffEq
using Plots
using Distributions
using CuArrays
using CUDA
```
## Introduction on Backward Kolmogorov Equations

Expand Down Expand Up @@ -81,13 +82,17 @@ trajectories = 100000
Now lets define our model m and the optimiser
```julia
m = Chain(Dense(d, 8, leakyrelu),Dense(8, 16, leakyrelu),Dense(16 , 8 , leakyrelu) , Dense(8 , 1))
m = fmap(cu , m)
use_gpu = false
if CUDA.functional() == true
m = fmap(CuArrays.cu , m)
use_gpu = true
end
opt = Flux.ADAM(0.01)
```
And then finally call the solver
```julia
sol = solve(prob, NeuralNetDiffEq.NNKolmogorov(m, opt, sdealg, ensemblealg), verbose = true, dt = dt,
dx = dx , trajectories = trajectories , abstol=1e-6, maxiters = 4200)
@time sol = solve(prob, NeuralNetDiffEq.NNKolmogorov(m, opt, sdealg, ensemblealg), verbose = true, dt = dt,
dx = dx , trajectories = trajectories , abstol=1e-6, maxiters = 4200 , use_gpu = use_gpu)
```
## Analyzing the solution
Now let us find a Monte-Carlo Solution and plot the both:
Expand All @@ -105,15 +110,19 @@ for x in x_out
ensembleprob_val = EnsembleProblem(prob , output_func = output_func )
sim_val = solve(ensembleprob_val, EM(), EnsembleThreads() , dt=0.01, trajectories=100000,adaptive=false)
s = reduce(hcat , sim_val.u)
global monte_carlo_sol = push!(monte_carlo_sol , mean(phi(s)))
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.
```julia
x_model = reshape(x_out, 1 , size(x_out)[1])
m = fmap(cu , m)
if use_gpu == true
m = fmap(cpu , m)
end
y_out = m(x_model)
y_out = reshape(y_out , 13 , 1)
```
Expand Down