G2PlusPlusDynamics.java

  1. package org.drip.sample.hjm;

  2. import org.drip.analytics.date.*;
  3. import org.drip.dynamics.hjm.G2PlusPlus;
  4. import org.drip.function.r1tor1.FlatUnivariate;
  5. import org.drip.numerical.common.FormatUtil;
  6. import org.drip.sequence.random.*;
  7. import org.drip.service.env.EnvManager;

  8. /*
  9.  * -*- mode: java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  10.  */

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

  73. /**
  74.  * <i>G2PlusPlusDynamics</i> demonstrates the Construction and Usage of the G2++ 2-Factor HJM Model Dynamics
  75.  * for the Evolution of the Short Rate.
  76.  *  
  77.  * <br><br>
  78.  *  <ul>
  79.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/AnalyticsCore.md">Analytics Core Module</a></li>
  80.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/FixedIncomeAnalyticsLibrary.md">Fixed Income Analytics Library</a></li>
  81.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/README.md">Sample</a></li>
  82.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/hjm/README.md">Generic HJM Evolution</a></li>
  83.  *  </ul>
  84.  * <br><br>
  85.  *
  86.  * @author Lakshmi Krishnamurthy
  87.  */

  88. public class G2PlusPlusDynamics {

  89.     private static final G2PlusPlus G2PlusPlusEvolver (
  90.         final double dblSigma,
  91.         final double dblA,
  92.         final double dblEta,
  93.         final double dblB,
  94.         final double dblRho,
  95.         final double dblStartingForwardRate)
  96.         throws Exception
  97.     {
  98.         return new G2PlusPlus (
  99.             dblSigma,
  100.             dblA,
  101.             dblEta,
  102.             dblB,
  103.             new UnivariateSequenceGenerator[] {
  104.                 new BoxMullerGaussian (
  105.                     0.,
  106.                     1.
  107.                 ),
  108.                 new BoxMullerGaussian (
  109.                     0.,
  110.                     1.
  111.                 )
  112.             },
  113.             dblRho,
  114.             new FlatUnivariate (dblStartingForwardRate)
  115.         );
  116.     }

  117.     private static final void ShortRateEvolution (
  118.         final G2PlusPlus g2pp,
  119.         final JulianDate dtStart,
  120.         final String strCurrency,
  121.         final String strViewTenor,
  122.         final double dblStartingShortRate)
  123.         throws Exception
  124.     {
  125.         int iDayStep = 2;
  126.         double dblX = 0.;
  127.         double dblY = 0.;
  128.         JulianDate dtSpot = dtStart;
  129.         double dblShortRate = dblStartingShortRate;

  130.         int iStartDate = dtStart.julian();

  131.         int iEndDate = dtStart.addTenor (strViewTenor).julian();

  132.         System.out.println ("\t|-----------------------------------------------------------------------||");

  133.         System.out.println ("\t|                                                                       ||");

  134.         System.out.println ("\t|         G2++ - 2-factor HJM Model - Short Rate Evolution Run          ||");

  135.         System.out.println ("\t|-----------------------------------------------------------------------||");

  136.         System.out.println ("\t|                                                                       ||");

  137.         System.out.println ("\t|    L->R:                                                              ||");

  138.         System.out.println ("\t|        Date                                                           ||");

  139.         System.out.println ("\t|        X (%)                                                          ||");

  140.         System.out.println ("\t|        X - Increment (%)                                              ||");

  141.         System.out.println ("\t|        Y (%)                                                          ||");

  142.         System.out.println ("\t|        Y - Increment (%)                                              ||");

  143.         System.out.println ("\t|        Phi (%)                                                        ||");

  144.         System.out.println ("\t|        Short Rate (%)                                                 ||");

  145.         System.out.println ("\t|-----------------------------------------------------------------------||");

  146.         while (dtSpot.julian() < iEndDate) {
  147.             int iSpotDate = dtSpot.julian();

  148.             double dblDeltaX = g2pp.deltaX (
  149.                 iStartDate,
  150.                 iSpotDate,
  151.                 dblX,
  152.                 iDayStep
  153.             );

  154.             dblX += dblDeltaX;

  155.             double dblDeltaY = g2pp.deltaY (
  156.                 iStartDate,
  157.                 iSpotDate,
  158.                 dblY,
  159.                 iDayStep
  160.             );

  161.             dblY += dblDeltaY;

  162.             double dblPhi = g2pp.phi (
  163.                 iStartDate,
  164.                 iSpotDate
  165.             );

  166.             dblShortRate = dblX + dblY + dblPhi;

  167.             System.out.println ("\t| [" + dtSpot + "] = " +
  168.                 FormatUtil.FormatDouble (dblX, 1, 2, 100.) + "% | " +
  169.                 FormatUtil.FormatDouble (dblDeltaX, 1, 2, 100.) + "% || " +
  170.                 FormatUtil.FormatDouble (dblY, 1, 2, 100.) + "% | " +
  171.                 FormatUtil.FormatDouble (dblDeltaY, 1, 2, 100.) + "% || " +
  172.                 FormatUtil.FormatDouble (dblPhi, 1, 2, 100.) + "% || " +
  173.                 FormatUtil.FormatDouble (dblShortRate, 1, 2, 100.) + "% || "
  174.             );

  175.             dtSpot = dtSpot.addBusDays (
  176.                 iDayStep,
  177.                 strCurrency
  178.             );
  179.         }

  180.         System.out.println ("\t|-----------------------------------------------------------------------||");
  181.     }

  182.     public static final void main (
  183.         final String[] astrArgs)
  184.         throws Exception
  185.     {
  186.         EnvManager.InitEnv ("");

  187.         JulianDate dtSpot = DateUtil.Today();

  188.         String strCurrency = "USD";
  189.         double dblStartingShortRate = 0.05;
  190.         double dblSigma = 0.05;
  191.         double dblA = 0.5;
  192.         double dblEta = 0.05;
  193.         double dblB = 0.5;
  194.         double dblRho = 0.5;

  195.         G2PlusPlus g2pp = G2PlusPlusEvolver (
  196.             dblSigma,
  197.             dblA,
  198.             dblEta,
  199.             dblB,
  200.             dblRho,
  201.             dblStartingShortRate
  202.         );

  203.         ShortRateEvolution (
  204.             g2pp,
  205.             dtSpot,
  206.             strCurrency,
  207.             "4M",
  208.             dblStartingShortRate
  209.         );

  210.         EnvManager.TerminateEnv();
  211.     }
  212. }