« blib/lib/Math/Business/BlackScholes/Binaries/Greeks/Theta.pm
↑ Summary
blib/lib/Math/Business/BlackScholes/Binaries/Greeks/Vanna.pm »
| 1 | package Math::Business::BlackScholes::Binaries::Greeks::Vega; | ||
| 2 | use strict; use warnings; | ||
| 3 | |||
| 4 | our $VERSION = '0.04'; | ||
| 5 | |||
| 6 | =head1 NAME | ||
| 7 | |||
| 8 | Math::Business::BlackScholes::Binaries::Greeks::Vega | ||
| 9 | |||
| 10 | =head1 DESCRIPTION | ||
| 11 | |||
| 12 | Gets the Vega 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( dgauss ); | ||
| 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 $vega = $S * sqrt($t) * exp( ( $mu - $r_q ) * $t ) * dgauss($d1); | ||
| 35 | return $vega; | ||
| 36 | } | ||
| 37 | |||
| 38 | sub vanilla_put { | ||
| 39 | my ( $S, $K, $t, $r_q, $mu, $vol ) = @_; | ||
| 40 | |||
| 41 | # Same as vega of vanilla call | ||
| 42 | return vanilla_call( $S, $K, $t, $r_q, $mu, $vol ); | ||
| 43 | } | ||
| 44 | |||
| 45 | sub call { | ||
| 46 | my ( $S, $U, $t, $r_q, $mu, $vol ) = @_; | ||
| 47 | |||
| 48 | my $d1 = | ||
| 49 | ( log( $S / $U ) + ( $mu + $vol * $vol / 2.0 ) * $t ) / | ||
| 50 | ( $vol * sqrt($t) ); | ||
| 51 | my $d2 = $d1 - $vol * sqrt($t); | ||
| 52 | my $vega = -exp( -$r_q * $t ) * dgauss($d2) * $d1 / $vol; | ||
| 53 | return $vega; | ||
| 54 | } | ||
| 55 | |||
| 56 | sub put { | ||
| 57 | my ( $S, $D, $t, $r_q, $mu, $vol ) = @_; | ||
| 58 | |||
| 59 | my $d1 = | ||
| 60 | ( log( $S / $D ) + ( $mu + $vol * $vol / 2.0 ) * $t ) / | ||
| 61 | ( $vol * sqrt($t) ); | ||
| 62 | my $d2 = $d1 - $vol * sqrt($t); | ||
| 63 | my $vega = exp( -$r_q * $t ) * dgauss($d2) * $d1 / $vol; | ||
| 64 | return $vega; | ||
| 65 | } | ||
| 66 | |||
| 67 | sub expirymiss { | ||
| 68 | my ( $S, $U, $D, $t, $r_q, $mu, $vol ) = @_; | ||
| 69 | |||
| 70 | return call( $S, $U, $t, $r_q, $mu, $vol ) + | ||
| 71 | put( $S, $D, $t, $r_q, $mu, $vol ); | ||
| 72 | } | ||
| 73 | |||
| 74 | sub expiryrange { | ||
| 75 | my ( $S, $U, $D, $t, $r_q, $mu, $vol ) = @_; | ||
| 76 | |||
| 77 | return -1 * expirymiss( $S, $U, $D, $t, $r_q, $mu, $vol ); | ||
| 78 | } | ||
| 79 | |||
| 80 | sub onetouch { | ||
| 81 | my ( $S, $U, $t, $r_q, $mu, $vol, $w ) = @_; | ||
| 82 | |||
| 83 | if ( not defined $w ) { | ||
| 84 | $w = 0; | ||
| 85 | } | ||
| 86 | |||
| 87 | my $sqrt_t = sqrt($t); | ||
| 88 | |||
| 89 | my $theta = ( $mu / $vol ) + ( 0.5 * $vol ); | ||
| 90 | |||
| 91 | my $theta_ = ( $mu / $vol ) - ( 0.5 * $vol ); | ||
| 92 | |||
| 93 | # Floor v_ squared at just above zero in case negative interest rates push it negative. | ||
| 94 | my $v_ = sqrt( max( $Math::Business::BlackScholes::Binaries::SMALL_VALUE_MU, ( $theta_ * $theta_ ) + ( 2 * ( 1 - $w ) * $r_q ) ) ); | ||
| 95 | |||
| 96 | my $e = ( log( $S / $U ) - ( $vol * $v_ * $t ) ) / ( $vol * $sqrt_t ); | ||
| 97 | |||
| 98 | my $e_ = ( -log( $S / $U ) - ( $vol * $v_ * $t ) ) / ( $vol * $sqrt_t ); | ||
| 99 | |||
| 100 | my $eta = ( $S > $U ) ? 1 : -1; | ||
| 101 | |||
| 102 | my $pa_e = | ||
| 103 | ( log( $U / $S ) / ( $vol * $vol * $sqrt_t ) ) + | ||
| 104 | ( ( $theta_ * $theta ) / ( $vol * $v_ ) * $sqrt_t ); | ||
| 105 | my $pa_e_ = | ||
| 106 | ( -log( $U / $S ) / ( $vol * $vol * $sqrt_t ) ) + | ||
| 107 | ( ( $theta_ * $theta ) / ( $vol * $v_ ) * $sqrt_t ); | ||
| 108 | my $A = | ||
| 109 | -( $theta + $theta_ + ( $theta_ * $theta / $v_ ) + $v_ ) / | ||
| 110 | ( $vol * $vol ); | ||
| 111 | my $A_ = | ||
| 112 | -( $theta + $theta_ - ( $theta_ * $theta / $v_ ) - $v_ ) / | ||
| 113 | ( $vol * $vol ); | ||
| 114 | |||
| 115 | my $part1 = | ||
| 116 | pnorm( -$eta * $e ) * $A * log( $U / $S ) - $eta * dgauss($e) * $pa_e; | ||
| 117 | my $part2 = | ||
| 118 | pnorm( $eta * $e_ ) * $A_ * log( $U / $S ) + $eta * dgauss($e_) * $pa_e_; | ||
| 119 | my $vega = | ||
| 120 | ( ( $U / $S )**( ( $theta_ + $v_ ) / $vol ) ) * $part1 + | ||
| 121 | ( ( $U / $S )**( ( $theta_ - $v_ ) / $vol ) ) * $part2; | ||
| 122 | |||
| 123 | return $vega * exp( -$w * $r_q * $t ); | ||
| 124 | } | ||
| 125 | |||
| 126 | sub notouch { | ||
| 127 | my ( $S, $U, $t, $r_q, $mu, $vol, $w ) = @_; | ||
| 128 | |||
| 129 | # No touch bet always pay out at end | ||
| 130 | $w = 1; | ||
| 131 | |||
| 132 | return -1 * onetouch( $S, $U, $t, $r_q, $mu, $vol, $w ); | ||
| 133 | } | ||
| 134 | |||
| 135 | sub upordown { | ||
| 136 | my ( $S, $U, $D, $t, $r_q, $mu, $vol, $w ) = @_; | ||
| 137 | |||
| 138 | # $w = 0, paid at hit | ||
| 139 | # $w = 1, paid at end | ||
| 140 | if ( not defined $w ) { $w = 0; } | ||
| 141 | |||
| 142 | return ot_up_ko_down_pelsser_1997( $S, $U, $D, $t, $r_q, $mu, $vol, $w ) + | ||
| 143 | ot_down_ko_up_pelsser_1997( $S, $U, $D, $t, $r_q, $mu, $vol, $w ); | ||
| 144 | } | ||
| 145 | |||
| 146 | sub w_common_function_pelsser_1997 { | ||
| 147 | my ( $S, $U, $D, $t, $r_q, $mu, $vol, $w, $eta ) = @_; | ||
| 148 | |||
| 149 | my $pi = Math::Trig::pi; | ||
| 150 | |||
| 151 | my $h = log( $U / $D ); | ||
| 152 | my $x = log( $S / $D ); | ||
| 153 | |||
| 154 | # $eta = 1, onetouch up knockout down | ||
| 155 | # $eta = 0, onetouch down knockout up | ||
| 156 | # This variable used to check stability | ||
| 157 | if ( not defined $eta ) { | ||
| 158 | die | ||
| 159 | "$0: (w_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."; | ||
| 160 | } | ||
| 161 | if ( $eta == 0 ) { $x = $h - $x; } | ||
| 162 | |||
| 163 | my $r_dash = $r_q * ( 1 - $w ); | ||
| 164 | my $mu_new = $mu - ( 0.5 * $vol * $vol ); | ||
| 165 | my $mu_dash = sqrt( max( $Math::Business::BlackScholes::Binaries::SMALL_VALUE_MU, ( $mu_new * $mu_new ) + ( 2 * $vol * $vol * $r_dash ) ) ); | ||
| 166 | |||
| 167 | my $omega = ( $vol * $vol ); | ||
| 168 | |||
| 169 | my $series_part = 0; | ||
| 170 | my $hyp_part = 0; | ||
| 171 | |||
| 172 | my $stability_constant = | ||
| 173 | Math::Business::BlackScholes::Binaries::get_stability_constant_pelsser_1997( | ||
| 174 | $S, $U, $D, $t, $r_q, $mu, $vol, $w, $eta, 1 ); | ||
| 175 | |||
| 176 | my $iterations_required = | ||
| 177 | Math::Business::BlackScholes::Binaries::get_min_iterations_pelsser_1997( | ||
| 178 | $S, $U, $D, $t, $r_q, $mu, $vol, $w ); | ||
| 179 | |||
| 180 | for ( my $k = 1 ; $k < $iterations_required ; $k++ ) { | ||
| 181 | my $lambda_k_dash = ( | ||
| 182 | 0.5 * ( | ||
| 183 | ( $mu_dash * $mu_dash ) / $omega + | ||
| 184 | ( $k * $k * $pi * $pi * $vol * $vol ) / ( $h * $h ) | ||
| 185 | ) | ||
| 186 | ); | ||
| 187 | |||
| 188 | # d{lambda_k}/dw | ||
| 189 | my $dlambdak_domega = | ||
| 190 | 0.5 * | ||
| 191 | ( -( $mu_new / $omega ) - | ||
| 192 | ( ( $mu_new * $mu_new ) / ( $omega * $omega ) ) + | ||
| 193 | ( ( $k * $k * $pi * $pi ) / ( $h * $h ) ) ); | ||
| 194 | |||
| 195 | my $beta_k = exp( -$lambda_k_dash * $t ) / $lambda_k_dash; | ||
| 196 | |||
| 197 | # d{beta_k}/d{lambda_k} | ||
| 198 | my $dbetak_dlambdak = | ||
| 199 | -exp( -$lambda_k_dash * $t ) * | ||
| 200 | ( ( $t * $lambda_k_dash + 1 ) / ( $lambda_k_dash**2 ) ); | ||
| 201 | |||
| 202 | # d{beta_k}/dw | ||
| 203 | my $dbetak_domega = $dlambdak_domega * $dbetak_dlambdak; | ||
| 204 | |||
| 205 | my $phi = | ||
| 206 | ( 1.0 / ( $h * $h ) ) * ( $omega * $dbetak_domega + $beta_k ) * $k; | ||
| 207 | |||
| 208 | $series_part += $phi * $pi * sin( $k * $pi * ( $h - $x ) / $h ); | ||
| 209 | |||
| 210 | # | ||
| 211 | # For vega, the stability function is 2* $vol * $phi, for volga/vanna it is different, | ||
| 212 | # but we shall ignore for now. | ||
| 213 | # | ||
| 214 | if ( $k == 1 | ||
| 215 | and ( not( abs( 2 * $vol * $phi ) < $stability_constant ) ) ) | ||
| 216 | { | ||
| 217 | die | ||
| 218 | "$0: PELSSER VEGA 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 VEGA stability conditions (2 * $vol * $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."; | ||
| 219 | } | ||
| 220 | } | ||
| 221 | |||
| 222 | my $alpha = $mu_dash / ( $vol * $vol ); | ||
| 223 | my $dalpha_domega = | ||
| 224 | -( ( $mu_new * $omega ) + | ||
| 225 | ( 2 * $mu_new * $mu_new ) + | ||
| 226 | ( 2 * $r_dash * $omega ) ) / | ||
| 227 | ( 2 * $alpha * $omega * $omega * $omega ); | ||
| 228 | |||
| 229 | # We have to handle the special case where the denominator approaches 0, see our documentation in | ||
| 230 | # quant/Documents/Breakout_bet.tex under the SVN "quant" module. | ||
| 231 | if ( ( Math::Trig::sinh( $alpha * $h )**2 ) == 0 ) { | ||
| 232 | $hyp_part = 0; | ||
| 233 | } | ||
| 234 | else { | ||
| 235 | $hyp_part = | ||
| 236 | ( $dalpha_domega / ( 2 * ( Math::Trig::sinh( $alpha * $h )**2 ) ) ) * | ||
| 237 | ( ( $h + $x ) * Math::Trig::sinh( $alpha * ( $h - $x ) ) - | ||
| 238 | ( $h - $x ) * Math::Trig::sinh( $alpha * ( $h + $x ) ) ); | ||
| 239 | } | ||
| 240 | |||
| 241 | my $dc_domega = ( $hyp_part - $series_part ) * exp( -$r_q * $w * $t ); | ||
| 242 | |||
| 243 | return $dc_domega; | ||
| 244 | } | ||
| 245 | |||
| 246 | sub ot_up_ko_down_pelsser_1997 { | ||
| 247 | my ( $S, $U, $D, $t, $r_q, $mu, $vol, $w ) = @_; | ||
| 248 | |||
| 249 | my $mu_new = $mu - ( 0.5 * $vol * $vol ); | ||
| 250 | my $h = log( $U / $D ); | ||
| 251 | my $x = log( $S / $D ); | ||
| 252 | my $omega = ( $vol * $vol ); | ||
| 253 | |||
| 254 | my $c = | ||
| 255 | Math::Business::BlackScholes::Binaries::common_function_pelsser_1997( $S, | ||
| 256 | $U, $D, $t, $r_q, $mu, $vol, $w, 1 ); | ||
| 257 | my $dc_domega = | ||
| 258 | w_common_function_pelsser_1997( $S, $U, $D, $t, $r_q, $mu, $vol, $w, 1 ); | ||
| 259 | |||
| 260 | my $dVu_domega = | ||
| 261 | -( ( 0.5 * $omega + $mu_new ) * ( $h - $x ) / ( $omega * $omega ) ) * $c; | ||
| 262 | $dVu_domega += $dc_domega; | ||
| 263 | $dVu_domega *= exp( $mu_new * ( $h - $x ) / $omega ); | ||
| 264 | |||
| 265 | return $dVu_domega * ( 2 * $vol ); | ||
| 266 | } | ||
| 267 | |||
| 268 | sub ot_down_ko_up_pelsser_1997 { | ||
| 269 | my ( $S, $U, $D, $t, $r_q, $mu, $vol, $w ) = @_; | ||
| 270 | |||
| 271 | my $mu_new = $mu - ( 0.5 * $vol * $vol ); | ||
| 272 | my $h = log( $U / $D ); | ||
| 273 | my $x = log( $S / $D ); | ||
| 274 | my $omega = ( $vol * $vol ); | ||
| 275 | |||
| 276 | my $c = | ||
| 277 | Math::Business::BlackScholes::Binaries::common_function_pelsser_1997( $S, | ||
| 278 | $U, $D, $t, $r_q, $mu, $vol, $w, 0 ); | ||
| 279 | my $dc_domega = | ||
| 280 | w_common_function_pelsser_1997( $S, $U, $D, $t, $r_q, $mu, $vol, $w, 0 ); | ||
| 281 | |||
| 282 | my $dVl_domega = | ||
| 283 | ( ( 0.5 * $omega + $mu_new ) * $x / ( $omega * $omega ) ) * $c; | ||
| 284 | $dVl_domega += $dc_domega; | ||
| 285 | $dVl_domega *= exp( -$mu_new * $x / $omega ); | ||
| 286 | |||
| 287 | return $dVl_domega * ( 2 * $vol ); | ||
| 288 | } | ||
| 289 | |||
| 290 | sub range { | ||
| 291 | my ( $S, $U, $D, $t, $r_q, $mu, $vol, $w ) = @_; | ||
| 292 | |||
| 293 | # Range always pay out at end | ||
| 294 | $w = 1; | ||
| 295 | |||
| 296 | return -1 * upordown( $S, $U, $D, $t, $r_q, $mu, $vol, $w ); | ||
| 297 | } | ||
| 298 | |||
| 299 | 1; | ||
| 300 |