[英]QuantLib Python Hull White Model - RuntimeError: time (20) is past max curve time (19)
I tried using QuantLib-python to run several iterations of a Hull-White model.我尝试使用 QuantLib-python 运行 Hull-White model 的多次迭代。 I followed along with the code and blog here: http://gouthamanbalaraman.com/blog/hull-white-simulation-quantlib-python.html
我跟着这里的代码和博客: http://gouthamanbalaraman.com/blog/hull-white-simulation-quantlib-python.html
I made some edits from Balaraman's code on the site.我在网站上对 Balaraman 的代码进行了一些编辑。 Namely, I changed the spot_curve from being a FlatForward to a ZeroCurve.
即,我将 spot_curve 从 FlatForward 更改为 ZeroCurve。 Now I keep getting an error.
现在我不断收到错误消息。 I am trying to model the zero curve data as seen in my code below.
我正在尝试 model 零曲线数据,如下面的代码所示。
Does anyone know how to fix this and implement the zero curve in QuantLib-python?有谁知道如何解决这个问题并在 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)
That error means you are trying to get a point (date) from your yield curve that is past the maximum maturity.该错误意味着您正试图从超过最大期限的收益率曲线中获得一个点(日期)。 Your yield curve has a maximum maturity of 19 years and your simulation is for 30 years...
您的收益率曲线最长期限为 19 年,而您的模拟期限为 30 年……
To avoid that error, either build your curve with extra maturities, or enable extrapolation:为避免该错误,请使用额外期限构建曲线,或启用外推:
spot_curve.enableExtrapolation()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.