« blib/lib/Math/Business/BlackScholes/Binaries/Greeks/Vega.pm
↑ Summary
lib/Math/Business/BlackScholes/Binaries/Greeks/Delta.pm »
| 1 | package Math::Business::BlackScholes::Binaries::Greeks::Vanna; | ||
| 2 | use strict; use warnings; | ||
| 3 | |||
| 4 | our $VERSION = '0.04'; | ||
| 5 | |||
| 6 | use List::Util qw( max ); | ||
| 7 | use Math::CDF qw( pnorm ); | ||
| 8 | use Math::Trig; | ||
| 9 | use Math::Business::BlackScholes::Binaries; | ||
| 10 | use Math::Business::BlackScholes::Binaries::Greeks::Delta; | ||
| 11 | use Math::Business::BlackScholes::Binaries::Greeks::Vega; | ||
| 12 | use Math::Business::BlackScholes::Binaries::Greeks::Math qw( dgauss ); | ||
| 13 | |||
| 14 | =head1 NAME | ||
| 15 | |||
| 16 | Math::Business::BlackScholes::Binaries::Greeks::Vanna | ||
| 17 | |||
| 18 | =head1 DESCRIPTION | ||
| 19 | |||
| 20 | Gets the Vanna for different options, Vanilla and Foreign for all our bet types | ||
| 21 | |||
| 22 | =head1 SUBROUTINES | ||
| 23 | |||
| 24 | See L<Math::Business::BlackScholes::Binaries::Greeks> | ||
| 25 | |||
| 26 | =cut | ||
| 27 | |||
| 28 | sub vanilla_call { | ||
| 29 | my ( $S, $K, $t, $r_q, $mu, $vol ) = @_; | ||
| 30 | |||
| 31 | my $d1 = | ||
| 32 | ( log( $S / $K ) + ( $mu + $vol * $vol / 2.0 ) * $t ) / | ||
| 33 | ( $vol * sqrt($t) ); | ||
| 34 | my $d2 = $d1 - ( $vol * sqrt($t) ); | ||
| 35 | |||
| 36 | my $vega = | ||
| 37 | Math::Business::BlackScholes::Binaries::Greeks::Vega::vanilla_call( $S, | ||
| 38 | $K, $t, $r_q, $mu, $vol ); | ||
| 39 | my $vanna = -$vega * $d2 / ( $S * $vol * sqrt($t) ); | ||
| 40 | return $vanna; | ||
| 41 | } | ||
| 42 | |||
| 43 | sub vanilla_put { | ||
| 44 | my ( $S, $K, $t, $r_q, $mu, $vol ) = @_; | ||
| 45 | |||
| 46 | # Same as vanna of vanilla call (because vega_vanilla_call = vega_vanilla_put) | ||
| 47 | return vanilla_call( $S, $K, $t, $r_q, $mu, $vol ); | ||
| 48 | } | ||
| 49 | |||
| 50 | sub call { | ||
| 51 | my ( $S, $U, $t, $r_q, $mu, $vol ) = @_; | ||
| 52 | |||
| 53 | my $d1 = | ||
| 54 | ( log( $S / $U ) + ( $mu + $vol * $vol / 2.0 ) * $t ) / | ||
| 55 | ( $vol * sqrt($t) ); | ||
| 56 | my $d2 = $d1 - ( $vol * sqrt($t) ); | ||
| 57 | |||
| 58 | my $vanna = | ||
| 59 | -dgauss($d2) * | ||
| 60 | exp( -$r_q * $t ) * | ||
| 61 | ( 1 - $d1 * $d2 ) / | ||
| 62 | ( $S * $vol * $vol * sqrt($t) ); | ||
| 63 | return $vanna; | ||
| 64 | } | ||
| 65 | |||
| 66 | sub put { | ||
| 67 | my ( $S, $D, $t, $r_q, $mu, $vol ) = @_; | ||
| 68 | |||
| 69 | return -1 * call( $S, $D, $t, $r_q, $mu, $vol ); | ||
| 70 | } | ||
| 71 | |||
| 72 | sub expirymiss { | ||
| 73 | my ( $S, $U, $D, $t, $r_q, $mu, $vol ) = @_; | ||
| 74 | |||
| 75 | return call( $S, $U, $t, $r_q, $mu, $vol ) + | ||
| 76 | put( $S, $D, $t, $r_q, $mu, $vol ); | ||
| 77 | } | ||
| 78 | |||
| 79 | sub expiryrange { | ||
| 80 | my ( $S, $U, $D, $t, $r_q, $mu, $vol ) = @_; | ||
| 81 | |||
| 82 | return -1 * expirymiss( $S, $U, $D, $t, $r_q, $mu, $vol ); | ||
| 83 | } | ||
| 84 | |||
| 85 | sub onetouch { | ||
| 86 | my ( $S, $U, $t, $r_q, $mu, $vol, $w ) = @_; | ||
| 87 | |||
| 88 | if ( not defined $w ) { | ||
| 89 | $w = 0; | ||
| 90 | } | ||
| 91 | |||
| 92 | my $sqrt_t = sqrt($t); | ||
| 93 | |||
| 94 | my $theta = ( ($mu) / $vol ) + ( 0.5 * $vol ); | ||
| 95 | |||
| 96 | my $theta_ = ( ($mu) / $vol ) - ( 0.5 * $vol ); | ||
| 97 | |||
| 98 | # Floor v_ squared at just above zero in case negative interest rates push it negative. | ||
| 99 | my $v_ = sqrt( max( $Math::Business::BlackScholes::Binaries::SMALL_VALUE_MU, ( $theta_ * $theta_ ) + ( 2 * ( 1 - $w ) * $r_q ) ) ); | ||
| 100 | |||
| 101 | my $e = ( log( $S / $U ) - ( $vol * $v_ * $t ) ) / ( $vol * $sqrt_t ); | ||
| 102 | my $e_ = ( -log( $S / $U ) - ( $vol * $v_ * $t ) ) / ( $vol * $sqrt_t ); | ||
| 103 | |||
| 104 | my $eta = ( $S > $U ) ? 1 : -1; | ||
| 105 | |||
| 106 | my $pa_e = | ||
| 107 | ( log( $U / $S ) / ( $vol * $vol * $sqrt_t ) ) + | ||
| 108 | ( ( $theta_ * $theta ) / ( $vol * $v_ ) * $sqrt_t ); | ||
| 109 | my $pa_e_ = | ||
| 110 | ( -log( $U / $S ) / ( $vol * $vol * $sqrt_t ) ) + | ||
| 111 | ( ( $theta_ * $theta ) / ( $vol * $v_ ) * $sqrt_t ); | ||
| 112 | |||
| 113 | my $A = | ||
| 114 | -( $theta + $theta_ + ( $theta_ * $theta / $v_ ) + $v_ ) / | ||
| 115 | ( $vol * $vol ); | ||
| 116 | my $A_ = | ||
| 117 | -( $theta + $theta_ - ( $theta_ * $theta / $v_ ) - $v_ ) / | ||
| 118 | ( $vol * $vol ); | ||
| 119 | |||
| 120 | my $d_ = ( log( $U / $S ) - $vol * $theta_ * $t ) / ( $vol * $sqrt_t ); | ||
| 121 | |||
| 122 | my ( $part1, $part2, $subpart_1_1, $subpart_1_2, $subpart_2_1, | ||
| 123 | $subpart_2_2 ); | ||
| 124 | |||
| 125 | $subpart_1_1 = | ||
| 126 | pnorm( -$eta * $e ) * $A * ( -$vol - ( $theta_ + $v_ ) * log( $U / $S ) ); | ||
| 127 | $subpart_1_2 = | ||
| 128 | $eta * | ||
| 129 | dgauss($e) / | ||
| 130 | $sqrt_t * | ||
| 131 | ( $d_ * $pa_e + $A * log( $U / $S ) - 1.0 / $vol ); | ||
| 132 | |||
| 133 | $subpart_2_1 = | ||
| 134 | pnorm( $eta * $e_ ) * | ||
| 135 | $A_ * | ||
| 136 | ( -$vol - ( $theta_ - $v_ ) * log( $U / $S ) ); | ||
| 137 | $subpart_2_2 = | ||
| 138 | $eta * | ||
| 139 | dgauss($e_) / | ||
| 140 | $sqrt_t * | ||
| 141 | ( $d_ * $pa_e_ - $A_ * log( $U / $S ) + 1.0 / $vol ); | ||
| 142 | |||
| 143 | $part1 = | ||
| 144 | ( ( $U / $S )**( ( $theta_ + $v_ ) / $vol ) ) * | ||
| 145 | ( $subpart_1_1 - $subpart_1_2 ); | ||
| 146 | $part2 = | ||
| 147 | ( ( $U / $S )**( ( $theta_ - $v_ ) / $vol ) ) * | ||
| 148 | ( $subpart_2_1 + $subpart_2_2 ); | ||
| 149 | |||
| 150 | return ( $part1 + $part2 ) * exp( -$w * $r_q * $t ) / ( $vol * $S ); | ||
| 151 | } | ||
| 152 | |||
| 153 | sub notouch { | ||
| 154 | my ( $S, $U, $t, $r_q, $mu, $vol, $w ) = @_; | ||
| 155 | |||
| 156 | # No touch bet always pay out at end | ||
| 157 | $w = 1; | ||
| 158 | |||
| 159 | # Since the value VALUE_NOTOUCH = D(T) - VALUE_ONETOUCH, where D(T) | ||
| 160 | # is the discount from time T, any derivative (other than with | ||
| 161 | # respect to time or discount rate) of the value of notouch | ||
| 162 | # is just the negative of the onetouch derivative. | ||
| 163 | return ( -1 * onetouch( $S, $U, $t, $r_q, $mu, $vol, $w ) ); | ||
| 164 | } | ||
| 165 | |||
| 166 | sub upordown { | ||
| 167 | my ( $S, $U, $D, $t, $r_q, $mu, $vol, $w ) = @_; | ||
| 168 | |||
| 169 | # $w = 0, paid at hit | ||
| 170 | # $w = 1, paid at end | ||
| 171 | if ( not defined $w ) { $w = 0; } | ||
| 172 | |||
| 173 | return ot_up_ko_down_pelsser_1997( $S, $U, $D, $t, $r_q, $mu, $vol, $w ) + | ||
| 174 | ot_down_ko_up_pelsser_1997( $S, $U, $D, $t, $r_q, $mu, $vol, $w ); | ||
| 175 | } | ||
| 176 | |||
| 177 | sub xw_common_function_pelsser_1997 { | ||
| 178 | my ( $S, $U, $D, $t, $r_q, $mu, $vol, $w, $eta ) = @_; | ||
| 179 | |||
| 180 | my $pi = Math::Trig::pi; | ||
| 181 | |||
| 182 | my $h = log( $U / $D ); | ||
| 183 | my $x = log( $S / $D ); | ||
| 184 | |||
| 185 | # $eta = 1, onetouch up knockout down | ||
| 186 | # $eta = 0, onetouch down knockout up | ||
| 187 | # This variable used to check stability | ||
| 188 | if ( not defined $eta ) { | ||
| 189 | die | ||
| 190 | "$0: (xw_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."; | ||
| 191 | } | ||
| 192 | if ( $eta == 0 ) { $x = $h - $x; } | ||
| 193 | |||
| 194 | my $r_dash = $r_q * ( 1 - $w ); | ||
| 195 | my $mu_new = $mu - ( 0.5 * $vol * $vol ); | ||
| 196 | my $mu_dash = sqrt( max( $Math::Business::BlackScholes::Binaries::SMALL_VALUE_MU, ( $mu_new * $mu_new ) + ( 2 * $vol * $vol * $r_dash ) ) ); | ||
| 197 | |||
| 198 | my $omega = ( $vol * $vol ); | ||
| 199 | |||
| 200 | my $series_part = 0; | ||
| 201 | my $hyp_part = 0; | ||
| 202 | |||
| 203 | my $stability_constant = | ||
| 204 | Math::Business::BlackScholes::Binaries::get_stability_constant_pelsser_1997( | ||
| 205 | $S, $U, $D, $t, $r_q, $mu, $vol, $w, $eta, 1 ); | ||
| 206 | |||
| 207 | my $iterations_required = | ||
| 208 | Math::Business::BlackScholes::Binaries::get_min_iterations_pelsser_1997( | ||
| 209 | $S, $U, $D, $t, $r_q, $mu, $vol, $w ); | ||
| 210 | |||
| 211 | for ( my $k = 1 ; $k < $iterations_required ; $k++ ) { | ||
| 212 | my $lambda_k_dash = ( | ||
| 213 | 0.5 * ( | ||
| 214 | ( $mu_dash * $mu_dash ) / ( $vol * $vol ) + | ||
| 215 | ( $k * $k * $pi * $pi * $vol * $vol ) / ( $h * $h ) | ||
| 216 | ) | ||
| 217 | ); | ||
| 218 | |||
| 219 | # d{lambda_k}/dw | ||
| 220 | my $dlambdak_domega = | ||
| 221 | 0.5 * | ||
| 222 | ( -( $mu_new / $omega ) - | ||
| 223 | ( ( $mu_new * $mu_new ) / ( $omega * $omega ) ) + | ||
| 224 | ( ( $k * $k * $pi * $pi ) / ( $h * $h ) ) ); | ||
| 225 | |||
| 226 | my $beta_k = exp( -$lambda_k_dash * $t ) / $lambda_k_dash; | ||
| 227 | |||
| 228 | # d{beta_k}/d{lambda_k} | ||
| 229 | my $dbetak_dlambdak = | ||
| 230 | -exp( -$lambda_k_dash * $t ) * | ||
| 231 | ( ( $t * $lambda_k_dash + 1 ) / ( $lambda_k_dash**2 ) ); | ||
| 232 | |||
| 233 | # d{beta_k}/dw | ||
| 234 | my $dbetak_domega = $dlambdak_domega * $dbetak_dlambdak; | ||
| 235 | |||
| 236 | my $phi = | ||
| 237 | ( 1.0 / ( $h * $h * $h ) ) * | ||
| 238 | ( $omega * $dbetak_domega + $beta_k ) * | ||
| 239 | $k * | ||
| 240 | $k; | ||
| 241 | |||
| 242 | $series_part += $phi * $pi * $pi * cos( $k * $pi * ( $h - $x ) / $h ); | ||
| 243 | |||
| 244 | if ( $k == 1 | ||
| 245 | and ( not( abs( 2 * $vol * $phi / $S ) < $stability_constant ) ) ) | ||
| 246 | { | ||
| 247 | die | ||
| 248 | "$0: PELSSER VANNA 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 VANNA stability conditions (2 * $vol * $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."; | ||
| 249 | } | ||
| 250 | } | ||
| 251 | |||
| 252 | my $alpha = $mu_dash / ( $vol * $vol ); | ||
| 253 | my $dalpha_domega = | ||
| 254 | -( ( $mu_new * $omega ) + | ||
| 255 | ( 2 * $mu_new * $mu_new ) + | ||
| 256 | ( 2 * $r_dash * $omega ) ) / | ||
| 257 | ( 2 * $alpha * $omega * $omega * $omega ); | ||
| 258 | |||
| 259 | # We have to handle the special case where the denominator approaches 0, see our documentation in | ||
| 260 | # quant/Documents/Breakout_bet.tex under the SVN "quant" module. | ||
| 261 | if ( ( Math::Trig::sinh( $alpha * $h )**2 ) == 0 ) { | ||
| 262 | $hyp_part = 0; | ||
| 263 | } | ||
| 264 | else { | ||
| 265 | $hyp_part = | ||
| 266 | -( $dalpha_domega * $alpha ) * | ||
| 267 | ( ( ( $h + $x ) * Math::Trig::cosh( $alpha * ( $h - $x ) ) ) + | ||
| 268 | ( ( $h - $x ) * Math::Trig::cosh( $alpha * ( $h + $x ) ) ) ) / | ||
| 269 | ( 2 * | ||
| 270 | Math::Trig::sinh( $alpha * $h ) * | ||
| 271 | Math::Trig::sinh( $alpha * $h ) ) + | ||
| 272 | $dalpha_domega * | ||
| 273 | ( Math::Trig::sinh( $alpha * ( $h - $x ) ) + | ||
| 274 | Math::Trig::sinh( $alpha * ( $h + $x ) ) ) / | ||
| 275 | ( 2 * | ||
| 276 | Math::Trig::sinh( $alpha * $h ) * | ||
| 277 | Math::Trig::sinh( $alpha * $h ) ); | ||
| 278 | } | ||
| 279 | |||
| 280 | my $d2c_domegadx = ( $hyp_part + $series_part ) * exp( -$r_q * $w * $t ); | ||
| 281 | |||
| 282 | return $d2c_domegadx; | ||
| 283 | } | ||
| 284 | |||
| 285 | sub ot_up_ko_down_pelsser_1997 { | ||
| 286 | my ( $S, $U, $D, $t, $r_q, $mu, $vol, $w ) = @_; | ||
| 287 | |||
| 288 | my $mu_new = $mu - ( 0.5 * $vol * $vol ); | ||
| 289 | my $h = log( $U / $D ); | ||
| 290 | my $x = log( $S / $D ); | ||
| 291 | my $omega = ( $vol * $vol ); | ||
| 292 | |||
| 293 | my $c = | ||
| 294 | Math::Business::BlackScholes::Binaries::common_function_pelsser_1997( $S, | ||
| 295 | $U, $D, $t, $r_q, $mu, $vol, $w, 1 ); | ||
| 296 | my $dc_domega = | ||
| 297 | Math::Business::BlackScholes::Binaries::Greeks::Vega::w_common_function_pelsser_1997( | ||
| 298 | $S, $U, $D, $t, $r_q, $mu, $vol, $w, 1 ); | ||
| 299 | my $dc_dx = | ||
| 300 | Math::Business::BlackScholes::Binaries::Greeks::Delta::x_common_function_pelsser_1997( | ||
| 301 | $S, $U, $D, $t, $r_q, $mu, $vol, $w, 1 ); | ||
| 302 | my $d2c_domegadx = | ||
| 303 | xw_common_function_pelsser_1997( $S, $U, $D, $t, $r_q, $mu, $vol, $w, 1 ); | ||
| 304 | |||
| 305 | my $d2Vu_domegadx = | ||
| 306 | ( ( ( ( 0.5 * $omega ) + $mu_new ) / ( $omega * $omega ) ) * | ||
| 307 | ( 1 + ( $mu_new / $omega ) * ( $h - $x ) ) * | ||
| 308 | exp( ( $mu_new / $omega ) * ( $h - $x ) ) * | ||
| 309 | $c ) - | ||
| 310 | ( ( ( ( 0.5 * $omega ) + $mu_new ) / ( $omega * $omega ) ) * | ||
| 311 | ( $h - $x ) * | ||
| 312 | exp( ( $mu_new / $omega ) * ( $h - $x ) ) * | ||
| 313 | $dc_dx ) - | ||
| 314 | ( ( $mu_new / $omega ) * | ||
| 315 | exp( ( $mu_new / $omega ) * ( $h - $x ) ) * | ||
| 316 | $dc_domega ) + | ||
| 317 | ( exp( ( $mu_new / $omega ) * ( $h - $x ) ) * $d2c_domegadx ); | ||
| 318 | |||
| 319 | return ( 2 * $vol / $S ) * $d2Vu_domegadx; | ||
| 320 | } | ||
| 321 | |||
| 322 | sub ot_down_ko_up_pelsser_1997 { | ||
| 323 | my ( $S, $U, $D, $t, $r_q, $mu, $vol, $w ) = @_; | ||
| 324 | |||
| 325 | my $mu_new = $mu - ( 0.5 * $vol * $vol ); | ||
| 326 | my $h = log( $U / $D ); | ||
| 327 | my $x = log( $S / $D ); | ||
| 328 | my $omega = ( $vol * $vol ); | ||
| 329 | |||
| 330 | my $c = | ||
| 331 | Math::Business::BlackScholes::Binaries::common_function_pelsser_1997( $S, | ||
| 332 | $U, $D, $t, $r_q, $mu, $vol, $w, 0 ); | ||
| 333 | my $dc_domega = | ||
| 334 | Math::Business::BlackScholes::Binaries::Greeks::Vega::w_common_function_pelsser_1997( | ||
| 335 | $S, $U, $D, $t, $r_q, $mu, $vol, $w, 0 ); | ||
| 336 | my $dc_dx = | ||
| 337 | Math::Business::BlackScholes::Binaries::Greeks::Delta::x_common_function_pelsser_1997( | ||
| 338 | $S, $U, $D, $t, $r_q, $mu, $vol, $w, 0 ); | ||
| 339 | my $d2c_domegadx = | ||
| 340 | xw_common_function_pelsser_1997( $S, $U, $D, $t, $r_q, $mu, $vol, $w, 0 ); | ||
| 341 | |||
| 342 | my $d2Vl_domegadx = | ||
| 343 | ( ( ( ( 0.5 * $omega ) + $mu_new ) / ( $omega * $omega ) ) * | ||
| 344 | ( 1 - ( $mu_new / $omega ) * $x ) * | ||
| 345 | exp( -( $mu_new / $omega ) * $x ) * | ||
| 346 | $c ) - | ||
| 347 | ( ( ( ( 0.5 * $omega ) + $mu_new ) / ( $omega * $omega ) ) * | ||
| 348 | $x * | ||
| 349 | exp( -( $mu_new / $omega ) * $x ) * | ||
| 350 | $dc_dx ) - | ||
| 351 | ( ( $mu_new / $omega ) * exp( -( $mu_new / $omega ) * $x ) * $dc_domega ) | ||
| 352 | - ( exp( -( $mu_new / $omega ) * $x ) * $d2c_domegadx ); | ||
| 353 | |||
| 354 | return ( 2 * $vol / $S ) * $d2Vl_domegadx; | ||
| 355 | } | ||
| 356 | |||
| 357 | sub range { | ||
| 358 | my ( $S, $U, $D, $t, $r_q, $mu, $vol, $w ) = @_; | ||
| 359 | |||
| 360 | # Range always pay out at end | ||
| 361 | $w = 1; | ||
| 362 | |||
| 363 | return -1 * upordown( $S, $U, $D, $t, $r_q, $mu, $vol, $w ); | ||
| 364 | } | ||
| 365 | |||
| 366 | 1; | ||
| 367 |