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

filter: Statementstmt 0.0% 0 / 116 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 3 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 20 Podpod 0.0% 0 / 13 totaltotal 0.0% 0 / 168 CC 9 SCAR 24.8
CC
9
Cov
0%
x_common_function_pelsser_1997
42.0
onetouch
12.0
upordown
6.0
CRAP 11.9
1 package Math::Business::BlackScholes::Binaries::Greeks::Delta;
2 use strict;
3 use warnings;
4  
5 our $VERSION = '0.05'; ## 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 use List::Util qw(max);
39 use Math::CDF qw(pnorm);
40 use Math::Trig;
41 use Math::Business::BlackScholes::Binaries;
42 use Math::Business::BlackScholes::Binaries::Greeks::Math qw( dgauss );
43  
44 sub vanilla_call {
45 my ($S, $K, $t, $r_q, $mu, $vol) = @_;
46  
47 my $d1 = (log($S / $K) + ($mu + $vol * $vol / 2.0) * $t) / ($vol * sqrt($t));
48  
49 return exp(($mu - $r_q) * $t) * pnorm($d1);
50 }
51  
52 sub vanilla_put {
53 my ($S, $K, $t, $r_q, $mu, $vol) = @_;
54  
55 my $d1 = (log($S / $K) + ($mu + $vol * $vol / 2.0) * $t) / ($vol * sqrt($t));
56  
57 return -exp(($mu - $r_q) * $t) * pnorm(-$d1);
58 }
59  
60 sub call {
61 my ($S, $U, $t, $r_q, $mu, $vol) = @_;
62  
63 my $d2 = (log($S / $U) + ($mu - $vol * $vol / 2.0) * $t) / ($vol * sqrt($t));
64  
65 return exp(-$r_q * $t) * dgauss($d2) / ($vol * sqrt($t) * $S);
66 }
67  
68 sub put {
69 my ($S, $D, $t, $r_q, $mu, $vol) = @_;
70  
71 my $d2 = (log($S / $D) + ($mu - $vol * $vol / 2.0) * $t) / ($vol * sqrt($t));
72  
73 return -exp(-$r_q * $t) * dgauss($d2) / ($vol * sqrt($t) * $S);
74 }
75  
76 sub expirymiss {
77 my ($S, $U, $D, $t, $r_q, $mu, $vol) = @_;
78  
79 return call($S, $U, $t, $r_q, $mu, $vol) + put($S, $D, $t, $r_q, $mu, $vol);
80 }
81  
82 sub expiryrange {
83 my ($S, $U, $D, $t, $r_q, $mu, $vol) = @_;
84  
85 return -1 * expirymiss($S, $U, $D, $t, $r_q, $mu, $vol);
86 }
87  
88 sub onetouch {
89 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 if (not defined $w) {
94 $w = 0;
95 }
96  
97 my $sqrt_t = sqrt($t);
98  
99 my $theta_ = ($mu / $vol) - (0.5 * $vol);
100  
101 # Floor v_ squared near zero in case negative interest rates push it negative.
102 my $v_ = sqrt(max($Math::Business::BlackScholes::Binaries::SMALL_VALUE_MU, ($theta_ * $theta_) + (2 * (1 - $w) * $r_q)));
103  
104 my $e = (log($S / $U) - ($vol * $v_ * $t)) / ($vol * $sqrt_t);
105  
106 my $e_ = (-log($S / $U) - ($vol * $v_ * $t)) / ($vol * $sqrt_t);
107  
108 my $eta = ($S > $U) ? 1 : -1;
109  
110 my $part1 =
111 ($theta_ + $v_) * pnorm(-$eta * $e) + $eta * dgauss($e) / $sqrt_t;
112 my $part2 =
113 ($theta_ - $v_) * pnorm($eta * $e_) + $eta * dgauss($e_) / $sqrt_t;
114  
115 my $delta = (($U / $S)**(($theta_ + $v_) / $vol)) * $part1 + (($U / $S)**(($theta_ - $v_) / $vol)) * $part2;
116  
117 return -$delta * exp(-$w * $r_q * $t) / ($vol * $S);
118 }
119  
120 sub notouch {
121 my ($S, $U, $t, $r_q, $mu, $vol, $w) = @_;
122  
123 # No touch bet always pay out at end
124 $w = 1;
125  
126 return -1 * onetouch($S, $U, $t, $r_q, $mu, $vol, $w);
127 }
128  
129 sub upordown {
130 my ($S, $U, $D, $t, $r_q, $mu, $vol, $w) = @_;
131  
132 # $w = 0, paid at hit
133 # $w = 1, paid at end
134 if (not defined $w) { $w = 0; }
135  
136 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 sub x_common_function_pelsser_1997 {
140 my ($S, $U, $D, $t, $r_q, $mu, $vol, $w, $eta) = @_;
141  
142 my $pi = Math::Trig::pi;
143  
144 my $h = log($U / $D);
145 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 if (not defined $eta) {
151 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 if ($eta == 0) { $x = $h - $x; }
155  
156 my $mu_new = $mu - (0.5 * $vol * $vol);
157 my $mu_dash =
158 sqrt(max($Math::Business::BlackScholes::Binaries::SMALL_VALUE_MU, ($mu_new * $mu_new) + (2 * $vol * $vol * $r_q * (1 - $w))));
159  
160 my $series_part = 0;
161 my $hyp_part = 0;
162  
163 my $stability_constant =
164 Math::Business::BlackScholes::Binaries::get_stability_constant_pelsser_1997($S, $U, $D, $t, $r_q, $mu, $vol, $w, $eta, 2);
165  
166 my $iterations_required = Math::Business::BlackScholes::Binaries::get_min_iterations_pelsser_1997($S, $U, $D, $t, $r_q, $mu, $vol, $w);
167  
168 for (my $k = 1; $k < $iterations_required; $k++) {
169 my $lambda_k_dash = (0.5 * (($mu_dash * $mu_dash) / ($vol * $vol) + ($k * $k * $pi * $pi * $vol * $vol) / ($h * $h)));
170  
171 my $phi = ($vol * $vol) / ($h * $h * $h) * exp(-$lambda_k_dash * $t) * $k * $k / $lambda_k_dash;
172  
173 $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 if ($k == 1 and (not(abs($phi / $S) < $stability_constant))) {
180 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 if (abs($mu_new) < $Math::Business::BlackScholes::Binaries::SMALL_VALUE_MU) {
187 my $sign = ($mu_new >= 0) ? 1 : -1;
188 $mu_new = $sign * $Math::Business::BlackScholes::Binaries::SMALL_VALUE_MU;
189 $mu_dash = sqrt(($mu_new * $mu_new) + (2 * $vol * $vol * $r_q * (1 - $w)));
190 }
191  
192 $hyp_part = ($mu_dash / ($vol * $vol)) * (Math::Trig::cosh($mu_dash * $x / ($vol * $vol)) / Math::Trig::sinh($mu_dash * $h / ($vol * $vol)));
193  
194 my $dc_dx = ($hyp_part + $series_part) * exp(-$r_q * $t * $w);
195  
196 return $dc_dx;
197 }
198  
199 sub ot_up_ko_down_pelsser_1997 {
200 my ($S, $U, $D, $t, $r_q, $mu, $vol, $w) = @_;
201  
202 my $mu_new = $mu - (0.5 * $vol * $vol);
203 my $h = log($U / $D);
204 my $x = log($S / $D);
205  
206 my $dVu_dx =
207 -(($mu_new / ($vol * $vol)) * Math::Business::BlackScholes::Binaries::common_function_pelsser_1997($S, $U, $D, $t, $r_q, $mu, $vol, $w, 1));
208  
209 $dVu_dx += x_common_function_pelsser_1997($S, $U, $D, $t, $r_q, $mu, $vol, $w, 1);
210 $dVu_dx *= exp($mu_new * ($h - $x) / ($vol * $vol));
211  
212 # dV/dS = dV/dx * dx/dS = dV/dx * 1/S
213 return $dVu_dx / $S;
214 }
215  
216 sub ot_down_ko_up_pelsser_1997 {
217 my ($S, $U, $D, $t, $r_q, $mu, $vol, $w) = @_;
218  
219 my $mu_new = $mu - (0.5 * $vol * $vol);
220 my $x = log($S / $D);
221  
222 my $dVl_dx =
223 -(($mu_new / ($vol * $vol)) * Math::Business::BlackScholes::Binaries::common_function_pelsser_1997($S, $U, $D, $t, $r_q, $mu, $vol, $w, 0));
224  
225 $dVl_dx -= x_common_function_pelsser_1997($S, $U, $D, $t, $r_q, $mu, $vol, $w, 0);
226 $dVl_dx *= exp(-$mu_new * $x / ($vol * $vol));
227  
228 # dV/dS = dV/dx * dx/dS = dV/dx * 1/S
229 return $dVl_dx / $S;
230 }
231  
232 sub range {
233 my ($S, $U, $D, $t, $r_q, $mu, $vol, $w) = @_;
234  
235 # Range always pay out at end
236 $w = 1;
237  
238 return -1 * upordown($S, $U, $D, $t, $r_q, $mu, $vol, $w);
239 }
240  
241 1;
242