尝试使用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;
}