2016-12-12 102 views
0

我的代碼的參數太多:布倫特函數調用

class ImpliedVol: 

    def __init__(self, flag, mkt_price, spot_price, strike, time_to_maturity, 
       lower_bound, upper_bound, risk_free_rate=0, maxiter=1000, 
       method='f'): 

     self.flag = flag 
     self.mkt_price = mkt_price 
     self.S = spot_price 
     self.K = strike 
     self.T = time_to_maturity 
     self.r = risk_free_rate 
     self.a = lower_bound 
     self.b = upper_bound 
     self.n = maxiter 
     self.method = method 

    def func(self, vol): 

     p = Pricer(self.flag, self.S, self.K, self.T, vol, self.r, self.method) 

     return p.get_price() - self.mkt_price 

    def get(self): 

     implied_vol = brentq(self.func, self.a, self.b, self.n) 

     return implied_vol 

某些參數創建類的實例正常工作,還呼籲func完美作品的方法需要:

obj.func(0.54) 
Out[11]: 
4.0457814868958174e-05 

但在我的實例上調用方法get返回以下錯誤:

/Users/~/miniconda3/lib/python3.5/site-packages/scipy/optimize/zeros.py in brentq(f, a, b, args, xtol, rtol, maxiter, full_output, disp) 
    436  if rtol < _rtol: 
    437   raise ValueError("rtol too small (%g < %g)" % (rtol, _rtol)) 
--> 438  r = _zeros._brentq(f,a,b,xtol,rtol,maxiter,args,full_output,disp) 
    439  return results_c(full_output, r) 
    440 

TypeError: func() takes 2 positional arguments but 3 were given 

回答

1

您正在定義:self.n = maxiter並將其作爲第4個參數調用brentq。然而,對於brentq簽名是:scipy.optimize.brentq(f, a, b, args=(), xtol=1e-12, rtol=4.4408920985006262e-16, maxiter=100, full_output=False, disp=True)

所以給它所需的位置ARGS(fab),並通過maxiter作爲關鍵字ARG。就像這樣:

brentq(self.func, self.a, self.b, maxiter=self.n) 

(也請你幫個忙,不要使用那些一個字母的變量名)