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

filter: Statementstmt 0.0% 0 / 127 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 / 182 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; use warnings;
3  
4 our $VERSION = '0.04';
5  
6 =head1 NAME
7  
8 Math::Business::BlackScholes::Binaries::Greeks::Theta
9  
10 =head1 DESCRIPTION
11  
12 Gets the Theta 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 use List::Util qw(max);
23 use Math::Trig;
24 use Math::CDF qw(pnorm);
25 use Math::Business::BlackScholes::Binaries;
26 use Math::Business::BlackScholes::Binaries::Greeks::Math qw(dgauss);
27  
28 sub vanilla_call {
29 my ( $S, $K, $t, $r_q, $mu, $vol ) = @_;
30  
31 my $d1 =
32 ( log( $S / $K ) + ($mu) * $t ) / ( $vol * sqrt($t) ) +
33 0.5 * $vol * sqrt($t);
34 my $d2 = $d1 - $vol * sqrt($t);
35  
36 my $theta =
37 -( $vol * $S * exp( ( $mu - $r_q ) * $t ) * dgauss($d1) ) /
38 ( 2 * sqrt($t) ) +
39 ( ( $r_q - $mu ) * $S * exp( ( $mu - $r_q ) * $t ) * pnorm($d1) ) -
40 ( $r_q * $K * exp( -$r_q * $t ) * pnorm($d2) );
41  
42 return $theta;
43 }
44  
45 sub vanilla_put {
46 my ( $S, $K, $t, $r_q, $mu, $vol ) = @_;
47  
48 my $d1 =
49 ( log( $S / $K ) + ($mu) * $t ) / ( $vol * sqrt($t) ) +
50 0.5 * $vol * sqrt($t);
51 my $d2 = $d1 - $vol * sqrt($t);
52  
53 my $theta =
54 -( $vol * $S * exp( ( $mu - $r_q ) * $t ) * dgauss( -$d1 ) ) /
55 ( 2 * sqrt($t) ) -
56 ( ( $r_q - $mu ) * $S * exp( ( $mu - $r_q ) * $t ) * pnorm( -$d1 ) ) +
57 ( $r_q * $K * exp( -$r_q * $t ) * pnorm( -$d2 ) );
58  
59 return $theta;
60 }
61  
62 sub call {
63 my ( $S, $U, $t, $r_q, $mu, $vol ) = @_;
64  
65 my $d1 =
66 ( log( $S / $U ) + ($mu) * $t ) / ( $vol * sqrt($t) ) +
67 0.5 * $vol * sqrt($t);
68 my $d2 = $d1 - $vol * sqrt($t);
69  
70 my $theta =
71 $r_q * pnorm($d2) +
72 dgauss($d2) * $d1 / ( 2 * $t ) -
73 dgauss($d2) * ($mu) / ( $vol * sqrt($t) );
74  
75 return $theta * exp( -$r_q * $t );
76 }
77  
78 sub put {
79 my ( $S, $D, $t, $r_q, $mu, $vol ) = @_;
80  
81 my $d1 =
82 ( log( $S / $D ) + ($mu) * $t ) / ( $vol * sqrt($t) ) +
83 0.5 * $vol * sqrt($t);
84 my $d2 = $d1 - $vol * sqrt($t);
85  
86 my $theta =
87 $r_q * pnorm( -$d2 ) -
88 dgauss($d2) * $d1 / ( 2 * $t ) +
89 dgauss($d2) * ($mu) / ( $vol * sqrt($t) );
90  
91 return $theta * exp( -$r_q * $t );
92 }
93  
94 sub expirymiss {
95 my ( $S, $U, $D, $t, $r_q, $mu, $vol ) = @_;
96  
97 return call( $S, $U, $t, $r_q, $mu, $vol ) +
98 put( $S, $D, $t, $r_q, $mu, $vol );
99 }
100  
101 sub expiryrange {
102 my ( $S, $U, $D, $t, $r_q, $mu, $vol ) = @_;
103  
104 return $r_q * exp( -$r_q * $t ) -
105 expirymiss( $S, $U, $D, $t, $r_q, $mu, $vol );
106 }
107  
108 sub onetouch {
109 my ( $S, $U, $t, $r_q, $mu, $vol, $w ) = @_;
110 if ( not defined $w ) {
111 $w = 0;
112 }
113  
114 my $sqrt_t = sqrt($t);
115  
116 my $theta = ( ($mu) / $vol ) + ( 0.5 * $vol );
117 my $theta_ = ( ($mu) / $vol ) - ( 0.5 * $vol );
118  
119 # Floor v_ squared at zero in case negative interest rates push it negative.
120 my $v_ = sqrt( max( $Math::Business::BlackScholes::Binaries::SMALL_VALUE_MU, ( $theta_ * $theta_ ) + ( 2 * ( 1 - $w ) * $r_q ) ) );
121  
122 my $e = ( log( $S / $U ) - ( $vol * $v_ * $t ) ) / ( $vol * $sqrt_t );
123  
124 my $eta = ( $S > $U ) ? 1 : -1;
125  
126 my $part1 =
127 $w * $r_q *
128 Math::Business::BlackScholes::Binaries::onetouch( $S, $U, $t, $r_q, $mu,
129 $vol, $w );
130 my $part2 =
131 $eta *
132 exp( -$w * $r_q * $t ) /
133 ( $vol * ( $t**1.5 ) ) *
134 ( ( $U / $S )**( ( $theta_ + $v_ ) / $vol ) ) *
135 dgauss($e) *
136 log( $U / $S );
137  
138 my $theta_onetouch = $part1 + $part2;
139  
140 return $theta_onetouch;
141 }
142  
143 sub notouch {
144 my ( $S, $U, $t, $r_q, $mu, $vol, $w ) = @_;
145  
146 # No touch bet always pay out at end
147 $w = 1;
148  
149 return $r_q * exp( -$r_q * $t ) -
150 onetouch( $S, $U, $t, $r_q, $mu, $vol, $w );
151 }
152  
153 sub upordown {
154 my ( $S, $U, $D, $t, $r_q, $mu, $vol, $w ) = @_;
155  
156 if ( ( $S >= $U ) || ( $S <= $D ) ) { return 0; }
157  
158 # $w = 0, paid at hit
159 # $w = 1, paid at end
160 if ( not defined $w ) { $w = 0; }
161  
162 return ot_up_ko_down_pelsser_1997( $S, $U, $D, $t, $r_q, $mu, $vol, $w ) +
163 ot_down_ko_up_pelsser_1997( $S, $U, $D, $t, $r_q, $mu, $vol, $w );
164 }
165  
166 sub common_function_pelsser_1997 {
167 my ( $S, $U, $D, $t, $r_q, $mu, $vol, $w, $eta ) = @_;
168  
169 my $pi = Math::Trig::pi;
170  
171 my $h = log( $U / $D );
172 my $x = log( $S / $D );
173  
174 # $eta = 1, onetouch up knockout down
175 # $eta = 0, onetouch down knockout up
176 # This variable used to check stability
177 if ( not defined $eta ) {
178 die
179 "$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.";
180 }
181 if ( $eta == 0 ) { $x = $h - $x; }
182  
183 my $mu_ = $mu - ( 0.5 * $vol * $vol );
184 my $mu_dash =
185 sqrt( max( $Math::Business::BlackScholes::Binaries::SMALL_VALUE_MU, ( $mu_ * $mu_ ) + ( 2 * $vol * $vol * $r_q * ( 1 - $w ) ) ) );
186  
187 my $hyp_part = 0;
188 my $series_part = 0;
189  
190 my $stability_constant =
191 Math::Business::BlackScholes::Binaries::get_stability_constant_pelsser_1997(
192 $S, $U, $D, $t, $r_q, $mu, $vol, $w, $eta, 1 );
193  
194 my $iterations_required =
195 Math::Business::BlackScholes::Binaries::get_min_iterations_pelsser_1997(
196 $S, $U, $D, $t, $r_q, $mu, $vol, $w );
197  
198 for ( my $k = 1 ; $k < $iterations_required ; $k++ ) {
199 my $lambda_k_dash = (
200 0.5 * (
201 ( $mu_dash * $mu_dash ) / ( $vol * $vol ) +
202 ( $k * $k * $pi * $pi * $vol * $vol ) / ( $h * $h )
203 )
204 );
205  
206 my $phi =
207 ( $vol * $vol ) / ( $h * $h ) * ( 1 + ( $r_q * $w / $lambda_k_dash ) )
208 * exp( -( $r_q * $w + $lambda_k_dash ) * $t )
209 * $k;
210  
211 $series_part += $phi * $pi * sin( $k * $pi * ( $h - $x ) / $h );
212  
213 if ( $k == 1 and ( not( abs($phi) < $stability_constant ) ) ) {
214 die
215 "$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.";
216 }
217 }
218  
219 # We have to handle the special case where the denominator approaches 0, see our documentation in
220 # quant/Documents/Breakout_bet.tex under the SVN "quant" module.
221 if ( ( Math::Trig::sinh( $mu_dash * $h / ( $vol * $vol ) ) ) == 0 ) {
222 $hyp_part = -( $r_q * $w ) * exp( -$r_q * $w * $t ) * ( $x / $h );
223 }
224 else {
225 $hyp_part =
226 -( $r_q * $w ) *
227 exp( -$r_q * $w * $t ) *
228 Math::Trig::sinh( $mu_dash * $x / ( $vol * $vol ) ) /
229 Math::Trig::sinh( $mu_dash * $h / ( $vol * $vol ) );
230 }
231  
232 my $dc_dT = ( $hyp_part + $series_part );
233  
234 return $dc_dT;
235 }
236  
237 sub ot_up_ko_down_pelsser_1997 {
238 my ( $S, $U, $D, $t, $r_q, $mu, $vol, $w ) = @_;
239  
240 my $mu_ = $mu - ( 0.5 * $vol * $vol );
241 my $h = log( $U / $D );
242 my $x = log( $S / $D );
243  
244 my $dc_dT =
245 common_function_pelsser_1997( $S, $U, $D, $t, $r_q, $mu, $vol, $w, 1 );
246  
247 my $dVu_dT = -exp( ( $mu_ / ( $vol * $vol ) ) * ( $h - $x ) ) * $dc_dT;
248 return $dVu_dT;
249 }
250  
251 sub ot_down_ko_up_pelsser_1997 {
252 my ( $S, $U, $D, $t, $r_q, $mu, $vol, $w ) = @_;
253  
254 my $mu_ = $mu - ( 0.5 * $vol * $vol );
255 my $h = log( $U / $D );
256 my $x = log( $S / $D );
257  
258 my $dc_dT =
259 common_function_pelsser_1997( $S, $U, $D, $t, $r_q, $mu, $vol, $w, 0 );
260  
261 my $dVl_dT = -exp( -( $mu_ / ( $vol * $vol ) ) * $x ) * $dc_dT;
262 return $dVl_dT;
263 }
264  
265 sub range {
266 my ( $S, $U, $D, $t, $r_q, $mu, $vol, $w ) = @_;
267  
268 # Range always pay out at end
269 $w = 1;
270  
271 return $r_q * exp( -$r_q * $t ) -
272 upordown( $S, $U, $D, $t, $r_q, $mu, $vol, $w );
273 }
274  
275 1;
276