ความคิดเห็นที่ 6
Black-Scholes in Perl By Jerome V. Braun
Perl is the "Swiss Army chainsaw" of languages that naturally also can be used for Black-Scholes:
=head2 BlackScholes
Routine to implement the Black and Scholes (1973) option pricing formula.
# usage $price = GBlackScholes($call_put_flag, $S, $X, $T, $r, $b, $v);
Here C<$call_put_flag> is either 'c' or 'p' for a call or put respectively,
=cut
sub BlackScholes { my ($call_put_flag, $S, $X, $T, $r, $v) = @_;
# calculate some auxiliary values my $d1 = ( log($S/$X) + ($r+$v**2/2)*$T ) / ( $v * $T**0.5 ); my $d2 = $d1 - $v * $T**0.5;
if ($call_put_flag eq 'c') { return $S * &CND($d1) - $X * exp( -$r * $T ) * &CND($d2); } else { # ($call_put_flag eq 'p') return $X * exp( -$r * $T ) * &CND(-$d2) - $S * &CND(-$d1); }
}
=head2 CND
Approximate the cumulative normal distribution. That is, the value of the integral of the standard normal density from minus infinity to C<$x>.
# usage $p = &CND($x);
=cut
sub CND { my $x = shift;
# the percentile under consideration
my $Pi = 3.141592653589793238;
# Taylor series coefficients my ($a1, $a2, $a3, $a4, $a5) = (0.319381530, -0.356563782, 1.781477937, -1.821255978, 1.330274429);
# use symmetry to perform the calculation to the right of 0 my $L = abs($x);
my $k = 1/( 1 + 0.2316419*$L);
my $CND = 1 - 1/(2*$Pi)**0.5 * exp(-$L**2/2) * ($a1*$k + $a2*$k**2 + $a3*$k**3 + $a4*$k**4 + $a5*$k**5);
# then return the appropriate value return ($x >= 0) ? $CND : 1-$CND;
}
จากคุณ :
:::GANTZ:::
- [
13 ก.ค. 50 13:36:46
]
|
|
|