Sensitivity of a SDE w.r.t tspan

Hi all,

I’m using SciMLSensitivity.jl to get the sensitivities of the transformation defined by:

f(S_T) = exp(- r * T) * max(S(T) - K, 0.0),

where S_T is the solution at time T of a SDE system of the form:

dS_t = \mu * S_t * dt + \sigma * S_t * dW_t .

I’m particularly interested in computing the derivative of f w.r.t t, i.e:

\frac{\partial f(S_t)}{\partial t} \vert_{t=0}

Unfortunately, I can’t get the correct results.

When running the following example not only \frac{\partial f(S_t)}{\partial t} \vert_{t=0} breaks but all the other derivatives too:

using DifferentialEquations
using SciMLSensitivity
using ForwardDiff
using Statistics


f(u, p, t) = p[2] * u
g(u, p, t) = p[3] * u
out(u, p)  = max(u(p[4])[1] - p[5], 0.0)

u0 = 100.
μ  = 0.02
σ  = 0.12
K  = 90.0
T  = 1.0
p  = [u0, μ, σ, T, K]

prob = SDEProblem{false}(f, g, u0, (0.0, 1.0), p)

function mean_of_solution(x)
    _prob = remake(prob; u0 = x[1], p = x, tspan=(0.0, x[4]))
    ens   = EnsembleProblem(_prob, output_func = (sol, i) -> (out(sol, x), false))
    sol   = solve(ens, EM(); dt=1/252, trajectories=10000, sensealg=ForwardDiffSensitivity())
    v     = exp(- x[2] * x[4]) * mean(sol)

    return v
end

ForwardDiff.gradient(sum_of_solution, p)

Moreover, when running this example

using DifferentialEquations
using SciMLSensitivity
using ForwardDiff
using Statistics


f(u, p, t) = p[2] * u
g(u, p, t) = p[3] * u
out(u, p)  = max(u(p[4])[1] - p[5], 0.0)

u0 = 100.
μ  = 0.02
σ  = 0.12
K  = 90.0
T  = 1.0
p  = [u0, μ, σ, T, K]

prob = SDEProblem{false}(f, g, u0, (0.0, 1.0), p)

function mean_of_solution(x)
    _prob = remake(prob; u0 = x[1], p = x, tspan=(0.0, 1.0))
    ens   = EnsembleProblem(_prob, output_func = (sol, i) -> (out(sol, x), false))
    sol   = solve(ens, EM(); dt=1/252, trajectories=10000, sensealg=ForwardDiffSensitivity())
    v     = exp(- x[2] * x[4]) * mean(sol)

    return v
end

ForwardDiff.gradient(sum_of_solution, p)

The results are all correct except for \frac{\partial f(S_t)}{\partial t} \vert_{t=0}.

Any idea about the causes of this behaviour?

Thanks in advance!

Can you open an issue? @frankschae can probably look into this.

Mathematically, I am ot sure your question makes sense. The solution of an SDE is only continuous in time. For example, take dX_t = dW_t

Hi Chris, yes sure :slight_smile: