|
| 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