我尝试使用 QuantLib-python 运行 Hull-White 模型的多次迭代。我跟着这里的代码和博客:http: //gouthamanbalaraman.com/blog/hull-white-simulation-quantlib-python.html
我在网站上对 Balaraman 的代码进行了一些编辑。即,我将 spot_curve 从 FlatForward 更改为 ZeroCurve。现在我不断收到错误消息。我正在尝试对零曲线数据进行建模,如下面的代码所示。
有谁知道如何解决这个问题并在 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)