PointAncillaryMetricsDynamics.java

  1. package org.drip.sample.lmm;

  2. import org.drip.analytics.date.JulianDate;
  3. import org.drip.analytics.definition.MarketSurface;
  4. import org.drip.dynamics.lmm.*;
  5. import org.drip.numerical.common.FormatUtil;
  6. import org.drip.sequence.random.*;
  7. import org.drip.service.env.EnvManager;
  8. import org.drip.spline.basis.PolynomialFunctionSetParams;
  9. import org.drip.spline.params.*;
  10. import org.drip.spline.stretch.MultiSegmentSequenceBuilder;
  11. import org.drip.state.creator.*;
  12. import org.drip.state.discount.*;
  13. import org.drip.state.forward.ForwardCurve;
  14. import org.drip.state.identifier.*;

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

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

  80. /**
  81.  * <i>PointAncillaryMetricsDynamics</i> demonstrates the Construction and Usage of the Point LIBOR State
  82.  * Evolver, and the eventual Evolution of the related Ancillary bDiscount/Forward Latent State Quantification
  83.  * Metrics. The References are:
  84.  *  
  85.  * <br><br>
  86.  *  <ul>
  87.  *      <li>
  88.  *          Goldys, B., M. Musiela, and D. Sondermann (1994): Log-normality of Rates and Term Structure
  89.  *              Models, The University of New South Wales.
  90.  *      </li>
  91.  *      <li>
  92.  *          Musiela, M. (1994): Nominal Annual Rates and Log-normal Volatility Structure, The University of
  93.  *              New South Wales.
  94.  *      </li>
  95.  *      <li>
  96.  *          Brace, A., D. Gatarek, and M. Musiela (1997): The Market Model of Interest Rate Dynamics,
  97.  *              Mathematical Finance 7 (2), 127-155.
  98.  *      </li>
  99.  *  </ul>
  100.  *  
  101.  * <br><br>
  102.  *  <ul>
  103.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/AnalyticsCore.md">Analytics Core Module</a></li>
  104.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/FixedIncomeAnalyticsLibrary.md">Fixed Income Analytics Library</a></li>
  105.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/README.md">Sample</a></li>
  106.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/lmm/README.md">LIBOR Market (LMM-BGM Variant) Evolution</a></li>
  107.  *  </ul>
  108.  * <br><br>
  109.  *
  110.  * @author Lakshmi Krishnamurthy
  111.  */

  112. public class PointAncillaryMetricsDynamics {

  113.     private static final MarketSurface FlatVolatilitySurface (
  114.         final JulianDate dtStart,
  115.         final String strCurrency,
  116.         final double dblFlatVol)
  117.         throws Exception
  118.     {
  119.         return ScenarioMarketSurfaceBuilder.CustomSplineWireSurface (
  120.             "VIEW_TARGET_VOLATILITY_SURFACE",
  121.             dtStart,
  122.             strCurrency,
  123.             new double[] {
  124.                 dtStart.julian(),
  125.                 dtStart.addYears (2).julian(),
  126.                 dtStart.addYears (4).julian(),
  127.                 dtStart.addYears (6).julian(),
  128.                 dtStart.addYears (8).julian(),
  129.                 dtStart.addYears (10).julian()
  130.             },
  131.             new double[] {
  132.                 dtStart.julian(),
  133.                 dtStart.addYears (2).julian(),
  134.                 dtStart.addYears (4).julian(),
  135.                 dtStart.addYears (6).julian(),
  136.                 dtStart.addYears (8).julian(),
  137.                 dtStart.addYears (10).julian()
  138.             },
  139.             new double[][] {
  140.                 {dblFlatVol, dblFlatVol, dblFlatVol, dblFlatVol, dblFlatVol, dblFlatVol},
  141.                 {dblFlatVol, dblFlatVol, dblFlatVol, dblFlatVol, dblFlatVol, dblFlatVol},
  142.                 {dblFlatVol, dblFlatVol, dblFlatVol, dblFlatVol, dblFlatVol, dblFlatVol},
  143.                 {dblFlatVol, dblFlatVol, dblFlatVol, dblFlatVol, dblFlatVol, dblFlatVol},
  144.                 {dblFlatVol, dblFlatVol, dblFlatVol, dblFlatVol, dblFlatVol, dblFlatVol},
  145.                 {dblFlatVol, dblFlatVol, dblFlatVol, dblFlatVol, dblFlatVol, dblFlatVol},
  146.             },
  147.             new SegmentCustomBuilderControl (
  148.                 MultiSegmentSequenceBuilder.BASIS_SPLINE_POLYNOMIAL,
  149.                 new PolynomialFunctionSetParams (4),
  150.                 SegmentInelasticDesignControl.Create (
  151.                     2,
  152.                     2
  153.                 ),
  154.                 null,
  155.                 null
  156.             ),
  157.             new SegmentCustomBuilderControl (
  158.                 MultiSegmentSequenceBuilder.BASIS_SPLINE_POLYNOMIAL,
  159.                 new PolynomialFunctionSetParams (4),
  160.                 SegmentInelasticDesignControl.Create (
  161.                     2,
  162.                     2
  163.                 ),
  164.                 null,
  165.                 null
  166.             )
  167.         );
  168.     }

  169.     private static final LognormalLIBORVolatility LLVInstance (
  170.         final int iSpotDate,
  171.         final ForwardLabel forwardLabel,
  172.         final MarketSurface[] aMS,
  173.         final double[][] aadblCorrelation,
  174.         final int iNumFactor)
  175.         throws Exception
  176.     {
  177.         UnivariateSequenceGenerator[] aUSG = new UnivariateSequenceGenerator[aMS.length];

  178.         for (int i = 0; i < aUSG.length; ++i)
  179.             aUSG[i] = new BoxMullerGaussian (0., 1.);

  180.         return new LognormalLIBORVolatility (
  181.             iSpotDate,
  182.             forwardLabel,
  183.             aMS,
  184.             new PrincipalFactorSequenceGenerator (
  185.                 aUSG,
  186.                 aadblCorrelation,
  187.                 iNumFactor
  188.             )
  189.         );
  190.     }

  191.     private static final void DisplayRunSnap (
  192.         final BGMPointUpdate bgmRunSnap)
  193.         throws Exception
  194.     {
  195.         System.out.println (
  196.             "\t| [" + new JulianDate (bgmRunSnap.evolutionStartDate()) +
  197.             " -> " + new JulianDate (bgmRunSnap.evolutionFinishDate()) +
  198.             "]  => " + FormatUtil.FormatDouble (bgmRunSnap.libor(), 1, 2, 100.) +
  199.             "% | " + FormatUtil.FormatDouble (bgmRunSnap.liborIncrement(), 2, 0, 10000.) +
  200.             " | " + FormatUtil.FormatDouble (bgmRunSnap.instantaneousEffectiveForwardRate(), 1, 2, 100.) +
  201.             "% | " + FormatUtil.FormatDouble (bgmRunSnap.instantaneousNominalForwardRate(), 1, 2, 100.) +
  202.             "% | "
  203.         );
  204.     }

  205.     public static final void main (
  206.         final String[] astrArgs)
  207.         throws Exception
  208.     {
  209.         EnvManager.InitEnv ("");

  210.         String strTenor = "3M";
  211.         String strCurrency = "USD";
  212.         double dblFlatVol1 = 0.35;
  213.         double dblFlatVol2 = 0.42;
  214.         double dblFlatVol3 = 0.27;
  215.         double dblZeroRate = 0.02;
  216.         double dblFlatForwardRate = 0.02;
  217.         int iNumRun = 20;

  218.         int[] aiNumFactor = {
  219.             1, 2, 3
  220.         };

  221.         double[][] aadblCorrelation = new double[][] {
  222.             {1.0, 0.1, 0.2},
  223.             {0.1, 1.0, 0.2},
  224.             {0.2, 0.1, 1.0}
  225.         };

  226.         ForwardLabel forwardLabel = ForwardLabel.Create (
  227.             strCurrency,
  228.             strTenor
  229.         );

  230.         FundingLabel fundingLabel = FundingLabel.Standard (
  231.             strCurrency
  232.         );

  233.         JulianDate dtSpot = org.drip.analytics.date.DateUtil.Today();

  234.         MarketSurface[] aMS = new MarketSurface[] {
  235.             FlatVolatilitySurface (
  236.                 dtSpot,
  237.                 strCurrency,
  238.                 dblFlatVol1
  239.             ),
  240.             FlatVolatilitySurface (
  241.                 dtSpot,
  242.                 strCurrency,
  243.                 dblFlatVol2
  244.             ),
  245.             FlatVolatilitySurface (
  246.                 dtSpot,
  247.                 strCurrency,
  248.                 dblFlatVol3
  249.             )
  250.         };

  251.         ForwardCurve fc = ScenarioForwardCurveBuilder.FlatForwardForwardCurve (
  252.             dtSpot,
  253.             forwardLabel,
  254.             dblFlatForwardRate
  255.         );

  256.         MergedDiscountForwardCurve dc = ScenarioDiscountCurveBuilder.ExponentiallyCompoundedFlatRate (
  257.             dtSpot,
  258.             strCurrency,
  259.             dblZeroRate
  260.         );

  261.         int iSpotDate = dtSpot.julian();

  262.         int iViewDate = dtSpot.addTenor ("1Y").julian();

  263.         int iViewTimeIncrement = 1;

  264.         for (int iNumFactor : aiNumFactor) {
  265.             System.out.println ("\n\n\t|----------------------------------------------------------------------------------------------------------|");

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

  267.             System.out.println ("\t|                             LOG-NORMAL LIBOR EVOLVER                                                     |");

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

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

  270.             System.out.println ("\t|       Num Factors: " + iNumFactor + "                                                                                     |");

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

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

  273.             System.out.println ("\t|       Adjacent Step LIBOR (%)                                                                            |");

  274.             System.out.println ("\t|       Adjacent Step LIBOR Increment (bp)                                                                 |");

  275.             System.out.println ("\t|       Adjacent Step Effective Annual Forward Rate (%)                                                    |");

  276.             System.out.println ("\t|       Adjacent Step Nominal Annual Forward Rate (%)                                                      |");

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

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

  279.             LognormalLIBORPointEvolver lle = new LognormalLIBORPointEvolver (
  280.                 fundingLabel,
  281.                 forwardLabel,
  282.                 LLVInstance (
  283.                     dtSpot.julian(),
  284.                     forwardLabel,
  285.                     aMS,
  286.                     aadblCorrelation,
  287.                     iNumFactor
  288.                 ),
  289.                 fc,
  290.                 dc
  291.             );

  292.             for (int iRun = 0; iRun < iNumRun; ++iRun)
  293.                 DisplayRunSnap (
  294.                     lle.evolve (
  295.                         iSpotDate,
  296.                         iViewDate,
  297.                         iViewTimeIncrement,
  298.                         null
  299.                     )
  300.                 );

  301.             System.out.println ("\t|----------------------------------------------------------------------------------------------------------|");
  302.         }

  303.         EnvManager.TerminateEnv();
  304.     }
  305. }