« blib/lib/Math/Business/BlackScholes/Binaries/Greeks/Gamma.pm
↑ Summary
blib/lib/Math/Business/BlackScholes/Binaries/Greeks/Theta.pm »
| 1 | package Math::Business::BlackScholes::Binaries::Greeks::Volga; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | |||
| 5 | our $VERSION = '0.05'; ## VERSION | ||
| 6 | |||
| 7 | use List::Util qw( max ); | ||
| 8 | use Math::Business::BlackScholes::Binaries; | ||
| 9 | use Math::Business::BlackScholes::Binaries::Greeks::Vega; | ||
| 10 | use Math::Business::BlackScholes::Binaries::Greeks::Math qw( dgauss ); | ||
| 11 | use Math::CDF qw( pnorm ); | ||
| 12 | use Math::Trig; | ||
| 13 | |||
| 14 | =head1 NAME | ||
| 15 | |||
| 16 | Math::Business::BlackScholes::Binaries::Greeks::Volga | ||
| 17 | |||
| 18 | =head1 DESCRIPTION | ||
| 19 | |||
| 20 | Gets the Volga for different options, Vanilla and Foreign for all our bet types | ||
| 21 | |||
| 22 | =cut | ||
| 23 | |||
| 24 | =head1 SUBROUTINES | ||
| 25 | |||
| 26 | See L<Math::Business::BlackScholes::Binaries::Greeks> | ||
| 27 | |||
| 28 | =cut | ||
| 29 | |||
| 30 | sub vanilla_call { | ||
| 31 | my ($S, $K, $t, $r_q, $mu, $vol) = @_; | ||
| 32 | |||
| 33 | my $d1 = (log($S / $K) + ($mu + $vol * $vol / 2.0) * $t) / ($vol * sqrt($t)); | ||
| 34 | my $d2 = $d1 - ($vol * sqrt($t)); | ||
| 35 | |||
| 36 | my $vega = Math::Business::BlackScholes::Binaries::Greeks::Vega::vanilla_call($S, $K, $t, $r_q, $mu, $vol); | ||
| 37 | |||
| 38 | my $volga = $vega * $d1 * $d2 / $vol; | ||
| 39 | return $volga; | ||
| 40 | } | ||
| 41 | |||
| 42 | sub vanilla_put { | ||
| 43 | my ($S, $K, $t, $r_q, $mu, $vol) = @_; | ||
| 44 | |||
| 45 | # Same as volga of vanilla call (because vega_vanilla_call = vega_vanilla_put) | ||
| 46 | return vanilla_call($S, $K, $t, $r_q, $mu, $vol); | ||
| 47 | } | ||
| 48 | |||
| 49 | sub call { | ||
| 50 | my ($S, $U, $t, $r_q, $mu, $vol) = @_; | ||
| 51 | |||
| 52 | my $d1 = (log($S / $U) + ($mu + $vol * $vol / 2.0) * $t) / ($vol * sqrt($t)); | ||
| 53 | my $d2 = $d1 - ($vol * sqrt($t)); | ||
| 54 | |||
| 55 | my $volga = -dgauss($d2) * exp(-$r_q * $t) / ($vol * $vol) * (-$d1 - $d2 + ($d1 * $d1 * $d2)); | ||
| 56 | return $volga; | ||
| 57 | } | ||
| 58 | |||
| 59 | sub put { | ||
| 60 | my ($S, $D, $t, $r_q, $mu, $vol) = @_; | ||
| 61 | |||
| 62 | return -1 * call($S, $D, $t, $r_q, $mu, $vol); | ||
| 63 | } | ||
| 64 | |||
| 65 | sub expirymiss { | ||
| 66 | my ($S, $U, $D, $t, $r_q, $mu, $vol) = @_; | ||
| 67 | |||
| 68 | return call($S, $U, $t, $r_q, $mu, $vol) + put($S, $D, $t, $r_q, $mu, $vol); | ||
| 69 | } | ||
| 70 | |||
| 71 | sub expiryrange { | ||
| 72 | my ($S, $U, $D, $t, $r_q, $mu, $vol) = @_; | ||
| 73 | |||
| 74 | return -1 * expirymiss($S, $U, $D, $t, $r_q, $mu, $vol); | ||
| 75 | } | ||
| 76 | |||
| 77 | sub onetouch { | ||
| 78 | my ($S, $U, $t, $r_q, $mu, $vol, $w) = @_; | ||
| 79 | if (not defined $w) { | ||
| 80 | $w = 0; | ||
| 81 | } | ||
| 82 | my $sqrt_t = sqrt($t); | ||
| 83 | |||
| 84 | my $theta = (($mu) / $vol) + (0.5 * $vol); | ||
| 85 | my $theta_ = (($mu) / $vol) - (0.5 * $vol); | ||
| 86 | |||
| 87 | # Floor v_ squared at just above zero in case negative interest rates push it negative. | ||
| 88 | my $v_ = sqrt(max($Math::Business::BlackScholes::Binaries::SMALL_VALUE_MU, ($theta_ * $theta_) + (2 * (1 - $w) * $r_q))); | ||
| 89 | |||
| 90 | my $e = (log($S / $U) - ($vol * $v_ * $t)) / ($vol * $sqrt_t); | ||
| 91 | my $e_ = (-log($S / $U) - ($vol * $v_ * $t)) / ($vol * $sqrt_t); | ||
| 92 | |||
| 93 | my $eta = ($S > $U) ? 1 : -1; | ||
| 94 | |||
| 95 | my $pa_e = (log($U / $S) / ($vol * $vol * $sqrt_t)) + ($theta_ * $theta) / ($vol * $v_) * $sqrt_t; | ||
| 96 | my $pa_e_ = (-log($U / $S) / ($vol * $vol * $sqrt_t)) + (($theta_ * $theta) / ($vol * $v_) * $sqrt_t); | ||
| 97 | |||
| 98 | my $A = -($theta + $theta_ + ($theta_ * $theta / $v_) + $v_) / ($vol * $vol); | ||
| 99 | my $A_ = -($theta + $theta_ - ($theta_ * $theta / $v_) - $v_) / ($vol * $vol); | ||
| 100 | |||
| 101 | my $g = 1 / ($vol * $vol * $v_) * (-$theta * $theta - $theta_ * $theta_ - $theta * $theta_ + $theta * $theta_ * $theta * $theta_ / ($v_ * $v_)); | ||
| 102 | |||
| 103 | my $pa_2_e = -2 * log($U / $S) / ($vol * $vol * $vol * $sqrt_t) + $g * $sqrt_t; | ||
| 104 | my $pa_2_e_ = 2 * log($U / $S) / ($vol * $vol * $vol * $sqrt_t) + $g * $sqrt_t; | ||
| 105 | |||
| 106 | my $pa_A = ($theta + $theta_) / ($vol * $vol * $vol) - (2 * $A + $g) / $vol; | ||
| 107 | my $pa_A_ = ($theta + $theta_) / ($vol * $vol * $vol) - (2 * $A_ - $g) / $vol; | ||
| 108 | |||
| 109 | my ($part1, $part2, $subpart_1_1, $subpart_1_2, $subpart_2_1, $subpart_2_2); | ||
| 110 | |||
| 111 | $subpart_1_1 = pnorm(-$eta * $e) * log($U / $S) * ($A * $A * log($U / $S) + $pa_A); | ||
| 112 | $subpart_1_2 = $eta * dgauss($e) * (2 * log($U / $S) * $A * $pa_e - $e * $pa_e * $pa_e + $pa_2_e); | ||
| 113 | |||
| 114 | $subpart_2_1 = pnorm($eta * $e_) * log($U / $S) * ($A_ * $A_ * log($U / $S) + $pa_A_); | ||
| 115 | $subpart_2_2 = $eta * dgauss($e_) * (2 * log($U / $S) * $A_ * $pa_e_ - $e_ * $pa_e_ * $pa_e_ + $pa_2_e_); | ||
| 116 | |||
| 117 | $part1 = (($U / $S)**(($theta_ + $v_) / $vol)) * ($subpart_1_1 - $subpart_1_2); | ||
| 118 | $part2 = (($U / $S)**(($theta_ - $v_) / $vol)) * ($subpart_2_1 + $subpart_2_2); | ||
| 119 | |||
| 120 | return exp(-$w * $r_q * $t) * ($part1 + $part2); | ||
| 121 | } | ||
| 122 | |||
| 123 | sub notouch { | ||
| 124 | my ($S, $U, $t, $r_q, $mu, $vol, $w) = @_; | ||
| 125 | |||
| 126 | # No touch bet always pay out at end | ||
| 127 | $w = 1; | ||
| 128 | |||
| 129 | # Since the value VALUE_NOTOUCH = D(T) - VALUE_ONETOUCH, where D(T) | ||
| 130 | # is the discount from time T, any derivative (other than with | ||
| 131 | # respect to time or discount rate) of the value of notouch | ||
| 132 | # is just the negative of the onetouch derivative. | ||
| 133 | return (-1 * onetouch($S, $U, $t, $r_q, $mu, $vol, $w)); | ||
| 134 | } | ||
| 135 | |||
| 136 | sub upordown { | ||
| 137 | my ($S, $U, $D, $t, $r_q, $mu, $vol, $w) = @_; | ||
| 138 | |||
| 139 | # $w = 0, paid at hit | ||
| 140 | # $w = 1, paid at end | ||
| 141 | if (not defined $w) { $w = 0; } | ||
| 142 | |||
| 143 | return ot_up_ko_down_pelsser_1997($S, $U, $D, $t, $r_q, $mu, $vol, $w) + 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 $mu_new = $mu - (0.5 * $vol * $vol); | ||
| 164 | my $mu_dash = sqrt(max($Math::Business::BlackScholes::Binaries::SMALL_VALUE_MU, ($mu_new * $mu_new) + (2 * $vol * $vol * $r_q * (1 - $w)))); | ||
| 165 | |||
| 166 | my $r_dash = $r_q * (1 - $w); | ||
| 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($S, $U, $D, $t, $r_q, $mu, $vol, $w, $eta, 1); | ||
| 174 | |||
| 175 | my $iterations_required = Math::Business::BlackScholes::Binaries::get_min_iterations_pelsser_1997($S, $U, $D, $t, $r_q, $mu, $vol, $w); | ||
| 176 | |||
| 177 | for (my $k = 1; $k < $iterations_required; $k++) { | ||
| 178 | my $lambda_k_dash = (0.5 * (($mu_dash * $mu_dash) / ($vol * $vol) + ($k * $k * $pi * $pi * $vol * $vol) / ($h * $h))); | ||
| 179 | |||
| 180 | # d{lambda_k}/dw | ||
| 181 | my $dlambdak_domega = 0.5 * (-($mu_new / $omega) - (($mu_new * $mu_new) / ($omega * $omega)) + (($k * $k * $pi * $pi) / ($h * $h))); | ||
| 182 | my $d2lambdak_domega2 = 0.5 * ($omega + 2 * $mu_new) / (2 * $omega * $omega); | ||
| 183 | $d2lambdak_domega2 *= (1 + (2 * $mu_new / $omega)); | ||
| 184 | |||
| 185 | # d{beta_k}/d{lambda_k} | ||
| 186 | my $dbetak_dlambdak = -exp(-$lambda_k_dash * $t) * (($t * $lambda_k_dash + 1) / ($lambda_k_dash**2)); | ||
| 187 | my $d2betak_dlambdak2 = -($t * $dbetak_dlambdak) + exp(-$lambda_k_dash * $t) * (($t / ($lambda_k_dash**2)) + (2 / ($lambda_k_dash**3))); | ||
| 188 | |||
| 189 | # d{beta_k}/dw | ||
| 190 | my $dbetak_domega = $dlambdak_domega * $dbetak_dlambdak; | ||
| 191 | my $d2betak_domega2 = ($dlambdak_domega * $dlambdak_domega * $d2betak_dlambdak2) + ($dbetak_dlambdak * $d2lambdak_domega2); | ||
| 192 | |||
| 193 | my $phi = (1.0 / ($h * $h)) * ($omega * $d2betak_domega2 + 2 * $dbetak_domega) * $k; | ||
| 194 | |||
| 195 | $series_part += $phi * $pi * sin($k * $pi * ($h - $x) / $h); | ||
| 196 | |||
| 197 | if ($k == 1 and (not(abs(4 * $vol * $vol * $phi) < $stability_constant))) { | ||
| 198 | die | ||
| 199 | "$0: PELSSER VOLGA 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 VOLGA stability conditions (4 * $vol * $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."; | ||
| 200 | } | ||
| 201 | } | ||
| 202 | |||
| 203 | my $alpha = $mu_dash / ($vol * $vol); | ||
| 204 | my $dalpha_domega = -(($mu_new * $omega) + (2 * $mu_new * $mu_new) + (2 * $r_dash * $omega)) / (2 * $alpha * $omega * $omega * $omega); | ||
| 205 | |||
| 206 | my $d2alpha_domega2 = $alpha * ($omega**3) * (2 * $mu_new + $omega - 4 * $r_dash); | ||
| 207 | $d2alpha_domega2 += | ||
| 208 | (($mu_new * $omega) + (2 * $mu_new * $mu_new) + (2 * $r_dash * $omega)) * | ||
| 209 | ((6 * $alpha * $omega * $omega) + (2 * $omega * $omega * $omega * $dalpha_domega)); | ||
| 210 | $d2alpha_domega2 = $d2alpha_domega2 / (4 * $alpha * $alpha * ($omega**6)); | ||
| 211 | |||
| 212 | # We have to handle the special case where the denominator approaches 0, see our documentation in | ||
| 213 | # quant/Documents/Breakout_bet.tex under the SVN "quant" module. | ||
| 214 | my $hyp_part1; | ||
| 215 | if ((Math::Trig::sinh($alpha * $h)**3) == 0) { | ||
| 216 | $hyp_part1 = 0; | ||
| 217 | } else { | ||
| 218 | $hyp_part1 = | ||
| 219 | Math::Trig::sinh($alpha * $h) * | ||
| 220 | ($h**2 - $x**2) * | ||
| 221 | (Math::Trig::cosh($alpha * ($h - $x)) - Math::Trig::cosh($alpha * ($h + $x))) - | ||
| 222 | (2 * $h * Math::Trig::cosh($alpha * $h)) * | ||
| 223 | ((($h + $x) * Math::Trig::sinh($alpha * ($h - $x))) - (($h - $x) * Math::Trig::sinh($alpha * ($h + $x)))); | ||
| 224 | $hyp_part1 *= ($dalpha_domega**2) / (2 * (Math::Trig::sinh($alpha * $h)**3)); | ||
| 225 | } | ||
| 226 | |||
| 227 | my $hyp_part2; | ||
| 228 | if ((Math::Trig::sinh($alpha * $h)**2) == 0) { | ||
| 229 | $hyp_part2 = 0; | ||
| 230 | } else { | ||
| 231 | $hyp_part2 = | ||
| 232 | ($d2alpha_domega2 / (2 * (Math::Trig::sinh($alpha * $h)**2))) * | ||
| 233 | (($h + $x) * Math::Trig::sinh($alpha * ($h - $x)) - ($h - $x) * Math::Trig::sinh($alpha * ($h + $x))); | ||
| 234 | } | ||
| 235 | |||
| 236 | $hyp_part = $hyp_part1 + $hyp_part2; | ||
| 237 | |||
| 238 | my $d2c_domega2 = ($hyp_part - $series_part) * exp(-$r_q * $w * $t); | ||
| 239 | |||
| 240 | return $d2c_domega2; | ||
| 241 | } | ||
| 242 | |||
| 243 | sub ot_up_ko_down_pelsser_1997 { | ||
| 244 | my ($S, $U, $D, $t, $r_q, $mu, $vol, $w) = @_; | ||
| 245 | |||
| 246 | my $mu_new = $mu - (0.5 * $vol * $vol); | ||
| 247 | my $h = log($U / $D); | ||
| 248 | my $x = log($S / $D); | ||
| 249 | my $omega = ($vol * $vol); | ||
| 250 | |||
| 251 | my $c = Math::Business::BlackScholes::Binaries::common_function_pelsser_1997($S, $U, $D, $t, $r_q, $mu, $vol, $w, 1); | ||
| 252 | my $dc_domega = Math::Business::BlackScholes::Binaries::Greeks::Vega::w_common_function_pelsser_1997($S, $U, $D, $t, $r_q, $mu, $vol, $w, 1); | ||
| 253 | my $d2c_domega2 = w_common_function_pelsser_1997($S, $U, $D, $t, $r_q, $mu, $vol, $w, 1); | ||
| 254 | |||
| 255 | my $Vu = Math::Business::BlackScholes::Binaries::ot_up_ko_down_pelsser_1997($S, $U, $D, $t, $r_q, $mu, $vol, $w); | ||
| 256 | my $dVu_domega = -((0.5 * $omega + $mu_new) * ($h - $x) / ($omega * $omega)) * $c; | ||
| 257 | $dVu_domega += $dc_domega; | ||
| 258 | $dVu_domega *= exp($mu_new * ($h - $x) / $omega); | ||
| 259 | |||
| 260 | my $d2Vu_domega2 = | ||
| 261 | -((((0.5 * $omega) + $mu_new) / ($omega * $omega)) * ($h - $x) * $dVu_domega) + | ||
| 262 | ((($omega + (2 * $mu_new)) / ($omega**3)) * ($h - $x) * $Vu) - | ||
| 263 | ((((0.5 * $omega) + $mu_new) / ($omega * $omega)) * ($h - $x) * exp($mu_new * ($h - $x) / $omega) * $dc_domega) + | ||
| 264 | (exp($mu_new * ($h - $x) / $omega) * $d2c_domega2); | ||
| 265 | |||
| 266 | return (4 * $vol * $vol * $d2Vu_domega2) + (2 * $dVu_domega); | ||
| 267 | } | ||
| 268 | |||
| 269 | sub ot_down_ko_up_pelsser_1997 { | ||
| 270 | my ($S, $U, $D, $t, $r_q, $mu, $vol, $w) = @_; | ||
| 271 | |||
| 272 | my $mu_new = $mu - (0.5 * $vol * $vol); | ||
| 273 | my $x = log($S / $D); | ||
| 274 | my $omega = ($vol * $vol); | ||
| 275 | |||
| 276 | my $c = Math::Business::BlackScholes::Binaries::common_function_pelsser_1997($S, $U, $D, $t, $r_q, $mu, $vol, $w, 0); | ||
| 277 | my $dc_domega = Math::Business::BlackScholes::Binaries::Greeks::Vega::w_common_function_pelsser_1997($S, $U, $D, $t, $r_q, $mu, $vol, $w, 0); | ||
| 278 | my $d2c_domega2 = w_common_function_pelsser_1997($S, $U, $D, $t, $r_q, $mu, $vol, $w, 0); | ||
| 279 | |||
| 280 | my $Vl = Math::Business::BlackScholes::Binaries::ot_down_ko_up_pelsser_1997($S, $U, $D, $t, $r_q, $mu, $vol, $w); | ||
| 281 | my $dVl_domega = ((0.5 * $omega + $mu_new) * $x / ($omega * $omega)) * $c; | ||
| 282 | $dVl_domega += $dc_domega; | ||
| 283 | $dVl_domega *= exp(-$mu_new * $x / $omega); | ||
| 284 | |||
| 285 | my $d2Vl_domega2 = | ||
| 286 | ((((0.5 * $omega) + $mu_new) / ($omega * $omega)) * $x * $dVl_domega) - | ||
| 287 | ((($omega + (2 * $mu_new)) / ($omega**3)) * $x * $Vl) + | ||
| 288 | ((((0.5 * $omega) + $mu_new) / ($omega * $omega)) * $x * exp(-$mu_new * $x / $omega) * $dc_domega) + | ||
| 289 | (exp(-$mu_new * $x / $omega) * $d2c_domega2); | ||
| 290 | |||
| 291 | return (4 * $vol * $vol * $d2Vl_domega2) + (2 * $dVl_domega); | ||
| 292 | } | ||
| 293 | |||
| 294 | sub range { | ||
| 295 | my ($S, $U, $D, $t, $r_q, $mu, $vol, $w) = @_; | ||
| 296 | |||
| 297 | # Range always pay out at end | ||
| 298 | $w = 1; | ||
| 299 | |||
| 300 | return -1 * upordown($S, $U, $D, $t, $r_q, $mu, $vol, $w); | ||
| 301 | } | ||
| 302 | |||
| 303 | 1; | ||
| 304 |