0
$\begingroup$

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()
$\endgroup$
3
$\begingroup$

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()

G2 Short Rate Simulation

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.

| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.