尝试使用 PHP 计算期权的隐含波动率。该BlackScholes
函数计算期权的价格,是期权$OptionValue
的价格,$guess
是一个估计值。
我的尝试基于类似的应用程序,但我的版本超时。我认为在纸上遍历循环对我来说很有意义。我做错了什么?
function ImpliedVolatility($type, $S, $X, $T, $r, $D, $OptionValue, $guess) {
$x1 = 0.01; //lower approx
$x2 = $guess * 2; //upper approx
$epsilon = 0.01;
$ValueGuess = BlackScholes($type, $S, $X, $T, $r, $D, $x2); //Initialize
while ( abs($OptionValue - $ValueGuess ) > $epsilon ) {
$xMid = ($x1 + $x2)/2; //new approx
$F1 = BlackScholes($type, $S, $X, $T, $r, $D, $x1);
$FMid = BlackScholes($type, $S, $X, $T, $r, $D, $xMid);
if ($FMid > $F1 ) {
$x1 = $xMid;
} else {
$x2 = $xMid;
}
$ValueGuess = BlackScholes($type, $S, $X, $T, $r, $D, $xMid);
}
return $xMid;
}