6
6
Simulates a Geometric Brownian Motion (GBM) process using the Euler-Maruyama method.
7
7
Returns the numerical solution.
8
8
"""
9
- function simulate_gbm ()
9
+ function gbm_problem ()
10
10
u0 = 100.0 # Initial stock price
11
11
tspan = (0.0 , 1.0 ) # Simulation for 1 year
12
12
process = GBMProcess (0.05 , 0.2 ) # μ = 5%, σ = 20%
13
13
sde_function = get_sde_function (process)
14
14
prob = SDEProblem (sde_function, u0, tspan)
15
- sol = solve (prob, EM (), dt= 0.01 ) # Euler-Maruyama solver
16
-
17
- return sol
15
+ return prob
18
16
end
19
17
20
18
# Simulate Heston process
24
22
Simulates the Heston stochastic volatility model using the Euler-Maruyama method.
25
23
Returns the numerical solution.
26
24
"""
27
- function simulate_heston ()
25
+ function heston_problem ()
28
26
u0 = [100.0 , 0.04 ] # Initial (Stock Price, Variance)
29
27
tspan = (0.0 , 1.0 ) # Simulation for 1 year
30
28
process = HestonProcess (0.05 , 2.0 , 0.04 , 0.3 , - 0.5 ) # Heston parameters
31
29
prob = SDEProblem (get_sde_function (process), u0, tspan, process)
32
- sol = solve (prob, EM (), dt= 0.01 ) # Euler-Maruyama solver
33
30
34
- return sol
31
+ return prob
35
32
end
36
33
37
34
# Running the simulations
38
35
using Revise, Plots, Hedgehog2, DifferentialEquations
39
36
40
- sol_gbm = simulate_gbm ()
41
- sol_heston = simulate_heston ()
37
+ gbm = gbm_problem ()
38
+ heston = heston_problem ()
42
39
43
40
# Plot GBM
41
+ sol_gbm = solve (gbm, EM (), dt= 0.01 ) # Euler-Maruyama solver
44
42
gbm_plot = plot (sol_gbm, plot_analytic = true , label= " GBM Path" , xlabel= " Time" , ylabel= " Stock Price" , lw= 2 )
45
43
46
44
# 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