Skip to content

Commit

Permalink
geometric brownian bridge fix
Browse files Browse the repository at this point in the history
  • Loading branch information
frankschae committed Feb 7, 2024
1 parent d330a47 commit 9923160
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 8 deletions.
15 changes: 12 additions & 3 deletions src/geometric_bm.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,23 @@ https://math.stackexchange.com/questions/412470/conditional-distribution-in-brow
=#
function gbm_bridge(dW, gbm, W, W0, Wh, q, h, u, p, t, rng)
if dW isa AbstractArray
return gbm.σ * sqrt((1 - q) * q * abs(h)) * wiener_randn(rng, dW) + q * Wh
gbb_mean = @.. log(W0) + q * (log(W0 + Wh) - log(W0))
gbb_std = sqrt((1 - q) * q * abs(h)) * gbm.σ

Check warning on line 31 in src/geometric_bm.jl

View check run for this annotation

Codecov / codecov/patch

src/geometric_bm.jl#L30-L31

Added lines #L30 - L31 were not covered by tests

x = gbb_mean + gbb_std * wiener_randn(rng, dW)
return exp.(x) - W0

Check warning on line 34 in src/geometric_bm.jl

View check run for this annotation

Codecov / codecov/patch

src/geometric_bm.jl#L33-L34

Added lines #L33 - L34 were not covered by tests
else
return gbm.σ * sqrt((1 - q) * q * abs(h)) * wiener_randn(rng, typeof(dW)) + q * Wh
gbb_mean = log(W0) + q * (log(W0+Wh) - log(W0))
gbb_std = sqrt((1 - q) * q * abs(h)) * gbm.σ

Check warning on line 37 in src/geometric_bm.jl

View check run for this annotation

Codecov / codecov/patch

src/geometric_bm.jl#L36-L37

Added lines #L36 - L37 were not covered by tests

x = gbb_mean + gbb_std * wiener_randn(rng, typeof(dW))
return exp(x) - W0

Check warning on line 40 in src/geometric_bm.jl

View check run for this annotation

Codecov / codecov/patch

src/geometric_bm.jl#L39-L40

Added lines #L39 - L40 were not covered by tests
end
end
function gbm_bridge!(rand_vec, gbm, W, W0, Wh, q, h, u, p, t, rng)
wiener_randn!(rng, rand_vec)
@.. rand_vec = gbm.σ * sqrt((1 - q) * q * abs(h)) * rand_vec + q * Wh
@.. rand_vec = gbm.σ * sqrt((1 - q) * q * abs(h)) * rand_vec + (log(W0) + q * (log(W0 + Wh) - log(W0)))
@.. rand_vec = exp(rand_vec) - W0

Check warning on line 46 in src/geometric_bm.jl

View check run for this annotation

Codecov / codecov/patch

src/geometric_bm.jl#L45-L46

Added lines #L45 - L46 were not covered by tests
end

@doc doc"""
Expand Down
27 changes: 22 additions & 5 deletions test/bridge_test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,29 @@
@test (timestep_mean(sol, 11)[1], 1.0, atol = 1e-16)
@test (timestep_meanvar(sol, 11)[2], 0.0, atol = 1e-16)

μ = 1.2
σ = 2.2
W = GeometricBrownianBridge(μ, σ, 0.0, 1.0, 0.0, 1.0, 0.0, 0.0)
prob = NoiseProblem(W, (0.0, 1.0))
μ = 0.10500000000000001
σ = 0.1
t0 = 0.0
tend = 10.0
GB0 = 1.0
GBend = 4.0

W = GeometricBrownianBridge(μ, σ, t0, tend, GB0, GBend)
prob = NoiseProblem(W, (t0, tend))
ensemble_prob = EnsembleProblem(prob)
@time sol = solve(ensemble_prob, dt = 0.1, trajectories = 100)
@time sol = solve(ensemble_prob, dt=1.0, trajectories=100000)
ts = t0:1.0:tend
for i = 2:10
t = ts[i]
mean = log(GB0) + (t - t0) / (tend - t0) * (log(GBend) - log(GB0))
var = (t - t0) / (tend - t0) * (tend - t) * σ^2

m, v = timestep_meanvar(sol, i)

@test (m, exp(mean + var / 2), atol=1e-2)
@test (v, (exp(var) - 1) * exp(2 * mean + var), atol=1e-2)
end


Random.seed!(100)
r = 100 # should be independent of the rate, so make it crazy
Expand Down

0 comments on commit 9923160

Please sign in to comment.