From 7a21fd8338f3d18397e911c6aaafb18b55a6c614 Mon Sep 17 00:00:00 2001 From: Hrushi20 Date: Fri, 29 Mar 2024 12:07:24 +0530 Subject: [PATCH] added nterest-rate term structure in g2process constructor Signed-off-by: Hrushi20 --- ql/processes/g2process.cpp | 19 +++++++++++++------ ql/processes/g2process.hpp | 7 +++++-- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/ql/processes/g2process.cpp b/ql/processes/g2process.cpp index 45f58b9c7c2..db8a6cabdb7 100644 --- a/ql/processes/g2process.cpp +++ b/ql/processes/g2process.cpp @@ -22,10 +22,10 @@ namespace QuantLib { - G2Process::G2Process(Real a, Real sigma, Real b, Real eta, Real rho) + G2Process::G2Process(const Handle& h, Real a, Real sigma, Real b, Real eta, Real rho) : a_(a), sigma_(sigma), b_(b), eta_(eta), rho_(rho), xProcess_(new QuantLib::OrnsteinUhlenbeckProcess(a, sigma, 0.0)), - yProcess_(new QuantLib::OrnsteinUhlenbeckProcess(b, eta, 0.0)) {} + yProcess_(new QuantLib::OrnsteinUhlenbeckProcess(b, eta, 0.0)), h_(h) {} Size G2Process::size() const { return 2; @@ -36,9 +36,16 @@ namespace QuantLib { } Array G2Process::drift(Time t, const Array& x) const { + Real alpha_drift = sigma_*sigma_/(2*a_)*(1-std::exp(-2*a_*t)); + Real shift = 0.0001; + Real f = h_->forwardRate(t, t, Continuous, NoFrequency); + Real fup = h_->forwardRate(t+shift, t+shift, Continuous, NoFrequency); + Real f_prime = (fup-f)/shift; + alpha_drift += a_*f+f_prime; + return { - xProcess_->drift(t, x[0]), - yProcess_->drift(t, x[1]) + xProcess_->drift(t, x[0]) + alpha_drift, + yProcess_->drift(t, x[1]) + alpha_drift }; } @@ -124,10 +131,10 @@ namespace QuantLib { } - G2ForwardProcess::G2ForwardProcess(Real a, Real sigma, Real b, Real eta, Real rho) + G2ForwardProcess::G2ForwardProcess(const Handle& h, Real a, Real sigma, Real b, Real eta, Real rho) : a_(a), sigma_(sigma), b_(b), eta_(eta), rho_(rho), xProcess_(new QuantLib::OrnsteinUhlenbeckProcess(a, sigma, 0.0)), - yProcess_(new QuantLib::OrnsteinUhlenbeckProcess(b, eta, 0.0)) {} + yProcess_(new QuantLib::OrnsteinUhlenbeckProcess(b, eta, 0.0)),h_(h) {} Size G2ForwardProcess::size() const { return 2; diff --git a/ql/processes/g2process.hpp b/ql/processes/g2process.hpp index b2cfaf3caac..7c6b1319266 100644 --- a/ql/processes/g2process.hpp +++ b/ql/processes/g2process.hpp @@ -26,6 +26,7 @@ #include #include +#include namespace QuantLib { @@ -33,7 +34,7 @@ namespace QuantLib { /*! \ingroup processes */ class G2Process : public StochasticProcess { public: - G2Process(Real a, Real sigma, Real b, Real eta, Real rho); + G2Process(const Handle& h, Real a, Real sigma, Real b, Real eta, Real rho); //! \name StochasticProcess interface //@{ Size size() const override; @@ -55,13 +56,14 @@ namespace QuantLib { Real x0_ = 0.0, y0_ = 0.0, a_, sigma_, b_, eta_, rho_; ext::shared_ptr xProcess_; ext::shared_ptr yProcess_; + Handle h_; }; //! %Forward %G2 stochastic process /*! \ingroup processes */ class G2ForwardProcess : public ForwardMeasureProcess { public: - G2ForwardProcess(Real a, Real sigma, Real b, Real eta, Real rho); + G2ForwardProcess(const Handle& h, Real a, Real sigma, Real b, Real eta, Real rho); //! \name StochasticProcess interface //@{ Size size() const override; @@ -80,6 +82,7 @@ namespace QuantLib { Real yForwardDrift(Time t, Time T) const; Real Mx_T(Real s, Real t, Real T) const; Real My_T(Real s, Real t, Real T) const; + Handle h_; }; }