Skip to content

Commit 5e18049

Browse files
committed
Added initial SDE DifferentialEquations.jl compatibility to be used in the future with Montecarlo and FFT methods, plus an example where Heston and BS are plotted.
1 parent 3791544 commit 5e18049

File tree

5 files changed

+181
-1
lines changed

5 files changed

+181
-1
lines changed

Project.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@ version = "1.0.0-DEV"
77
Accessors = "7d9f7c33-5ae7-4f3b-8dc6-eff91059b697"
88
BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf"
99
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
10+
DifferentialEquations = "0c46a032-eb83-5123-abaf-570d42b7fbaa"
1011
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
1112
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
1213

1314
[compat]
1415
Accessors = "0.1.42"
1516
BenchmarkTools = "1.6.0"
1617
Dates = "1.11.0"
18+
DifferentialEquations = "7.16.0"
1719
Distributions = "0.25.117"
1820
ForwardDiff = "0.10.38"
1921
julia = "1.11"

examples/Project.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
Accessors = "7d9f7c33-5ae7-4f3b-8dc6-eff91059b697"
33
BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf"
44
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
5+
DifferentialEquations = "0c46a032-eb83-5123-abaf-570d42b7fbaa"
56
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
67
Hedgehog2 = "7f16798b-0e18-40de-98af-932948254698"
78
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
89
Pluto = "c3e4b0f8-55cb-11ea-2926-15256bba5781"
9-
Revise = "295af30f-e4ad-537b-8983-00126c2a3abe"
10+
PyPlot = "d330b81b-6aea-500a-939a-2ce795aea3ee"
11+
Revise = "295af30f-e4ad-537b-8983-00126c2a3abe"

examples/gbm_simulation.jl

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

src/Hedgehog2.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@ include("pricing_methods/pricing_methods.jl")
1212
include("pricing_methods/black_scholes.jl")
1313
include("pricing_methods/cox_ross_rubinstein.jl")
1414
include("sensitivity_methods/delta_methods.jl")
15+
include("stochastic_processes/stochastic_processes.jl")
1516

1617
end
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
using DifferentialEquations
2+
3+
export AbstractStochasticProcess, GBMProcess, HestonProcess, get_drift, get_diffusion, get_analytic_solution, get_sde_function
4+
5+
# Abstract type for stochastic processes
6+
"""
7+
AbstractStochasticProcess
8+
9+
An abstract type representing a generic stochastic process.
10+
"""
11+
abstract type AbstractStochasticProcess end
12+
13+
# Define the GBM process
14+
"""
15+
GBMProcess <: AbstractStochasticProcess
16+
17+
Represents a Geometric Brownian Motion (GBM) process with drift `μ` and volatility `σ`.
18+
"""
19+
struct GBMProcess <: AbstractStochasticProcess
20+
μ
21+
σ
22+
end
23+
24+
# Define the Heston process
25+
"""
26+
HestonProcess <: AbstractStochasticProcess
27+
28+
Represents the Heston stochastic volatility model with parameters:
29+
- `μ`: Drift of the asset price
30+
- `κ`: Mean reversion speed of variance
31+
- `θ`: Long-run variance
32+
- `σ`: Volatility of variance
33+
- `ρ`: Correlation between asset and variance processes
34+
"""
35+
struct HestonProcess <: AbstractStochasticProcess
36+
μ
37+
κ
38+
θ
39+
σ
40+
ρ
41+
end
42+
43+
# Drift function for GBM
44+
"""
45+
drift(process::GBMProcess, u, t)
46+
47+
Computes the drift term of the GBM process at time `t` for state `u`.
48+
Drift equation: `du/dt = μ * u`
49+
"""
50+
function drift(process::GBMProcess, u, t)
51+
return process.μ .* u # Element-wise broadcasting for array compatibility
52+
end
53+
54+
# Drift function for Heston
55+
"""
56+
drift(process::HestonProcess, u, t)
57+
58+
Computes the drift term of the Heston process at time `t` for state `u = (S, V)`.
59+
Drift equations:
60+
- `dS/dt = μ * S`
61+
- `dV/dt = κ * (θ - V)`
62+
"""
63+
function drift(process::HestonProcess, u, t)
64+
S, V = u
65+
return [process.μ * S, process.κ * (process.θ - V)] # Drift for (Stock price, Variance)
66+
end
67+
68+
# Diffusion function for GBM
69+
"""
70+
diffusion(process::GBMProcess, u, t)
71+
72+
Computes the diffusion term of the GBM process at time `t` for state `u`.
73+
Diffusion equation: `dW_t = σ * u * dB_t`
74+
"""
75+
function diffusion(process::GBMProcess, u, t)
76+
return process.σ .* u # Element-wise broadcasting for array compatibility
77+
end
78+
79+
# Diffusion function for Heston
80+
"""
81+
diffusion(process::HestonProcess, u, t)
82+
83+
Computes the diffusion term of the Heston process at time `t` for state `u = (S, V)`.
84+
Diffusion equations:
85+
- `dS_t = σ * sqrt(V) * dB_t`
86+
- `dV_t = ξ * sqrt(V) * dW_t`, with correlation `ρ`.
87+
"""
88+
function diffusion(process::HestonProcess, u, t)
89+
S, V = u
90+
return [process.σ * S * sqrt(V), process.σ * sqrt(V)] # Diffusion for (Stock price, Variance)
91+
end
92+
93+
# Higher-order functions to return drift! and diffusion!
94+
"""
95+
get_drift!(process::AbstractStochasticProcess)
96+
97+
Returns the in-place drift function `drift!` for the given process.
98+
"""
99+
function get_drift(process::P) where P<:AbstractStochasticProcess
100+
return (u, p, t) -> drift(process, u, t)
101+
end
102+
103+
"""
104+
get_diffusion!(process::AbstractStochasticProcess)
105+
106+
Returns the in-place diffusion function `diffusion!` for the given process.
107+
"""
108+
function get_diffusion(process::P) where P<:AbstractStochasticProcess
109+
return (u, p, t) -> diffusion(process, u, t)
110+
end
111+
112+
function get_analytic_solution(process::P) where P <: AbstractStochasticProcess
113+
return Nothing()
114+
end
115+
116+
function get_analytic_solution(::GBMProcess)
117+
return (u₀, p, t, W) -> u₀ * exp((0.05 - (0.2^2) / 2) * t + 0.2 * W)
118+
end
119+
120+
function get_sde_function(process::P) where P<:AbstractStochasticProcess
121+
return SDEFunction(get_drift(process), get_diffusion(process), analytic=get_analytic_solution(process))
122+
end
123+

0 commit comments

Comments
 (0)