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

左心房为你撑大大i 提交于 2021-01-07 04:52:41

问题


I tried using QuantLib-python to run several iterations of a Hull-White model. I followed along with the code and blog here: http://gouthamanbalaraman.com/blog/hull-white-simulation-quantlib-python.html

I made some edits from Balaraman's code on the site. Namely, I changed the spot_curve from being a FlatForward to a ZeroCurve. Now I keep getting an error. I am trying to model the zero curve data as seen in my code below.

Does anyone know how to fix this and implement the zero curve in 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)

回答1:


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...

To avoid that error, either build your curve with extra maturities, or enable extrapolation:

spot_curve.enableExtrapolation()


来源:https://stackoverflow.com/questions/63181287/quantlib-python-hull-white-model-runtimeerror-time-20-is-past-max-curve-tim

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!