Simulating G2 Short Rates with QuantLib’s Process
Summary
The document shows how to simulate short-rate paths from a two-factor G2 interest-rate process in QuantLib’s Python interface. Its central method is to advance the process one time step at a time with the process’s evolution method, supplying two Gaussian random variates for the factors. The simulated short rate is formed by summing the two factor values, and the example plots paths over a time grid.
The discussion contrasts this workflow with a Hull–White path-generator example and notes that the Python G2 process interface in the cited version did not expose the model’s discount-bond method. The example is practical API guidance rather than a validation of the model or simulation results. It does not assess calibration, convergence, or pricing accuracy, and the shown path array indexing may omit the final time point, so users should check their own output dimensions and initialization.
Key ideas
- The G2 process can be simulated in Python by calling its evolution method at each time step.
- Each evolution step requires Gaussian shocks for both model factors.
- The short rate in the example is calculated as the sum of the two factor values.
- The cited QuantLib Python version lacked access to the G2 discount-bond method.
- The example demonstrates interface use but does not establish calibration quality or pricing accuracy.
Tags
Full text
# How to use G2Process in Py QuantLib
# How to use G2Process in Py QuantLib
I'm trying to do MC using `G2Process` object. The example I'm trying to mimic is in this link. Below is a code snippet of what I did. Please will someone guide me on why I'm using the interface incorrectly? Thanks.
```
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()
```
## Answer by AnonymousJ (score 4, accepted)
https://quant.stackexchange.com/a/51515
I solved what I was seeking. The `G2Process` object has method `evolve` to simulate the MC path. A working code is shown below. Any constructive feedback/comments are welcome. Thanks.
```
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()
```
I also checked if the zero coupon bond could be calculated during the MC simulation via `G2` object. Unfortunately, method `discountBond` is not in the available in v1.17; it's available in QuantLib C++. Hopefully, future version will include it. Thanks.Shown in full with attribution under the source's licence. Licence: CC BY-SA 4.0 (Stack Exchange)
This summary was written by Stratmill's research agent from the original; it is not a copy of the source.