blib/lib/Math/Business/BlackScholesMerton/NonBinaries.pm

filter: Statementstmt 96.5% 113 / 117 Branchbran 83.3% 15 / 18 Conditioncond 66.6% 2 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 91.3% 21 / 23 Podpod 100% 12 / 12 totaltotal 93.1% 163 / 175 CC 11 SCAR 9.5
CC
11
Cov
94%
standardbarrier
7.1
_l_max
2.0
_l_min
2.0
CRAP 2.6
« lib/Math/Business/BlackScholesMerton/Binaries.pm ↑ Summary blib/lib/Math/Business/BlackScholesMerton.pm »
1 package Math::Business::BlackScholesMerton::NonBinaries;
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 4 ▶use List::Util qw(min max);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use Math::CDF qw(pnorm);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use POSIX qw(ceil);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use Machine::Epsilon;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 4 ▶use constant PI => 3.14159265359;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 4 our $VERSION = '1.25'; ## VERSION
14  
15 =head1 NAME
16  
17 Math::Business::BlackScholesMerton::NonBinaries
18  
19 =head1 SYNOPSIS
20  
21 use Math::Business::BlackScholesMerton::NonBinaries;
22  
23 # price of a Call spread option
24 my $price_call_option = Math::Business::BlackScholesMerton::NonBinaries::vanilla_call(
25 1.35, # stock price
26 1.34, # barrier
27 (7/365), # time
28 0.002, # payout currency interest rate (0.05 = 5%)
29 0.001, # quanto drift adjustment (0.05 = 5%)
30 0.11, # volatility (0.3 = 30%)
31 );
32  
33 =head1 DESCRIPTION
34  
35 Contains non-binary option pricing formula.
36  
37 =cut
38  
39 =head2 vanilla_call
40  
41 USAGE
42 my $price = vanilla_call($S, $K, $t, $r_q, $mu, $sigma);
43  
44 DESCRIPTION
45 Price of a Vanilla Call
46  
47 =cut
48  
49 2 ▶sub vanilla_call {
Subroutine vanilla_call: called CC=1 SCAR=0.0
Pod documented
50 2 my ($S, $K, $t, $r_q, $mu, $sigma) = @_;
51  
52 2 my $d1 = (log($S / $K) + ($mu + $sigma * $sigma / 2.0) * $t) / ($sigma * sqrt($t));
53 2 my $d2 = $d1 - ($sigma * sqrt($t));
54  
55 2 return exp(-$r_q * $t) * ($S * exp($mu * $t) * pnorm($d1) - $K * pnorm($d2));
56 }
57  
58 =head2 vanilla_put
59  
60 USAGE
61 my $price = vanilla_put($S, $K, $t, $r_q, $mu, sigma)
62  
63 DESCRIPTION
64 Price a standard Vanilla Put
65  
66 =cut
67  
68 2 ▶sub vanilla_put {
Subroutine vanilla_put: called CC=1 SCAR=0.0
Pod documented
69 2 my ($S, $K, $t, $r_q, $mu, $sigma) = @_;
70  
71 2 my $d1 = (log($S / $K) + ($mu + $sigma * $sigma / 2.0) * $t) / ($sigma * sqrt($t));
72 2 my $d2 = $d1 - ($sigma * sqrt($t));
73  
74 2 return -1 * exp(-$r_q * $t) * ($S * exp($mu * $t) * pnorm(-$d1) - $K * pnorm(-$d2));
75 }
76  
77 =head2 lbfloatcall
78  
79 USAGE
80 my $price = lbfloatcall($S, $K, $t, $r_q, $mu, $sigma, $S_max, $S_min)
81  
82 DESCRIPTION
83 Price of a Lookback Float Call
84  
85 =cut
86  
87 3 ▶sub lbfloatcall {
Subroutine lbfloatcall: called CC=1 SCAR=0.0
Pod documented
88 3 my ($S, $K, $t, $r_q, $mu, $sigma, $S_max, $S_min) = @_;
89  
90 3 $S_max = undef;
91 3 my $d1 = _d1_function($S, $S_min, $t, $r_q, $mu, $sigma);
92 3 my $d2 = $d1 - ($sigma * sqrt($t));
93  
94 3 my $value = exp(-$r_q * $t) * ($S * exp($mu * $t) * pnorm($d1) - $S_min * pnorm($d2) + _l_min($S, $S_min, $t, $r_q, $mu, $sigma));
95  
96 3 return $value;
97 }
98  
99 =head2 lbfloatput
100  
101 USAGE
102 my $price = lbfloatcall($S, $K, $t, $r_q, $mu, $sigma, $S_max, $S_min)
103  
104 DESCRIPTION
105 Price of a Lookback Float Put
106  
107 =cut
108  
109 3 ▶sub lbfloatput { # Floating Strike Put
Subroutine lbfloatput: called CC=1 SCAR=0.0
Pod documented
110 3 my ($S, $K, $t, $r_q, $mu, $sigma, $S_max, $S_min) = @_;
111  
112 3 $S_min = undef;
113 3 my $d1 = _d1_function($S, $S_max, $t, $r_q, $mu, $sigma);
114 3 my $d2 = $d1 - ($sigma * sqrt($t));
115  
116 3 my $value = exp(-$r_q * $t) * ($S_max * pnorm(-$d2) - $S * exp($mu * $t) * pnorm(-$d1) + _l_max($S, $S_max, $t, $r_q, $mu, $sigma));
117  
118 3 return $value;
119 }
120  
121 =head2 lbfixedcall
122  
123 USAGE
124 my $price = lbfixedcall($S, $K, $t, $r_q, $mu, $sigma, $S_max, $S_min)
125  
126 DESCRIPTION
127 Price of a Lookback Fixed Call
128  
129 =cut
130  
131 2 ▶sub lbfixedcall {
Subroutine lbfixedcall: called CC=1 SCAR=0.0
Pod documented
132 2 my ($S, $K, $t, $r_q, $mu, $sigma, $S_max, $S_min) = @_;
133  
134 2 $S_min = undef;
135 2 my $K_max = max($S_max, $K);
136 2 my $d1 = _d1_function($S, $K_max, $t, $r_q, $mu, $sigma);
137 2 my $d2 = $d1 - ($sigma * sqrt($t));
138  
139 2 my $value =
140 exp(-$r_q * $t) * (max($S_max - $K, 0.0) + $S * exp($mu * $t) * pnorm($d1) - $K_max * pnorm($d2) + _l_max($S, $K_max, $t, $r_q, $mu, $sigma));
141  
142 2 return $value;
143 }
144  
145 =head2 lbfixedput
146  
147 USAGE
148 my $price = lbfixedput($S, $K, $t, $r_q, $mu, $sigma, $S_max, $S_min)
149  
150 DESCRIPTION
151 Price of a Lookback Fixed Put
152  
153 =cut
154  
155 2 ▶sub lbfixedput {
Subroutine lbfixedput: called CC=1 SCAR=0.0
Pod documented
156 2 my ($S, $K, $t, $r_q, $mu, $sigma, $S_max, $S_min) = @_;
157  
158 2 $S_max = undef;
159 2 my $K_min = min($S_min, $K);
160 2 my $d1 = _d1_function($S, $K_min, $t, $r_q, $mu, $sigma);
161 2 my $d2 = $d1 - ($sigma * sqrt($t));
162  
163 2 my $value = exp(-$r_q * $t) *
164 (max($K - $S_min, 0.0) + $K_min * pnorm(-$d2) - $S * exp($mu * $t) * pnorm(-$d1) + _l_min($S, $K_min, $t, $r_q, $mu, $sigma));
165  
166 2 return $value;
167 }
168  
169 =head2 lbhighlow
170  
171 USAGE
172 my $price = lbhighlow($S, $K, $t, $r_q, $mu, $sigma, $S_max, $S_min)
173  
174 DESCRIPTION
175 Price of a Lookback High Low
176  
177 =cut
178  
179 1 ▶sub lbhighlow {
Subroutine lbhighlow: called CC=1 SCAR=0.0
Pod documented
180 1 my ($S, $K, $t, $r_q, $mu, $sigma, $S_max, $S_min) = @_;
181  
182 1 my $value = lbfloatcall($S, $S_min, $t, $r_q, $mu, $sigma, $S_max, $S_min) + lbfloatput($S, $S_max, $t, $r_q, $mu, $sigma, $S_max, $S_min);
183  
184 1 return $value;
185 }
186  
187 =head2 _d1_function
188  
189 returns the d1 term common to many BlackScholesMerton formulae.
190  
191 =cut
192  
193 20 ▶sub _d1_function {
Subroutine _d1_function: called CC=1 SCAR=0.0
194 20 my ($S, $K, $t, $r_q, $mu, $sigma) = @_;
195  
196 20 my $value = (log($S / $K) + ($mu + $sigma * $sigma * 0.5) * $t) / ($sigma * sqrt($t));
197  
198 20 return $value;
199 }
200  
201 =head2 _l_max
202  
203 This is a common function use to calculate the lookbacks options price. See [5] for details.
204  
205 =cut
206  
207 5 ▶sub _l_max {
Subroutine _l_max: called CC=2 SCAR=6.9
208 5 my ($S, $K, $t, $r_q, $mu, $sigma) = @_;
209  
210 5 my $d1 = _d1_function($S, $K, $t, $r_q, $mu, $sigma);
211 5 my $value;
212  
213 5 ▶ if ($mu) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mu) { } 3 2 5
214 3 $value =
215 $S *
216 ($sigma**2) /
217 (2.0 * $mu) *
218 (-($S / $K)**(-2.0 * $mu / ($sigma**2)) * pnorm($d1 - 2.0 * $mu / $sigma * sqrt($t)) + exp($mu * $t) * pnorm($d1));
219 } else {
220 2 $value = $S * ($sigma * sqrt($t)) * (dnorm($d1) + $d1 * pnorm($d1));
221 }
222  
223 5 return $value;
224 }
225  
226 =head2 _l_min
227  
228 This is a common function use to calculate the lookbacks options price. See [5] for details.
229  
230 =cut
231  
232 5 ▶sub _l_min {
Subroutine _l_min: called CC=2 SCAR=6.9
233 5 my ($S, $K, $t, $r_q, $mu, $sigma) = @_;
234  
235 5 my $d1 = _d1_function($S, $K, $t, $r_q, $mu, $sigma);
236 5 my $value;
237  
238 5 ▶ if ($mu) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mu) { } 3 2 5
239 3 $value =
240 $S *
241 ($sigma**2) /
242 (2.0 * $mu) *
243 (($S / $K)**(-2.0 * $mu / ($sigma**2)) * pnorm(-$d1 + 2.0 * $mu / $sigma * sqrt($t)) - exp($mu * $t) * pnorm(-$d1));
244 } else {
245 2 $value = $S * ($sigma * sqrt($t)) * (dnorm($d1) + $d1 * (pnorm($d1) - 1));
246 }
247  
248 5 return $value;
249 }
250  
251 =head2 dnorm
252  
253 Standard normal density function
254  
255 =cut
256  
257 4 ▶sub dnorm { # Standard normal density function
Subroutine dnorm: called CC=1 SCAR=0.0
Pod documented
258 4 my $x = shift;
259  
260 4 my $value = exp(-$x**2 / 2) / sqrt(2.0 * PI);
261  
262 4 return $value;
263 }
264  
265 =head2 callspread
266  
267 USAGE
268 my $price = callspread($S, $U, $D, $t, $r_q, $mu, $sigmaU, $sigmaD);
269  
270 DESCRIPTION
271 Price of a CALL SPREAD
272  
273 =cut
274  
275 0 ▶sub callspread {
Subroutine callspread: not called CC=1 SCAR=6.9
Pod documented
276 0 my ($S, $U, $D, $t, $r_q, $mu, $sigmaU, $sigmaD) = @_;
277  
278 0 return vanilla_call($S, $D, $t, $r_q, $mu, $sigmaD) - vanilla_call($S, $U, $t, $r_q, $mu, $sigmaU);
279 }
280  
281 =head2 putspread
282  
283 USAGE
284 my $price = putspread($S, $U, $D, $t, $r_q, $mu, $sigmaU, $sigmaD);
285  
286 DESCRIPTION
287 Price of a PUT SPREAD
288  
289 =cut
290  
291 0 ▶sub putspread {
Subroutine putspread: not called CC=1 SCAR=6.9
Pod documented
292 0 my ($S, $U, $D, $t, $r_q, $mu, $sigmaU, $sigmaD) = @_;
293  
294 0 return vanilla_put($S, $U, $t, $r_q, $mu, $sigmaU) - vanilla_put($S, $D, $t, $r_q, $mu, $sigmaD);
295 }
296  
297 =head2 standardbarrier
298  
299 A function implemented by Diethelm Wuertz.
300  
301 Description of parameters:
302  
303 $S - starting spot
304 $H - barrier
305 $X - exercise price
306 $K - cash rebate
307  
308 References:
309 Haug, Chapter 2.10.1
310  
311 =cut
312  
313 2 ▶sub standardbarrier {
Subroutine standardbarrier: called CC=7 SCAR=19.6
Pod documented
314 2 my ($S, $H, $X, $K, $tiy, $r, $q, $sigma, $type) = @_;
315  
316 2 ▶ die 'wrong type[' . $type . ']' unless $type eq 'c' or $type eq 'p';
Branch: 1 branch
BranchTrueFalseTotal
unless $type eq "c" or $type eq "p" 0 2 2
Condition66%2/3
$type eq "c" or $type eq "p"
l!l&&r!l&&!r
110
MC/DC0%0/2
$type eq "c" or $type eq "p"
$type eq "c" $type eq "p"
Truth table66%2/3
A or B
A= $type eq "c"B= $type eq "p"
ABresult
1X1
011
000
317  
318 2 my $mu = ($q - $sigma**2 / 2) / $sigma**2;
319 2 my $lambda = sqrt($mu**2 + 2 * $r / $sigma**2);
320 2 my $X1 = log($S / $X) / ($sigma * sqrt($tiy)) + (1 + $mu) * $sigma * sqrt($tiy);
321 2 my $X2 = log($S / $H) / ($sigma * sqrt($tiy)) + (1 + $mu) * $sigma * sqrt($tiy);
322 2 my $y1 = (log($H**2 / ($S * $X)) / ($sigma * sqrt($tiy)) + (1 + $mu) * $sigma * sqrt($tiy));
323 2 my $y2 = log($H / $S) / ($sigma * sqrt($tiy)) + (1 + $mu) * $sigma * sqrt($tiy);
324 2 my $Z = log($H / $S) / ($sigma * sqrt($tiy)) + $lambda * $sigma * sqrt($tiy);
325 2 ▶ my ($eta, $phi) = $type eq 'c' ? (1, 1) : (-1, -1);
Branch: 1 branch
BranchTrueFalseTotal
$type eq 'c' ? : 1 1 2
326  
327 2 my $f1 = ($phi * $S * exp(($q - $r) * $tiy) * pnorm($phi * $X1) - $phi * $X * exp(-$r * $tiy) * pnorm($phi * $X1 - $phi * $sigma * sqrt($tiy)));
328  
329 2 my $f2 = ($phi * $S * exp(($q - $r) * $tiy) * pnorm($phi * $X2) - $phi * $X * exp(-$r * $tiy) * pnorm($phi * $X2 - $phi * $sigma * sqrt($tiy)));
330  
331 2 my $f3 = ($phi * $S * exp(($q - $r) * $tiy) * ($H / $S)**(2 * ($mu + 1)) * pnorm($eta * $y1) -
332 $phi * $X * exp(-$r * $tiy) * ($H / $S)**(2 * $mu) * pnorm($eta * $y1 - $eta * $sigma * sqrt($tiy)));
333  
334 2 my $f4 = ($phi * $S * exp(($q - $r) * $tiy) * ($H / $S)**(2 * ($mu + 1)) * pnorm($eta * $y2) -
335 $phi * $X * exp(-$r * $tiy) * ($H / $S)**(2 * $mu) * pnorm($eta * $y2 - $eta * $sigma * sqrt($tiy)));
336  
337 2 my $f6 = (
338 $K * (
339 ($H / $S)**($mu + $lambda) * pnorm($eta * $Z) + ($H / $S)**($mu - $lambda) * pnorm($eta * $Z - 2 * $eta * $lambda * $sigma * sqrt($tiy)))
340 );
341  
342 2 ▶ if ($X >= $H) {
Branch: 1 branch
BranchTrueFalseTotal
if ($X >= $H) 1 1 2
343 1 ▶ return $type eq 'c' ? $f1 - $f3 + $f6 : $f2 - $f4 + $f6;
Branch: 1 branch
BranchTrueFalseTotal
$type eq 'c' ? : 1 0 1
344 }
345  
346 1 ▶ return $type eq 'c' ? $f2 + $f6 - $f4 : $f1 - $f3 + $f6;
Branch: 1 branch
BranchTrueFalseTotal
$type eq 'c' ? : 0 1 1
347 }
348  
349 =head2 doubleknockout
350  
351 Description of parameters:
352  
353 $S - spot
354 $H2 - high barrier
355 $H1 - low barrier
356 $K - payout strike
357 $tiy - time in years
358 $sigma - volatility
359 $mu - mean
360 $r - interest rate
361 $type - 'c' for buy or 'p' for sell
362  
363 Reference:
364 https://core.ac.uk/download/pdf/19187200.pdf
365  
366 =cut
367  
368 2 ▶sub doubleknockout {
Subroutine doubleknockout: called CC=2 SCAR=6.9
Pod documented
369 2 my ($S, $H2, $H1, $K, $tiy, $mu, $sigma, $r, $type) = @_;
370  
371 2 my $eps = machine_epsilon();
372 2 my $l = log($H2 / $H1);
373 2 my $x = log($S / $H1);
374 2 my $d = log($K / $H1);
375  
376 2 my $k = ceil(sqrt(((-2 * log($eps) / $tiy) - ($mu / $sigma)**2) / ((PI * $sigma / $l)**2)));
377  
378 2 ▶ if ($type eq 'c') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "c") 1 1 2
379 return
380 1 exp(-$r * $tiy) *
381 ($H1 * (_calculate_q(1, $l, $l, $mu, $sigma, $x, $k, $tiy) - _calculate_q(1, $d, $l, $mu, $sigma, $x, $k, $tiy)) -
382 $K * (_calculate_q(0, $l, $l, $mu, $sigma, $x, $k, $tiy) - _calculate_q(0, $d, $l, $mu, $sigma, $x, $k, $tiy)));
383 }
384  
385 return
386 1 exp(-$r * $tiy) *
387 ($K * (_calculate_q(0, $d, $l, $mu, $sigma, $x, $k, $tiy) - _calculate_q(0, 0, $l, $mu, $sigma, $x, $k, $tiy)) -
388 $H1 * (_calculate_q(1, $d, $l, $mu, $sigma, $x, $k, $tiy) - _calculate_q(1, 0, $l, $mu, $sigma, $x, $k, $tiy)));
389 }
390  
391 8 ▶sub _calculate_q {
Subroutine _calculate_q: called CC=2 SCAR=6.9
392 8 my ($alpha, $y, $l, $mu, $sigma, $x, $k, $tiy) = @_;
393  
394 8 my $z = 0;
395 8 ▶ for (my $i = 1; $i <= $k; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $k) 256 8 264
396 256 my $lambda = 0.5 * (($mu / $sigma)**2 + ($i * PI * $sigma / $l)**2);
397 256 $z +=
398 exp(-$lambda * $tiy) *
399 sin($i * PI * $x / $l) *
400 ((($mu / ($sigma)**2 + $alpha) * sin($i * PI * $y / $l) - ($i * PI / $l) * cos($i * PI * $y / $l)) /
401 (($mu / ($sigma)**2 + $alpha)**2 + ($i * PI / $l)**2));
402 }
403  
404 8 return (2 / $l) * exp(($mu / $sigma**2) * ($y - $x)) * exp($alpha * $y) * $z;
405 }
406  
407 4 1;
« lib/Math/Business/BlackScholesMerton/Binaries.pm ↑ Summary blib/lib/Math/Business/BlackScholesMerton.pm »