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

filter: Statementstmt 0.0% 0 / 124 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 6 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 20 Podpod 0.0% 0 / 13 totaltotal 0.0% 0 / 179 CC 9 SCAR 22.1
CC
9
Cov
0%
common_function_pelsser_1997
30.0
onetouch
12.0
upordown
12.0
CRAP 9.1
1 package Math::Business::BlackScholes::Binaries::Greeks::Theta;
2 use strict;
3 use warnings;
4  
5 our $VERSION = '0.05'; ## VERSION
6  
7 =head1 NAME
8  
9 Math::Business::BlackScholes::Binaries::Greeks::Theta
10  
11 =head1 DESCRIPTION
12  
13 Gets the Theta for different options, Vanilla and Foreign for all our bet types
14  
15 =cut
16  
17 =head1 SUBROUTINES
18  
19 See L<Math::Business::BlackScholes::Binaries::Greeks>
20  
21 =cut
22  
23 use List::Util qw(max);
24 use Math::Trig;
25 use Math::CDF qw(pnorm);
26 use Math::Business::BlackScholes::Binaries;
27 use Math::Business::BlackScholes::Binaries::Greeks::Math qw(dgauss);
28  
29 sub vanilla_call {
30 my ($S, $K, $t, $r_q, $mu, $vol) = @_;
31  
32 my $d1 = (log($S / $K) + ($mu) * $t) / ($vol * sqrt($t)) + 0.5 * $vol * sqrt($t);
33 my $d2 = $d1 - $vol * sqrt($t);
34  
35 my $theta =
36 -($vol * $S * exp(($mu - $r_q) * $t) * dgauss($d1)) / (2 * sqrt($t)) +
37 (($r_q - $mu) * $S * exp(($mu - $r_q) * $t) * pnorm($d1)) -
38 ($r_q * $K * exp(-$r_q * $t) * pnorm($d2));
39  
40 return $theta;
41 }
42  
43 sub vanilla_put {
44 my ($S, $K, $t, $r_q, $mu, $vol) = @_;
45  
46 my $d1 = (log($S / $K) + ($mu) * $t) / ($vol * sqrt($t)) + 0.5 * $vol * sqrt($t);
47 my $d2 = $d1 - $vol * sqrt($t);
48  
49 my $theta =
50 -($vol * $S * exp(($mu - $r_q) * $t) * dgauss(-$d1)) / (2 * sqrt($t)) -
51 (($r_q - $mu) * $S * exp(($mu - $r_q) * $t) * pnorm(-$d1)) +
52 ($r_q * $K * exp(-$r_q * $t) * pnorm(-$d2));
53  
54 return $theta;
55 }
56  
57 sub call {
58 my ($S, $U, $t, $r_q, $mu, $vol) = @_;
59  
60 my $d1 = (log($S / $U) + ($mu) * $t) / ($vol * sqrt($t)) + 0.5 * $vol * sqrt($t);
61 my $d2 = $d1 - $vol * sqrt($t);
62  
63 my $theta = $r_q * pnorm($d2) + dgauss($d2) * $d1 / (2 * $t) - dgauss($d2) * ($mu) / ($vol * sqrt($t));
64  
65 return $theta * exp(-$r_q * $t);
66 }
67  
68 sub put {
69 my ($S, $D, $t, $r_q, $mu, $vol) = @_;
70  
71 my $d1 = (log($S / $D) + ($mu) * $t) / ($vol * sqrt($t)) + 0.5 * $vol * sqrt($t);
72 my $d2 = $d1 - $vol * sqrt($t);
73  
74 my $theta = $r_q * pnorm(-$d2) - dgauss($d2) * $d1 / (2 * $t) + dgauss($d2) * ($mu) / ($vol * sqrt($t));
75  
76 return $theta * exp(-$r_q * $t);
77 }
78  
79 sub expirymiss {
80 my ($S, $U, $D, $t, $r_q, $mu, $vol) = @_;
81  
82 return call($S, $U, $t, $r_q, $mu, $vol) + put($S, $D, $t, $r_q, $mu, $vol);
83 }
84  
85 sub expiryrange {
86 my ($S, $U, $D, $t, $r_q, $mu, $vol) = @_;
87  
88 return $r_q * exp(-$r_q * $t) - expirymiss($S, $U, $D, $t, $r_q, $mu, $vol);
89 }
90  
91 sub onetouch {
92 my ($S, $U, $t, $r_q, $mu, $vol, $w) = @_;
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 at 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 $eta = ($S > $U) ? 1 : -1;
107  
108 my $part1 = $w * $r_q * Math::Business::BlackScholes::Binaries::onetouch($S, $U, $t, $r_q, $mu, $vol, $w);
109 my $part2 = $eta * exp(-$w * $r_q * $t) / ($vol * ($t**1.5)) * (($U / $S)**(($theta_ + $v_) / $vol)) * dgauss($e) * log($U / $S);
110  
111 my $theta_onetouch = $part1 + $part2;
112  
113 return $theta_onetouch;
114 }
115  
116 sub notouch {
117 my ($S, $U, $t, $r_q, $mu, $vol, $w) = @_;
118  
119 # No touch bet always pay out at end
120 $w = 1;
121  
122 return $r_q * exp(-$r_q * $t) - onetouch($S, $U, $t, $r_q, $mu, $vol, $w);
123 }
124  
125 sub upordown {
126 my ($S, $U, $D, $t, $r_q, $mu, $vol, $w) = @_;
127  
128 if (($S >= $U) || ($S <= $D)) { return 0; }
129  
130 # $w = 0, paid at hit
131 # $w = 1, paid at end
132 if (not defined $w) { $w = 0; }
133  
134 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);
135 }
136  
137 sub common_function_pelsser_1997 {
138 my ($S, $U, $D, $t, $r_q, $mu, $vol, $w, $eta) = @_;
139  
140 my $pi = Math::Trig::pi;
141  
142 my $h = log($U / $D);
143 my $x = log($S / $D);
144  
145 # $eta = 1, onetouch up knockout down
146 # $eta = 0, onetouch down knockout up
147 # This variable used to check stability
148 if (not defined $eta) {
149 die
150 "$0: (common_function_pelsser_1997) Wrong usage of this function for S=$S, U=$U, D=$D, t=$t, r=$r_q, mu=$mu, vol=$vol, w=$w. eta not defined.";
151 }
152 if ($eta == 0) { $x = $h - $x; }
153  
154 my $mu_ = $mu - (0.5 * $vol * $vol);
155 my $mu_dash =
156 sqrt(max($Math::Business::BlackScholes::Binaries::SMALL_VALUE_MU, ($mu_ * $mu_) + (2 * $vol * $vol * $r_q * (1 - $w))));
157  
158 my $hyp_part = 0;
159 my $series_part = 0;
160  
161 my $stability_constant =
162 Math::Business::BlackScholes::Binaries::get_stability_constant_pelsser_1997($S, $U, $D, $t, $r_q, $mu, $vol, $w, $eta, 1);
163  
164 my $iterations_required = Math::Business::BlackScholes::Binaries::get_min_iterations_pelsser_1997($S, $U, $D, $t, $r_q, $mu, $vol, $w);
165  
166 for (my $k = 1; $k < $iterations_required; $k++) {
167 my $lambda_k_dash = (0.5 * (($mu_dash * $mu_dash) / ($vol * $vol) + ($k * $k * $pi * $pi * $vol * $vol) / ($h * $h)));
168  
169 my $phi = ($vol * $vol) / ($h * $h) * (1 + ($r_q * $w / $lambda_k_dash)) * exp(-($r_q * $w + $lambda_k_dash) * $t) * $k;
170  
171 $series_part += $phi * $pi * sin($k * $pi * ($h - $x) / $h);
172  
173 if ($k == 1 and (not(abs($phi) < $stability_constant))) {
174 die
175 "$0: PELSSER THETA formula for S=$S, U=$U, D=$D, t=$t, r=$r_q, mu=$mu, vol=$vol, w=$w, eta=$eta cannot be evaluated because PELSSER THETA stability conditions ($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.";
176 }
177 }
178  
179 # We have to handle the special case where the denominator approaches 0, see our documentation in
180 # quant/Documents/Breakout_bet.tex under the SVN "quant" module.
181 if ((Math::Trig::sinh($mu_dash * $h / ($vol * $vol))) == 0) {
182 $hyp_part = -($r_q * $w) * exp(-$r_q * $w * $t) * ($x / $h);
183 } else {
184 $hyp_part =
185 -($r_q * $w) * exp(-$r_q * $w * $t) * Math::Trig::sinh($mu_dash * $x / ($vol * $vol)) / Math::Trig::sinh($mu_dash * $h / ($vol * $vol));
186 }
187  
188 my $dc_dT = ($hyp_part + $series_part);
189  
190 return $dc_dT;
191 }
192  
193 sub ot_up_ko_down_pelsser_1997 {
194 my ($S, $U, $D, $t, $r_q, $mu, $vol, $w) = @_;
195  
196 my $mu_ = $mu - (0.5 * $vol * $vol);
197 my $h = log($U / $D);
198 my $x = log($S / $D);
199  
200 my $dc_dT = common_function_pelsser_1997($S, $U, $D, $t, $r_q, $mu, $vol, $w, 1);
201  
202 my $dVu_dT = -exp(($mu_ / ($vol * $vol)) * ($h - $x)) * $dc_dT;
203 return $dVu_dT;
204 }
205  
206 sub ot_down_ko_up_pelsser_1997 {
207 my ($S, $U, $D, $t, $r_q, $mu, $vol, $w) = @_;
208  
209 my $mu_ = $mu - (0.5 * $vol * $vol);
210 my $x = log($S / $D);
211  
212 my $dc_dT = common_function_pelsser_1997($S, $U, $D, $t, $r_q, $mu, $vol, $w, 0);
213  
214 my $dVl_dT = -exp(-($mu_ / ($vol * $vol)) * $x) * $dc_dT;
215 return $dVl_dT;
216 }
217  
218 sub range {
219 my ($S, $U, $D, $t, $r_q, $mu, $vol, $w) = @_;
220  
221 # Range always pay out at end
222 $w = 1;
223  
224 return $r_q * exp(-$r_q * $t) - upordown($S, $U, $D, $t, $r_q, $mu, $vol, $w);
225 }
226  
227 1;
228