| 1 | package Math::Business::BlackScholes::Binaries::Greeks::Delta; | ||
| 2 | use strict; use warnings; | ||
| 3 | |||
| 4 | our $VERSION = '0.04'; | ||
| 5 | |||
| 6 | =head1 NAME | ||
| 7 | |||
| 8 | Math::Business::BlackScholes::Binaries::Greeks::Delta | ||
| 9 | |||
| 10 | =head1 DESCRIPTION | ||
| 11 | |||
| 12 | Gets the delta for different options, Vanilla and Foreign for all contract types | ||
| 13 | |||
| 14 | =head1 COMMENTS | ||
| 15 | |||
| 16 | It is tricky to decide what form to use. Should the delta be with respect to | ||
| 17 | 1/$S, or with respect to $S? For the binary bets, whether foreign or domestic | ||
| 18 | we are differentiating with respect to $S. | ||
| 19 | |||
| 20 | For a vanilla, the correct way should be with respect to 1/$S (so that we know | ||
| 21 | how many units of the domestic currency to hedge), but to keep things standard, | ||
| 22 | we do it with respect to $S. | ||
| 23 | |||
| 24 | For example take USDJPY vanilla call with premium in USD. Thus this is a vanilla | ||
| 25 | contract on JPY. Thus delta with respect to 1/$S tells us how many units of JPY | ||
| 26 | to hedge, but with respect to $S, there really isn't a meaning and needs to be | ||
| 27 | converted back before interpretation. | ||
| 28 | |||
| 29 | =cut | ||
| 30 | |||
| 31 | =head1 SUBROUTINES | ||
| 32 | |||
| 33 | See L<Math::Business::BlackScholes::Binaries::Greeks> | ||
| 34 | |||
| 35 | =cut | ||
| 36 | |||
| 37 | use List::Util qw(max); | ||
| 38 | use Math::CDF qw(pnorm); | ||
| 39 | use Math::Trig; | ||
| 40 | use Math::Business::BlackScholes::Binaries; | ||
| 41 | use Math::Business::BlackScholes::Binaries::Greeks::Math qw( dgauss ); | ||
| 42 | |||
| 43 | sub vanilla_call { | ||
| 44 | my ( $S, $K, $t, $r_q, $mu, $vol ) = @_; | ||
| 45 | |||
| 46 | my $d1 = | ||
| 47 | ( log( $S / $K ) + ( $mu + $vol * $vol / 2.0 ) * $t ) / | ||
| 48 | ( $vol * sqrt($t) ); | ||
| 49 | |||
| 50 | return exp( ( $mu - $r_q ) * $t ) * pnorm($d1); | ||
| 51 | } | ||
| 52 | |||
| 53 | sub vanilla_put { | ||
| 54 | my ( $S, $K, $t, $r_q, $mu, $vol ) = @_; | ||
| 55 | |||
| 56 | my $d1 = | ||
| 57 | ( log( $S / $K ) + ( $mu + $vol * $vol / 2.0 ) * $t ) / | ||
| 58 | ( $vol * sqrt($t) ); | ||
| 59 | |||
| 60 | return -exp( ( $mu - $r_q ) * $t ) * pnorm( -$d1 ); | ||
| 61 | } | ||
| 62 | |||
| 63 | sub call { | ||
| 64 | my ( $S, $U, $t, $r_q, $mu, $vol ) = @_; | ||
| 65 | |||
| 66 | my $d2 = | ||
| 67 | ( log( $S / $U ) + ( $mu - $vol * $vol / 2.0 ) * $t ) / | ||
| 68 | ( $vol * sqrt($t) ); | ||
| 69 | |||
| 70 | return exp( -$r_q * $t ) * dgauss($d2) / ( $vol * sqrt($t) * $S ); | ||
| 71 | } | ||
| 72 | |||
| 73 | sub put { | ||
| 74 | my ( $S, $D, $t, $r_q, $mu, $vol ) = @_; | ||
| 75 | |||
| 76 | my $d2 = | ||
| 77 | ( log( $S / $D ) + ( $mu - $vol * $vol / 2.0 ) * $t ) / | ||
| 78 | ( $vol * sqrt($t) ); | ||
| 79 | |||
| 80 | return -exp( -$r_q * $t ) * dgauss($d2) / ( $vol * sqrt($t) * $S ); | ||
| 81 | } | ||
| 82 | |||
| 83 | sub expirymiss { | ||
| 84 | my ( $S, $U, $D, $t, $r_q, $mu, $vol ) = @_; | ||
| 85 | |||
| 86 | return call( $S, $U, $t, $r_q, $mu, $vol ) + | ||
| 87 | put( $S, $D, $t, $r_q, $mu, $vol ); | ||
| 88 | } | ||
| 89 | |||
| 90 | sub expiryrange { | ||
| 91 | my ( $S, $U, $D, $t, $r_q, $mu, $vol ) = @_; | ||
| 92 | |||
| 93 | return -1 * expirymiss( $S, $U, $D, $t, $r_q, $mu, $vol ); | ||
| 94 | } | ||
| 95 | |||
| 96 | sub onetouch { | ||
| 97 | my ( $S, $U, $t, $r_q, $mu, $vol, $w ) = @_; | ||
| 98 | |||
| 99 | # w = 0, rebate paid at hit. | ||
| 100 | # w = 1, rebate paid at end. | ||
| 101 | if ( not defined $w ) { | ||
| 102 | $w = 0; | ||
| 103 | } | ||
| 104 | |||
| 105 | my $sqrt_t = sqrt($t); | ||
| 106 | |||
| 107 | my $theta = ( $mu / $vol ) + ( 0.5 * $vol ); | ||
| 108 | |||
| 109 | my $theta_ = ( $mu / $vol ) - ( 0.5 * $vol ); | ||
| 110 | |||
| 111 | # Floor v_ squared near zero in case negative interest rates push it negative. | ||
| 112 | my $v_ = sqrt( max( $Math::Business::BlackScholes::Binaries::SMALL_VALUE_MU, ( $theta_ * $theta_ ) + ( 2 * ( 1 - $w ) * $r_q ) ) ); | ||
| 113 | |||
| 114 | my $e = ( log( $S / $U ) - ( $vol * $v_ * $t ) ) / ( $vol * $sqrt_t ); | ||
| 115 | |||
| 116 | my $e_ = ( -log( $S / $U ) - ( $vol * $v_ * $t ) ) / ( $vol * $sqrt_t ); | ||
| 117 | |||
| 118 | my $eta = ( $S > $U ) ? 1 : -1; | ||
| 119 | |||
| 120 | my $part1 = | ||
| 121 | ( $theta_ + $v_ ) * pnorm( -$eta * $e ) + $eta * dgauss($e) / $sqrt_t; | ||
| 122 | my $part2 = | ||
| 123 | ( $theta_ - $v_ ) * pnorm( $eta * $e_ ) + $eta * dgauss($e_) / $sqrt_t; | ||
| 124 | |||
| 125 | my $delta = | ||
| 126 | ( ( $U / $S )**( ( $theta_ + $v_ ) / $vol ) ) * $part1 + | ||
| 127 | ( ( $U / $S )**( ( $theta_ - $v_ ) / $vol ) ) * $part2; | ||
| 128 | |||
| 129 | return -$delta * exp( -$w * $r_q * $t ) / ( $vol * $S ); | ||
| 130 | } | ||
| 131 | |||
| 132 | sub notouch { | ||
| 133 | my ( $S, $U, $t, $r_q, $mu, $vol, $w ) = @_; | ||
| 134 | |||
| 135 | # No touch bet always pay out at end | ||
| 136 | $w = 1; | ||
| 137 | |||
| 138 | return -1 * onetouch( $S, $U, $t, $r_q, $mu, $vol, $w ); | ||
| 139 | } | ||
| 140 | |||
| 141 | sub upordown { | ||
| 142 | my ( $S, $U, $D, $t, $r_q, $mu, $vol, $w ) = @_; | ||
| 143 | |||
| 144 | # $w = 0, paid at hit | ||
| 145 | # $w = 1, paid at end | ||
| 146 | if ( not defined $w ) { $w = 0; } | ||
| 147 | |||
| 148 | return ot_up_ko_down_pelsser_1997( $S, $U, $D, $t, $r_q, $mu, $vol, $w ) + | ||
| 149 | ot_down_ko_up_pelsser_1997( $S, $U, $D, $t, $r_q, $mu, $vol, $w ); | ||
| 150 | } | ||
| 151 | |||
| 152 | sub x_common_function_pelsser_1997 { | ||
| 153 | my ( $S, $U, $D, $t, $r_q, $mu, $vol, $w, $eta ) = @_; | ||
| 154 | |||
| 155 | my $pi = Math::Trig::pi; | ||
| 156 | |||
| 157 | my $h = log( $U / $D ); | ||
| 158 | my $x = log( $S / $D ); | ||
| 159 | |||
| 160 | # $eta = 1, onetouch up knockout down | ||
| 161 | # $eta = 0, onetouch down knockout up | ||
| 162 | # This variable used to check stability | ||
| 163 | if ( not defined $eta ) { | ||
| 164 | die | ||
| 165 | "$0: (x_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."; | ||
| 166 | } | ||
| 167 | if ( $eta == 0 ) { $x = $h - $x; } | ||
| 168 | |||
| 169 | my $mu_new = $mu - ( 0.5 * $vol * $vol ); | ||
| 170 | my $mu_dash = | ||
| 171 | sqrt( max( $Math::Business::BlackScholes::Binaries::SMALL_VALUE_MU, ( $mu_new * $mu_new ) + ( 2 * $vol * $vol * $r_q * ( 1 - $w ) ) ) ); | ||
| 172 | |||
| 173 | my $series_part = 0; | ||
| 174 | my $hyp_part = 0; | ||
| 175 | |||
| 176 | my $stability_constant = | ||
| 177 | Math::Business::BlackScholes::Binaries::get_stability_constant_pelsser_1997( | ||
| 178 | $S, $U, $D, $t, $r_q, $mu, $vol, $w, $eta, 2 ); | ||
| 179 | |||
| 180 | my $iterations_required = | ||
| 181 | Math::Business::BlackScholes::Binaries::get_min_iterations_pelsser_1997( | ||
| 182 | $S, $U, $D, $t, $r_q, $mu, $vol, $w ); | ||
| 183 | |||
| 184 | for ( my $k = 1 ; $k < $iterations_required ; $k++ ) { | ||
| 185 | my $lambda_k_dash = ( | ||
| 186 | 0.5 * ( | ||
| 187 | ( $mu_dash * $mu_dash ) / ( $vol * $vol ) + | ||
| 188 | ( $k * $k * $pi * $pi * $vol * $vol ) / ( $h * $h ) | ||
| 189 | ) | ||
| 190 | ); | ||
| 191 | |||
| 192 | my $phi = | ||
| 193 | ( $vol * $vol ) / | ||
| 194 | ( $h * $h * $h ) * | ||
| 195 | exp( -$lambda_k_dash * $t ) * | ||
| 196 | $k * | ||
| 197 | $k / | ||
| 198 | $lambda_k_dash; | ||
| 199 | |||
| 200 | $series_part += $phi * $pi * $pi * cos( $k * $pi * ( $h - $x ) / $h ); | ||
| 201 | |||
| 202 | # | ||
| 203 | # For delta, the stability function is $phi/$S, for gamma it is different, | ||
| 204 | # but we shall ignore for now. | ||
| 205 | # | ||
| 206 | if ( $k == 1 and ( not( abs( $phi / $S ) < $stability_constant ) ) ) { | ||
| 207 | die | ||
| 208 | "$0: PELSSER DELTA 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 DELTA stability conditions ($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."; | ||
| 209 | } | ||
| 210 | } | ||
| 211 | |||
| 212 | # Need to take care when $mu goes to zero | ||
| 213 | if ( | ||
| 214 | abs($mu_new) < $Math::Business::BlackScholes::Binaries::SMALL_VALUE_MU ) | ||
| 215 | { | ||
| 216 | my $sign = ( $mu_new >= 0 ) ? 1 : -1; | ||
| 217 | $mu_new = | ||
| 218 | $sign * $Math::Business::BlackScholes::Binaries::SMALL_VALUE_MU; | ||
| 219 | $mu_dash = sqrt( | ||
| 220 | ( $mu_new * $mu_new ) + ( 2 * $vol * $vol * $r_q * ( 1 - $w ) ) ); | ||
| 221 | } | ||
| 222 | |||
| 223 | $hyp_part = | ||
| 224 | ( $mu_dash / ( $vol * $vol ) ) * | ||
| 225 | ( Math::Trig::cosh( $mu_dash * $x / ( $vol * $vol ) ) / | ||
| 226 | Math::Trig::sinh( $mu_dash * $h / ( $vol * $vol ) ) ); | ||
| 227 | |||
| 228 | my $dc_dx = ( $hyp_part + $series_part ) * exp( -$r_q * $t * $w ); | ||
| 229 | |||
| 230 | return $dc_dx; | ||
| 231 | } | ||
| 232 | |||
| 233 | sub ot_up_ko_down_pelsser_1997 { | ||
| 234 | my ( $S, $U, $D, $t, $r_q, $mu, $vol, $w ) = @_; | ||
| 235 | |||
| 236 | my $mu_new = $mu - ( 0.5 * $vol * $vol ); | ||
| 237 | my $h = log( $U / $D ); | ||
| 238 | my $x = log( $S / $D ); | ||
| 239 | |||
| 240 | my $dVu_dx = -( | ||
| 241 | ( $mu_new / ( $vol * $vol ) ) * | ||
| 242 | Math::Business::BlackScholes::Binaries::common_function_pelsser_1997( | ||
| 243 | $S, $U, $D, $t, $r_q, $mu, $vol, $w, 1 | ||
| 244 | ) | ||
| 245 | ); | ||
| 246 | |||
| 247 | $dVu_dx += | ||
| 248 | x_common_function_pelsser_1997( $S, $U, $D, $t, $r_q, $mu, $vol, $w, 1 ); | ||
| 249 | $dVu_dx *= exp( $mu_new * ( $h - $x ) / ( $vol * $vol ) ); | ||
| 250 | |||
| 251 | # dV/dS = dV/dx * dx/dS = dV/dx * 1/S | ||
| 252 | return $dVu_dx / $S; | ||
| 253 | } | ||
| 254 | |||
| 255 | sub ot_down_ko_up_pelsser_1997 { | ||
| 256 | my ( $S, $U, $D, $t, $r_q, $mu, $vol, $w ) = @_; | ||
| 257 | |||
| 258 | my $mu_new = $mu - ( 0.5 * $vol * $vol ); | ||
| 259 | my $h = log( $U / $D ); | ||
| 260 | my $x = log( $S / $D ); | ||
| 261 | |||
| 262 | my $dVl_dx = -( | ||
| 263 | ( $mu_new / ( $vol * $vol ) ) * | ||
| 264 | Math::Business::BlackScholes::Binaries::common_function_pelsser_1997( | ||
| 265 | $S, $U, $D, $t, $r_q, $mu, $vol, $w, 0 | ||
| 266 | ) | ||
| 267 | ); | ||
| 268 | |||
| 269 | $dVl_dx -= | ||
| 270 | x_common_function_pelsser_1997( $S, $U, $D, $t, $r_q, $mu, $vol, $w, 0 ); | ||
| 271 | $dVl_dx *= exp( -$mu_new * $x / ( $vol * $vol ) ); | ||
| 272 | |||
| 273 | # dV/dS = dV/dx * dx/dS = dV/dx * 1/S | ||
| 274 | return $dVl_dx / $S; | ||
| 275 | } | ||
| 276 | |||
| 277 | sub range { | ||
| 278 | my ( $S, $U, $D, $t, $r_q, $mu, $vol, $w ) = @_; | ||
| 279 | |||
| 280 | # Range always pay out at end | ||
| 281 | $w = 1; | ||
| 282 | |||
| 283 | return -1 * upordown( $S, $U, $D, $t, $r_q, $mu, $vol, $w ); | ||
| 284 | } | ||
| 285 | |||
| 286 | 1; | ||
| 287 |