PrincipalComponentQMDynamics.java

  1. package org.drip.sample.hjm;

  2. import org.drip.analytics.date.*;
  3. import org.drip.analytics.definition.MarketSurface;
  4. import org.drip.analytics.support.Helper;
  5. import org.drip.dynamics.hjm.*;
  6. import org.drip.function.definition.R1ToR1;
  7. import org.drip.function.r1tor1.FlatUnivariate;
  8. import org.drip.numerical.common.FormatUtil;
  9. import org.drip.sequence.random.*;
  10. import org.drip.service.env.EnvManager;
  11. import org.drip.spline.basis.PolynomialFunctionSetParams;
  12. import org.drip.spline.params.*;
  13. import org.drip.spline.stretch.MultiSegmentSequenceBuilder;
  14. import org.drip.state.creator.ScenarioMarketSurfaceBuilder;
  15. import org.drip.state.identifier.*;

  16. /*
  17.  * -*- mode: java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  18.  */

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

  81. /**
  82.  * <i>PrincipalComponentQMDynamics</i> demonstrates the Construction and Usage of the Principal Component
  83.  * Based Gaussian Model Dynamics for the Evolution of the Discount Factor Quantification Metrics - the
  84.  * Instantaneous Forward Rate, the LIBOR Forward Rate, the Shifted LIBOR Forward Rate, the Short Rate, the
  85.  * Compounded Short Rate, and the Price.
  86.  *  
  87.  * <br><br>
  88.  *  <ul>
  89.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/AnalyticsCore.md">Analytics Core Module</a></li>
  90.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/FixedIncomeAnalyticsLibrary.md">Fixed Income Analytics Library</a></li>
  91.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/README.md">Sample</a></li>
  92.  *      <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>
  93.  *  </ul>
  94.  * <br><br>
  95.  *
  96.  * @author Lakshmi Krishnamurthy
  97.  */

  98. public class PrincipalComponentQMDynamics {

  99.     private static final MarketSurface FlatVolatilitySurface (
  100.         final JulianDate dtStart,
  101.         final String strCurrency,
  102.         final double dblFlatVol)
  103.         throws Exception
  104.     {
  105.         return ScenarioMarketSurfaceBuilder.CustomSplineWireSurface (
  106.             "VIEW_TARGET_VOLATILITY_SURFACE",
  107.             dtStart,
  108.             strCurrency,
  109.             new double[] {
  110.                 dtStart.julian(),
  111.                 dtStart.addYears (2).julian(),
  112.                 dtStart.addYears (4).julian(),
  113.                 dtStart.addYears (6).julian(),
  114.                 dtStart.addYears (8).julian(),
  115.                 dtStart.addYears (10).julian()
  116.             },
  117.             new double[] {
  118.                 dtStart.julian(),
  119.                 dtStart.addYears (2).julian(),
  120.                 dtStart.addYears (4).julian(),
  121.                 dtStart.addYears (6).julian(),
  122.                 dtStart.addYears (8).julian(),
  123.                 dtStart.addYears (10).julian()
  124.             },
  125.             new double[][] {
  126.                 {dblFlatVol, dblFlatVol, dblFlatVol, dblFlatVol, dblFlatVol, dblFlatVol},
  127.                 {dblFlatVol, dblFlatVol, dblFlatVol, dblFlatVol, dblFlatVol, dblFlatVol},
  128.                 {dblFlatVol, dblFlatVol, dblFlatVol, dblFlatVol, dblFlatVol, dblFlatVol},
  129.                 {dblFlatVol, dblFlatVol, dblFlatVol, dblFlatVol, dblFlatVol, dblFlatVol},
  130.                 {dblFlatVol, dblFlatVol, dblFlatVol, dblFlatVol, dblFlatVol, dblFlatVol},
  131.                 {dblFlatVol, dblFlatVol, dblFlatVol, dblFlatVol, dblFlatVol, dblFlatVol},
  132.             },
  133.             new SegmentCustomBuilderControl (
  134.                 MultiSegmentSequenceBuilder.BASIS_SPLINE_POLYNOMIAL,
  135.                 new PolynomialFunctionSetParams (4),
  136.                 SegmentInelasticDesignControl.Create (
  137.                     2,
  138.                     2
  139.                 ),
  140.                 null,
  141.                 null
  142.             ),
  143.             new SegmentCustomBuilderControl (
  144.                 MultiSegmentSequenceBuilder.BASIS_SPLINE_POLYNOMIAL,
  145.                 new PolynomialFunctionSetParams (4),
  146.                 SegmentInelasticDesignControl.Create (
  147.                     2,
  148.                     2
  149.                 ),
  150.                 null,
  151.                 null
  152.             )
  153.         );
  154.     }

  155.     private static final MultiFactorStateEvolver HJMInstance (
  156.         final JulianDate dtStart,
  157.         final String strCurrency,
  158.         final String strTenor,
  159.         final MarketSurface mktSurfFlatVol1,
  160.         final MarketSurface mktSurfFlatVol2,
  161.         final MarketSurface mktSurfFlatVol3,
  162.         final R1ToR1 auForwardRate,
  163.         final int iNumFactor)
  164.         throws Exception
  165.     {
  166.         MultiFactorVolatility mfv = new MultiFactorVolatility (
  167.             new MarketSurface[] {
  168.                 mktSurfFlatVol1,
  169.                 mktSurfFlatVol2,
  170.                 mktSurfFlatVol3
  171.             },
  172.             new PrincipalFactorSequenceGenerator (
  173.                 new UnivariateSequenceGenerator[] {
  174.                     new BoxMullerGaussian (
  175.                         0.,
  176.                         1.
  177.                     ),
  178.                     new BoxMullerGaussian (
  179.                         0.,
  180.                         1.
  181.                     ),
  182.                     new BoxMullerGaussian (
  183.                         0.,
  184.                         1.
  185.                     )
  186.                 },
  187.                 new double[][] {
  188.                     {1.0, 0.1, 0.2},
  189.                     {0.1, 1.0, 0.2},
  190.                     {0.2, 0.1, 1.0}
  191.                 },
  192.                 iNumFactor
  193.             )
  194.         );

  195.         return new MultiFactorStateEvolver (
  196.             FundingLabel.Standard (strCurrency),
  197.             ForwardLabel.Create (
  198.                 strCurrency,
  199.                 strTenor
  200.             ),
  201.             mfv,
  202.             auForwardRate
  203.         );
  204.     }

  205.     private static final ShortForwardRateUpdate InitQMSnap (
  206.         final JulianDate dtStart,
  207.         final String strCurrency,
  208.         final String strViewTenor,
  209.         final String strTenor,
  210.         final double dblInitialForwardRate,
  211.         final double dblInitialPrice)
  212.         throws Exception
  213.     {
  214.         return ShortForwardRateUpdate.Create (
  215.             FundingLabel.Standard (strCurrency),
  216.             ForwardLabel.Create (
  217.                 strCurrency,
  218.                 strTenor
  219.             ),
  220.             dtStart.julian(),
  221.             dtStart.julian(),
  222.             dtStart.addTenor (strViewTenor).julian(),
  223.             dblInitialForwardRate,
  224.             0.,
  225.             dblInitialForwardRate,
  226.             0.,
  227.             dblInitialForwardRate + (365.25 / Helper.TenorToDays (strTenor)),
  228.             0.,
  229.             dblInitialForwardRate,
  230.             0.,
  231.             dblInitialForwardRate,
  232.             0.,
  233.             dblInitialPrice,
  234.             0.
  235.         );
  236.     }

  237.     private static final void QMEvolution (
  238.         final MultiFactorStateEvolver hjm,
  239.         final JulianDate dtStart,
  240.         final String strCurrency,
  241.         final String strViewTenor,
  242.         final ShortForwardRateUpdate qmInitial)
  243.         throws Exception
  244.     {
  245.         int iViewDate = dtStart.addTenor (strViewTenor).julian();

  246.         int iDayStep = 2;
  247.         ShortForwardRateUpdate qm = qmInitial;
  248.         JulianDate dtSpot = dtStart;

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

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

  251.         System.out.println ("\t|    Multi-Factor PCA-Based Gaussian HJM Quantification Metric Run                                                              ||");

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

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

  254.         System.out.println ("\t|        Number of Prinicipal Components: " + hjm.mfv().msg().numFactor() + "                                                                                     ||");

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

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

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

  258.         System.out.println ("\t|            Instantaneous Forward Rate (%)                                                                                     ||");

  259.         System.out.println ("\t|            Instantaneous Forward Rate - Change (%)                                                                            ||");

  260.         System.out.println ("\t|            LIBOR Forward Rate (%)                                                                                             ||");

  261.         System.out.println ("\t|            LIBOR Forward Rate - Change (%)                                                                                    ||");

  262.         System.out.println ("\t|            Shifted LIBOR Forward Rate (%)                                                                                     ||");

  263.         System.out.println ("\t|            Shifted LIBOR Forward Rate - Change (%)                                                                            ||");

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

  265.         System.out.println ("\t|            Short Rate - Change (%)                                                                                            ||");

  266.         System.out.println ("\t|            Continuously Compounded Short Rate (%)                                                                             ||");

  267.         System.out.println ("\t|            Continuously Compounded Short Rate - Change (%)                                                                    ||");

  268.         System.out.println ("\t|            Price                                                                                                              ||");

  269.         System.out.println ("\t|            Price - Change                                                                                                     ||");

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

  271.         while (dtSpot.julian() < iViewDate) {
  272.             int iSpotDate = dtSpot.julian();

  273.             qm = (ShortForwardRateUpdate) hjm.evolve (
  274.                 iSpotDate,
  275.                 iViewDate,
  276.                 iDayStep,
  277.                 qm
  278.             );

  279.             System.out.println ("\t| [" + dtSpot + "] = " +
  280.                 FormatUtil.FormatDouble (qm.instantaneousForwardRate(), 1, 2, 100.) + "% | " +
  281.                 FormatUtil.FormatDouble (qm.instantaneousForwardRateIncrement(), 1, 2, 100.) + "% || " +
  282.                 FormatUtil.FormatDouble (qm.liborForwardRate(), 1, 2, 100.) + "% | " +
  283.                 FormatUtil.FormatDouble (qm.liborForwardRateIncrement(), 1, 2, 100.) + "% || " +
  284.                 FormatUtil.FormatDouble (qm.shiftedLIBORForwardRate(), 1, 4, 1.) + " | " +
  285.                 FormatUtil.FormatDouble (qm.shiftedLIBORForwardRateIncrement(), 1, 2, 100.) + "% || " +
  286.                 FormatUtil.FormatDouble (qm.shortRate(), 1, 2, 100.) + "% | " +
  287.                 FormatUtil.FormatDouble (qm.shortRateIncrement(), 1, 2, 100.) + "% || " +
  288.                 FormatUtil.FormatDouble (qm.compoundedShortRate(), 1, 2, 100.) + "% | " +
  289.                 FormatUtil.FormatDouble (qm.compoundedShortRateIncrement(), 1, 2, 100.) + "% || " +
  290.                 FormatUtil.FormatDouble (qm.price(), 2, 2, 100.) + " | " +
  291.                 FormatUtil.FormatDouble (qm.priceIncrement(), 1, 2, 100.) + " || "
  292.             );

  293.             dtSpot = dtSpot.addBusDays (
  294.                 iDayStep,
  295.                 strCurrency
  296.             );
  297.         }

  298.         System.out.println ("\t|-------------------------------------------------------------------------------------------------------------------------------||");
  299.     }

  300.     public static final void main (
  301.         final String[] astrArgs)
  302.         throws Exception
  303.     {
  304.         EnvManager.InitEnv ("");

  305.         String strCurrency = "USD";
  306.         double dblFlatVol1 = 0.007;
  307.         double dblFlatVol2 = 0.009;
  308.         double dblFlatVol3 = 0.004;
  309.         double dblFlatForwardRate = 0.05;
  310.         double dblInitialPrice = 0.9875;
  311.         String strViewTenor = "3M";
  312.         String strTenor = "3M";

  313.         JulianDate dtSpot = DateUtil.Today();

  314.         MarketSurface mktSurfFlatVol1 = FlatVolatilitySurface (
  315.             dtSpot,
  316.             strCurrency,
  317.             dblFlatVol1
  318.         );

  319.         MarketSurface mktSurfFlatVol2 = FlatVolatilitySurface (
  320.             dtSpot,
  321.             strCurrency,
  322.             dblFlatVol2
  323.         );

  324.         MarketSurface mktSurfFlatVol3 = FlatVolatilitySurface (
  325.             dtSpot,
  326.             strCurrency,
  327.             dblFlatVol3
  328.         );

  329.         ShortForwardRateUpdate qmInitial = InitQMSnap (
  330.             dtSpot,
  331.             strCurrency,
  332.             strViewTenor,
  333.             strTenor,
  334.             dblFlatForwardRate,
  335.             dblInitialPrice
  336.         );

  337.         int[] aiNumFactor = new int[] {
  338.             1, 2, 3
  339.         };

  340.         for (int iNumFactor : aiNumFactor) {
  341.             MultiFactorStateEvolver hjm = HJMInstance (
  342.                 dtSpot,
  343.                 strCurrency,
  344.                 strTenor,
  345.                 mktSurfFlatVol1,
  346.                 mktSurfFlatVol2,
  347.                 mktSurfFlatVol3,
  348.                 new FlatUnivariate (dblFlatForwardRate),
  349.                 iNumFactor
  350.             );

  351.             QMEvolution (
  352.                 hjm,
  353.                 dtSpot,
  354.                 strCurrency,
  355.                 strViewTenor,
  356.                 qmInitial
  357.             );
  358.         }

  359.         EnvManager.TerminateEnv();
  360.     }
  361. }