« 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; | ||
| 3 | use warnings; | ||
| 4 | |||
| 5 | our $VERSION = '0.05'; ## VERSION | ||
| 6 | |||
| 7 | =head1 NAME | ||
| 8 | |||
| 9 | Math::Business::BlackScholes::Binaries::Greeks::Vega | ||
| 10 | |||
| 11 | =head1 DESCRIPTION | ||
| 12 | |||
| 13 | Gets the Vega for different options, Vanilla and Foreign for all our bet types | ||
| 14 | |||
| 15 | =cut | ||
| 16 | |||
| 17 | =head1 SUBROUTINES | ||
| 18 | |||
| 19 | See L<Math::Business::BlackScholes::Binaries::Greeks> | ||
| 20 | |||
| 21 | =cut | ||
| 22 | |||
| 23 | use List::Util qw( max ); | ||
| 24 | use Math::CDF qw( pnorm ); | ||
| 25 | use Math::Trig; | ||
| 26 | use Math::Business::BlackScholes::Binaries; | ||
| 27 | use Math::Business::BlackScholes::Binaries::Greeks::Math qw( dgauss ); | ||
| 28 | |||
| 29 | sub vanilla_call { | ||
| 30 | my ($S, $K, $t, $r_q, $mu, $vol) = @_; | ||
| 31 | |||
| 32 | my $d1 = (log($S / $K) + ($mu + $vol * $vol / 2.0) * $t) / ($vol * sqrt($t)); | ||
| 33 | my $vega = $S * sqrt($t) * exp(($mu - $r_q) * $t) * dgauss($d1); | ||
| 34 | return $vega; | ||
| 35 | } | ||
| 36 | |||
| 37 | sub vanilla_put { | ||
| 38 | my ($S, $K, $t, $r_q, $mu, $vol) = @_; | ||
| 39 | |||
| 40 | # Same as vega of vanilla call | ||
| 41 | return vanilla_call($S, $K, $t, $r_q, $mu, $vol); | ||
| 42 | } | ||
| 43 | |||
| 44 | sub call { | ||
| 45 | my ($S, $U, $t, $r_q, $mu, $vol) = @_; | ||
| 46 | |||
| 47 | my $d1 = (log($S / $U) + ($mu + $vol * $vol / 2.0) * $t) / ($vol * sqrt($t)); | ||
| 48 | my $d2 = $d1 - $vol * sqrt($t); | ||
| 49 | my $vega = -exp(-$r_q * $t) * dgauss($d2) * $d1 / $vol; | ||
| 50 | return $vega; | ||
| 51 | } | ||
| 52 | |||
| 53 | sub put { | ||
| 54 | my ($S, $D, $t, $r_q, $mu, $vol) = @_; | ||
| 55 | |||
| 56 | my $d1 = (log($S / $D) + ($mu + $vol * $vol / 2.0) * $t) / ($vol * sqrt($t)); | ||
| 57 | my $d2 = $d1 - $vol * sqrt($t); | ||
| 58 | my $vega = exp(-$r_q * $t) * dgauss($d2) * $d1 / $vol; | ||
| 59 | return $vega; | ||
| 60 | } | ||
| 61 | |||
| 62 | sub expirymiss { | ||
| 63 | my ($S, $U, $D, $t, $r_q, $mu, $vol) = @_; | ||
| 64 | |||
| 65 | return call($S, $U, $t, $r_q, $mu, $vol) + put($S, $D, $t, $r_q, $mu, $vol); | ||
| 66 | } | ||
| 67 | |||
| 68 | sub expiryrange { | ||
| 69 | my ($S, $U, $D, $t, $r_q, $mu, $vol) = @_; | ||
| 70 | |||
| 71 | return -1 * expirymiss($S, $U, $D, $t, $r_q, $mu, $vol); | ||
| 72 | } | ||
| 73 | |||
| 74 | sub onetouch { | ||
| 75 | my ($S, $U, $t, $r_q, $mu, $vol, $w) = @_; | ||
| 76 | |||
| 77 | if (not defined $w) { | ||
| 78 | $w = 0; | ||
| 79 | } | ||
| 80 | |||
| 81 | my $sqrt_t = sqrt($t); | ||
| 82 | |||
| 83 | my $theta = ($mu / $vol) + (0.5 * $vol); | ||
| 84 | |||
| 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 | |||
| 92 | my $e_ = (-log($S / $U) - ($vol * $v_ * $t)) / ($vol * $sqrt_t); | ||
| 93 | |||
| 94 | my $eta = ($S > $U) ? 1 : -1; | ||
| 95 | |||
| 96 | my $pa_e = (log($U / $S) / ($vol * $vol * $sqrt_t)) + (($theta_ * $theta) / ($vol * $v_) * $sqrt_t); | ||
| 97 | my $pa_e_ = (-log($U / $S) / ($vol * $vol * $sqrt_t)) + (($theta_ * $theta) / ($vol * $v_) * $sqrt_t); | ||
| 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 $part1 = | ||
| 102 | pnorm(-$eta * $e) * $A * log($U / $S) - $eta * dgauss($e) * $pa_e; | ||
| 103 | my $part2 = | ||
| 104 | pnorm($eta * $e_) * $A_ * log($U / $S) + $eta * dgauss($e_) * $pa_e_; | ||
| 105 | my $vega = (($U / $S)**(($theta_ + $v_) / $vol)) * $part1 + (($U / $S)**(($theta_ - $v_) / $vol)) * $part2; | ||
| 106 | |||
| 107 | return $vega * exp(-$w * $r_q * $t); | ||
| 108 | } | ||
| 109 | |||
| 110 | sub notouch { | ||
| 111 | my ($S, $U, $t, $r_q, $mu, $vol, $w) = @_; | ||
| 112 | |||
| 113 | # No touch bet always pay out at end | ||
| 114 | $w = 1; | ||
| 115 | |||
| 116 | return -1 * onetouch($S, $U, $t, $r_q, $mu, $vol, $w); | ||
| 117 | } | ||
| 118 | |||
| 119 | sub upordown { | ||
| 120 | my ($S, $U, $D, $t, $r_q, $mu, $vol, $w) = @_; | ||
| 121 | |||
| 122 | # $w = 0, paid at hit | ||
| 123 | # $w = 1, paid at end | ||
| 124 | if (not defined $w) { $w = 0; } | ||
| 125 | |||
| 126 | 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); | ||
| 127 | } | ||
| 128 | |||
| 129 | sub w_common_function_pelsser_1997 { | ||
| 130 | my ($S, $U, $D, $t, $r_q, $mu, $vol, $w, $eta) = @_; | ||
| 131 | |||
| 132 | my $pi = Math::Trig::pi; | ||
| 133 | |||
| 134 | my $h = log($U / $D); | ||
| 135 | my $x = log($S / $D); | ||
| 136 | |||
| 137 | # $eta = 1, onetouch up knockout down | ||
| 138 | # $eta = 0, onetouch down knockout up | ||
| 139 | # This variable used to check stability | ||
| 140 | if (not defined $eta) { | ||
| 141 | die | ||
| 142 | "$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."; | ||
| 143 | } | ||
| 144 | if ($eta == 0) { $x = $h - $x; } | ||
| 145 | |||
| 146 | my $r_dash = $r_q * (1 - $w); | ||
| 147 | my $mu_new = $mu - (0.5 * $vol * $vol); | ||
| 148 | my $mu_dash = sqrt(max($Math::Business::BlackScholes::Binaries::SMALL_VALUE_MU, ($mu_new * $mu_new) + (2 * $vol * $vol * $r_dash))); | ||
| 149 | |||
| 150 | my $omega = ($vol * $vol); | ||
| 151 | |||
| 152 | my $series_part = 0; | ||
| 153 | my $hyp_part = 0; | ||
| 154 | |||
| 155 | my $stability_constant = | ||
| 156 | Math::Business::BlackScholes::Binaries::get_stability_constant_pelsser_1997($S, $U, $D, $t, $r_q, $mu, $vol, $w, $eta, 1); | ||
| 157 | |||
| 158 | my $iterations_required = Math::Business::BlackScholes::Binaries::get_min_iterations_pelsser_1997($S, $U, $D, $t, $r_q, $mu, $vol, $w); | ||
| 159 | |||
| 160 | for (my $k = 1; $k < $iterations_required; $k++) { | ||
| 161 | my $lambda_k_dash = (0.5 * (($mu_dash * $mu_dash) / $omega + ($k * $k * $pi * $pi * $vol * $vol) / ($h * $h))); | ||
| 162 | |||
| 163 | # d{lambda_k}/dw | ||
| 164 | my $dlambdak_domega = 0.5 * (-($mu_new / $omega) - (($mu_new * $mu_new) / ($omega * $omega)) + (($k * $k * $pi * $pi) / ($h * $h))); | ||
| 165 | |||
| 166 | my $beta_k = exp(-$lambda_k_dash * $t) / $lambda_k_dash; | ||
| 167 | |||
| 168 | # d{beta_k}/d{lambda_k} | ||
| 169 | my $dbetak_dlambdak = -exp(-$lambda_k_dash * $t) * (($t * $lambda_k_dash + 1) / ($lambda_k_dash**2)); | ||
| 170 | |||
| 171 | # d{beta_k}/dw | ||
| 172 | my $dbetak_domega = $dlambdak_domega * $dbetak_dlambdak; | ||
| 173 | |||
| 174 | my $phi = | ||
| 175 | (1.0 / ($h * $h)) * ($omega * $dbetak_domega + $beta_k) * $k; | ||
| 176 | |||
| 177 | $series_part += $phi * $pi * sin($k * $pi * ($h - $x) / $h); | ||
| 178 | |||
| 179 | # | ||
| 180 | # For vega, the stability function is 2* $vol * $phi, for volga/vanna it is different, | ||
| 181 | # but we shall ignore for now. | ||
| 182 | # | ||
| 183 | if ($k == 1 | ||
| 184 | and (not(abs(2 * $vol * $phi) < $stability_constant))) | ||
| 185 | { | ||
| 186 | die | ||
| 187 | "$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."; | ||
| 188 | } | ||
| 189 | } | ||
| 190 | |||
| 191 | my $alpha = $mu_dash / ($vol * $vol); | ||
| 192 | my $dalpha_domega = -(($mu_new * $omega) + (2 * $mu_new * $mu_new) + (2 * $r_dash * $omega)) / (2 * $alpha * $omega * $omega * $omega); | ||
| 193 | |||
| 194 | # We have to handle the special case where the denominator approaches 0, see our documentation in | ||
| 195 | # quant/Documents/Breakout_bet.tex under the SVN "quant" module. | ||
| 196 | if ((Math::Trig::sinh($alpha * $h)**2) == 0) { | ||
| 197 | $hyp_part = 0; | ||
| 198 | } else { | ||
| 199 | $hyp_part = | ||
| 200 | ($dalpha_domega / (2 * (Math::Trig::sinh($alpha * $h)**2))) * | ||
| 201 | (($h + $x) * Math::Trig::sinh($alpha * ($h - $x)) - ($h - $x) * Math::Trig::sinh($alpha * ($h + $x))); | ||
| 202 | } | ||
| 203 | |||
| 204 | my $dc_domega = ($hyp_part - $series_part) * exp(-$r_q * $w * $t); | ||
| 205 | |||
| 206 | return $dc_domega; | ||
| 207 | } | ||
| 208 | |||
| 209 | sub ot_up_ko_down_pelsser_1997 { | ||
| 210 | my ($S, $U, $D, $t, $r_q, $mu, $vol, $w) = @_; | ||
| 211 | |||
| 212 | my $mu_new = $mu - (0.5 * $vol * $vol); | ||
| 213 | my $h = log($U / $D); | ||
| 214 | my $x = log($S / $D); | ||
| 215 | my $omega = ($vol * $vol); | ||
| 216 | |||
| 217 | my $c = Math::Business::BlackScholes::Binaries::common_function_pelsser_1997($S, $U, $D, $t, $r_q, $mu, $vol, $w, 1); | ||
| 218 | my $dc_domega = w_common_function_pelsser_1997($S, $U, $D, $t, $r_q, $mu, $vol, $w, 1); | ||
| 219 | |||
| 220 | my $dVu_domega = | ||
| 221 | -((0.5 * $omega + $mu_new) * ($h - $x) / ($omega * $omega)) * $c; | ||
| 222 | $dVu_domega += $dc_domega; | ||
| 223 | $dVu_domega *= exp($mu_new * ($h - $x) / $omega); | ||
| 224 | |||
| 225 | return $dVu_domega * (2 * $vol); | ||
| 226 | } | ||
| 227 | |||
| 228 | sub ot_down_ko_up_pelsser_1997 { | ||
| 229 | my ($S, $U, $D, $t, $r_q, $mu, $vol, $w) = @_; | ||
| 230 | |||
| 231 | my $mu_new = $mu - (0.5 * $vol * $vol); | ||
| 232 | my $x = log($S / $D); | ||
| 233 | my $omega = ($vol * $vol); | ||
| 234 | |||
| 235 | my $c = Math::Business::BlackScholes::Binaries::common_function_pelsser_1997($S, $U, $D, $t, $r_q, $mu, $vol, $w, 0); | ||
| 236 | my $dc_domega = w_common_function_pelsser_1997($S, $U, $D, $t, $r_q, $mu, $vol, $w, 0); | ||
| 237 | |||
| 238 | my $dVl_domega = | ||
| 239 | ((0.5 * $omega + $mu_new) * $x / ($omega * $omega)) * $c; | ||
| 240 | $dVl_domega += $dc_domega; | ||
| 241 | $dVl_domega *= exp(-$mu_new * $x / $omega); | ||
| 242 | |||
| 243 | return $dVl_domega * (2 * $vol); | ||
| 244 | } | ||
| 245 | |||
| 246 | sub range { | ||
| 247 | my ($S, $U, $D, $t, $r_q, $mu, $vol, $w) = @_; | ||
| 248 | |||
| 249 | # Range always pay out at end | ||
| 250 | $w = 1; | ||
| 251 | |||
| 252 | return -1 * upordown($S, $U, $D, $t, $r_q, $mu, $vol, $w); | ||
| 253 | } | ||
| 254 | |||
| 255 | 1; | ||
| 256 |