Skip to content

Commit 99bbc46

Browse files
authored
Merge pull request #2 from aleCombi/Adding-Implied-Vol-Surface-to-BlackScholesInputs
Adding implied vol surface to black scholes inputs
2 parents 0b4d7dd + cffa4ef commit 99bbc46

28 files changed

+953
-476
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
**/Manifest.toml
2+
**/.vscode

Project.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ version = "1.0.0-DEV"
66
[deps]
77
Accessors = "7d9f7c33-5ae7-4f3b-8dc6-eff91059b697"
88
BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf"
9+
Bessels = "0e736298-9ec6-45e8-9647-e4fc86a2fe38"
910
DataInterpolations = "82cc6244-b520-54b8-b5a6-8a565e85f1d0"
1011
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
12+
DiffEqGPU = "071ae1c0-96b5-11e9-1965-c90190d839ea"
1113
DiffEqNoiseProcess = "77a26b50-5914-5dd7-bc55-306e6241c503"
1214
DifferentialEquations = "0c46a032-eb83-5123-abaf-570d42b7fbaa"
1315
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
@@ -20,6 +22,7 @@ Optimization = "7f7a1694-90dd-40f0-9382-eb1efda571ba"
2022
Polynomials = "f27b6e38-b328-58d1-80ce-0feddd5e7a45"
2123
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
2224
Roots = "f2b01f46-fcfa-551c-844a-d8ac1e96c665"
25+
SciMLBase = "0bca4576-84f4-4d90-8ffe-ffa030f20462"
2326
SpecialFunctions = "276daf66-3868-5448-9aa4-cd146d93841b"
2427
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
2528
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
@@ -28,8 +31,10 @@ Zygote = "e88e6eb3-aa80-5325-afca-941959d7151f"
2831
[compat]
2932
Accessors = "0.1.42"
3033
BenchmarkTools = "1.6.0"
34+
Bessels = "0.2.8"
3135
DataInterpolations = "7.2.0"
3236
Dates = "1.11.0"
37+
DiffEqGPU = "3.4.1"
3338
DiffEqNoiseProcess = "5.24.1"
3439
DifferentialEquations = "7.16.0"
3540
Distributions = "0.25"
@@ -42,6 +47,7 @@ Optimization = "4.1.2"
4247
Polynomials = "4.0.19"
4348
Random = "1.11.0"
4449
Roots = "2.2.6"
50+
SciMLBase = "2.77.2"
4551
SpecialFunctions = "2.5.0"
4652
StaticArrays = "1.9.13"
4753
Statistics = "1.11.1"

examples/Project.toml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,19 @@
11
[deps]
22
Accessors = "7d9f7c33-5ae7-4f3b-8dc6-eff91059b697"
33
BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf"
4+
Bessels = "0e736298-9ec6-45e8-9647-e4fc86a2fe38"
45
DataInterpolations = "82cc6244-b520-54b8-b5a6-8a565e85f1d0"
56
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
7+
DifferentialEquations = "0c46a032-eb83-5123-abaf-570d42b7fbaa"
8+
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
69
Hedgehog2 = "7f16798b-0e18-40de-98af-932948254698"
10+
Integrals = "de52edbc-65ea-441a-8357-d3a637375a31"
711
NonlinearSolve = "8913a72c-1f9b-4ce2-8d82-65094dcecaec"
812
Optimization = "7f7a1694-90dd-40f0-9382-eb1efda571ba"
13+
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
14+
Polynomials = "f27b6e38-b328-58d1-80ce-0feddd5e7a45"
15+
ProfileView = "c46f51b8-102a-5cf2-8d2c-8597cb0e0da7"
16+
Revise = "295af30f-e4ad-537b-8983-00126c2a3abe"
17+
Roots = "f2b01f46-fcfa-551c-844a-d8ac1e96c665"
18+
SpecialFunctions = "276daf66-3868-5448-9aa4-cd146d93841b"
19+
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"

examples/black_scholes_sensi.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ bs_method = BlackScholesAnalytic()
2727
# ------------------------------
2828
# Define lenses
2929
# ------------------------------
30-
vol_lens = @optic _.market.sigma
31-
spot_lens = @optic _.market.spot
30+
vol_lens = @optic _.market_inputs.sigma
31+
spot_lens = @optic _.market_inputs.spot
3232

3333
# ------------------------------
3434
# Vega (1st order w.r.t. sigma)

examples/carr_madan_heston.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,5 @@ method_heston = Hedgehog2.CarrMadan(α, boundary, HestonDynamics())
2929

3030
# Define pricer
3131

32-
println(solve(payoff, market_inputs, method_heston).price)
32+
println(solve(PricingProblem(payoff, market_inputs), method_heston).price)
3333

