Skip to content

Commit 8efa650

Browse files
committed
Adjusted FD for relative bumps
1 parent 9de48aa commit 8efa650

File tree

3 files changed

+64
-13
lines changed

3 files changed

+64
-13
lines changed

examples/comparisons/american.jl

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,16 @@ steps_lsm = 100
3636
seed = 42
3737

3838
# Create deterministic seeds for reproducibility
39-
path_seeds = rand(Xoshiro(seed), UInt64, trajectories)
40-
lsm_config = SimulationConfig(trajectories, steps=steps_lsm)
39+
seeds = Base.rand(UInt64, trajectories)
40+
lsm_config = SimulationConfig(trajectories, steps=steps_lsm, variance_reduction=Hedgehog2.Antithetic(), seeds=seeds)
4141
degree = 5 # Polynomial degree for regression
4242
lsm_method = LSM(dynamics, BlackScholesExact(), lsm_config, degree)
4343

4444
# ------------------------------
4545
# Define lenses for Greeks
4646
# ------------------------------
4747
vol_lens = VolLens(1,1)
48-
spot_lens = @optic _.market_inputs.spot
48+
spot_lens = Hedgehog2.SpotLens()
4949
rate_lens = ZeroRateSpineLens(1)
5050
lenses = (spot_lens, vol_lens, rate_lens)
5151

@@ -58,8 +58,9 @@ df_put = run_model_comparison_table(
5858
[crr_method, lsm_method],
5959
lenses;
6060
ad_method = ForwardAD(),
61-
fd_method = FiniteDifference(1e-4),
61+
fd_method = FiniteDifference(1e-2),
6262
analytic_method = nothing, # No analytic for American
63+
use_belapsed = true
6364
)
6465
println(df_put)
6566

@@ -84,7 +85,9 @@ df_call_div = run_model_comparison_table(
8485
[crr_method, lsm_method],
8586
lenses;
8687
ad_method = ForwardAD(),
87-
fd_method = FiniteDifference(1e-4),
88-
analytic_method = nothing # No analytic for American
88+
fd_method = FiniteDifference(1e-2),
89+
analytic_method = nothing, # No analytic for American
90+
use_belapsed = true
91+
8992
)
9093
println(df_call_div)

examples/polished_examples/lsm.jl

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using Revise, Hedgehog2, BenchmarkTools, Dates
2+
using Accessors
3+
import Accessors: @optic
4+
using Test
5+
using DataFrames
6+
using Random
7+
8+
# ------------------------------
9+
# Define payoff and pricing problem
10+
# ------------------------------
11+
strike = 100.0
12+
reference_date = Date(2020, 1, 1)
13+
expiry = reference_date + Year(1)
14+
rate = 0.05
15+
spot = 100.0
16+
sigma = 0.2
17+
18+
# --- American Put Option ---
19+
american_put = VanillaOption(strike, expiry, American(), Put(), Spot())
20+
market_inputs = BlackScholesInputs(reference_date, rate, spot, sigma)
21+
put_prob = PricingProblem(american_put, market_inputs)
22+
23+
# Create deterministic seeds for reproducibility
24+
seeds = Base.rand(UInt64, trajectories)
25+
lsm_config = SimulationConfig(trajectories, steps=steps_lsm, variance_reduction=Hedgehog2.Antithetic(), seeds=seeds)
26+
degree = 5 # Polynomial degree for regression
27+
lsm_method = LSM(dynamics, BlackScholesExact(), lsm_config, degree)
28+
29+
# ------------------------------
30+
# Define lenses for Greeks
31+
# ------------------------------
32+
vol_lens = VolLens(1,1)
33+
spot_lens = Hedgehog2.SpotLens()
34+
rate_lens = ZeroRateSpineLens(1)
35+
lenses = (spot_lens, vol_lens, rate_lens)
36+
37+
delta_prob = GreekProblem(put_prob, spot_lens)
38+
39+
ad_sol = solve(delta_prob, ForwardAD(), lsm_method)
40+
fd_sol = solve(delta_prob, FiniteDifference(1), lsm_method)
41+
42+
price_sol = solve(put_prob, lsm_method).price
43+
44+
market_inputs_bumped = BlackScholesInputs(reference_date, rate, spot + 1, sigma)
45+
put_prob_bumped = PricingProblem(american_put, market_inputs_bumped)
46+
47+
price_sol_bumped = solve(put_prob_bumped, lsm_method).price
48+

src/greeks/greeks_problem.jl

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,29 +46,29 @@ end
4646

4747
function compute_fd_derivative(::FDForward, prob, lens, ε, pricing_method)
4848
x₀ = lens(prob)
49-
prob_up = set(prob, lens, x₀ + ε)
49+
prob_up = set(prob, lens, x₀ * (1 + ε))
5050

5151
v_up = solve(prob_up, pricing_method).price
5252
v₀ = solve(prob, pricing_method).price
53-
return (v_up - v₀) / ε
53+
return (v_up - v₀) / (x₀ * ε)
5454
end
5555

5656
function compute_fd_derivative(::FDBackward, prob, lens, ε, pricing_method)
5757
x₀ = lens(prob)
58-
prob_down = set(prob, lens, x₀ - ε)
58+
prob_down = set(prob, lens, x₀ * (1 - ε))
5959
v_down = solve(prob_down, pricing_method).price
6060
v₀ = solve(prob, pricing_method).price
61-
return (v₀ - v_down) / ε
61+
return (v₀ - v_down) / (x₀ * ε)
6262
end
6363

6464
function compute_fd_derivative(::FDCentral, prob, lens, ε, pricing_method)
6565
x₀ = lens(prob)
6666

67-
prob_up = set(prob, lens, x₀ + ε)
68-
prob_down = set(prob, lens, x₀ - ε)
67+
prob_up = set(prob, lens, x₀ * (1 + ε))
68+
prob_down = set(prob, lens, x₀ * (1 - ε))
6969
v_up = solve(prob_up, pricing_method).price
7070
v_down = solve(prob_down, pricing_method).price
71-
return (v_up - v_down) / (2ε)
71+
return (v_up - v_down) / (2ε * x₀)
7272
end
7373

7474
function solve(

0 commit comments

Comments
 (0)