Bisection Method & Weighted Bisection Method
A super chill walkthrough of the bisection method — arguably the simplest root-finding algorithm ever — plus a weighted twist, then putting it to work on implied volatility, with VBA and Python code.
Some original drawings have corrections. Read the notes on those drawings alongside the worked steps.
Alright, we’re playing the “find the solution” game from here on out. So — among all the computer-based root-finding methods out there, the absolute easiest one is the Bisection Method. And it is ridiculously, insanely simple!!!
Here’s the basic concept of the bisection method:

That’s it, right there. This spot is what we want to find, right now.

OK so first — let’s say we pick initial values $a$ and $b$ like this.
(Suddenly — wait, what???? Just keep reading and you’ll see what I mean)
But why do we set the initial values as $a$ and $b$ like that???
That’s what
is about: we pick them so that $f(a)$ and $f(b)$ have opposite signs, and assume $f$ is continuous on that interval. That brackets at least one root.

And now let’s say $c$ is the midpoint of $a$ and $b$. Now we need to make a call.
Is $f(a)f(c) \lt 0$? Or is $f(c)f(b) \lt 0$? Which one is it???
$f(a)f(c) \lt 0$, right???? Then — let’s say $c'$ is the midpoint between $a$ and $c$.

Now that $c'$ is pinned down, we gotta make the same call again: is $f(a)f(c') \lt 0$?????? Or is $f(c')f(c) \lt 0$??? Since $f(c')f(c) \lt 0$, the new $c''$ gets set to the midpoint of $c$ and $c'$…
You get where this is going, right? We just keep running this algorithm, over and over and over~~~~~ and eventually, when we’ve arrived at $c,c',c'',\ldots$, it won’t be exactly the solution, but it’ll be basically the same thing, yeah?
That’s root-finding via the bisection method!!!
Let’s try it. Find the solution of
![]()
using the bisection method, as practice.

Python Version

I also made a video of the first five bisection steps using Python
import matplotlib.pyplot as plt
import numpy as np
# The annotations are English, so Matplotlib's default font is sufficient.
def test_function(x):
return x**3 - 5*x**2 + 3*x - 2.7
for i in range(1, 6):
if i ==1:
limit = 0.001
A = -2
B = 10
x = np.linspace(-2, 7, 100)
y = test_function(x)
fig = plt.figure(figsize=(20, 10))
ax = fig.add_subplot(111)
fig = plt.plot(x, y)
plt.axvline(x=0, color='k')
plt.pause(1)
plt.axhline(y=0, color='k')
plt.pause(1)
plt.scatter(A, 0, c='r', marker='*')
plt.pause(1)
ax.annotate('initial value', xy=(A, 0), xytext=(A+0.1, 1))
plt.scatter(B, 0, c='r', marker='*')
plt.pause(1)
ax.annotate('initial value', xy=(B, 0), xytext=(B+0.1, 1))
C = (A + B) / 2
plt.scatter(C, 0, marker='*')
ax.annotate('this time', xy=(C, 0), xytext=(C+1+i, 100),
arrowprops=dict(facecolor='black', width=0.1, headlength=0.4, headwidth=1))
plt.pause(1)
fa = test_function(A)
fc = test_function(C)
if abs(fc) < limit or abs(B - A) < limit:
plt.scatter(C, 0, marker='*')
ax.annotate('last time', xy=(C, 0), xytext=(C + 1, 110),
arrowprops=dict(facecolor='black', width=0.1, headlength=0.4, headwidth=1))
plt.title('Ans, bro: ' + str(C) + ' yo')
break
elif fa * fc < 0:
B = C
else:
A = C

Pretty close to the solution I got with the help of an app!
(I mean — that app’s gotta have some kind of algorithm running under the hood too, right? lol)
Just leaving things at screenshots like this feels a little irresponsible though… Back when I was studying this on my own, I’d work through it like this.



One warning about this stopping test: a small function value alone does not guarantee a small error in x when the curve is flat. That is a residual-test issue, not a failure of bisection itself. The bracket width provides a direct position-error bound, so check that too.

