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

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