« blib/lib/Math/Business/BlackScholes/Binaries/Greeks/Delta.pm
↑ Summary
blib/lib/Math/Business/BlackScholes/Binaries/Greeks/Volga.pm »
| 1 | package Math::Business::BlackScholes::Binaries::Greeks::Gamma; | ||
| 2 | use strict; use warnings; | ||
| 3 | |||
| 4 | our $VERSION = '0.04'; | ||
| 5 | |||
| 6 | =head1 NAME | ||
| 7 | |||
| 8 | Math::Business::BlackScholes::Binaries::Greeks::Gamma | ||
| 9 | |||
| 10 | =head1 DESCRIPTION | ||
| 11 | |||
| 12 | Gets the gamma 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::CDF qw( pnorm ); | ||
| 24 | use Math::Trig; | ||
| 25 | use Math::Business::BlackScholes::Binaries; | ||
| 26 | use Math::Business::BlackScholes::Binaries::Greeks::Math qw( ddgauss dgauss ); | ||
| 27 | |||
| 28 | sub vanilla_call { | ||
| 29 | my ( $S0, $Strike, $t, $r_q, $mu, $vol ) = @_; | ||
| 30 | |||
| 31 | my $d1 = | ||
| 32 | ( log( $S0 / $Strike ) + ( $mu + ( ( $vol**2 ) / 2 ) ) * $t ) / | ||
| 33 | ( $vol * sqrt($t) ); | ||
| 34 | |||
| 35 | my $gamma = | ||
| 36 | dgauss($d1) * exp( ( $mu - $r_q ) * $t ) / ( $S0 * $vol * sqrt($t) ); | ||
| 37 | |||
| 38 | return $gamma; | ||
| 39 | } | ||
| 40 | |||
| 41 | sub vanilla_put { | ||
| 42 | return vanilla_call(@_); | ||
| 43 | } | ||
| 44 | |||
| 45 | sub call { | ||
| 46 | my ( $S, $U, $t, $r_q, $mu, $vol ) = @_; | ||
| 47 | |||
| 48 | my $v = $mu - ( $vol**2 ) / 2; | ||
| 49 | my $a = log( $U / $S ); | ||
| 50 | |||
| 51 | my $da = -1 / $S; | ||
| 52 | my $dda = 1 / ( $S * $S ); | ||
| 53 | |||
| 54 | my $q = ( $a - $v * $t ) / ( $vol * sqrt($t) ); | ||
| 55 | my $dq = $da / ( $vol * sqrt($t) ); | ||
| 56 | my $ddq = $dda / ( $vol * sqrt($t) ); | ||
| 57 | |||
| 58 | my $gamma = | ||
| 59 | -exp( -$r_q * $t ) * ( ddgauss($q) * $dq * $dq + dgauss($q) * $ddq ); | ||
| 60 | |||
| 61 | return $gamma; | ||
| 62 | } | ||
| 63 | |||
| 64 | sub put { | ||
| 65 | my ( $S, $D, $t, $r_q, $mu, $vol ) = @_; | ||
| 66 | |||
| 67 | my $v = $mu - ( $vol**2 ) / 2; | ||
| 68 | my $b = log( $D / $S ); | ||
| 69 | my $db = -1 / $S; | ||
| 70 | my $ddb = 1 / ( $S * $S ); | ||
| 71 | |||
| 72 | my $q = ( $b - $v * $t ) / ( $vol * sqrt($t) ); | ||
| 73 | my $dq = $db / ( $vol * sqrt($t) ); | ||
| 74 | my $ddq = $ddb / ( $vol * sqrt($t) ); | ||
| 75 | |||
| 76 | my $gamma = | ||
| 77 | exp( -$r_q * $t ) * ( ddgauss($q) * $dq * $dq + dgauss($q) * $ddq ); | ||
| 78 | |||
| 79 | return $gamma; | ||
| 80 | } | ||
| 81 | |||
| 82 | sub expirymiss { | ||
| 83 | my ( $S, $U, $D, $t, $r_q, $mu, $vol ) = @_; | ||
| 84 | |||
| 85 | return call( $S, $U, $t, $r_q, $mu, $vol ) + | ||
| 86 | put( $S, $D, $t, $r_q, $mu, $vol ); | ||
| 87 | } | ||
| 88 | |||
| 89 | sub expiryrange { | ||
| 90 | my ( $S, $U, $D, $t, $r_q, $mu, $vol ) = @_; | ||
| 91 | |||
| 92 | return -1 * expirymiss( $S, $U, $D, $t, $r_q, $mu, $vol ); | ||
| 93 | } | ||
| 94 | |||
| 95 | sub onetouch { | ||
| 96 | my ( $S, $U, $t, $r_q, $mu, $vol, $w ) = @_; | ||
| 97 | if ( not defined $w ) { | ||
| 98 | $w = 0; | ||
| 99 | } | ||
| 100 | |||
| 101 | my $sqrt_t = sqrt($t); | ||
| 102 | |||
| 103 | my $theta = ( ($mu) / $vol ) + ( 0.5 * $vol ); | ||
| 104 | |||
| 105 | my $theta_ = ( ($mu) / $vol ) - ( 0.5 * $vol ); | ||
| 106 | |||
| 107 | # Floor v_ squared near zero in case negative interest rates push it negative. | ||
| 108 | my $v_ = sqrt( max( $Math::Business::BlackScholes::Binaries::SMALL_VALUE_MU, ( $theta_ * $theta_ ) + ( 2 * ( 1 - $w ) * $r_q ) ) ); | ||
| 109 | |||
| 110 | my $e = ( log( $S / $U ) - ( $vol * $v_ * $t ) ) / ( $vol * $sqrt_t ); | ||
| 111 | |||
| 112 | my $e_ = ( -log( $S / $U ) - ( $vol * $v_ * $t ) ) / ( $vol * $sqrt_t ); | ||
| 113 | |||
| 114 | my $eta = ( $S > $U ) ? 1 : -1; | ||
| 115 | |||
| 116 | my $part1 = | ||
| 117 | ( ( $U / $S )**( ( $theta_ + $v_ ) / $vol ) ) * | ||
| 118 | pnorm( -$eta * $e ) * | ||
| 119 | ( $r_q * ( 1 - $w ) + ($mu) * ( $theta_ + $v_ ) / $vol ); | ||
| 120 | my $part2 = | ||
| 121 | ( ( $U / $S )**( ( $theta_ - $v_ ) / $vol ) ) * | ||
| 122 | pnorm( $eta * $e_ ) * | ||
| 123 | ( $r_q * ( 1 - $w ) + ($mu) * ( $theta_ - $v_ ) / $vol ); | ||
| 124 | my $part3 = | ||
| 125 | $eta * | ||
| 126 | ( ( $U / $S )**( ( $theta_ + $v_ ) / $vol ) ) * | ||
| 127 | dgauss($e) * | ||
| 128 | ( -$e_ * 0.5 / $t + ($mu) / ( $vol * $sqrt_t ) ); | ||
| 129 | my $part4 = | ||
| 130 | $eta * | ||
| 131 | ( ( $U / $S )**( ( $theta_ - $v_ ) / $vol ) ) * | ||
| 132 | dgauss($e_) * | ||
| 133 | ( $e * 0.5 / $t + ($mu) / ( $vol * $sqrt_t ) ); | ||
| 134 | |||
| 135 | my $gamma = $part1 + $part2 + $part3 + $part4; | ||
| 136 | return $gamma * 2 * exp( -$w * $r_q * $t ) / ( $vol * $vol * $S * $S ); | ||
| 137 | } | ||
| 138 | |||
| 139 | sub notouch { | ||
| 140 | my ( $S, $U, $t, $r_q, $mu, $vol, $w ) = @_; | ||
| 141 | |||
| 142 | # No touch bet always pay out at end | ||
| 143 | $w = 1; | ||
| 144 | |||
| 145 | return -1 * onetouch( $S, $U, $t, $r_q, $mu, $vol, $w ); | ||
| 146 | } | ||
| 147 | |||
| 148 | sub upordown { | ||
| 149 | my ( $S, $U, $D, $t, $r_q, $mu, $vol, $w ) = @_; | ||
| 150 | |||
| 151 | # $w = 0, paid at hit | ||
| 152 | # $w = 1, paid at end | ||
| 153 | if ( not defined $w ) { $w = 0; } | ||
| 154 | |||
| 155 | return ot_up_ko_down_pelsser_1997( $S, $U, $D, $t, $r_q, $mu, $vol, $w ) + | ||
| 156 | ot_down_ko_up_pelsser_1997( $S, $U, $D, $t, $r_q, $mu, $vol, $w ); | ||
| 157 | } | ||
| 158 | |||
| 159 | sub xx_common_function_pelsser_1997 { | ||
| 160 | my ( $S, $U, $D, $t, $r_q, $mu, $vol, $w, $eta ) = @_; | ||
| 161 | |||
| 162 | my $pi = Math::Trig::pi; | ||
| 163 | |||
| 164 | my $h = log( $U / $D ); | ||
| 165 | my $x = log( $S / $D ); | ||
| 166 | |||
| 167 | # $eta = 1, onetouch up knockout down | ||
| 168 | # $eta = 0, onetouch down knockout up | ||
| 169 | # This variable used to check stability | ||
| 170 | if ( not defined $eta ) { | ||
| 171 | die | ||
| 172 | "$0: (xx_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."; | ||
| 173 | } | ||
| 174 | if ( $eta == 0 ) { $x = $h - $x; } | ||
| 175 | |||
| 176 | my $mu_ = $mu - ( 0.5 * $vol * $vol ); | ||
| 177 | my $mu_dash = | ||
| 178 | sqrt( max( $Math::Business::BlackScholes::Binaries::SMALL_VALUE_MU, ( $mu_ * $mu_ ) + ( 2 * $vol * $vol * $r_q * ( 1 - $w ) ) ) ); | ||
| 179 | |||
| 180 | my $series_part = 0; | ||
| 181 | my $hyp_part = 0; | ||
| 182 | |||
| 183 | my $stability_constant = | ||
| 184 | Math::Business::BlackScholes::Binaries::get_stability_constant_pelsser_1997( | ||
| 185 | $S, $U, $D, $t, $r_q, $mu, $vol, $w, $eta, 3 ); | ||
| 186 | |||
| 187 | my $iterations_required = | ||
| 188 | Math::Business::BlackScholes::Binaries::get_min_iterations_pelsser_1997( | ||
| 189 | $S, $U, $D, $t, $r_q, $mu, $vol, $w ); | ||
| 190 | |||
| 191 | for ( my $k = 1 ; $k < $iterations_required ; $k++ ) { | ||
| 192 | my $lambda_k_dash = ( | ||
| 193 | 0.5 * ( | ||
| 194 | ( $mu_dash * $mu_dash ) / ( $vol * $vol ) + | ||
| 195 | ( $k * $k * $pi * $pi * $vol * $vol ) / ( $h * $h ) | ||
| 196 | ) | ||
| 197 | ); | ||
| 198 | |||
| 199 | my $phi = | ||
| 200 | ( $vol * $vol ) / ( $h**4 ) * exp( -$lambda_k_dash * $t ) * ( $k**3 ) | ||
| 201 | / $lambda_k_dash; | ||
| 202 | |||
| 203 | $series_part += $phi * ( $pi**3 ) * sin( $k * $pi * ( $h - $x ) / $h ); | ||
| 204 | |||
| 205 | if ( $k == 1 | ||
| 206 | and ( not( abs( $phi / ( $S**2 ) ) < $stability_constant ) ) ) | ||
| 207 | { | ||
| 208 | die | ||
| 209 | "$0: PELSSER GAMMA 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 GAMMA stability conditions ($phi / ($S * $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."; | ||
| 210 | } | ||
| 211 | } | ||
| 212 | |||
| 213 | # Need to take care when $mu goes to zero | ||
| 214 | if ( abs($mu_) < $Math::Business::BlackScholes::Binaries::SMALL_VALUE_MU ) { | ||
| 215 | my $sign = ( $mu_ >= 0 ) ? 1 : -1; | ||
| 216 | $mu_ = $sign * $Math::Business::BlackScholes::Binaries::SMALL_VALUE_MU; | ||
| 217 | $mu_dash = | ||
| 218 | sqrt( max ( $Math::Business::BlackScholes::Binaries::SMALL_VALUE_MU, ( $mu_ * $mu_ ) + ( 2 * $vol * $vol * $r_q * ( 1 - $w ) ) ) ); | ||
| 219 | } | ||
| 220 | |||
| 221 | $hyp_part = | ||
| 222 | ( ( $mu_dash**2 ) / ( $vol**4 ) ) * | ||
| 223 | ( Math::Trig::sinh( $mu_dash * $x / ( $vol * $vol ) ) / | ||
| 224 | Math::Trig::sinh( $mu_dash * $h / ( $vol * $vol ) ) ); | ||
| 225 | |||
| 226 | my $d2c_dwdx = ( $hyp_part + $series_part ) * exp( -$r_q * $t * $w ); | ||
| 227 | |||
| 228 | return $d2c_dwdx; | ||
| 229 | } | ||
| 230 | |||
| 231 | sub ot_up_ko_down_pelsser_1997 { | ||
| 232 | my ( $S, $U, $D, $t, $r_q, $mu, $vol, $w ) = @_; | ||
| 233 | |||
| 234 | my $mu_ = $mu - ( 0.5 * $vol * $vol ); | ||
| 235 | my $h = log( $U / $D ); | ||
| 236 | my $x = log( $S / $D ); | ||
| 237 | |||
| 238 | my $c = | ||
| 239 | Math::Business::BlackScholes::Binaries::common_function_pelsser_1997( $S, | ||
| 240 | $U, $D, $t, $r_q, $mu, $vol, $w, 1 ); | ||
| 241 | my $dc_dx = | ||
| 242 | Math::Business::BlackScholes::Binaries::Greeks::Delta::x_common_function_pelsser_1997( | ||
| 243 | $S, $U, $D, $t, $r_q, $mu, $vol, $w, 1 ); | ||
| 244 | my $d2c_dx2 = | ||
| 245 | xx_common_function_pelsser_1997( $S, $U, $D, $t, $r_q, $mu, $vol, $w, 1 ); | ||
| 246 | |||
| 247 | my $dVu_dx = -( | ||
| 248 | ( $mu_ / ( $vol * $vol ) ) * | ||
| 249 | Math::Business::BlackScholes::Binaries::common_function_pelsser_1997( | ||
| 250 | $S, $U, $D, $t, $r_q, $mu, $vol, $w, 1 | ||
| 251 | ) | ||
| 252 | ); | ||
| 253 | $dVu_dx += | ||
| 254 | Math::Business::BlackScholes::Binaries::Greeks::Delta::x_common_function_pelsser_1997( | ||
| 255 | $S, $U, $D, $t, $r_q, $mu, $vol, $w, 1 ); | ||
| 256 | $dVu_dx *= exp( $mu_ * ( $h - $x ) / ( $vol * $vol ) ); | ||
| 257 | |||
| 258 | my $d2Vu_dx2 = | ||
| 259 | ( ( ( $mu_**2 ) / ( $vol**4 ) ) * | ||
| 260 | exp( ( $mu_ / ( $vol * $vol ) ) * ( $h - $x ) ) * | ||
| 261 | $c ) - | ||
| 262 | ( 2 * | ||
| 263 | ( $mu_ / ( $vol**2 ) ) * | ||
| 264 | exp( ( $mu_ / ( $vol * $vol ) ) * ( $h - $x ) ) * | ||
| 265 | $dc_dx ) + | ||
| 266 | ( exp( ( $mu_ / ( $vol**2 ) ) * ( $h - $x ) ) * $d2c_dx2 ); | ||
| 267 | |||
| 268 | return ( 1 / ( $S**2 ) ) * ( $d2Vu_dx2 - $dVu_dx ); | ||
| 269 | } | ||
| 270 | |||
| 271 | sub ot_down_ko_up_pelsser_1997 { | ||
| 272 | my ( $S, $U, $D, $t, $r_q, $mu, $vol, $w ) = @_; | ||
| 273 | |||
| 274 | my $mu_ = $mu - ( 0.5 * $vol * $vol ); | ||
| 275 | my $h = log( $U / $D ); | ||
| 276 | my $x = log( $S / $D ); | ||
| 277 | |||
| 278 | my $c = | ||
| 279 | Math::Business::BlackScholes::Binaries::common_function_pelsser_1997( $S, | ||
| 280 | $U, $D, $t, $r_q, $mu, $vol, $w, 0 ); | ||
| 281 | my $dc_dx = | ||
| 282 | Math::Business::BlackScholes::Binaries::Greeks::Delta::x_common_function_pelsser_1997( | ||
| 283 | $S, $U, $D, $t, $r_q, $mu, $vol, $w, 0 ); | ||
| 284 | my $d2c_dx2 = | ||
| 285 | xx_common_function_pelsser_1997( $S, $U, $D, $t, $r_q, $mu, $vol, $w, 0 ); | ||
| 286 | |||
| 287 | my $dVl_dx = -( | ||
| 288 | ( $mu_ / ( $vol * $vol ) ) * | ||
| 289 | Math::Business::BlackScholes::Binaries::common_function_pelsser_1997( | ||
| 290 | $S, $U, $D, $t, $r_q, $mu, $vol, $w, 0 | ||
| 291 | ) | ||
| 292 | ); | ||
| 293 | $dVl_dx -= | ||
| 294 | Math::Business::BlackScholes::Binaries::Greeks::Delta::x_common_function_pelsser_1997( | ||
| 295 | $S, $U, $D, $t, $r_q, $mu, $vol, $w, 0 ); | ||
| 296 | $dVl_dx *= exp( -$mu_ * $x / ( $vol * $vol ) ); | ||
| 297 | |||
| 298 | my $d2Vl_dx2 = | ||
| 299 | ( ( ( $mu_**2 ) / ( $vol**4 ) ) * exp( -( $mu_ / ( $vol * $vol ) ) * $x ) | ||
| 300 | * $c ) + | ||
| 301 | ( 2 * | ||
| 302 | ( $mu_ / ( $vol**2 ) ) * | ||
| 303 | exp( -( $mu_ / ( $vol * $vol ) ) * $x ) * | ||
| 304 | $dc_dx ) + | ||
| 305 | ( exp( -( $mu_ / ( $vol**2 ) ) * $x ) * $d2c_dx2 ); | ||
| 306 | |||
| 307 | return ( 1 / ( $S**2 ) ) * ( $d2Vl_dx2 - $dVl_dx ); | ||
| 308 | } | ||
| 309 | |||
| 310 | sub range { | ||
| 311 | my ( $S, $U, $D, $t, $r_q, $mu, $vol, $w ) = @_; | ||
| 312 | |||
| 313 | # Range always pay out at end | ||
| 314 | $w = 1; | ||
| 315 | |||
| 316 | return -1 * upordown( $S, $U, $D, $t, $r_q, $mu, $vol, $w ); | ||
| 317 | } | ||
| 318 | |||
| 319 | 1; | ||
| 320 |