-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathimp_vol_call.py
executable file
·66 lines (48 loc) · 1.33 KB
/
imp_vol_call.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
from scipy import stats
from numpy import log, sqrt, exp
from pylab import *
import time
def imp_vol_call(C,S,X,rd,q,T):
# computes the implied volatility
# for a call when the traded call price is C.
# C and X must be column vectors of equal size
C=C;
X=X;
T=T*1.;
NC=len(C);
NX=len(X);
if NC == NX:
S=S*1.;
r=rd*1.;
q=q*1.;
ff=[];
for nc in range(NC):
sigma=.3;
error=.000001;
dv=error+1.;
tic = time.time();
while abs(dv)>error:
d1=(log(S/X[nc])+(r-q+sigma**2/2.)*T)/(sigma*sqrt(T));
d2=d1-sigma*sqrt(T);
nd1 = stats.norm.cdf(d1);
nd2 = stats.norm.cdf(d2);
npd1 = stats.norm.pdf(d1);
PriceError=S*exp(-q*T)*nd1-X[nc]*exp(-r*T)*nd2-C[nc];
Vega=S*sqrt(T)*exp(-q*T)*npd1;
if Vega==0:
disp('No Volatility can be found')
sigma=NaN;
break
dv=PriceError/Vega;
sigma=sigma-dv;
time2=time.time()-tic;
if time2>60:
disp('the routine did not converge within 60 seconds')
sigma=NaN;
break
ff.append(sigma);
else:
disp('C and X are not of equal size')
ff=NaN;
f=ff;
return f