SciML / DiffEqNoiseProcess.jl

A library of noise processes for stochastic systems like stochastic differential equations (SDEs) and other systems that are present in scientific machine learning (SciML)
https://docs.sciml.ai/DiffEqNoiseProcess/stable/
Other
63 stars 29 forks source link

Unexpected simulation behaviour of `GeometricBrownianBridge` process. #186

Closed vwiela closed 7 months ago

vwiela commented 7 months ago

Hey,

I wanted to simulate a geometric Brownian Motion of the form $$GBB(t)=\exp\left(\beta t+\delta B(t)\right),$$ where $B(t)$ is just some standard Brownian Motion. Or expressed as a SDE, I want to simulate the solution of $$dGBB(t)=\mu GBB(t)dt+\sigma GBB(t)dB(t)$$ with $\mu=\beta+\frac{\delta^2}{2}$ and $\sigma=\delta$. (The latter formulation as an SDE might be more consistent to the parameters in your implementation of a Geometric Brownian Motion.) However, I want to condition this on start and endpoint, so simulate a Geometric Brownian Bridge.

What I did at first was to simulate standard Brownian Bridges and apply the linear transform and exponential function to it, for example with the below code

using Plots
using DifferentialEquations
using Distributions
using Random

# package of SciML for Noise Processes including Brownian Bridges
using DiffEqNoiseProcess

# step size of the solver
dt = 0.01

# conditions for the Brownian bridges
t0 = 0.0
tend = 10.0
W0 = 0.0
Wend = 4.0

# parameters for the linear transformation
beta = 0.1
delta = 0.1

# first simulate a Brownian Bridge and apply linear transform and exponential by hand

function BB_to_GBB(beta, delta, BB)
    GBB = [exp(beta*(i-1)*dt+delta*BB.u[i]) for i in eachindex(BB.u)]    
    return GBB
end

SimpleBB = BrownianBridge(t0, tend, W0, Wend)
BBprob = NoiseProblem(SimpleBB, (t0, tend))

BBensemble_prob = EnsembleProblem(BBprob)
BBensemble_sol = solve(BBensemble_prob, trajectories = 1000, dt = dt)

TransBBmean = mean([BB_to_GBB(beta, delta, BBensemble_sol[i]) for i in eachindex(BBensemble_sol)]);
plot(t0:dt:tend+dt, TransBBmean)

This works nicely and the mean process looks as I would expect it, some kind of exponentially looking curve between beginning and endpoint. Screenshot from 2024-01-16 15-56-19

I then found that the DiffEqNoiseProcess.jl package also has a function for directly simulating Brownian Bridges. So I tried this

GB0 = exp(beta*t0+delta*W0)
GBend = exp(beta*tend+delta*Wend)

# reformulate to get the parameters of the SDE expression
mu = beta+delta^2/2
GBB = GeometricBrownianBridge(mu, delta, t0, tend, GB0, GBend)
GBBprob = NoiseProblem(GBB, (t0, tend))

GBBensemble_prob = EnsembleProblem(GBBprob)
GBBensemble_sol = solve(GBBensemble_prob, trajectories = 1000, dt = dt)
GBBmean = mean([GBBensemble_sol[i].u for i in eachindex(GBBensemble_sol)]);

plt = plot(t0:dt:tend+dt, GBBmean)
plot!(plt, t0:dt:tend+dt, TransBBmean)

Screenshot from 2024-01-16 15-58-37

But as you can see from the plot, the mean is now just a linear interpolation between start and endpoint. For me this is what I would expect from a normal Brownian bridge but not the Geometric. Moreover, I would expect the two simulation approaches to be equivalent, so the plots should be the same.

Either I am getting something conceptually wrong in the use of your functions (which can totally be the case), or the implementation of GeometricBrownianBridge does not work as it should.

Would appreciate any help on this.

Thanks Vincent

ChrisRackauckas commented 7 months ago

@frankschae this is the one I was mentioning. You said you had a source to double check the equations?

frankschae commented 7 months ago

yeah I thought I'd seen it in one of the books of Simo Särkkä -- but it seems that it only contains the bridge for an OU process. I guess our implementation should actually do exp(beta*(i-1)*dt+delta*BB.u[i]) as well?

ChrisRackauckas commented 7 months ago

Yes I believe so.

vwiela commented 7 months ago

I think transforming the Brownian Bridge might be the easiest. Just for your interest, one can also get an closed form for the expectation of the Geometric Brownian Bridge and it showed to coincide with the empirical mean of the transformed BB. I guess this is clear since the formulations are equivalent, so just as a side-remark.