@@ -4,11 +4,20 @@ export ForwardAD, FiniteDifference, GreekProblem, SecondOrderGreekProblem
4
4
# Method types
5
5
abstract type GreekMethod end
6
6
7
+ abstract type FDScheme end
8
+
9
+ struct FDForward <: FDScheme end
10
+ struct FDBackward <: FDScheme end
11
+ struct FDCentral <: FDScheme end
12
+
7
13
struct ForwardAD <: GreekMethod end
8
- struct FiniteDifference <: GreekMethod
9
- bump:: Float64
14
+ struct FiniteDifference{S<: FDScheme } <: GreekMethod
15
+ bump
16
+ scheme:: S
10
17
end
11
18
19
+ FiniteDifference (bump) = FiniteDifference (bump, CentralFiniteDifference ())
20
+
12
21
# First-order GreekProblem
13
22
struct GreekProblem{P, L}
14
23
pricing_problem:: P
@@ -26,19 +35,38 @@ function solve(gprob::GreekProblem, ::ForwardAD, pricing_method::P) where P<:Abs
26
35
return (greek = deriv,)
27
36
end
28
37
29
- function solve (gprob:: GreekProblem , method:: FiniteDifference , pricing_method:: P ) where P<: AbstractPricingMethod
30
- prob = gprob. pricing_problem
31
- lens = gprob. wrt
32
- ε = method. bump
33
-
38
+ function compute_fd_derivative (:: ForwardFiniteDifference , prob, lens, ε, pricing_method)
34
39
x₀ = lens (prob)
35
40
prob_up = set (prob, lens, x₀ + ε)
41
+ v_up = solve (prob_up, pricing_method). price
42
+ v₀ = solve (prob, pricing_method). price
43
+ return (v_up - v₀) / ε
44
+ end
45
+
46
+ function compute_fd_derivative (:: BackwardFiniteDifference , prob, lens, ε, pricing_method)
47
+ x₀ = lens (prob)
36
48
prob_down = set (prob, lens, x₀ - ε)
49
+ v_down = solve (prob_down, pricing_method). price
50
+ v₀ = solve (prob, pricing_method). price
51
+ return (v₀ - v_down) / ε
52
+ end
37
53
54
+ function compute_fd_derivative (:: CentralFiniteDifference , prob, lens, ε, pricing_method)
55
+ x₀ = lens (prob)
56
+ prob_up = set (prob, lens, x₀ + ε)
57
+ prob_down = set (prob, lens, x₀ - ε)
38
58
v_up = solve (prob_up, pricing_method). price
39
59
v_down = solve (prob_down, pricing_method). price
60
+ return (v_up - v_down) / (2 ε)
61
+ end
62
+
63
+ function solve (gprob:: GreekProblem , method:: FiniteDifference{S} , pricing_method:: P ) where {S<: FiniteDifferenceScheme , P<: AbstractPricingMethod }
64
+ prob = gprob. pricing_problem
65
+ lens = gprob. wrt
66
+ ε = method. bump
67
+ scheme = method. scheme
40
68
41
- deriv = (v_up - v_down) / ( 2 ε )
69
+ deriv = compute_fd_derivative (scheme, prob, lens, ε, pricing_method )
42
70
return (greek = deriv,)
43
71
end
44
72
0 commit comments