Skip to content

Commit ace54c1

Browse files
committed
Added montecarlo methods benchmark
1 parent 7fd8d8e commit ace54c1

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

examples/montecarlo_benchmark.jl

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
using Revise
2+
using Hedgehog
3+
using Dates
4+
using Printf
5+
using BenchmarkTools
6+
using Random
7+
8+
function test_eur()
9+
spot = 100.0
10+
strike = 100.0
11+
rate = 0.05
12+
sigma = 0.20
13+
reference_date = Date(2023, 1, 1)
14+
expiry = reference_date + Year(1)
15+
16+
# Create the payoff (European call option)
17+
payoff = VanillaOption(strike, expiry, European(), Call(), Spot())
18+
19+
# Create market inputs
20+
market_inputs = BlackScholesInputs(reference_date, rate, spot, sigma)
21+
22+
# Create pricing problem
23+
prob = PricingProblem(payoff, market_inputs)
24+
25+
trajectories = 5_000
26+
mc_exact_method =
27+
MonteCarlo(LognormalDynamics(), BlackScholesExact(), SimulationConfig(trajectories))
28+
mc_exact_solution = solve(prob, mc_exact_method)
29+
@show mc_exact_solution.price
30+
31+
display(@benchmark solve($prob, $mc_exact_method))
32+
end
33+
34+
function test_am()
35+
@show "american"
36+
# Define market inputs
37+
strike = 10.0
38+
reference_date = Date(2020, 1, 1)
39+
expiry = reference_date + Year(1)
40+
rate = 0.05
41+
spot = 10.0
42+
sigma = 0.2
43+
market_inputs = BlackScholesInputs(reference_date, rate, spot, sigma)
44+
45+
# Define payoff
46+
american_payoff = VanillaOption(strike, expiry, American(), Put(), Spot())
47+
48+
# -- Wrap everything into a pricing problem
49+
prob = PricingProblem(american_payoff, market_inputs)
50+
51+
# --- LSM using `solve(...)` style
52+
dynamics = LognormalDynamics()
53+
trajectories = 10_000
54+
steps_lsm = 100
55+
56+
strategy = BlackScholesExact()
57+
config = Hedgehog.SimulationConfig(trajectories; steps=steps_lsm, variance_reduction=Hedgehog.Antithetic()
58+
#variance_reduction=Hedgehog.NoVarianceReduction()
59+
)
60+
degree = 5
61+
lsm_method = LSM(dynamics, strategy, config, degree)
62+
63+
lsm_solution = Hedgehog.solve(prob, lsm_method)
64+
65+
@show lsm_solution.price
66+
67+
display(@benchmark Hedgehog.solve($prob, $lsm_method))
68+
end
69+
70+
test_eur()
71+
test_am()

0 commit comments

Comments
 (0)