簡體   English   中英

QuantLib Python 船體白色 Model - 運行時錯誤:時間 (20) 已超過最大曲線時間 (19)

[英]QuantLib Python Hull White Model - RuntimeError: time (20) is past max curve time (19)

我嘗試使用 QuantLib-python 運行 Hull-White model 的多次迭代。 我跟着這里的代碼和博客: http://gouthamanbalaraman.com/blog/hull-white-simulation-quantlib-python.html

我在網站上對 Balaraman 的代碼進行了一些編輯。 即,我將 spot_curve 從 FlatForward 更改為 ZeroCurve。 現在我不斷收到錯誤消息。 我正在嘗試 model 零曲線數據,如下面的代碼所示。

有誰知道如何解決這個問題並在 QuantLib-python 中實現零曲線?

from QuantLib import *
import utils
import numpy as np
%matplotlib inline

##Assign all variables
sigma = 0.015
a = 0.1
timestep = 30
length = 30 # in years
day_count = Thirty360()
start_date = Date(19, 11, 1989)
calendar = UnitedStates()
interpolation = Linear()
compounding = Compounded
compoundingFrequency = Annual

dates = [Date(19,11,1990), Date(19,11,1991), Date(19,11,1992), 
         Date(19,11,1993), Date(19,11,1994), Date(19,11,1995), 
         Date(19,11,1996), Date(19,11,1997), Date(19,11,1998),
         Date(19,11,1999), Date(19,11,2000), Date(19,11,2001),
         Date(19,11,2002), Date(19,11,2003), Date(19,11,2004),
         Date(19,11,2005), Date(19,11,2006), Date(19,11,2007),
         Date(19,11,2008), Date(19,11,2009)]

zeros = [0.115974,0.118913,0.120676,0.121751,0.122455,0.122988,
         0.12347,0.123972,0.124527,0.125147,0.125831,0.126573,
         0.127359,0.128178,0.129016,0.129863,0.130708,0.131544,
         0.132364,0.133162]



#setup spot curve. Notable difference is the ZeroCurve instead of FlatForward

spot_curve = ZeroCurve(dates, zeros, day_count, calendar, interpolation, compounding, compoundingFrequency)
spot_curve_handle = YieldTermStructureHandle(spot_curve)



#The Hull-White process is constructed by passing the term-structure, a and sigma. 
#To create the path generator, one has to provide a random sequence generator along 
#with other simulation inputs such as timestep and `length.

hw_process = HullWhiteProcess(spot_curve_handle, a, sigma)
rng = GaussianRandomSequenceGenerator(
    UniformRandomSequenceGenerator(timestep, UniformRandomGenerator()))
seq = GaussianPathGenerator(hw_process, length, timestep, rng, False)


#define generate paths function
def generate_paths(num_paths, timestep):
    arr = np.zeros((num_paths, timestep+1))
    for i in range(num_paths):
        sample_path = seq.next()
        path = sample_path.value()
        time = [path.time(j) for j in range(len(path))]
        value = [path[j] for j in range(len(path))]
        arr[i, :] = np.array(value)
    return np.array(time), arr


#plotting short rates
num_paths = 100
paths = generate_paths(num_paths, timestep)
fig, ax = utils.plot()
for i in range(num_paths):
    ax.plot(time, paths[i, :], lw=0.8, alpha=0.6)
ax.set_title("Hull-White Short Rate Simulation");
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-3-366fe665a669> in <module>
     62 #plotting short rates
     63 num_paths = 100
---> 64 paths = generate_paths(num_paths, timestep)
     65 fig, ax = utils.plot()
     66 for i in range(num_paths):

<ipython-input-3-366fe665a669> in generate_paths(num_paths, timestep)
     52     arr = np.zeros((num_paths, timestep+1))
     53     for i in range(num_paths):
---> 54         sample_path = seq.next()
     55         path = sample_path.value()
     56         time = [path.time(j) for j in range(len(path))]

~/opt/anaconda3/lib/python3.7/site-packages/QuantLib/QuantLib.py in next(self)
  22328 
  22329     def next(self):
> 22330         return _QuantLib.GaussianPathGenerator_next(self)
  22331 
  22332     def antithetic(self):

RuntimeError: time (20) is past max curve time (19)

該錯誤意味着您正試圖從超過最大期限的收益率曲線中獲得一個點(日期)。 您的收益率曲線最長期限為 19 年,而您的模擬期限為 30 年……

為避免該錯誤,請使用額外期限構建曲線,或啟用外推:

spot_curve.enableExtrapolation()

暫無
暫無

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

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