Quantlib python Heston模型:生成路径,获取“Boost断言失败:px!=0”

2024-04-24 04:18:59 发布

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

我试图在Quantlib python中使用GaussianPathGenerator和HestonProcess生成底层路径。但它给出了“RuntimeError:Boost断言失败:px!=0”

如果我用BS或HullWhite进程替换Heston进程,则生成的路径很好。有人知道为什么赫斯顿一号不工作吗

非常感谢你的帮助

将QuantLib作为ql导入

today = ql.Date(21, 10, 2020)
daycount = ql.Actual360()
calendar = ql.TARGET()
r_ts = ql.YieldTermStructureHandle(ql.FlatForward(today, 0.02, daycount))
d_ts = ql.YieldTermStructureHandle(ql.FlatForward(today, 0.01, daycount))
v0, kappa, theta, sigma, rho = 0.03, 10, 0.03, 0.4, 0.3
hs_process = ql.HestonProcess(r_ts, d_ts, ql.QuoteHandle(ql.SimpleQuote(100)), v0, kappa, theta, sigma, rho)

bs_process = ql.BlackScholesProcess(ql.QuoteHandle(ql.SimpleQuote(100)), r_ts, 
                                    ql.BlackVolTermStructureHandle(ql.BlackConstantVol(0, calendar, 0.20, daycount)))

timestep = 50
length = 1.0
rng = ql.GaussianRandomSequenceGenerator(ql.UniformRandomSequenceGenerator(timestep, ql.UniformRandomGenerator()))
seq = ql.GaussianPathGenerator(hs_process, length, timestep, rng, False)

path_1 = seq.next()

RuntimeError                              Traceback (most recent call last)
<ipython-input-23-e8c522a62dd1> in <module>
----> 1 path1 = seq.next()

~\Anaconda3\lib\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: Boost assertion failed: px != 0

Tags: self路径todayprocessseqnextqlboost
1条回答
网友
1楼 · 发布于 2024-04-24 04:18:59

Heston过程是二维的(它同时演化基础的波动性),因此需要将其传递给GaussianMultiPathGenerator。不幸的是,包装中的SWIG机制没有捕捉到类型不匹配;它只是尝试将进程强制转换为一维进程,当强制转换失败时,会产生一个空指针

出于同样的原因,您必须用2*timesteps初始化GaussianRandomSequenceGenerator,因为它必须为过程中的两个变量提供足够的随机数

总之,您的代码应为:

rng = ql.GaussianRandomSequenceGenerator(ql.UniformRandomSequenceGenerator(2*timestep, ql.UniformRandomGenerator()))
times = list(ql.TimeGrid(length, timestep))
seq = ql.GaussianMultiPathGenerator(hs_process, times, rng)

调用path = seq.next()将返回一个Multipath实例path.value()[0]将是基础的路径,path.value()[1]将是其波动的路径

相关问题 更多 >