Reputation: 123
I am trying to compute implied volatility of black scholes formula using python. however, I have problem with my code. I keep getting this error message when I running the code:
RuntimeWarning: divide by zero encountered in true_divide
v = sigmaOld - bs_option_call(v, s, k, r, t, call_price1)/fprime(sigmaOld, s, k, r, t)e here
and here is my code:
while True:
for (v, k, s, t, call_price1) in zip(sigma, K, S, Ta, call_price_list):
sigmaOld = v
v = sigmaOld - bs_option_call(v, s, k, r, t, call_price1) / fprime(sigmaOld, s, k, r, t)
if scipy.absolute( v - sigmaOld ) < epsilon:
break
print(sigma)
where fprime
is
def fprime(sigma, S, K, r, T):
logSoverK = log(S / K)
numerd1 = logSoverK + (r + sigma**2 / 2) * T
d1 = numerd1 / (sigma*sqrt(T))
return S * sqrt(T) * norm.pdf(d1) * exp(-r * T)
and K, Ta, S, sigma, call_price_list are lists and r is just a number.
I tried to use
import numpy as np
np.seterr(divide='ignore', invalid='ignore')
but it was not useful for me for some reason!
can anyone please have a look at my code and tell me what is my mistake! Many thanks in advance
Upvotes: 4
Views: 11302
Reputation: 19252
Printing errors and warnings to stderr is default Python functionality.
You're getting this warning because you're dividing by zero i.e. the fprime
is returning zero.
If you want to suppress the warning using warning filters
:
np.seterr(divide='ignore')
It'll tell Numpy to ignore the divide by zero warning - all the allowed parameters for seterr
.
Upvotes: 2