SinaQuant
SinaQuant

Reputation: 1

QuantLib Error: Root Not Bracketed - Adjusting Lower and Upper Bounds for Root Finding

I am using the following Python code to compute implied volatility for vanilla options:

import QuantLib as ql

exercise = ql.EuropeanExercise(expiry_date)
ql.Settings.instance().evaluationDate = calculation_date
payoff = ql.PlainVanillaPayoff(ql.Option.Call, strike)
option = ql.VanillaOption(payoff, exercise)
S = ql.QuoteHandle(ql.SimpleQuote(spot))
r = ql.YieldTermStructureHandle(ql.FlatForward(0, calendar, r0, day_count))
q = ql.YieldTermStructureHandle(ql.FlatForward(0, calendar, q0, day_count))
sigma = ql.BlackVolTermStructureHandle(ql.BlackConstantVol(0, calendar, 10, day_count))
process = ql.BlackScholesMertonProcess(S, q, r, sigma)
iv = option.impliedVolatility(option_price, process)

However, I encounter the following error for some instances:

RuntimeError: root not bracketed: f[1e-07,4] -> [1.017420e-05,1.017420e-05]

This error indicates that the root-finding algorithm cannot find a root within the given bounds, which are 1e-7 (lower bound) and 4 (upper bound). My question is how to adjust these bounds?

Upvotes: 0

Views: 145

Answers (1)

Luigi Ballabio
Luigi Ballabio

Reputation: 4368

The interface of the method is

    Volatility impliedVolatility(
         Real price,
         const ext::shared_ptr<GeneralizedBlackScholesProcess>& process,
         Real accuracy = 1.0e-4,
         Size maxEvaluations = 100,
         Volatility minVol = 1.0e-7,
         Volatility maxVol = 4.0) const;

(see https://github.com/lballabio/QuantLib/blob/v1.35/ql/instruments/vanillaoption.hpp) so you can pass a different minVol and maxVol.

However, this might not be the real problem. The error you reported shows that the option has the same value for the current lower and upper bound, which seems to me to imply that for whatever reason the value of the volatility has no effect. You might try if that's the case by passing different values to sigma and checking if the option value changes. If it doesn't, you'll have to diagnose why (maybe the option is at expiration, or the strike is unrealistic? I can't say without knowing the values you're passing); fixing that issue will probably fix the implied vol calculation as well.

Upvotes: 0

Related Questions