1- using Revise, Hedgehog, BenchmarkTools, Dates
2-
3- """ Example code with benchmarks"""
4-
5- # Define market inputs
6- reference_date = Date (2020 , 1 , 1 )
7-
8- # Define Heston model parameters
9- S0 = 100 # Initial stock price
10- V0 = 0.010201 # Initial variance
11- κ = 6.21 # Mean reversion speed
12- θ = 0.019 # Long-run variance
13- σ = 0.61 # Volatility of variance
14- ρ = - 0.7 # Correlation
15- r = 0.0319 # Risk-free rate
16- T = 1.0 # Time to maturity
17- market_inputs = Hedgehog. HestonInputs (reference_date, r, S0, V0, κ, θ, σ, ρ)
18- bs_market_inputs = BlackScholesInputs (reference_date, r, S0, sqrt (V0))
19- # Define payoff
20- expiry = reference_date + Day (365 )
21- strike = 100
22- payoff =
23- VanillaOption (strike, expiry, Hedgehog. European (), Hedgehog. Call (), Hedgehog. Spot ())
24-
25- # Define carr madan method
26- boundary = 32
27- α = 1
28- method_heston = Hedgehog. CarrMadan (α, boundary, HestonDynamics ())
29-
30- # Define pricer
31- pricing_problem = PricingProblem (payoff, market_inputs)
32- analytic_sol = Hedgehog. solve (pricing_problem, method_heston)
33-
34- dynamics = HestonDynamics ()
35- trajectories = 10000
36- config = Hedgehog. SimulationConfig (trajectories; steps= 100 , variance_reduction= Hedgehog. NoVarianceReduction ())
37- config_exact = Hedgehog. SimulationConfig (trajectories; steps= 1 , variance_reduction= Hedgehog. NoVarianceReduction ())
38-
39- montecarlo_method = MonteCarlo (dynamics, EulerMaruyama (), config)
40- montecarlo_method_exact = MonteCarlo (dynamics, HestonBroadieKaya (), config_exact)
41-
42- solution = Hedgehog. solve (pricing_problem, montecarlo_method)
43- solution_exact = Hedgehog. solve (pricing_problem, montecarlo_method_exact)
44-
45- @show solution. price
46- @show analytic_sol. price
47- @show solution_exact. price
48-
49- @btime Hedgehog. solve ($ pricing_problem, $ montecarlo_method_exact). price
50- @btime Hedgehog. solve ($ pricing_problem, $ montecarlo_method). price
1+ using Test
2+ using Hedgehog
3+ using Dates
4+ using Random
515
6+ @testset " Heston Exact Simulation vs Euler Maruyama" begin
7+ # Define the vanilla option payoff
8+ strike = 100.0
9+ expiry_date = Date (2025 , 12 , 31 )
10+ payoff = VanillaOption (strike, expiry_date, European (), Call (), Spot ())
11+
12+ # Define the Heston model market inputs using positional arguments
13+ reference_date = Date (2025 , 1 , 1 )
14+ spot = 100.0
15+ rate = 0.05
16+ heston_inputs = HestonInputs (
17+ reference_date,
18+ rate,
19+ spot,
20+ 1.5 , # kappa
21+ 0.04 , # theta
22+ 0.3 , # sigma
23+ - 0.6 , # rho
24+ 0.04 # v0
25+ )
26+
27+ # Create the pricing problem
28+ problem = PricingProblem (payoff, heston_inputs)
29+
30+ # Generate a vector of seeds for reproducibility
31+ num_paths = 10_000
32+ rng = MersenneTwister (42 )
33+ seeds = rand (rng, UInt, 10 * num_paths)
34+
35+ # Pricing with Broadie-Kaya (Heston Exact) using Antithetic variance reduction
36+ mc_exact_method = MonteCarlo (
37+ HestonDynamics (),
38+ HestonBroadieKaya (),
39+ SimulationConfig (num_paths; seeds= seeds)
40+ )
41+ solution_exact = solve (problem, mc_exact_method)
42+ price_exact = solution_exact. price
43+
44+ # Pricing with Euler-Maruyama using Antithetic variance reduction
45+ mc_euler_method = MonteCarlo (
46+ HestonDynamics (),
47+ EulerMaruyama (),
48+ SimulationConfig (num_paths; steps= 200 , seeds= seeds, variance_reduction= Hedgehog. Antithetic ())
49+ )
50+ solution_euler = solve (problem, mc_euler_method)
51+ price_euler = solution_euler. price
52+
53+ # Pricing with Carr-Madan
54+ carr_madan_method = CarrMadan (1.0 , 32.0 , HestonDynamics ())
55+ solution_carr_madan = solve (problem, carr_madan_method)
56+ price_carr_madan = solution_carr_madan. price
57+
58+ # Compare the prices with a lower tolerance
59+ @test isapprox (price_exact, price_euler, rtol= 5e-2 )
60+ @test isapprox (price_exact, price_carr_madan, rtol= 2e-2 )
61+ end
62+
63+ @testset " Heston Exact Simulation vs Carr-Madan" begin
64+ # Define the vanilla option payoff
65+ strike = 100.0
66+ expiry_date = Date (2025 , 12 , 31 )
67+ payoff = VanillaOption (strike, expiry_date, European (), Call (), Spot ())
68+
69+ # Define the Heston model market inputs using positional arguments
70+ reference_date = Date (2025 , 1 , 1 )
71+ spot = 100.0
72+ rate = 0.05
73+ heston_inputs = HestonInputs (
74+ reference_date,
75+ rate,
76+ spot,
77+ 1.5 , # kappa
78+ 0.04 , # theta
79+ 0.3 , # sigma
80+ - 0.6 , # rho
81+ 0.04 # v0
82+ )
83+
84+ # Create the pricing problem
85+ problem = PricingProblem (payoff, heston_inputs)
86+
87+ # Generate a vector of seeds for reproducibility
88+ num_paths = 10_000
89+ rng = MersenneTwister (42 )
90+ seeds = rand (rng, UInt, num_paths)
91+
92+ # Pricing with Broadie-Kaya (Heston Exact) using Antithetic variance reduction
93+ mc_exact_method = MonteCarlo (
94+ HestonDynamics (),
95+ HestonBroadieKaya (),
96+ SimulationConfig (num_paths; seeds= seeds)
97+ )
98+ solution_exact = solve (problem, mc_exact_method)
99+ price_exact = solution_exact. price
100+
101+ # Pricing with Carr-Madan
102+ carr_madan_method = CarrMadan (1.0 , 32.0 , HestonDynamics ())
103+ solution_carr_madan = solve (problem, carr_madan_method)
104+ price_carr_madan = solution_carr_madan. price
105+
106+ # Compare the prices with a lower tolerance
107+ @test isapprox (price_exact, price_carr_madan, rtol= 2e-2 )
108+ end
0 commit comments