You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为QuantLib的UniformRandomGenerator()设置固定种子?

解决Hull-White路径模拟中固定随机种子的问题

你遇到的问题是设置UniformRandomGenerator种子后仍得到可变路径,核心原因是UniformRandomGenerator的种子初始化方式在QuantLib中稳定性不足,或者需要更规范的随机数生成器配置。以下是两种可行的解决方案:

方案1:显式初始化并设置种子

先创建UniformRandomGenerator实例,再通过setSeed()方法固定种子,避免匿名初始化可能导致的状态异常:

import QuantLib as ql
import matplotlib.pyplot as plt
import numpy as np

sigma = 0.1
a = 0.1
timestep = 3
length = 1 # in years
forward_rate = 0.05
day_count = ql.Actual365Fixed()
todays_date = ql.Date(15, 1, 2015)

ql.Settings.instance().evaluationDate = todays_date

spot_curve = ql.FlatForward(todays_date, ql.QuoteHandle(ql.SimpleQuote(forward_rate)), day_count)
spot_curve_handle = ql.YieldTermStructureHandle(spot_curve)

hw_process = ql.HullWhiteProcess(spot_curve_handle, a, sigma)

# 显式创建均匀随机生成器并设置种子
ur_gen = ql.UniformRandomGenerator()
ur_gen.setSeed(0)
urs_gen = ql.UniformRandomSequenceGenerator(timestep, ur_gen)
rng = ql.GaussianRandomSequenceGenerator(urs_gen)

seq = ql.GaussianPathGenerator(hw_process, length, timestep, rng, False)

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

num_paths = 1
time, path = generate_paths(num_paths, timestep)
for i in range(path.shape[1]):
    print(path[0][i])

方案2:使用MersenneTwisterUniformRng更稳定的生成器

QuantLib提供的MersenneTwisterUniformRng是更可靠的伪随机数生成器,种子设置的稳定性更高,推荐使用:

import QuantLib as ql
import matplotlib.pyplot as plt
import numpy as np

sigma = 0.1
a = 0.1
timestep = 3
length = 1 # in years
forward_rate = 0.05
day_count = ql.Actual365Fixed()
todays_date = ql.Date(15, 1, 2015)

ql.Settings.instance().evaluationDate = todays_date

spot_curve = ql.FlatForward(todays_date, ql.QuoteHandle(ql.SimpleQuote(forward_rate)), day_count)
spot_curve_handle = ql.YieldTermStructureHandle(spot_curve)

hw_process = ql.HullWhiteProcess(spot_curve_handle, a, sigma)

# 使用MersenneTwister生成器固定种子
rng = ql.GaussianRandomSequenceGenerator(
    ql.UniformRandomSequenceGenerator(
        timestep, 
        ql.MersenneTwisterUniformRng(0)
    )
)

seq = ql.GaussianPathGenerator(hw_process, length, timestep, rng, False)

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

num_paths = 1
time, path = generate_paths(num_paths, timestep)
for i in range(path.shape[1]):
    print(path[0][i])

额外注意事项

  • 如果需要多次生成相同路径(比如循环生成多组路径),每次生成前都需要重新初始化随机数生成器并设置种子,避免生成器状态推进导致路径变化。
  • 确保ql.Settings.instance().evaluationDate固定,日期变化也会影响Hull-White过程的路径计算。

内容的提问来源于stack exchange,提问作者WinstonSmith

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 00:53:09