Skip to content

Commit 873b18a

Browse files
committed
Testing heston exact against heston
1 parent dfd1a8d commit 873b18a

File tree

6 files changed

+24
-16
lines changed

6 files changed

+24
-16
lines changed

examples/Project.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
1010
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
1111
Pluto = "c3e4b0f8-55cb-11ea-2926-15256bba5781"
1212
PyPlot = "d330b81b-6aea-500a-939a-2ce795aea3ee"
13+
RandomNumbers = "e6cf234a-135c-5ec9-84dd-332b85af5143"
1314
Revise = "295af30f-e4ad-537b-8983-00126c2a3abe"

examples/heston_noise.jl

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
using Revise, Hedgehog2, Distributions, DifferentialEquations, Random, Plots
22

33
# Define Heston model parameters
4-
S0 = 100.0 # Initial stock price
4+
S0 = 1.0 # Initial stock price
55
V0 = 0.04 # Initial variance
66
κ = 2.0 # Mean reversion speed
77
θ = 0.04 # Long-run variance
8-
σ = 0.3 # Volatility of variance
8+
σ = 0.02 # Volatility of variance
99
ρ = -0.7 # Correlation
1010
r = 0.05 # Risk-free rate
1111
T = 1.0 # Time to maturity
@@ -19,9 +19,19 @@ heston_noise = Hedgehog2.HestonNoise(0.0, heston_dist)
1919
# Define `NoiseProblem`
2020
problem = NoiseProblem(heston_noise, (0.0, T))
2121

22+
trajectories = 10000
2223
# Solve with multiple trajectories
23-
solution = solve(EnsembleProblem(problem), dt=T, trajectories=10)
24+
solution_h = solve(EnsembleProblem(problem), dt=T, trajectories=trajectories)
2425

25-
# Directly plot the solution
26-
plot(solution, title="Exact Heston Model Simulations",
27-
xlabel="Time", ylabel="Log Stock Price")
26+
u0 = [S0, V0] # Initial (Stock Price, Variance)
27+
tspan = (0.0, 1.0) # Simulation for 1 year
28+
process = HestonProcess(r, κ, θ, σ, ρ) # Heston parameters
29+
prob = SDEProblem(get_sde_function(process), u0, tspan, process)
30+
ensemble_problem = EnsembleProblem(prob)
31+
solution_h2 = solve(ensemble_problem, dt=T, trajectories=trajectories)
32+
33+
final_prices_2 = [sol.u[end][1] for sol in solution_h2] # Extract S_T for each trajectory
34+
final_prices = [sol.u[end] for sol in solution_h] # Transform log-prices to prices
35+
36+
println(mean(final_prices_2.^3))
37+
println(mean(final_prices.^3))

examples/montecarlo_bs.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ strike = 1.2
1515
payoff = VanillaOption(strike, expiry, Hedgehog2.European(), Hedgehog2.Call(), Hedgehog2.Spot())
1616

1717
# Define montecarlo methods
18-
trajectories = 100000000
18+
trajectories = 10000
1919
method = Hedgehog2.BSMontecarlo(trajectories)
2020

2121
# Define pricer

src/distributions/heston.jl

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,20 @@ end
3030
3131
Samples `log(S_T)` using the exact Broadie-Kaya method.
3232
"""
33-
function rand(rng::AbstractRNG, d::HestonDistribution)
33+
function rand(rng, d::HestonDistribution)
3434
κ, θ, σ, ρ, V0, T, S0, r = d.κ, d.θ, d.σ, d.ρ, d.V0, d.T, d.S0, d.r
35-
3635
# 1. Sample V_T using the noncentral chi-squared distribution
37-
ν = (4κθ) / σ^2
36+
ν = (4κ * θ) / σ^2
3837
ψ = (4κ * exp(-κ * T) * V0) /^2 * (1 - exp(-κ * T)))
39-
V_T =^2 * (1 - exp(-κ * T)) / (4κ)) * rand(rng, NoncentralChisq(ν, ψ))
38+
V_T =^2 * (1 - exp(-κ * T)) / (4κ)) * Distributions.rand(rng, NoncentralChisq(ν, ψ))
4039

4140
# 2. Sample log(S_T) given V_T
4241
μ_XT = log(S0) + (r - 0.5 * V0) * T + (1 - exp(-κ * T)) *- V0) / (2κ)
4342
var_XT =^2 * V0 * (1 - exp(-κ * T))^2) / (2κ) + (1 - ρ^2) * (V0 * T + θ * (T - (1 - exp(-κ * T)) / κ))
4443

4544
log_S_T = μ_XT + sqrt(var_XT) * randn(rng) # Sample from Normal(μ_XT, sqrt(var_XT))
4645

47-
return log_S_T
46+
return exp(log_S_T)
4847
end
4948

5049
"""

src/pricing_methods/montecarlo.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,5 @@ function compute_price(payoff::VanillaOption{European, C, Spot}, market_inputs::
2222
final_payoffs = (payoff.(last.(solution.u)) + payoff.(last.(antithetic_solution.u))) * 0.5
2323
mean_payoff = mean(final_payoffs)
2424
var_payoff = var(final_payoffs)
25-
return mean_payoff, var_payoff
25+
return mean_payoff
2626
end

src/stochastic_processes/heston_noise.jl

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,5 @@ function HestonNoise(t0, heston_dist::HestonDistribution, Z0=nothing)
1717
return rand(rng, heston_dist) # Calls exact Heston sampler
1818
end
1919

20-
return NoiseProcess(t0, log_S0, Z0, Heston_dist, nothing)
20+
return NoiseProcess{false}(t0, log_S0, Z0, Heston_dist, (dW, W, W0, Wh, q, h, u, p, t, rng) -> 1)
2121
end
22-
23-

0 commit comments

Comments
 (0)