簡體   English   中英

繪制波動率表面時出現 QuantLib 錯誤

[英]QuantLib error while Plotting Volatility Surface

我正在嘗試使用以下代碼 plot 波動率表面:

        plot_years = np.arange(0, 2, 0.1)
    plot_strikes = np.arange(535, 750, 1)
    fig = plt.figure()
    ax = fig.gca(projection='3d')
    X, Y = np.meshgrid(plot_strikes, plot_years)
    Z = np.array([black_var_surface.blackVol(y, x) 
                  for xr, yr in zip(X, Y) 
                      for x, y in zip(xr,yr) ]
                 ).reshape(len(X), len(X[0]))
    
    surf = ax.plot_surface(X,Y,Z, rstride=1, cstride=1, cmap=cm.coolwarm, 
                    linewidth=0.1)
    fig.colorbar(surf, shrink=0.5, aspect=5)

但我得到這個錯誤:


    TypeError                                 Traceback (most recent call last)
    <ipython-input-55-8132b1b292ed> in <module>
          4 ax = fig.gca(projection='3d')
          5 X, Y = np.meshgrid(plot_strikes, plot_years)
    ----> 6 Z = np.array([black_var_surface.blackVol(y, x) 
          7               for xr, yr in zip(X, Y)
          8                   for x, y in zip(xr,yr) ]
    
    <ipython-input-55-8132b1b292ed> in <listcomp>(.0)
          4 ax = fig.gca(projection='3d')
          5 X, Y = np.meshgrid(plot_strikes, plot_years)
    ----> 6 Z = np.array([black_var_surface.blackVol(y, x) 
          7               for xr, yr in zip(X, Y)
          8                   for x, y in zip(xr,yr) ]
    
    ~\anaconda3\lib\site-packages\QuantLib\QuantLib.py in blackVol(self, *args)
       7566 
       7567     def blackVol(self, *args):
    -> 7568         return _QuantLib.BlackVolTermStructure_blackVol(self, *args)
       7569 
       7570     def blackVariance(self, *args):
    
    TypeError: Wrong number or type of arguments for overloaded function 'BlackVolTermStructure_blackVol'.
      Possible C/C++ prototypes are:
        BlackVolTermStructure::blackVol(Date const &,Real,bool) const
        BlackVolTermStructure::blackVol(Date const &,Real) const
        BlackVolTermStructure::blackVol(Time,Real,bool) const
        BlackVolTermStructure::blackVol(Time,Real) const

我使用的是舊版本的 package 嗎? 因為我正在使用 Goutham Balaraman 在 2016 年分享的筆記本。

謝謝您的幫助 !

The QuantLib functions and class methods are exposed from C++ through wrappers that perform type conversion from Python types to the underlying C++ types. The obvious ones are defined (Python int to C++ int , Python float to C++ double , even Python int to C++ double if needed) but others are not.

在您的情況下, C++ function 需要兩個雙打,但是xy是 numpy 類型(您可以使用print(type(x))print(type(x))進行檢查) y來自np.arange(0, 2, 0.1)並且是np.float64類型,它可以轉換為float然后 C++ double x相反,來自np.arange(535, 750, 1)並且是np.int64類型,它不會自動轉換為float ,因此出現錯誤。

使這項工作的一種方法是顯式轉換變量,即

black_var_surface.blackVol(y, float(x))

另一種是使用

plot_strikes = np.arange(535.0, 750.0, 1.0)

它生成一個np.float64而不是np.int64的數組。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM