lib/Math/Business/BlackScholes/Binaries/Greeks/Delta.pm

filter: Statementstmt 95.1% 99 / 104 Branchbran 72.2% 13 / 18 Conditioncond 66.6% 2 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 20 / 20 Podpod 0.0% 0 / 13 totaltotal 83.7% 134 / 160 CC 11 SCAR 12.4
CC
11
Cov
91%
x_common_function_pelsser_1997
9.1
onetouch
3.0
upordown
2.0
CRAP 3.4
↑ Summary lib/Math/Business/BlackScholes/Binaries/Greeks/Gamma.pm »
1 package Math::Business::BlackScholes::Binaries::Greeks::Delta;
2 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 3 our $VERSION = '0.06'; ## VERSION
6  
7 =head1 NAME
8  
9 Math::Business::BlackScholes::Binaries::Greeks::Delta
10  
11 =head1 DESCRIPTION
12  
13 Gets the delta for different options, Vanilla and Foreign for all contract types
14  
15 =head1 COMMENTS
16  
17 It is tricky to decide what form to use. Should the delta be with respect to
18 1/$S, or with respect to $S? For the binary bets, whether foreign or domestic
19 we are differentiating with respect to $S.
20  
21 For a vanilla, the correct way should be with respect to 1/$S (so that we know
22 how many units of the domestic currency to hedge), but to keep things standard,
23 we do it with respect to $S.
24  
25 For example take USDJPY vanilla call with premium in USD. Thus this is a vanilla
26 contract on JPY. Thus delta with respect to 1/$S tells us how many units of JPY
27 to hedge, but with respect to $S, there really isn't a meaning and needs to be
28 converted back before interpretation.
29  
30 =cut
31  
32 =head1 SUBROUTINES
33  
34 See L<Math::Business::BlackScholes::Binaries::Greeks>
35  
36 =cut
37  
38 3 ▶use List::Util qw(max);
Subroutine BEGIN: called CC=1 SCAR=0.0
39 3 ▶use Math::CDF qw(pnorm);
Subroutine BEGIN: called CC=1 SCAR=0.0
40 3 ▶use Math::Trig;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 3 ▶use Math::Business::BlackScholesMerton::Binaries;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 3 ▶use Math::Business::BlackScholes::Binaries::Greeks::Math qw( dgauss );
Subroutine BEGIN: called CC=1 SCAR=0.0
43  
44 6 ▶sub vanilla_call {
Subroutine vanilla_call: called CC=1 SCAR=0.0
Pod undocumented
45 6 my ($S, $K, $t, $r_q, $mu, $vol) = @_;
46  
47 6 my $d1 = (log($S / $K) + ($mu + $vol * $vol / 2.0) * $t) / ($vol * sqrt($t));
48  
49 6 return exp(($mu - $r_q) * $t) * pnorm($d1);
50 }
51  
52 6 ▶sub vanilla_put {
Subroutine vanilla_put: called CC=1 SCAR=0.0
Pod undocumented
53 6 my ($S, $K, $t, $r_q, $mu, $vol) = @_;
54  
55 6 my $d1 = (log($S / $K) + ($mu + $vol * $vol / 2.0) * $t) / ($vol * sqrt($t));
56  
57 6 return -exp(($mu - $r_q) * $t) * pnorm(-$d1);
58 }
59  
60 16 ▶sub call {
Subroutine call: called CC=1 SCAR=0.0
Pod undocumented
61 16 my ($S, $U, $t, $r_q, $mu, $vol) = @_;
62  
63 16 my $d2 = (log($S / $U) + ($mu - $vol * $vol / 2.0) * $t) / ($vol * sqrt($t));
64  
65 16 return exp(-$r_q * $t) * dgauss($d2) / ($vol * sqrt($t) * $S);
66 }
67  
68 16 ▶sub put {
Subroutine put: called CC=1 SCAR=0.0
Pod undocumented
69 16 my ($S, $D, $t, $r_q, $mu, $vol) = @_;
70  
71 16 my $d2 = (log($S / $D) + ($mu - $vol * $vol / 2.0) * $t) / ($vol * sqrt($t));
72  
73 16 return -exp(-$r_q * $t) * dgauss($d2) / ($vol * sqrt($t) * $S);
74 }
75  
76 10 ▶sub expirymiss {
Subroutine expirymiss: called CC=1 SCAR=0.0
Pod undocumented
77 10 my ($S, $U, $D, $t, $r_q, $mu, $vol) = @_;
78  
79 10 return call($S, $U, $t, $r_q, $mu, $vol) + put($S, $D, $t, $r_q, $mu, $vol);
80 }
81  
82 5 ▶sub expiryrange {
Subroutine expiryrange: called CC=1 SCAR=0.0
Pod undocumented
83 5 my ($S, $U, $D, $t, $r_q, $mu, $vol) = @_;
84  
85 5 return -1 * expirymiss($S, $U, $D, $t, $r_q, $mu, $vol);
86 }
87  
88 13 ▶sub onetouch {
Subroutine onetouch: called CC=3 SCAR=11.0
Pod undocumented
89 13 my ($S, $U, $t, $r_q, $mu, $vol, $w) = @_;
90  
91 # w = 0, rebate paid at hit.
92 # w = 1, rebate paid at end.
93 13 ▶ if (not defined $w) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $w) 7 6 13
94 7 $w = 0;
95 }
96  
97 13 my $sqrt_t = sqrt($t);
98  
99 13 my $theta_ = ($mu / $vol) - (0.5 * $vol);
100  
101 # Floor v_ squared near zero in case negative interest rates push it negative.
102 13 my $v_ = sqrt(max($Math::Business::BlackScholesMerton::Binaries::SMALL_VALUE_MU, ($theta_ * $theta_) + (2 * (1 - $w) * $r_q)));
103  
104 13 my $e = (log($S / $U) - ($vol * $v_ * $t)) / ($vol * $sqrt_t);
105  
106 13 my $e_ = (-log($S / $U) - ($vol * $v_ * $t)) / ($vol * $sqrt_t);
107  
108 13 ▶ my $eta = ($S > $U) ? 1 : -1;
Branch: 1 branch
BranchTrueFalseTotal
$S > $U ? : 4 9 13
109  
110 13 my $part1 =
111 ($theta_ + $v_) * pnorm(-$eta * $e) + $eta * dgauss($e) / $sqrt_t;
112 13 my $part2 =
113 ($theta_ - $v_) * pnorm($eta * $e_) + $eta * dgauss($e_) / $sqrt_t;
114  
115 13 my $delta = (($U / $S)**(($theta_ + $v_) / $vol)) * $part1 + (($U / $S)**(($theta_ - $v_) / $vol)) * $part2;
116  
117 13 return -$delta * exp(-$w * $r_q * $t) / ($vol * $S);
118 }
119  
120 6 ▶sub notouch {
Subroutine notouch: called CC=1 SCAR=0.0
Pod undocumented
121 6 my ($S, $U, $t, $r_q, $mu, $vol, $w) = @_;
122  
123 # No touch bet always pay out at end
124 6 $w = 1;
125  
126 6 return -1 * onetouch($S, $U, $t, $r_q, $mu, $vol, $w);
127 }
128  
129 13 ▶sub upordown {
Subroutine upordown: called CC=2 SCAR=6.9
Pod undocumented
130 13 my ($S, $U, $D, $t, $r_q, $mu, $vol, $w) = @_;
131  
132 # $w = 0, paid at hit
133 # $w = 1, paid at end
134 13 ▶ if (not defined $w) { $w = 0; }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $w) 7 6 13
135  
136 13 return ot_up_ko_down_pelsser_1997($S, $U, $D, $t, $r_q, $mu, $vol, $w) + ot_down_ko_up_pelsser_1997($S, $U, $D, $t, $r_q, $mu, $vol, $w);
137 }
138  
139 104 ▶sub x_common_function_pelsser_1997 {
Subroutine x_common_function_pelsser_1997: called CC=8 SCAR=22.1
Pod undocumented
140 104 my ($S, $U, $D, $t, $r_q, $mu, $vol, $w, $eta) = @_;
141  
142 104 my $pi = Math::Trig::pi;
143  
144 104 my $h = log($U / $D);
145 104 my $x = log($S / $D);
146  
147 # $eta = 1, onetouch up knockout down
148 # $eta = 0, onetouch down knockout up
149 # This variable used to check stability
150 104 ▶ if (not defined $eta) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $eta) 0 104 104
151 0 die
152 "$0: (x_common_function_pelsser_1997) Wrong usage of this function for S=$S, U=$U, D=$D, t=$t, r_q=$r_q, mu=$mu, vol=$vol, w=$w. eta not defined.";
153 }
154 104 ▶ if ($eta == 0) { $x = $h - $x; }
Branch: 1 branch
BranchTrueFalseTotal
if ($eta == 0) 52 52 104
155  
156 104 my $mu_new = $mu - (0.5 * $vol * $vol);
157 104 my $mu_dash =
158 sqrt(max($Math::Business::BlackScholesMerton::Binaries::SMALL_VALUE_MU, ($mu_new * $mu_new) + (2 * $vol * $vol * $r_q * (1 - $w))));
159  
160 104 my $series_part = 0;
161 104 my $hyp_part = 0;
162  
163 104 my $stability_constant =
164 Math::Business::BlackScholesMerton::Binaries::get_stability_constant_pelsser_1997($S, $U, $D, $t, $r_q, $mu, $vol, $w, $eta, 2);
165  
166 104 my $iterations_required = Math::Business::BlackScholesMerton::Binaries::get_min_iterations_pelsser_1997($S, $U, $D, $t, $r_q, $mu, $vol, $w);
167  
168 104 ▶ for (my $k = 1; $k < $iterations_required; $k++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k < $iterations_required) 2280 104 2384
169 2280 my $lambda_k_dash = (0.5 * (($mu_dash * $mu_dash) / ($vol * $vol) + ($k * $k * $pi * $pi * $vol * $vol) / ($h * $h)));
170  
171 2280 my $phi = ($vol * $vol) / ($h * $h * $h) * exp(-$lambda_k_dash * $t) * $k * $k / $lambda_k_dash;
172  
173 2280 $series_part += $phi * $pi * $pi * cos($k * $pi * ($h - $x) / $h);
174  
175 #
176 # For delta, the stability function is $phi/$S, for gamma it is different,
177 # but we shall ignore for now.
178 #
179 2280 ▶ if ($k == 1 and (not(abs($phi / $S) < $stability_constant))) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k == 1 and not abs $phi / $S < $stability_constant) 0 2280 2280
Condition66%2/3
$k == 1 and not abs $phi / $S < $stability_constant
!ll&&!rl&&r
21761040
MC/DC0%0/2
$k == 1 and not abs $phi / $S < $stability_constant
$k == 1 not abs $phi / $S < $stability_constant
Truth table66%2/3
A and not(B)
A= $k == 1B= not abs $phi / $S < $stability_constant
ABresult
0X0
100
111
180 0 die
181 "$0: PELSSER DELTA formula for S=$S, U=$U, D=$D, t=$t, r_q=$r_q, mu=$mu, vol=$vol, w=$w, eta=$eta cannot be evaluated because PELSSER DELTA stability conditions ($phi / $S less than $stability_constant) not met. This could be due to barriers too big, volatilities too low, interest/dividend rates too high, or machine accuracy too low.";
182 }
183 }
184  
185 # Need to take care when $mu goes to zero
186 104 ▶ if (abs($mu_new) < $Math::Business::BlackScholesMerton::Binaries::SMALL_VALUE_MU) {
Branch: 1 branch
BranchTrueFalseTotal
if (abs $mu_new < $Math::Business::BlackScholesMerton::Binaries::SMALL_VALUE_MU) 0 104 104
187 0 ▶ my $sign = ($mu_new >= 0) ? 1 : -1;
Branch: 1 branch
BranchTrueFalseTotal
$mu_new >= 0 ? : 0 0 0
188 0 $mu_new = $sign * $Math::Business::BlackScholesMerton::Binaries::SMALL_VALUE_MU;
189 0 $mu_dash = sqrt(($mu_new * $mu_new) + (2 * $vol * $vol * $r_q * (1 - $w)));
190 }
191  
192 104 $hyp_part = ($mu_dash / ($vol * $vol)) * (Math::Trig::cosh($mu_dash * $x / ($vol * $vol)) / Math::Trig::sinh($mu_dash * $h / ($vol * $vol)));
193  
194 104 my $dc_dx = ($hyp_part + $series_part) * exp(-$r_q * $t * $w);
195  
196 104 return $dc_dx;
197 }
198  
199 13 ▶sub ot_up_ko_down_pelsser_1997 {
Subroutine ot_up_ko_down_pelsser_1997: called CC=1 SCAR=0.0
Pod undocumented
200 13 my ($S, $U, $D, $t, $r_q, $mu, $vol, $w) = @_;
201  
202 13 my $mu_new = $mu - (0.5 * $vol * $vol);
203 13 my $h = log($U / $D);
204 13 my $x = log($S / $D);
205  
206 13 my $dVu_dx =
207 -(($mu_new / ($vol * $vol)) *
208 Math::Business::BlackScholesMerton::Binaries::common_function_pelsser_1997($S, $U, $D, $t, $r_q, $mu, $vol, $w, 1));
209  
210 13 $dVu_dx += x_common_function_pelsser_1997($S, $U, $D, $t, $r_q, $mu, $vol, $w, 1);
211 13 $dVu_dx *= exp($mu_new * ($h - $x) / ($vol * $vol));
212  
213 # dV/dS = dV/dx * dx/dS = dV/dx * 1/S
214 13 return $dVu_dx / $S;
215 }
216  
217 13 ▶sub ot_down_ko_up_pelsser_1997 {
Subroutine ot_down_ko_up_pelsser_1997: called CC=1 SCAR=0.0
Pod undocumented
218 13 my ($S, $U, $D, $t, $r_q, $mu, $vol, $w) = @_;
219  
220 13 my $mu_new = $mu - (0.5 * $vol * $vol);
221 13 my $x = log($S / $D);
222  
223 13 my $dVl_dx =
224 -(($mu_new / ($vol * $vol)) *
225 Math::Business::BlackScholesMerton::Binaries::common_function_pelsser_1997($S, $U, $D, $t, $r_q, $mu, $vol, $w, 0));
226  
227 13 $dVl_dx -= x_common_function_pelsser_1997($S, $U, $D, $t, $r_q, $mu, $vol, $w, 0);
228 13 $dVl_dx *= exp(-$mu_new * $x / ($vol * $vol));
229  
230 # dV/dS = dV/dx * dx/dS = dV/dx * 1/S
231 13 return $dVl_dx / $S;
232 }
233  
234 6 ▶sub range {
Subroutine range: called CC=1 SCAR=0.0
Pod undocumented
235 6 my ($S, $U, $D, $t, $r_q, $mu, $vol, $w) = @_;
236  
237 # Range always pay out at end
238 6 $w = 1;
239  
240 6 return -1 * upordown($S, $U, $D, $t, $r_q, $mu, $vol, $w);
241 }
242  
243 3 1;
244  
↑ Summary lib/Math/Business/BlackScholes/Binaries/Greeks/Gamma.pm »