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

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