Something like that, you know???????
Back to Implied Volatility
Wait!!!!!!!!!!! But hold on — why were we even learning the bisection method again? — Implied Volatility (Financial Engineering Programming #13)
Right — it was specifically to calculate implied volatility.
So this time, armed with the bisection method, let’s go ahead and calculate implied volatility. One more time: implied volatility is the $\sigma$ value that, when you plug it into the BS formula, makes the BS value equal to the market value!!!!!!!!!
To do that, we need a function procedure where we toss in all 5 inputs — $s, k, T, r, \sigma$ — boing! — and the option price goes plop! right out. So let’s start by making the function procedure that does the BS option pricing, and write the whole thing out from there.
(procedure, procedure…)



I’ll also put up some pictures with commentary added to the code.





It’s simple enough code that it’s easy to follow, but note one copying trap first. The old VBA’s final zero is a failure marker, not a measured zero volatility. Check that the starting price bracket contains the quote, and report nonconvergence explicitly instead of returning a plausible volatility value. The Python example below searches only $0.0001\le\sigma\le0.3$; the pictured VBA upper bound is 3. A quote outside the chosen bracket can still have an implied volatility elsewhere.

Python Version
import math
import scipy as sp
import scipy.stats
norm = sp.stats.norm(loc=0, scale=1)
def BS(S, k, r, sigma, t, opOption):
d1 = (math.log(S / k) + r * t) / (sigma * math.sqrt(t)) + (0.5 * sigma * math.sqrt(t))
d2 = d1 - (sigma * math.sqrt(t))
if opOption == True:
bi = 1
else:
bi = -1
BS = bi * (S*norm.cdf(bi*d1) - k*math.exp(-r*t)*norm.cdf(bi*d2))
return BS
def IV_using_Bisection(S, k, r, t, real_price, opOption):
price_tol = 1e-9
sigma_tol = 1e-9
hi_sigma = 0.3
lo_sigma = 0.0001
if S <= 0 or k <= 0 or t <= 0:
raise ValueError("S, k and t must be positive")
low_price = BS(S, k, r, lo_sigma, t, opOption)
high_price = BS(S, k, r, hi_sigma, t, opOption)
if not low_price <= real_price <= high_price:
raise ValueError("Option price is outside this volatility bracket")
for i in range(101):
sigma = 0.5 * (hi_sigma + lo_sigma)
bs_value = BS(S, k, r, sigma, t, opOption)
diff = bs_value - real_price
if abs(diff) < price_tol and hi_sigma - lo_sigma < sigma_tol:
return sigma
if diff < 0:
lo_sigma = sigma
else:
hi_sigma = sigma
raise RuntimeError("Bisection did not converge within the iteration limit")
S=50; k=50; r=0.1; t=0.5
real_price = 2.5
opOption = False
print(IV_using_Bisection(S=S, k=k, r=r, t=t, real_price=real_price, opOption=opOption))
Weighted Bisection Method
This bisection method might look a little dumb. It just blindly goes bam!!!!!! the midpoint!!!!!!!!! and plugs that in, so… it does get closer to the solution, it does get closer, but…
So, to get closer a little bit smarter, someone put their head to work and came up with another bisection method. It’s called false position (or the weighted bisection method here), and — hmm… I think it could be a way to cut down the number of tries a bit. It is not guaranteed to converge faster than midpoint bisection; one endpoint can get stuck. I’ll explain the idea with pictures and move on.
Before, it was “no questions asked, no arguing — midpoint!!!!” Now, instead of going to the midpoint of $a$ and $b$, we take the function values at $a$ and $b$ into account, write down the equation of the straight line between those two points, and then —

OK so we want to get over there, to the solution — here’s how we get closer:

We draw the line between the two function values, and call the x-intercept of that line $c$ again.

Then from that $c$, we set up the line between the two points again, and call the x-intercept of that line $c$ again, heh heh heh heh

You can see how it’s homing in on the solution now, right?
OK so before, we could just say $c = (a+b)/2$, which was easy, but here — how do we write the formula for the next $c$??????!?!?!
The equation of the line through $(a,\ f(a))$ and $(b,\ f(b))$ is

That’s the line’s equation, and

So, using this, let’s find the solution of
![]()
— the same game we played above — with the weighted bisection method. We can just reuse the function procedure we already wrote, so let’s just knock out the sub procedure. heh heh

Python version
def f(x):
return x ** 3 - 5 * x ** 2 + 3 * x - 2.7
A = 4
B = 5
limit = 1e-12
for i in range(251):
fa = f(A)
fb = f(B)
c = (fa * (A - B)) / (fb - fa) + A
fc = f(c)
if abs(fc) < limit:
break
else:
pass
if fa * fc < 0:
B = c
else:
A = c
else:
raise RuntimeError('False position did not meet the tolerance')
print('The solution of the given equation is ', c)
Heads-up. The figures here are the original notes, slips and all. A few things to watch for:
- In the VBA Sub Bisection (typed and handwritten), the value at the midpoint is stored as fb = f(C); it should be fc = f(C), the name the next lines test.
- In the VBA BS function, IIf(callput, 1, -1) should use the parameter: IIf(opOption, 1, -1) (the handwritten copy also writes Iff for IIf). As written, callput is an empty, undeclared variable, so the function always prices the put, and the 0.2612 in the Immediate window is the put’s implied volatility; the call’s, for a price of 2.5 with $S = K = 50$, $r = 0.1$ and $T = 0.5$, is about 0.050.
- The stopping check compares
abs(diff)withepsilon, not epsilon with itself. The handwritten lower-volatility bound is illustrative; the typed routine uses 0.0001.
Questions or corrections?
Did a step lose you, or does a number look off? Email me or use the contact page. Include the note title and the step or figure you mean.
Feedback goes to me privately by email; it is not posted as a public comment.