# Running a Stratonovich SDE backwards

**URL:** <https://discourse.julialang.org/t/running-a-stratonovich-sde-backwards/58940>\
**Category:** New to Julia\
**Tags:** diffeq, sde\
**Created:** [April 9, 2021, 7:29pm UTC](https://discourse.julialang.org/t/running-a-stratonovich-sde-backwards/58940 "2021-04-09T19:29:06Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![miguelbiron](https://avatars.discourse-cdn.com/v4/letter/m/258eb7/32.png) [@miguelbiron](https://discourse.julialang.org/u/miguelbiron)\
**Post date:** [April 9, 2021, 7:29pm UTC](https://discourse.julialang.org/t/running-a-stratonovich-sde-backwards/58940/1 "2021-04-09T19:29:06Z")

</div>

I’m very new to Julia, SDE’s and DifferentialEquations, so please bear with me. I need to solve a Stratonovich SDE both forward and backward in time, and arrive at the exact same solution. I understand that this should be possible to do using the EulerHeun solver but I haven’t gotten it to work.

This is what I have so far. I tried to adapt the code used in [this answer](https://discourse.julialang.org/t/solving-stochastic-differential-equations-in-different-time-windows/14807/16) to a related question about solving an SDE en separate windows in a consistent manner.

```julia
using DifferentialEquations, RandomNumbers

# Define the problem
f(dx, x, u, t) = (dx .= x)
g(dx, x, u, t) = (dx .= x)
dt = 2^(-6)

# Solve for 0-1
rng = Xorshifts.Xoroshiro128Plus(1)
x0 = [1 / 2]
tspan = (0., 1.)
noise = WienerProcess(0., 0., rng=rng, reseed=false)
prob = SDEProblem(f, g, x0, tspan, noise=noise)
sol_part1 = solve(prob, EulerHeun(), dt=dt)
plot(noise.t,noise.u)

# Solve for 1-0
fback(dx, x, u, t) = (dx .= -x) # reverse drift
gback(dx, x, u, t) = (dx .= -x) # reverse diffusion
x1 = sol_part1.u[end] # start from the end
prob = SDEProblem(fback,gback, x1, reverse(tspan), noise=noise) # reverse tspan
sol_part2 = solve(prob, EulerHeun(), dt=-dt) # negate dt
sol_part2.u[end][1] ≈ x0[1] # not even remotely similar

```

---

<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:** [April 10, 2021, 11:12am UTC](https://discourse.julialang.org/t/running-a-stratonovich-sde-backwards/58940/2 "2021-04-10T11:12:39Z")

</div>

@frankschae has done a lot of this:

> **[GSoC 2020: High weak order SDE solvers and their utility in neural SDEs | FS](https://frankschae.github.io/post/gsoc2020-high-weak-order-solvers-sde-adjoints/)**
>
> First and foremost, I would like to thank my mentors Chris Rackauckas, Moritz Schauer, and Yingbo Ma for their willingness to supervise me in this Google Summer of Code project. Although we are still at the very beginning of the project, we already...

See the tests in:

> <https://github.com/SciML/DiffEqNoiseProcess.jl/blob/master/test/reversal_test.jl>

That should help you get a working version.

---

<div class="post-metadata">

**Author:** ![mschauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mschauer/32/13946_2.png) [@mschauer](https://discourse.julialang.org/u/mschauer)\
**Post date:** [April 10, 2021, 11:34am UTC](https://discourse.julialang.org/t/running-a-stratonovich-sde-backwards/58940/3 "2021-04-10T11:34:19Z")

</div>

> [@ChrisRackauckas](#):
>
> ```julia
> 100)
> 
> ```

Do you have to feed the reverse of the noise?

---

<div class="post-metadata">

**Author:** ![miguelbiron](https://avatars.discourse-cdn.com/v4/letter/m/258eb7/32.png) [@miguelbiron](https://discourse.julialang.org/u/miguelbiron)\
**Post date:** [April 10, 2021, 5:26pm UTC](https://discourse.julialang.org/t/running-a-stratonovich-sde-backwards/58940/4 "2021-04-10T17:26:42Z")

</div>

This is exactly the paper I’m trying to replicate. Thank you so much!

---

<div class="post-metadata">

**Author:** ![frankschae](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/frankschae/32/8382_2.png) [@frankschae](https://discourse.julialang.org/u/frankschae)\
**Post date:** [April 12, 2021, 8:08am UTC](https://discourse.julialang.org/t/running-a-stratonovich-sde-backwards/58940/5 "2021-04-12T08:08:12Z")

</div>

yes, to go along the same noise values (or noise values drawn from the bridge distribution) of the forward pass.
