Skip to content

Commit aa94b46

Browse files
committed
Testing analytical bs greeks
1 parent 04c018b commit aa94b46

File tree

3 files changed

+56
-1
lines changed

3 files changed

+56
-1
lines changed

examples/black_scholes_sensi.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ println("Volga (Finite Diff): $volga_fd")
115115
# ------------------------------
116116
# Theta (1st order w.r.t. expiry)
117117
# ------------------------------
118+
# note that derivatives are in ticks, hence very small
118119
thetaproblem = Hedgehog2.GreekProblem(euro_pricing_prob, @optic _.payoff.expiry)
119120
theta_ad = solve(thetaproblem, ForwardAD(), bs_method).greek
120121
theta_fd = solve(thetaproblem, FiniteDifference(1), bs_method).greek

src/greeks/greeks_problem.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ function solve(gprob::GreekProblem, ::AnalyticGreek, ::BlackScholesAnalytic)
134134
T = yearfrac(prob.market.referenceDate, prob.payoff.expiry)
135135
K = prob.payoff.strike
136136

137-
D = df(prob.market.rate, T)
137+
D = df(prob.market.rate, prob.payoff.expiry)
138138
F = prob.market.spot / D
139139
T = sqrt(T)
140140
d1 = (log(F / K) + 0.5 * σ^2 * T) /* T)

test/greeks.jl

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,57 @@ import Accessors: @optic
6161
@test isapprox(ad_val, fd_val; rtol=1e-5)
6262
end
6363
end
64+
65+
using Test
66+
using Hedgehog2
67+
using Dates
68+
using Accessors
69+
import Accessors: @optic
70+
71+
@testset "Greeks Agreement Test" begin
72+
strike = 1.0
73+
expiry = Date(2020, 1, 2)
74+
reference_date = Date(2020, 1, 1)
75+
rate = 0.03
76+
spot = 1.0
77+
sigma = 1.0
78+
79+
underlying = Hedgehog2.Forward()
80+
payoff = VanillaOption(strike, expiry, European(), Put(), underlying)
81+
market_inputs = BlackScholesInputs(reference_date, rate, spot, sigma)
82+
pricing_prob = PricingProblem(payoff, market_inputs)
83+
bs_method = BlackScholesAnalytic()
84+
85+
vol_lens = @optic _.market.sigma
86+
spot_lens = @optic _.market.spot
87+
88+
# Vega
89+
gprob = GreekProblem(pricing_prob, vol_lens)
90+
vega_ad = solve(gprob, ForwardAD(), bs_method).greek
91+
vega_fd = solve(gprob, FiniteDifference(1e-4), bs_method).greek
92+
vega_an = solve(gprob, AnalyticGreek(), bs_method).greek
93+
@test isapprox(vega_ad, vega_fd; rtol=1e-5)
94+
@test isapprox(vega_ad, vega_an; rtol=1e-5)
95+
96+
# Gamma
97+
gammaprob = SecondOrderGreekProblem(pricing_prob, spot_lens, spot_lens)
98+
gamma_ad = solve(gammaprob, ForwardAD(), bs_method).greek
99+
gamma_fd = solve(gammaprob, FiniteDifference(1e-4), bs_method).greek
100+
gamma_an = solve(gammaprob, AnalyticGreek(), bs_method).greek
101+
@test isapprox(gamma_ad, gamma_fd; rtol=1e-5)
102+
@test isapprox(gamma_ad, gamma_an; rtol=1e-5)
103+
104+
# Volga
105+
volgaprob = SecondOrderGreekProblem(pricing_prob, vol_lens, vol_lens)
106+
volga_ad = solve(volgaprob, ForwardAD(), bs_method).greek
107+
volga_fd = solve(volgaprob, FiniteDifference(1e-4), bs_method).greek
108+
volga_an = solve(volgaprob, AnalyticGreek(), bs_method).greek
109+
@test isapprox(volga_ad, volga_fd; rtol=1e-3)
110+
@test isapprox(volga_ad, volga_an; rtol=1e-5)
111+
112+
# Theta (no analytic yet)
113+
thetaprob = GreekProblem(pricing_prob, @optic _.payoff.expiry)
114+
theta_ad = solve(thetaprob, ForwardAD(), bs_method).greek
115+
theta_fd = solve(thetaprob, FiniteDifference(1), bs_method).greek
116+
@test isapprox(theta_ad, theta_fd; rtol=1e-5)
117+
end

0 commit comments

Comments
 (0)