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

[FEAT]: Add NNStopping #81

Merged
merged 38 commits into from
Feb 9, 2024
Merged

Conversation

ashutosh-b-b
Copy link
Contributor

@ashutosh-b-b ashutosh-b-b commented Jan 26, 2024

Checklist

  • Appropriate tests were added
  • Any code changes were done in a way that does not break public API
  • All documentation related to code changes were updated
  • The new code follows the
    contributor guidelines, in particular the SciML Style Guide and
    COLPRAC.
  • Any new documentation only uses public API

Additional context

Add any other context about the problem here.

@ashutosh-b-b ashutosh-b-b marked this pull request as ready for review January 26, 2024 20:15
Copy link

codecov bot commented Jan 26, 2024

Codecov Report

Attention: 2 lines in your changes are missing coverage. Please review.

Comparison is base (3f1df12) 58.27% compared to head (66372d3) 63.06%.
Report is 8 commits behind head on main.

Files Patch % Lines
src/DeepBSDE.jl 0.00% 1 Missing ⚠️
src/DeepBSDE_Han.jl 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main      #81      +/-   ##
==========================================
+ Coverage   58.27%   63.06%   +4.78%     
==========================================
  Files           7        8       +1     
  Lines         580      647      +67     
==========================================
+ Hits          338      408      +70     
+ Misses        242      239       -3     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@ashutosh-b-b ashutosh-b-b changed the title Add NNStopping [FEAT]: Add NNStopping Jan 27, 2024
@ChrisRackauckas
Copy link
Member

Missing docs

```
We then define a `SDEProblem`:
```julia
prob = SDEProblem(f, sigma, u0, tspan; payoff = g)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

payoff isn't a valid kwarg here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is valid. We pick the payoff and redefine the SDEProblem to simulate:

g = prob.kwargs[:payoff]
sde_prob = SDEProblem(prob.f, prob.u0, prob.tspan)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, that does not match the interface. We should throw an error in SciMLBase if that's done. I'll add that error soon to catch this better, but that means we need to make sure this satisfies a real interface. But since it's a package about solving PDEs, we should have the description in the PDE form.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In that case, I ll pick up the dispatch from the NNKolmogorov PR and put it here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW, we can't simulate the SDE with this kwarg. But we can still construct it. The check should be at the construction level.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added the dispatch : a935123
And updated NNStopping to work with it : 71fe64c


Similar to DeepSplitting and DeepBSDE, NNStopping evaluates the PDE as a Stochastic Differential Equation. Consider an Obstacle PDE of the form:
```math
max\lbrace\partial_t u(t,x) + \mu(t, x) \nabla_x u(t,x) + \frac{1}{2} \sigma^2(t, x) \Delta_x u(t,x) , g(t,x) - u(t,x)\rbrace
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a PIDE?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically no. Its an obstacle PDE with non linear term f = 0:
image

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I see a difference? It just requires f = 0. That can be checked.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, I have a dispatch for f = 0 in the NNKolmogorov PR
:

function PIDEProblem(g,
μ,
σ,
tspan,
xspan;
p = nothing,
x0_sample = NoSampling(),
noise_rate_prototype = nothing,
kwargs...)

We can use that, with a kwarg for g. How does that sound?

* `xspan`: The domain of system state. This can be a tuple of floats for single dimension, and a vector of tuples for multiple dimensions. Where each tuple corresponds to a dimension of state vector.
* `noise_rate_prototype` : Incase of a non diagonal noise, the prototype of `dx` in `σ`
"""
function PIDEProblem(μ,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not just use nothing for f? I don't see why this would need another dispatch.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We donot need another one. But we need to change the existing one. The checks are hardcoded and always expect f ang g in a certain form.
I updated the only definition of PIDEProblem to accomodate defining all problems here: 901470e
Added some tests for PIDEProblem as well : 130cd20

@@ -62,14 +62,14 @@ In `HighDimPDE.jl` the right parameter combination $\theta$ is found by iterativ
`DeepSplitting` allows obtaining $u(t,x)$ on a single point $x \in \Omega$ with the keyword $x$.

```julia
prob = PIDEProblem(g, f, μ, σ, x, tspan)
prob = PIDEProblem(μ, σ, x, tspan, g, f,)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a breaking change

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. We will need a breaking release. I'd suggest we do that after NNKolmogorov is in

Comment on lines 53 to 55
* f -> f(x, y, u(x, t), u(y, t), ∇u(x, t), ∇u(y, t), p, t)
- Semilinear Parabolic Partial Differential Equation
* f -> f(X, u, σᵀ∇u, p, t)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should really just have one form?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically the equations are themselves different. And f means different things here.
For PIDEs (DeepSplitting and MLP) :
image

For DeepBSDE and DeepBSDE_Han :
image

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can consider naming them different. i.e. the integrated one f and the other one h

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As discussed, I added a new problem : 518a2f1

Comment on lines +57 to +61
* f -> `nothing`
* x0 -> nothing, xspan must be provided.
- Obstacle Partial Differential Equation
* f -> `nothing`
* g -> `nothing`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If these can be nothing, make them kwargs?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are now kwargs for ParabolicPDEProblem

