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

filter: Statementstmt 97.6% 123 / 126 Branchbran 81.2% 13 / 16 Conditioncond 66.6% 2 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 20 / 20 Podpod 0.0% 0 / 13 totaltotal 87.7% 158 / 180 CC 10 SCAR 10.2
CC
10
Cov
95%
w_common_function_pelsser_1997
7.2
onetouch
3.0
upordown
2.0
CRAP 2.8
« lib/Math/Business/BlackScholes/Binaries/Greeks/Theta.pm ↑ Summary lib/Math/Business/BlackScholes/Binaries/Greeks/Vanna.pm »
1 package Math::Business::BlackScholes::Binaries::Greeks::Vega;
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::Vega
9  
10 =head1 DESCRIPTION
11  
12 Gets the Vega for different options, Vanilla and Foreign for all our bet types
13  
14 =cut
15  
16 =head1 SUBROUTINES
17  
18 See L<Math::Business::BlackScholes::Binaries::Greeks>
19  
20 =cut
21  
22 1 ▶use List::Util qw( max );
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 ▶use Math::CDF qw( pnorm );
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 ▶use Math::Trig;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 1 ▶use Math::Business::BlackScholes::Binaries;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 1 ▶use Math::Business::BlackScholes::Binaries::Greeks::Math qw( dgauss );
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 sub vanilla_call {
29 36 ▶ my ( $S, $K, $t, $r_q, $mu, $vol ) = @_;
Subroutine vanilla_call: called CC=1 SCAR=0.0
Pod undocumented
30  
31 36 my $d1 =
32 ( log( $S / $K ) + ( $mu + $vol * $vol / 2.0 ) * $t ) /
33 ( $vol * sqrt($t) );
34 36 my $vega = $S * sqrt($t) * exp( ( $mu - $r_q ) * $t ) * dgauss($d1);
35 36 return $vega;
36 }
37  
38 sub vanilla_put {
39 6 ▶ my ( $S, $K, $t, $r_q, $mu, $vol ) = @_;
Subroutine vanilla_put: called CC=1 SCAR=0.0
Pod undocumented
40  
41 # Same as vega of vanilla call
42 6 return vanilla_call( $S, $K, $t, $r_q, $mu, $vol );
43 }
44  
45 sub call {
46 16 ▶ my ( $S, $U, $t, $r_q, $mu, $vol ) = @_;
Subroutine call: called CC=1 SCAR=0.0
Pod undocumented
47  
48 16 my $d1 =
49 ( log( $S / $U ) + ( $mu + $vol * $vol / 2.0 ) * $t ) /
50 ( $vol * sqrt($t) );
51 16 my $d2 = $d1 - $vol * sqrt($t);
52 16 my $vega = -exp( -$r_q * $t ) * dgauss($d2) * $d1 / $vol;
53 16 return $vega;
54 }
55  
56 sub put {
57 16 ▶ my ( $S, $D, $t, $r_q, $mu, $vol ) = @_;
Subroutine put: called CC=1 SCAR=0.0
Pod undocumented
58  
59 16 my $d1 =
60 ( log( $S / $D ) + ( $mu + $vol * $vol / 2.0 ) * $t ) /
61 ( $vol * sqrt($t) );
62 16 my $d2 = $d1 - $vol * sqrt($t);
63 16 my $vega = exp( -$r_q * $t ) * dgauss($d2) * $d1 / $vol;
64 16 return $vega;
65 }
66  
67 sub expirymiss {
68 10 ▶ my ( $S, $U, $D, $t, $r_q, $mu, $vol ) = @_;
Subroutine expirymiss: called CC=1 SCAR=0.0
Pod undocumented
69  
70 10 return call( $S, $U, $t, $r_q, $mu, $vol ) +
71 put( $S, $D, $t, $r_q, $mu, $vol );
72 }
73  
74 sub expiryrange {
75 5 ▶ my ( $S, $U, $D, $t, $r_q, $mu, $vol ) = @_;
Subroutine expiryrange: called CC=1 SCAR=0.0
Pod undocumented
76  
77 5 return -1 * expirymiss( $S, $U, $D, $t, $r_q, $mu, $vol );
78 }
79  
80 sub onetouch {
81 13 ▶ my ( $S, $U, $t, $r_q, $mu, $vol, $w ) = @_;
Subroutine onetouch: called CC=3 SCAR=11.0
Pod undocumented
82  
83 13 ▶ if ( not defined $w ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $w) 7 6 13
84 7 $w = 0;
85 }
86  
87 13 my $sqrt_t = sqrt($t);
88  
89 13 my $theta = ( $mu / $vol ) + ( 0.5 * $vol );
90  
91 13 my $theta_ = ( $mu / $vol ) - ( 0.5 * $vol );
92  
93 # Floor v_ squared at just above zero in case negative interest rates push it negative.
94 13 my $v_ = sqrt( max( $Math::Business::BlackScholes::Binaries::SMALL_VALUE_MU, ( $theta_ * $theta_ ) + ( 2 * ( 1 - $w ) * $r_q ) ) );
95  
96 13 my $e = ( log( $S / $U ) - ( $vol * $v_ * $t ) ) / ( $vol * $sqrt_t );
97  
98 13 my $e_ = ( -log( $S / $U ) - ( $vol * $v_ * $t ) ) / ( $vol * $sqrt_t );
99  
100 13 ▶ my $eta = ( $S > $U ) ? 1 : -1;
Branch: 1 branch
BranchTrueFalseTotal
$S > $U ? : 4 9 13
101  
102 13 my $pa_e =
103 ( log( $U / $S ) / ( $vol * $vol * $sqrt_t ) ) +
104 ( ( $theta_ * $theta ) / ( $vol * $v_ ) * $sqrt_t );
105 13 my $pa_e_ =
106 ( -log( $U / $S ) / ( $vol * $vol * $sqrt_t ) ) +
107 ( ( $theta_ * $theta ) / ( $vol * $v_ ) * $sqrt_t );
108 13 my $A =
109 -( $theta + $theta_ + ( $theta_ * $theta / $v_ ) + $v_ ) /
110 ( $vol * $vol );
111 13 my $A_ =
112 -( $theta + $theta_ - ( $theta_ * $theta / $v_ ) - $v_ ) /
113 ( $vol * $vol );
114  
115 13 my $part1 =
116 pnorm( -$eta * $e ) * $A * log( $U / $S ) - $eta * dgauss($e) * $pa_e;
117 13 my $part2 =
118 pnorm( $eta * $e_ ) * $A_ * log( $U / $S ) + $eta * dgauss($e_) * $pa_e_;
119 13 my $vega =
120 ( ( $U / $S )**( ( $theta_ + $v_ ) / $vol ) ) * $part1 +
121 ( ( $U / $S )**( ( $theta_ - $v_ ) / $vol ) ) * $part2;
122  
123 13 return $vega * exp( -$w * $r_q * $t );
124 }
125  
126 sub notouch {
127 6 ▶ my ( $S, $U, $t, $r_q, $mu, $vol, $w ) = @_;
Subroutine notouch: called CC=1 SCAR=0.0
Pod undocumented
128  
129 # No touch bet always pay out at end
130 6 $w = 1;
131  
132 6 return -1 * onetouch( $S, $U, $t, $r_q, $mu, $vol, $w );
133 }
134  
135 sub upordown {
136 13 ▶ my ( $S, $U, $D, $t, $r_q, $mu, $vol, $w ) = @_;
Subroutine upordown: called CC=2 SCAR=6.9
Pod undocumented
137  
138 # $w = 0, paid at hit
139 # $w = 1, paid at end
140 13 ▶ if ( not defined $w ) { $w = 0; }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $w) 7 6 13
141  
142 13 return ot_up_ko_down_pelsser_1997( $S, $U, $D, $t, $r_q, $mu, $vol, $w ) +
143 ot_down_ko_up_pelsser_1997( $S, $U, $D, $t, $r_q, $mu, $vol, $w );
144 }
145  
146 sub w_common_function_pelsser_1997 {
147 78 ▶ my ( $S, $U, $D, $t, $r_q, $mu, $vol, $w, $eta ) = @_;
Subroutine w_common_function_pelsser_1997: called CC=7 SCAR=19.7
Pod undocumented
148  
149 78 my $pi = Math::Trig::pi;
150  
151 78 my $h = log( $U / $D );
152 78 my $x = log( $S / $D );
153  
154 # $eta = 1, onetouch up knockout down
155 # $eta = 0, onetouch down knockout up
156 # This variable used to check stability
157 78 ▶ if ( not defined $eta ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $eta) 0 78 78
158 0 die
159 "$0: (w_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.";
160 }
161 78 ▶ if ( $eta == 0 ) { $x = $h - $x; }
Branch: 1 branch
BranchTrueFalseTotal
if ($eta == 0) 39 39 78
162  
163 78 my $r_dash = $r_q * ( 1 - $w );
164 78 my $mu_new = $mu - ( 0.5 * $vol * $vol );
165 78 my $mu_dash = sqrt( max( $Math::Business::BlackScholes::Binaries::SMALL_VALUE_MU, ( $mu_new * $mu_new ) + ( 2 * $vol * $vol * $r_dash ) ) );
166  
167 78 my $omega = ( $vol * $vol );
168  
169 78 my $series_part = 0;
170 78 my $hyp_part = 0;
171  
172 78 my $stability_constant =
173 Math::Business::BlackScholes::Binaries::get_stability_constant_pelsser_1997(
174 $S, $U, $D, $t, $r_q, $mu, $vol, $w, $eta, 1 );
175  
176 78 my $iterations_required =
177 Math::Business::BlackScholes::Binaries::get_min_iterations_pelsser_1997(
178 $S, $U, $D, $t, $r_q, $mu, $vol, $w );
179  
180 78 ▶ for ( my $k = 1 ; $k < $iterations_required ; $k++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k < $iterations_required) 1710 78 1788
181 1710 my $lambda_k_dash = (
182 0.5 * (
183 ( $mu_dash * $mu_dash ) / $omega +
184 ( $k * $k * $pi * $pi * $vol * $vol ) / ( $h * $h )
185 )
186 );
187  
188 # d{lambda_k}/dw
189 1710 my $dlambdak_domega =
190 0.5 *
191 ( -( $mu_new / $omega ) -
192 ( ( $mu_new * $mu_new ) / ( $omega * $omega ) ) +
193 ( ( $k * $k * $pi * $pi ) / ( $h * $h ) ) );
194  
195 1710 my $beta_k = exp( -$lambda_k_dash * $t ) / $lambda_k_dash;
196  
197 # d{beta_k}/d{lambda_k}
198 1710 my $dbetak_dlambdak =
199 -exp( -$lambda_k_dash * $t ) *
200 ( ( $t * $lambda_k_dash + 1 ) / ( $lambda_k_dash**2 ) );
201  
202 # d{beta_k}/dw
203 1710 my $dbetak_domega = $dlambdak_domega * $dbetak_dlambdak;
204  
205 1710 my $phi =
206 ( 1.0 / ( $h * $h ) ) * ( $omega * $dbetak_domega + $beta_k ) * $k;
207  
208 1710 $series_part += $phi * $pi * sin( $k * $pi * ( $h - $x ) / $h );
209  
210 #
211 # For vega, the stability function is 2* $vol * $phi, for volga/vanna it is different,
212 # but we shall ignore for now.
213 #
214 1710 ▶ if ( $k == 1
Branch: 1 branch
BranchTrueFalseTotal
if ($k == 1 and not abs 2 * $vol * $phi < $stability_constant) 0 1710 1710
Condition66%2/3
$k == 1 and not abs 2 * $vol * $phi < $stability_constant
!ll&&!rl&&r
1632780
MC/DC0%0/2
$k == 1 and not abs 2 * $vol * $phi < $stability_constant
$k == 1 not abs 2 * $vol * $phi < $stability_constant
Truth table66%2/3
A and not(B)
A= $k == 1B= not abs 2 * $vol * $phi < $stability_constant
ABresult
0X0
100
111
215 and ( not( abs( 2 * $vol * $phi ) < $stability_constant ) ) )
216 {
217 0 die
218 "$0: PELSSER VEGA 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 VEGA stability conditions (2 * $vol * $phi 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.";
219 }
220 }
221  
222 78 my $alpha = $mu_dash / ( $vol * $vol );
223 78 my $dalpha_domega =
224 -( ( $mu_new * $omega ) +
225 ( 2 * $mu_new * $mu_new ) +
226 ( 2 * $r_dash * $omega ) ) /
227 ( 2 * $alpha * $omega * $omega * $omega );
228  
229 # We have to handle the special case where the denominator approaches 0, see our documentation in
230 # quant/Documents/Breakout_bet.tex under the SVN "quant" module.
231 78 ▶ if ( ( Math::Trig::sinh( $alpha * $h )**2 ) == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (Math::Trig::sinh($alpha * $h) ** 2 == 0) { } 0 78 78
232 0 $hyp_part = 0;
233 }
234 else {
235 78 $hyp_part =
236 ( $dalpha_domega / ( 2 * ( Math::Trig::sinh( $alpha * $h )**2 ) ) ) *
237 ( ( $h + $x ) * Math::Trig::sinh( $alpha * ( $h - $x ) ) -
238 ( $h - $x ) * Math::Trig::sinh( $alpha * ( $h + $x ) ) );
239 }
240  
241 78 my $dc_domega = ( $hyp_part - $series_part ) * exp( -$r_q * $w * $t );
242  
243 78 return $dc_domega;
244 }
245  
246 sub ot_up_ko_down_pelsser_1997 {
247 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
248  
249 13 my $mu_new = $mu - ( 0.5 * $vol * $vol );
250 13 my $h = log( $U / $D );
251 13 my $x = log( $S / $D );
252 13 my $omega = ( $vol * $vol );
253  
254 13 my $c =
255 Math::Business::BlackScholes::Binaries::common_function_pelsser_1997( $S,
256 $U, $D, $t, $r_q, $mu, $vol, $w, 1 );
257 13 my $dc_domega =
258 w_common_function_pelsser_1997( $S, $U, $D, $t, $r_q, $mu, $vol, $w, 1 );
259  
260 13 my $dVu_domega =
261 -( ( 0.5 * $omega + $mu_new ) * ( $h - $x ) / ( $omega * $omega ) ) * $c;
262 13 $dVu_domega += $dc_domega;
263 13 $dVu_domega *= exp( $mu_new * ( $h - $x ) / $omega );
264  
265 13 return $dVu_domega * ( 2 * $vol );
266 }
267  
268 sub ot_down_ko_up_pelsser_1997 {
269 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
270  
271 13 my $mu_new = $mu - ( 0.5 * $vol * $vol );
272 13 my $h = log( $U / $D );
273 13 my $x = log( $S / $D );
274 13 my $omega = ( $vol * $vol );
275  
276 13 my $c =
277 Math::Business::BlackScholes::Binaries::common_function_pelsser_1997( $S,
278 $U, $D, $t, $r_q, $mu, $vol, $w, 0 );
279 13 my $dc_domega =
280 w_common_function_pelsser_1997( $S, $U, $D, $t, $r_q, $mu, $vol, $w, 0 );
281  
282 13 my $dVl_domega =
283 ( ( 0.5 * $omega + $mu_new ) * $x / ( $omega * $omega ) ) * $c;
284 13 $dVl_domega += $dc_domega;
285 13 $dVl_domega *= exp( -$mu_new * $x / $omega );
286  
287 13 return $dVl_domega * ( 2 * $vol );
288 }
289  
290 sub range {
291 6 ▶ my ( $S, $U, $D, $t, $r_q, $mu, $vol, $w ) = @_;
Subroutine range: called CC=1 SCAR=0.0
Pod undocumented
292  
293 # Range always pay out at end
294 6 $w = 1;
295  
296 6 return -1 * upordown( $S, $U, $D, $t, $r_q, $mu, $vol, $w );
297 }
298  
299 1 1;
300  
« lib/Math/Business/BlackScholes/Binaries/Greeks/Theta.pm ↑ Summary lib/Math/Business/BlackScholes/Binaries/Greeks/Vanna.pm »