挥发性表面的3D图

时间:2018-06-20 12:04:38

标签: python python-3.x numpy 3d finance

我正在尝试绘制隐含波动率的3D表面。但是我一直在为BS功能获取此错误消息:

 line 29, in _amin
return umr_minimum(a, axis, None, out, keepdims)

TypeError:“ builtin_function_or_method”实例与“ builtin_function_or_method”实例之间不支持“ <=”

我正在运行的代码:

def CND(X):
   return float(norm.cdf(X))


def BlackScholes(v=0.8,CallPutFlag = 'c',S = 110.,X = 100.,T = 1.,r = 0.01):

   try:
      d1 = (log(S/X)+(r+v*v/2.)*T)/(v*sqrt(T))
      d2 = d1-v*sqrt(T)

      if CallPutFlag=='c':
         return S*CND(d1)-X*exp(-r*T)*CND(d2)

      else:
         return X*exp(-r*T)*CND(-d2)-S*CND(-d1)

   except: return 0

def calc_impl_vol(price = 5., right = 'c', underlying = 110., strike = 100., time = 1., rf = 0.005, inc = 0.001):

   f = lambda x: BlackScholes(x,CallPutFlag=right,S=underlying,X=strike,T=time,r=rf)-price

   return optimize.brentq(f,0.,5.) 

def mesh_plot2(X,Y,Z):
   fig = plt.figure()
   ax = Axes3D(fig, azim = -29, elev = 50)
   XX,YY,ZZ = make_surf(X,Y,Z)
   ax.plot_surface(XX,YY,ZZ, color = 'white')
   ax.contour(XX,YY,ZZ)
   plt.xlabel("expiry")
   plt.ylabel("strike")  

def make_surf(X,Y,Z):
   XX,YY = np.meshgrid(np.linspace(min(X),max(X),230),np.linspace(min(Y),max(Y),230))
   ZZ = griddata(np.array([X,Y]).T,np.array(Z),(XX,YY), method='linear')
   return XX,YY,ZZ   

F=pd.read_excel('test_data.xlsx')

def get_surf(F):

   q=F
   q.reset_index(inplace=True)

   vals = []


   for index, row in q.iterrows():
       if row['Type'] == 'c':
           underlying = float(row['Underlying_Price'])
           price = (float(row['ask'])+float(row['bid']))/2.0
           expd = (row['exp'])       
           exp1 = (expd) / (365)
           try:
               impl = calc_impl_vol(price, 'c', underlying, float(row['Strike']), exp1)
               vals.append([exp,float(row['Strike']),impl]) 
           except:
                   pass
   vals = np.array(vals).T
   mesh_plot2(vals[0],vals[1],vals[2])



surf=get_surf(F)

我怀疑错误在vals中,因为它是作为“ numpy模块的ndarray对象”返回的。

我已经尝试了初学者python编码思想可以想到的一切。

有没有人看到我的错误。

0 个答案:

没有答案