* `x`: point where `u(x,t)` is approximated. Is required even in the case where `x0_sample` is provided. Determines the dimensionality of the PDE.
* `tspan`: timespan of the problem.
* `g` : initial condition, of the form `g(x, p, t)`.
* `f` : when defining PIDE : nonlinear function, of the form `f(x, y, u(x, t), u(y, t), ∇u(x, t), ∇u(y, t), p, t)`. When defining Semilinear PDE: `f(X, u, σᵀ∇u, p, t)`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, only one form. Multi-forms never work well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is resolved with the introduction of ParabolicPDEProblem

Comment on lines +57 to +60
* f -> `nothing`
* x0 -> nothing, xspan must be provided.
- Obstacle Partial Differential Equation
* f -> `nothing`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think there's tests for f = nothing on the other methods.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are f is nothing for Kolmogorov :

@test prob.f == nothing

@ashutosh-b-b
Copy link
Contributor Author

Updating for ParabolicPDEProblem:

518a2f1 : This separates PIDEProblem and a ParabolicPDEProblem.
ParabolicPDEProblem:

$$ \begin{aligned} (\partial_t u)(t,x) &= f\big(t,x, u(t,x), ( \nabla_x u )(t,x) \big) d{\bf x} + \big\langle \mu(t,x), ( \nabla_x u )( t,x ) \big\rangle + \tfrac{1}{2} \text{Trace} \big(\sigma(t,x) [ \sigma(t,x) ]^* ( \text{Hess}_x u)(t, x ) \big). \end{aligned} $$
# g(x) = u(T,x)
ParabolicPDEProblem(μ, σ, x0, tspan, g, f)
## When f = 0 for Kolmogorov Eqn , instead of x0 we have xspan where we sample multiple initial values. 
ParabolicPDEProblem(μ, σ, nothing, tspan, f; xspan)
## When f = 0 and instead of `g` we have `payoff` 
ParabolicPDEProblem(μ, σ, x0, tspan; payoff = payoff_func)

PIDEProblem:

$$ \begin{aligned} (\partial_t u)(t,x) &= \int_{\Omega} f\big(t,x,{\bf x}, u(t,x),u(t,{\bf x}), ( \nabla_x u )(t,x ),( \nabla_x u )(t,{\bf x} ) \big) d{\bf x} + \big\langle \mu(t,x), ( \nabla_x u )( t,x ) \big\rangle + \tfrac{1}{2} \text{Trace} \big(\sigma(t,x) [ \sigma(t,x) ]^* ( \text{Hess}_x u)(t, x ) \big). \end{aligned} $$
PIDEProblem(μ, σ, x0, tspan, g, f)

g, f = prob.g, prob.f
g= prob.g
f = if isa(prob, ParabolicPDEProblem)
(y, z, v_y, v_z, ∇v_y, ∇v_z, p, t) -> prob.f(y, v_y, ∇v_y, p, t )
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's not a direct translation like that though, since the interpretation of f is different?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is, since here (for ParabolicPDEProblem) we are interested in non - integrating variables only. The integrating variables are z , v_z and ∇v_z.

Here's an example : Consider Earlier HighDimPDE.jl MLP

  • For Allen Cahn we had:

    HighDimPDE.jl/test/MLP.jl

    Lines 143 to 146 in e44771b

    X0 = fill(0.0f0, d) # initial point
    g(X) = 1.0f0 ./ (2.0f0 .+ 4.0f-1 * sum(X .^ 2)) # initial condition
    a(u) = u - u^3
    f(y, z, v_y, v_z, ∇v_y, ∇v_z, p, t) = -a.(v_y)

    Notice that only v_y has been passed. That is how it is written for MLP. When the function is not integrated, pass only non-integrating values.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But how do you handle the local nonlinearity of the parabolic problem in a PIDEProblem?

Copy link
Contributor Author

@ashutosh-b-b ashutosh-b-b Feb 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If its a PIDEProblem : we mean to integrate f and we pass in the integrating vars, if its a PIDEProblem we donot integrate f we just pass in the non integrating vars. The anonymous function was added so as to not break things in MLP and DeepSplitting as much, since they already pass in variables as mentioned above. The always expect f to be (y, z, v_y, v_z, ∇v_y, ∇v_z, p, t) and expected the user to only pass non integrating vars incase of a ParabolicPDE. Which is not the case anymore since, the f for ParabolicPDEProblem has only non integrating vars

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where are the two forms written down?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't write the PDE for the second one.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As in the docstring?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added and updated docs here : 9534761 and here : a7358f7

@ChrisRackauckas
Copy link
Member

The solvers should list which problems they support somewhere.

@ashutosh-b-b
Copy link
Contributor Author

The solvers should list which problems they support somewhere.

I made the docstrings to show types now : 0f1c148
as well as added a section for problems supported for each solver:
66372d3

@ChrisRackauckas ChrisRackauckas merged commit ed7a4bf into SciML:main Feb 9, 2024
11 of 12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants