QuantLib Python外壳白色模型运行时错误:时间(20)超过最大曲线时间(19)

2024-04-27 03:03:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试使用QuantLib python运行Hull White模型的多次迭代。我遵循了下面的代码和博客: http://gouthamanbalaraman.com/blog/hull-white-simulation-quantlib-python.html

我在网站上对巴拉曼的代码做了一些编辑。也就是说,我将spot_曲线从一条平头曲线改为一条零曲线。现在我不断地得到一个错误。我正在尝试建模零曲线数据,如下面的代码所示

有人知道如何解决这个问题并在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)

Tags: pathinfordatetimenprange曲线
1条回答
网友
1楼 · 发布于 2024-04-27 03:03:42

该错误意味着您正试图从收益率曲线中获取超过最大到期日的点(日期)。你的收益率曲线的最长期限为19年,你的模拟期限为30年

为避免该错误,请使用额外到期日构建曲线,或启用外推:

spot_curve.enableExtrapolation()

相关问题 更多 >