I was given this assignment:
"Implement the American Algo. and the bisection Algo. to find the implied volatility of an option with the following parameters,
dt = T/N (delta t)
R = 0.03 (continuously compounded)
u= 1/d (up factor)
N=4
S0 = 100 (starting price)
T= 1.0 (time)
K = 100 (strike price)
[sigma 'a', sigma 'b'] = [0.10, 0.40]
Stop your algo. when the difference between the observed option price and the model option is less than epsilon = 10^-4. Report the binomial implied volatility for this American option. ".
On paper, I know how to apply the bisection theorem and solve this problem (i.e. if I was given, let us say 2 iterations). However, I just started learning python a couple of months ago and am struggling a bit.
So far, after doing quite a bit of reading online and books, many programs use the Black-Scholes Model (which my professor does not want us to use). I have been able to come up with this code, most of it which came from "Derivatives Analytics with Python by Yves Hilpisch".
import math
import numpy as np
def set_parameters(otype = 1, M=4): # M = N
otype: int
if otype ==1:
S0 = 100
T = 1
r = 0.03
sigmaA = 0.10
sigmaB = 0.40
sigmaC = ((sigmaA+sigmaB)/2)
dt = T/M #Time interval
df = math.exp(-r*dt) #Discount factor
u = math.exp(sigmaC*math.sqrt(dt)) #up factor
d = 1/u #down factor
p=(math.exp(r*dt) - d) /(u-d)
return S0, T, r, sigmaC, M, dt, df, u, d, p
def inner_value(S, otype=1):
otype: int
if otype ==1:
return np.maximum(100-S, 0)
def CRR_option_valuation(otype, M=4):
S0, T, r, sigmaC, M, dt, df, u, d, p = set_parameters(otype, M)
#Array generation for stock prices
mu = np.arange(M+1)
mu = np.resize(mu, (M+1, M+1))
md = np.transpose(mu)
mu = u**(mu-md)
md = d**md
S = S0*mu*md
#Valuation by backwards induction
h = inner_value(S, otype) #Inner value matrix
V = inner_value(S, otype) #value matrix
C = np.zeros((M+1, M+1), dtype = np.float)
ex = np.zeros((M+1, M+1), dtype=np.float) #Exercise matrix
z = 0
for i in range(M-1, -1, -1):
C[0:M-z, i] = (p*V[0:M-z, i+1] + (1-p)*V[1:M-z+1, i+1])*df
V[0:M-z, i] = np.where(h[0:M-z, i] > C[0:M-z, i], h[0:M-z, i], C[0:M-z, i])
ex[0:M-z, i] = np.where(h[0:M-z,i] > C[0:M-z, i], 1, 0)
z+=1
return V[0,0]
My questions are:
1) Why is nothing outputted at the end of this program? (Or is it just that I am not giving python enough time to run)
2) Do I need to create another function to use the bisection theorem. I.e. If V(0,0) is < or > the original observed price of $9.25, the volatility parameters will change).
3) What am I missing in my code to complete it?