<- all tokdocs

Implied Volatility in Python: A 16-Line Bisection Script, Checked

By , founder of Mavric and Apso

View on TikTok ->

Implied volatility is the volatility number that makes the Black-Scholes formula reproduce an option's market price. A TikTok from @quantfinancetogo shows a 16-line Python script that finds it by bisection and reports 24.1% for a $12 call. Every number in the walkthrough reproduces when the script is run, but the script has no failure branch, so a price outside its search range makes it print a wrong answer without any warning.

What is implied volatility?

Black-Scholes prices a call from five inputs. Four of them can be looked up: the stock price S, the strike K, the interest rate r and the time to expiry t. The fifth, volatility σ, cannot. The video's point is that traders set the option price through supply and demand, and the formula is then run backwards to recover the volatility that matches that price.

There is no closed-form way to do that inversion, because σ sits inside d₁, inside d₂ and inside the normal CDF applied to both. What makes the inversion work is that the call price always rises with volatility, so each price corresponds to exactly one σ.

The script and what it prints

The example uses S = 100, K = 100, r = 5% and t = 1 year, with a market price of $12:

from math import erf, exp, log
N = lambda x: .5 + erf(x / 2 ** .5) / 2   # normal
S, K, r, t = 100, 100, .05, 1  # four known inputs
def call(s):  # Black and Scholes, volatility s
    v = s * t ** .5
    d1 = (log(S / K) + (r + s * s / 2) * t) / v
    return S * N(d1) - K * exp(-r * t) * N(d1 - v)
market = 12  # the price paid on the market
lo, hi = 0, 1  # volatility between 0 and 100 %
for step in range(1, 60):  # bisection search
    s = (lo + hi) / 2; c = call(s)  # the middle
    print(step, round(s, 5), round(c, 4))
    if abs(c - market) < .005: break  # it fits
    if c > market: hi = s  # too expensive
    else: lo = s  # too cheap
print('implied volatility', round(s, 4))

Run unmodified, it prints ten steps, starting at 1 0.5 21.7926 and ending at 10 0.24121 12.0036, then implied volatility 0.2412. Every row matches the table shown in the video. The Black-Scholes code is correct: the normal CDF is 0.5 + erf(x/√2)/2, and d1 - v is d₂ = d₁ − σ√t. Solving to full precision gives 0.24111689, so the spoken figure of 24.1% is right.

Where the script fails

The search range is hard-coded as 0 to 100% volatility, and the loop has no check for whether it found a match. If the price never fits, the loop runs out and the last midpoint gets printed as the answer.

For this option, the no-arbitrage lower bound is S − Ke⁻ʳᵗ = 100 − 95.122942 = 4.877058, and the price at 100% volatility is 39.8402. Three test prices show the problem:

  • At a market price of 4.0, below the lower bound, no volatility can produce it. The script prints implied volatility 0.0.
  • At 2.0 it does the same.
  • At 44.0, which needs volatility above 100%, it prints implied volatility 1.0.

In all three cases the program exits with status 0 and no warning. Real quotes can break these bounds, and single-stock options often trade above 100% implied volatility. The fix is to widen the upper bound and raise an error when the loop ends without a match. SciPy's brentq enforces this by requiring that "f(a) and f(b) must have opposite signs".

Does 10 steps always fit to half a cent?

The video's description says "After 10 steps the price fits to half a cent". That is the script's break condition, and it holds for this run: the step-10 price misses by 0.36 cents. It is not a general rule. Ten bisection steps only guarantee the volatility is within 0.000977 of the root. With this option's vega of 37.81 dollars per unit of volatility, that allows a price miss of about 3.7 cents. Changing the market price to $30, the same script takes 12 steps and reports 0.7219.

Volatility smile vs skew

One chart plots implied volatility falling steadily from about 36% at strike 60 to about 21% at strike 140, and a label calls it a "smile". Wikipedia's implied-volatility article reserves "smile" for a curve that turns up at both ends and calls the downward-sloping equity shape a "volatility skew" or "half frown". The drawn curve is a skew.

The reason given for it is correct: protection against a sharp fall is in demand, so it costs more and implies higher volatility. The effect has a date. Rubinstein (1994) found S&P 500 implied volatilities roughly flat across strikes before the October 1987 crash and skewed afterwards.

Does implied volatility predict how much a stock will move?

The clip closes by saying an option price "tells you how much the market expects the stock to swing until expiry", and draws 24.1% implied against 18.4% historical. Carr and Wu (2009) found the variance risk premium negative for all five stock indexes and most of the 35 stocks they studied. That means implied volatility tends to run above the volatility that follows.

What production code uses instead

Bisection cannot overshoot, which makes it a sound teaching choice, but production solvers are faster. SciPy's brentq combines bisection with inverse quadratic interpolation, with a default xtol of 2e-12. Peter Jäckel's "Let's Be Rational", wrapped as lets_be_rational and used by py_vollib, reaches full 64-bit precision "with as little as two iterations".

Bottom line

The math in the video is correct and the 24.1% answer reproduces. Before using the script on real quotes, add a check that the loop found a match and widen the volatility range. The chart labelled "smile" shows a skew.

Sources

Published October 24, 2026. Based on a TikTok by @quantfinancetogo. Full video notes (markdown)