2016-12-12 22 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)

ので(fab)それを必要な位置引数を与え、キーワード引数としてmaxiterを渡します。

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

(また、自分に好意を行い、それらの1文字の変数名を使用しないでください)

:このように
関連する問題