Skip to content

Commit 4b62943

Browse files
committed
Added ensemble plot for gbm simulation
1 parent 5e18049 commit 4b62943

File tree

1 file changed

+20
-15
lines changed

1 file changed

+20
-15
lines changed

examples/gbm_simulation.jl

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,13 @@
66
Simulates a Geometric Brownian Motion (GBM) process using the Euler-Maruyama method.
77
Returns the numerical solution.
88
"""
9-
function simulate_gbm()
9+
function gbm_problem()
1010
u0 = 100.0 # Initial stock price
1111
tspan = (0.0, 1.0) # Simulation for 1 year
1212
process = GBMProcess(0.05, 0.2) # μ = 5%, σ = 20%
1313
sde_function = get_sde_function(process)
1414
prob = SDEProblem(sde_function, u0, tspan)
15-
sol = solve(prob, EM(), dt=0.01) # Euler-Maruyama solver
16-
17-
return sol
15+
return prob
1816
end
1917

2018
# Simulate Heston process
@@ -24,29 +22,36 @@ end
2422
Simulates the Heston stochastic volatility model using the Euler-Maruyama method.
2523
Returns the numerical solution.
2624
"""
27-
function simulate_heston()
25+
function heston_problem()
2826
u0 = [100.0, 0.04] # Initial (Stock Price, Variance)
2927
tspan = (0.0, 1.0) # Simulation for 1 year
3028
process = HestonProcess(0.05, 2.0, 0.04, 0.3, -0.5) # Heston parameters
3129
prob = SDEProblem(get_sde_function(process), u0, tspan, process)
32-
sol = solve(prob, EM(), dt=0.01) # Euler-Maruyama solver
3330

34-
return sol
31+
return prob
3532
end
3633

3734
# Running the simulations
3835
using Revise, Plots, Hedgehog2, DifferentialEquations
3936

40-
sol_gbm = simulate_gbm()
41-
sol_heston = simulate_heston()
37+
gbm = gbm_problem()
38+
heston = heston_problem()
4239

4340
# Plot GBM
41+
sol_gbm = solve(gbm, EM(), dt=0.01) # Euler-Maruyama solver
4442
gbm_plot = plot(sol_gbm, plot_analytic = true, label="GBM Path", xlabel="Time", ylabel="Stock Price", lw=2)
4543

4644
# Plot Heston
47-
heston_plot = plot!(sol_heston.t, [u[1] for u in sol_heston.u], label="Heston Stock Price", xlabel="Time", ylabel="Stock Price", lw=2)
48-
heston_plot_var = plot!(sol_heston.t, [u[2] for u in sol_heston.u], label="Variance", lw=2)
49-
50-
display(gbm_plot)
51-
display(heston_plot)
52-
display(heston_plot_var)
45+
sol_heston = solve(heston, EM(), dt=0.01) # Euler-Maruyama solver
46+
plot(sol_heston.t, [u[1] for u in sol_heston.u], label="Heston Stock Price", xlabel="Time", ylabel="Stock Price", lw=2)
47+
plot(sol_heston.t, [u[2] for u in sol_heston.u], label="Variance", lw=2)
48+
49+
# ensemble GBM for 1000 trajectories
50+
ensembleprob = EnsembleProblem(gbm)
51+
sol = solve(ensembleprob, EnsembleThreads(), trajectories = 1000)
52+
53+
using DifferentialEquations.EnsembleAnalysis
54+
summ = EnsembleSummary(sol, 0:0.01:1)
55+
plot(summ, labels = "Middle 95%")
56+
summ = EnsembleSummary(sol, 0:0.01:1; quantiles = [0.25, 0.75])
57+
plot!(summ, labels = "Middle 50%", legend = true)

0 commit comments

Comments
 (0)