G2PlusPlus.java

  1. package org.drip.dynamics.hjm;

  2. /*
  3.  * -*- mode: java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  4.  */

  5. /*!
  6.  * Copyright (C) 2020 Lakshmi Krishnamurthy
  7.  * Copyright (C) 2019 Lakshmi Krishnamurthy
  8.  * Copyright (C) 2018 Lakshmi Krishnamurthy
  9.  * Copyright (C) 2017 Lakshmi Krishnamurthy
  10.  * Copyright (C) 2016 Lakshmi Krishnamurthy
  11.  * Copyright (C) 2015 Lakshmi Krishnamurthy
  12.  *
  13.  *  This file is part of DROP, an open-source library targeting analytics/risk, transaction cost analytics,
  14.  *      asset liability management analytics, capital, exposure, and margin analytics, valuation adjustment
  15.  *      analytics, and portfolio construction analytics within and across fixed income, credit, commodity,
  16.  *      equity, FX, and structured products. It also includes auxiliary libraries for algorithm support,
  17.  *      numerical analysis, numerical optimization, spline builder, model validation, statistical learning,
  18.  *      and computational support.
  19.  *  
  20.  *      https://lakshmidrip.github.io/DROP/
  21.  *  
  22.  *  DROP is composed of three modules:
  23.  *  
  24.  *  - DROP Product Core - https://lakshmidrip.github.io/DROP-Product-Core/
  25.  *  - DROP Portfolio Core - https://lakshmidrip.github.io/DROP-Portfolio-Core/
  26.  *  - DROP Computational Core - https://lakshmidrip.github.io/DROP-Computational-Core/
  27.  *
  28.  *  DROP Product Core implements libraries for the following:
  29.  *  - Fixed Income Analytics
  30.  *  - Loan Analytics
  31.  *  - Transaction Cost Analytics
  32.  *
  33.  *  DROP Portfolio Core implements libraries for the following:
  34.  *  - Asset Allocation Analytics
  35.  *  - Asset Liability Management Analytics
  36.  *  - Capital Estimation Analytics
  37.  *  - Exposure Analytics
  38.  *  - Margin Analytics
  39.  *  - XVA Analytics
  40.  *
  41.  *  DROP Computational Core implements libraries for the following:
  42.  *  - Algorithm Support
  43.  *  - Computation Support
  44.  *  - Function Analysis
  45.  *  - Model Validation
  46.  *  - Numerical Analysis
  47.  *  - Numerical Optimizer
  48.  *  - Spline Builder
  49.  *  - Statistical Learning
  50.  *
  51.  *  Documentation for DROP is Spread Over:
  52.  *
  53.  *  - Main                     => https://lakshmidrip.github.io/DROP/
  54.  *  - Wiki                     => https://github.com/lakshmiDRIP/DROP/wiki
  55.  *  - GitHub                   => https://github.com/lakshmiDRIP/DROP
  56.  *  - Repo Layout Taxonomy     => https://github.com/lakshmiDRIP/DROP/blob/master/Taxonomy.md
  57.  *  - Javadoc                  => https://lakshmidrip.github.io/DROP/Javadoc/index.html
  58.  *  - Technical Specifications => https://github.com/lakshmiDRIP/DROP/tree/master/Docs/Internal
  59.  *  - Release Versions         => https://lakshmidrip.github.io/DROP/version.html
  60.  *  - Community Credits        => https://lakshmidrip.github.io/DROP/credits.html
  61.  *  - Issues Catalog           => https://github.com/lakshmiDRIP/DROP/issues
  62.  *  - JUnit                    => https://lakshmidrip.github.io/DROP/junit/index.html
  63.  *  - Jacoco                   => https://lakshmidrip.github.io/DROP/jacoco/index.html
  64.  *
  65.  *  Licensed under the Apache License, Version 2.0 (the "License");
  66.  *      you may not use this file except in compliance with the License.
  67.  *  
  68.  *  You may obtain a copy of the License at
  69.  *      http://www.apache.org/licenses/LICENSE-2.0
  70.  *  
  71.  *  Unless required by applicable law or agreed to in writing, software
  72.  *      distributed under the License is distributed on an "AS IS" BASIS,
  73.  *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  74.  *  
  75.  *  See the License for the specific language governing permissions and
  76.  *      limitations under the License.
  77.  */

  78. /**
  79.  * <i>G2PlusPlus</i> provides the Hull-White-type, but 2F Gaussian HJM Short Rate Dynamics Implementation.
  80.  *
  81.  *  <br><br>
  82.  *  <ul>
  83.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ProductCore.md">Product Core Module</a></li>
  84.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/FixedIncomeAnalyticsLibrary.md">Fixed Income Analytics</a></li>
  85.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/dynamics/README.md">HJM, Hull White, LMM, and SABR Dynamic Evolution Models</a></li>
  86.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/dynamics/hjm/README.md">HJM Based Latent State Evolution</a></li>
  87.  *  </ul>
  88.  *
  89.  * @author Lakshmi Krishnamurthy
  90.  */

  91. public class G2PlusPlus {
  92.     private double _dblA = java.lang.Double.NaN;
  93.     private double _dblB = java.lang.Double.NaN;
  94.     private double _dblEta = java.lang.Double.NaN;
  95.     private double _dblRho = java.lang.Double.NaN;
  96.     private double _dblSigma = java.lang.Double.NaN;
  97.     private org.drip.function.definition.R1ToR1 _auIFRInitial = null;
  98.     private org.drip.sequence.random.UnivariateSequenceGenerator[] _aRSG = null;

  99.     /**
  100.      * G2PlusPlus Constructor
  101.      *
  102.      * @param dblSigma Sigma
  103.      * @param dblA A
  104.      * @param dblEta Eta
  105.      * @param dblB B
  106.      * @param aRSG Array of the Random Sequence Generators
  107.      * @param dblRho Rho
  108.      * @param auIFRInitial The Initial Instantaneous Forward Rate Term Structure
  109.      *
  110.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  111.      */

  112.     public G2PlusPlus (
  113.         final double dblSigma,
  114.         final double dblA,
  115.         final double dblEta,
  116.         final double dblB,
  117.         final org.drip.sequence.random.UnivariateSequenceGenerator[] aRSG,
  118.         final double dblRho,
  119.         final org.drip.function.definition.R1ToR1 auIFRInitial)
  120.         throws java.lang.Exception
  121.     {
  122.         if (!org.drip.numerical.common.NumberUtil.IsValid (_dblSigma = dblSigma) ||
  123.             !org.drip.numerical.common.NumberUtil.IsValid (_dblA = dblA) ||
  124.                 !org.drip.numerical.common.NumberUtil.IsValid (_dblEta = dblEta) ||
  125.                     !org.drip.numerical.common.NumberUtil.IsValid (_dblB = dblB) || null == (_aRSG = aRSG) || 2
  126.                         != _aRSG.length || !org.drip.numerical.common.NumberUtil.IsValid (_dblRho = dblRho) ||
  127.                             null == (_auIFRInitial = auIFRInitial))
  128.             throw new java.lang.Exception ("G2PlusPlus ctr: Invalid Inputs");
  129.     }

  130.     /**
  131.      * Retrieve Sigma
  132.      *
  133.      * @return Sigma
  134.      */

  135.     public double sigma()
  136.     {
  137.         return _dblSigma;
  138.     }

  139.     /**
  140.      * Retrieve A
  141.      *
  142.      * @return A
  143.      */

  144.     public double a()
  145.     {
  146.         return _dblA;
  147.     }

  148.     /**
  149.      * Retrieve Eta
  150.      *
  151.      * @return Eta
  152.      */

  153.     public double eta()
  154.     {
  155.         return _dblEta;
  156.     }

  157.     /**
  158.      * Retrieve B
  159.      *
  160.      * @return B
  161.      */

  162.     public double b()
  163.     {
  164.         return _dblB;
  165.     }

  166.     /**
  167.      * Retrieve the Initial Instantaneous Forward Rate Term Structure
  168.      *
  169.      * @return The Initial Instantaneous Forward Rate Term Structure
  170.      */

  171.     public org.drip.function.definition.R1ToR1 ifrInitialTermStructure()
  172.     {
  173.         return _auIFRInitial;
  174.     }

  175.     /**
  176.      * Retrieve the Random Sequence Generator Array
  177.      *
  178.      * @return The Random Sequence Generator Array
  179.      */

  180.     public org.drip.sequence.random.UnivariateSequenceGenerator[] rsg()
  181.     {
  182.         return _aRSG;
  183.     }

  184.     /**
  185.      * Retrieve Rho
  186.      *
  187.      * @return Rho
  188.      */

  189.     public double rho()
  190.     {
  191.         return _dblRho;
  192.     }

  193.     /**
  194.      * Compute the G2++ Phi
  195.      *
  196.      * @param iSpotDate The Spot Date
  197.      * @param iViewDate The View Date
  198.      *
  199.      * @return The G2++ Phi
  200.      *
  201.      * @throws java.lang.Exception Thrown if the G2++ Phi cannot be computed
  202.      */

  203.     public double phi (
  204.         final int iSpotDate,
  205.         final int iViewDate)
  206.         throws java.lang.Exception
  207.     {
  208.         if (iSpotDate > iViewDate) throw new java.lang.Exception ("G2PlusPlus::phi => Invalid Inputs");

  209.         double dblSpotViewDCF = 1. * (iViewDate - iSpotDate) / 365.25;

  210.         double dblFactor1Phi = _dblSigma / _dblA * (1. - java.lang.Math.exp (-1. * _dblA * dblSpotViewDCF));

  211.         double dblFactor2Phi = _dblEta / _dblB * (1. - java.lang.Math.exp (-1. * _dblB * dblSpotViewDCF));

  212.         return _auIFRInitial.evaluate (iViewDate) + 0.5 * dblFactor1Phi * dblFactor1Phi + 0.5 * dblFactor2Phi
  213.             * dblFactor2Phi;
  214.     }

  215.     /**
  216.      * Compute the X Increment
  217.      *
  218.      * @param iSpotDate The Spot Date
  219.      * @param iViewDate The View Date
  220.      * @param dblX The X Value
  221.      * @param iSpotTimeIncrement The Spot Time Increment
  222.      *
  223.      * @return The X Increment
  224.      *
  225.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  226.      */

  227.     public double deltaX (
  228.         final int iSpotDate,
  229.         final int iViewDate,
  230.         final double dblX,
  231.         final int iSpotTimeIncrement)
  232.         throws java.lang.Exception
  233.     {
  234.         if (iSpotDate > iViewDate || !org.drip.numerical.common.NumberUtil.IsValid (dblX))
  235.             throw new java.lang.Exception ("G2PlusPlus::deltaX => Invalid Inputs");

  236.         double dblAnnualizedIncrement = 1. * iSpotTimeIncrement / 365.25;

  237.         return -1. * _dblA * dblX * dblAnnualizedIncrement + _dblSigma * java.lang.Math.sqrt
  238.             (dblAnnualizedIncrement) * _aRSG[0].random();
  239.     }

  240.     /**
  241.      * Compute the Y Increment
  242.      *
  243.      * @param iSpotDate The Spot Date
  244.      * @param iViewDate The View Date
  245.      * @param dblY The Y Value
  246.      * @param iSpotTimeIncrement The Spot Time Increment
  247.      *
  248.      * @return The Y Increment
  249.      *
  250.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  251.      */

  252.     public double deltaY (
  253.         final int iSpotDate,
  254.         final int iViewDate,
  255.         final double dblY,
  256.         final int iSpotTimeIncrement)
  257.         throws java.lang.Exception
  258.     {
  259.         if (iSpotDate > iViewDate || !org.drip.numerical.common.NumberUtil.IsValid (dblY))
  260.             throw new java.lang.Exception ("G2PlusPlus::deltaY => Invalid Inputs");

  261.         double dblAnnualizedIncrement = 1. * iSpotTimeIncrement / 365.25;

  262.         return -1. * _dblB * dblY * dblAnnualizedIncrement + _dblEta * java.lang.Math.sqrt
  263.             (dblAnnualizedIncrement) * _aRSG[1].random();
  264.     }
  265. }