量化庫

如何在 Py QuantLib 中使用 G2Process

  • March 6, 2020

我正在嘗試使用G2Process對象進行 MC。我試圖模仿的例子在這個連結中。下面是我所做的程式碼片段。請有人指導我為什麼我不正確地使用界面?謝謝。

import numpy as np
import QuantLib as ql
import matplotlib.pyplot as plt
from scipy.integrate import cumtrapz
ql.__version__

if __name__ == "__main__":
   a = 0.1
   sigma = 0.2
   b = 0.4
   eta = 0.17
   rho = -0.8
   timestep = 360
   length = 30 # in years
   forward_rate = 0.05
   day_count = ql.Thirty360()
   todays_date = ql.Date(15, 1, 2015)
   ql.Settings.instance().evaluationDate = todays_date

   yield_curve = ql.FlatForward(todays_date, ql.QuoteHandle(ql.SimpleQuote(forward_rate)), day_count)
   spot_curve_handle = ql.YieldTermStructureHandle(yield_curve)
   hw_process = ql.HullWhiteProcess(spot_curve_handle, a, sigma)
   rng = ql.GaussianRandomSequenceGenerator(ql.UniformRandomSequenceGenerator(timestep, ql.UniformRandomGenerator(125)))
   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 = 128
   time, paths = generate_paths(num_paths, timestep)
   for i in range(num_paths):
       plt.plot(time, paths[i, :], lw=0.8, alpha=0.6)
   plt.title("HW Short Rate Simulation")
   plt.show()

我解決了我正在尋找的東西。該G2Process對象具有evolve模擬 MC 路徑的方法。工作程式碼如下所示。歡迎任何建設性的回饋/意見。謝謝。

import numpy as np
import QuantLib as ql
import matplotlib.pyplot as plt
from scipy.integrate import cumtrapz
ql.__version__

if __name__ == "__main__":

   a = 0.1
   sigma = 0.2
   b = 0.4
   eta = 0.17
   rho = -0.8
   timestep = 360
   length = 30 # in years

   g2_process = ql.G2Process(a, sigma, b, eta, rho)

   num_paths = 200
   grid = ql.TimeGrid(length, timestep)
   rng = ql.GaussianRandomSequenceGenerator(ql.UniformRandomSequenceGenerator(2, ql.UniformRandomGenerator(125)))

   def generate_paths(num_paths, timestep):
       dz = ql.Array(2, 0.0)
       path = np.zeros(shape = (num_paths, timestep+1))
       sample = ql.Array(2, 0.0)
       for i in range(num_paths):
           ir = ql.Array(2, 0.0)
           for j in range(timestep):
               ir = g2_process.evolve(grid[j], ir, grid.dt(j), dz)              
               sample = rng.nextSequence().value()
               dz[0] = sample[0]
               dz[1] = sample[1]
               path[i,j] = np.sum(ir)
       time = np.array([grid[j] for j in range(len(grid))])
       return time, path

   time, paths = generate_paths(num_paths, timestep)
   arr = np.zeros(shape = (num_paths, timestep))
   for i in range(num_paths):
       plt.plot(time, paths[i, :])
   plt.title("G2 Short Rate Simulation")
   plt.show()

G2 短期模擬

我還檢查了是否可以在 MC 模擬期間通過G2對象計算零息債券。不幸的是,方法discountBond在 v1.17 中不可用;它在 QuantLib C++ 中可用。希望未來的版本會包含它。謝謝。

引用自:https://quant.stackexchange.com/questions/51508