examples/lsm_binomial.jl

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using Revise, Hedgehog2, BenchmarkTools, Dates, Random, SciMLBase
1+
using Revise, Hedgehog2, BenchmarkTools, Dates, Random
22

33
# Define payoff
44
strike = 1.0
@@ -9,29 +9,41 @@ american_payoff = VanillaOption(strike, expiry, Hedgehog2.American(), Call(), Sp
99
reference_date = Date(2020, 1, 1)
1010
rate = 0.2
1111
spot = 1.0
12-
sigma = 0.4
12+
sigma = 0.03
1313
market_inputs = BlackScholesInputs(reference_date, rate, spot, sigma)
1414

1515
# -- Wrap everything into a pricing problem
1616
prob = PricingProblem(american_payoff, market_inputs)
1717

1818
# --- Cox–Ross–Rubinstein using `solve(...)` style
19-
steps_crr = 80
19+
steps_crr = 8
2020
crr_method = CoxRossRubinsteinMethod(steps_crr)
21-
crr_solution = solve(prob, crr_method)
21+
crr_solution = Hedgehog2.solve(prob, crr_method)
2222

2323
println("Cox Ross Rubinstein American Price:")
2424
println(crr_solution.price)
2525

2626
# --- LSM using `solve(...)` style
2727
dynamics = LognormalDynamics()
28-
trajectories = 1000
28+
trajectories = 10000
2929
steps_lsm = 100
3030

31-
strategy = BlackScholesExact(trajectories, steps_lsm)
32-
degree = 3
33-
lsm_method = LSM(dynamics, strategy, degree)
34-
lsm_solution = solve(prob, lsm_method)
31+
strategy = BlackScholesExact()
32+
config = Hedgehog2.SimulationConfig(trajectories; steps=steps_lsm, variance_reduction=Hedgehog2.Antithetic())
33+
degree = 5
34+
lsm_method = LSM(dynamics, strategy, config, degree)
35+
lsm_solution = Hedgehog2.solve(prob, lsm_method)
3536

3637
println("LSM American Price:")
3738
println(lsm_solution.price)
39+
40+
euro_prob = PricingProblem(
41+
VanillaOption(strike, expiry, Hedgehog2.European(), Call(), Spot()),
42+
market_inputs,
43+
)
44+
black_scholes_method = BlackScholesAnalytic()
45+
bs_solution = Hedgehog2.solve(euro_prob, black_scholes_method)
46+
println("Black Scholes European Price:")
47+
println(bs_solution.price)
48+
49+
@btime Hedgehog2.solve(prob, lsm_method)

examples/montecarlo_seeds.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using DifferentialEquations
2+
3+
μ = 0.02
4+
σ = 0.04
5+
t0 = 0.0
6+
W0 = 0.0
7+
tspan = (0.0,1.0)
8+
trajectories = 100
9+
proc = GeometricBrownianMotionProcess(μ, σ, t0, W0, nothing)
10+
noise = NoiseProblem(proc, tspan)
11+
seeds = rand(UInt64,trajectories)
12+
prob_func = (prob, i, repeat) -> remake(prob; seed=seeds[i])
13+
problem = EnsembleProblem(noise; prob_func=prob_func)
14+
sol = solve(problem, EM(); trajectories=trajectories, dt=1.0)

examples/polished_examples/american_options.jl

Whitespace-only changes.
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
using Revise, Hedgehog2, BenchmarkTools, Dates
2+
using Accessors
3+
import Accessors: @optic
4+
5+
# ------------------------------
6+
# Define payoff and pricing problem
7+
# ------------------------------
8+
strike = 1.0
9+
expiry = Date(2020, 1, 2)
10+
underlying = Hedgehog2.Forward()
11+
12+
euro_payoff = VanillaOption(strike, expiry, European(), Put(), underlying)
13+
14+
reference_date = Date(2020, 1, 1)
15+
rate = 0.03
16+
spot = 1.0
17+
sigma = 1.0
18+
19+
market_inputs = BlackScholesInputs(reference_date, rate, spot, sigma)
20+
euro_pricing_prob = PricingProblem(euro_payoff, market_inputs)
21+
22+
# ------------------------------
23+
# Pricing method
24+
# ------------------------------
25+
bs_method = BlackScholesAnalytic()
26+
27+
# ------------------------------
28+
# Define lenses
29+
# ------------------------------
30+
vol_lens = @optic _.market_inputs.sigma
31+
spot_lens = @optic _.market_inputs.spot
32+
33+
# ------------------------------
34+
# Delta (1st order w.r.t. spot)
35+
# ------------------------------
36+
delta_prob = Hedgehog2.GreekProblem(euro_pricing_prob, spot_lens)
37+
fd_method = FiniteDifference(1E-4, Hedgehog2.FDForward())
38+
ad_method = ForwardAD()
39+
analytic_method = AnalyticGreek()
40+
pricing_method = BlackScholesAnalytic()
41+
42+
@btime solve($euro_pricing_prob, $pricing_method)
43+
@btime solve($delta_prob, $fd_method, $pricing_method)
44+
@btime solve($delta_prob, $ad_method, $pricing_method)
45+
@btime solve($delta_prob, $analytic_method, $pricing_method)
46+
47+
rate_greek_prob = GreekProblem(euro_pricing_prob, ZeroRateSpineLens(1))
48+
solve(rate_greek_prob, ad_method, pricing_method)
49+
@btime solve($rate_greek_prob, $ad_method, $pricing_method)
50+
@btime solve($rate_greek_prob, $fd_method, $pricing_method)
51+
52+
vol_greek_prob = GreekProblem(euro_pricing_prob, VolLens(1,1))
53+
@btime solve($vol_greek_prob, $ad_method, $pricing_method)
54+
55+
56+
steps = 80
57+
crr = CoxRossRubinsteinMethod(steps)
58+
solve(euro_pricing_prob, crr)

examples/carr_madan.jl renamed to examples/polished_examples/carr_madan.jl

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,38 +20,41 @@ method_analytic = BlackScholesAnalytic()
2020
method_carr_madan = CarrMadan(1.0, 16.0, LognormalDynamics())
2121

2222
# -- Solve for prices
23-
sol_analytic = solve(prob, method_analytic)
24-
sol_carr = solve(prob, method_carr_madan)
23+
sol_analytic = Hedgehog2.solve(prob, method_analytic)
24+
sol_carr = Hedgehog2.solve(prob, method_carr_madan)
25+
26+
@btime Hedgehog2.solve($prob, $method_analytic)
27+
@btime Hedgehog2.solve($prob, $method_carr_madan)
2528

2629
println("Analytic price: ", sol_analytic.price)
2730
println("Carr-Madan price: ", sol_carr.price)
2831

2932
# --- Greeks via GreekProblem
3033

3134
# Accessors
32-
spot_lens = @optic _.market.spot
33-
sigma_lens = @optic _.market.sigma
35+
spot_lens = @optic _.market_inputs.spot
36+
sigma_lens = Hedgehog2.VolLens(1,1)
3437

3538
# Methods
3639
fd = FiniteDifference(1e-3)
3740
ad = ForwardAD()
3841

3942
println("\n--- Greeks (Analytic Method) ---")
40-
delta_fd = solve(GreekProblem(prob, spot_lens), fd, method_analytic).greek
41-
vega_fd = solve(GreekProblem(prob, sigma_lens), fd, method_analytic).greek
42-
delta_ad = solve(GreekProblem(prob, spot_lens), ad, method_analytic).greek
43-
vega_ad = solve(GreekProblem(prob, sigma_lens), ad, method_analytic).greek
43+
delta_fd = Hedgehog2.solve(GreekProblem(prob, spot_lens), fd, method_analytic).greek
44+
vega_fd = Hedgehog2.solve(GreekProblem(prob, sigma_lens), fd, method_analytic).greek
45+
delta_ad = Hedgehog2.solve(GreekProblem(prob, spot_lens), ad, method_analytic).greek
46+
vega_ad = Hedgehog2.solve(GreekProblem(prob, sigma_lens), ad, method_analytic).greek
4447

4548
println("FD Delta (analytic): ", delta_fd)
4649
println("AD Delta (analytic): ", delta_ad)
4750
println("FD Vega (analytic): ", vega_fd)
4851
println("AD Vega (analytic): ", vega_ad)
4952

5053
println("\n--- Greeks (Carr-Madan Method) ---")
51-
delta_fd_cm = solve(GreekProblem(prob, spot_lens), fd, method_carr_madan).greek
52-
vega_fd_cm = solve(GreekProblem(prob, sigma_lens), fd, method_carr_madan).greek
53-
delta_ad_cm = solve(GreekProblem(prob, spot_lens), ad, method_carr_madan).greek
54-
vega_ad_cm = solve(GreekProblem(prob, sigma_lens), ad, method_carr_madan).greek
54+
delta_fd_cm = Hedgehog2.solve(GreekProblem(prob, spot_lens), fd, method_carr_madan).greek
55+
vega_fd_cm = Hedgehog2.solve(GreekProblem(prob, sigma_lens), fd, method_carr_madan).greek
56+
delta_ad_cm = Hedgehog2.solve(GreekProblem(prob, spot_lens), ad, method_carr_madan).greek
57+
vega_ad_cm = Hedgehog2.solve(GreekProblem(prob, sigma_lens), ad, method_carr_madan).greek
5558

5659
println("FD Delta (Carr-Madan): ", delta_fd_cm)
5760
println("AD Delta (Carr-Madan): ", delta_ad_cm)

0 commit comments

Comments
 (0)