This code uses R's vectorization features to execute a sort of parallel bisection for computing implied volatility.
I used this code to compute implied volatility of a bunch of options (~ 200 thoudands).
A loop with uniroot
would take several minutes which I reduced to few seconds.
The example below uses GBSOption
function from package fOptions.
bs.opt <- function(S, K, T, r, sig) GBSOption('c', S, K, T, r, r, sig)@price
impvol <- implied.vol(bs.opt)
impliedVol <- impvol(UnderlyingPrice, Strike, BusinessDays/252, RiskFreeRate, Premium)