# Sensitivity of a SDE w.r.t tspan

**URL:** <https://discourse.julialang.org/t/sensitivity-of-a-sde-w-r-t-tspan/83998>\
**Category:** Modelling & Simulations\
**Tags:** sde, sciml, autodiff\
**Created:** [July 9, 2022, 6:45pm UTC](https://discourse.julialang.org/t/sensitivity-of-a-sde-w-r-t-tspan/83998 "2022-07-09T18:45:21Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![stochasticguy](https://avatars.discourse-cdn.com/v4/letter/s/278dde/32.png) [@stochasticguy](https://discourse.julialang.org/u/stochasticguy)\
**Post date:** [July 9, 2022, 6:45pm UTC](https://discourse.julialang.org/t/sensitivity-of-a-sde-w-r-t-tspan/83998/1 "2022-07-09T18:45:21Z")

</div>

Hi all,

I’m using [SciMLSensitivity.jl](https://github.com/SciML/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:

```nohighlight
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

```nohighlight
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!

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [July 20, 2022, 12:20pm UTC](https://discourse.julialang.org/t/sensitivity-of-a-sde-w-r-t-tspan/83998/2 "2022-07-20T12:20:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![rveltz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rveltz/32/2707_2.png) [@rveltz](https://discourse.julialang.org/u/rveltz)\
**Post date:** [July 20, 2022, 12:43pm UTC](https://discourse.julialang.org/t/sensitivity-of-a-sde-w-r-t-tspan/83998/3 "2022-07-20T12:43:01Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![stochasticguy](https://avatars.discourse-cdn.com/v4/letter/s/278dde/32.png) [@stochasticguy](https://discourse.julialang.org/u/stochasticguy)\
**Post date:** [July 21, 2022, 3:51pm UTC](https://discourse.julialang.org/t/sensitivity-of-a-sde-w-r-t-tspan/83998/4 "2022-07-21T15:51:44Z")

</div>

Hi Chris, yes sure 🙂
