PathVertexForwardState.java

  1. package org.drip.sample.govviemc;

  2. import org.drip.analytics.date.*;
  3. import org.drip.measure.discrete.SequenceGenerator;
  4. import org.drip.numerical.common.FormatUtil;
  5. import org.drip.service.env.EnvManager;
  6. import org.drip.service.template.LatentMarketStateBuilder;
  7. import org.drip.state.govvie.GovvieCurve;

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

  71. /**
  72.  * <i>PathVertexForwardState</i> demonstrates the Simulations of the Forward Govvie State at Paths and
  73.  * Vertexes.
  74.  *  
  75.  * <br><br>
  76.  *  <ul>
  77.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/AnalyticsCore.md">Analytics Core Module</a></li>
  78.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/FixedIncomeAnalyticsLibrary.md">Fixed Income Analytics Library</a></li>
  79.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/README.md">Sample</a></li>
  80.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/govviemc/README.md">Govvie Curve Monte Carlo Runs</a></li>
  81.  *  </ul>
  82.  * <br><br>
  83.  *
  84.  * @author Lakshmi Krishnamurthy
  85.  */

  86. public class PathVertexForwardState {

  87.     private static final GovvieCurve GovvieCurve (
  88.         final JulianDate dtSpot,
  89.         final String strCode,
  90.         final String[] astrTenor,
  91.         final double[] adblCoupon,
  92.         final double[] adblYield)
  93.         throws Exception
  94.     {
  95.         JulianDate[] adtMaturity = new JulianDate[astrTenor.length];
  96.         JulianDate[] adtEffective = new JulianDate[astrTenor.length];

  97.         for (int i = 0; i < astrTenor.length; ++i) {
  98.             adtEffective[i] = dtSpot;

  99.             adtMaturity[i] = dtSpot.addTenor (astrTenor[i]);
  100.         }

  101.         return LatentMarketStateBuilder.GovvieCurve (
  102.             strCode,
  103.             dtSpot,
  104.             adtEffective,
  105.             adtMaturity,
  106.             adblCoupon,
  107.             adblYield,
  108.             "Yield",
  109.             LatentMarketStateBuilder.SHAPE_PRESERVING
  110.         );
  111.     }

  112.     public static final void main (
  113.         final String[] astrArgs)
  114.         throws Exception
  115.     {
  116.         EnvManager.InitEnv ("");

  117.         JulianDate dtSpot = DateUtil.CreateFromYMD (
  118.             2017,
  119.             DateUtil.MARCH,
  120.             24
  121.         );

  122.         int iNumPath = 50;
  123.         double dblVolatility = 0.10;
  124.         String strTreasuryCode = "UST";

  125.         String[] astrTenor = new String[] {
  126.             "01Y",
  127.             "02Y",
  128.             "03Y",
  129.             "05Y",
  130.             "07Y",
  131.             "10Y",
  132.             "20Y",
  133.             "30Y"
  134.         };

  135.         double[] adblTreasuryCoupon = new double[] {
  136.             0.0100,
  137.             0.0100,
  138.             0.0125,
  139.             0.0150,
  140.             0.0200,
  141.             0.0225,
  142.             0.0250,
  143.             0.0300
  144.         };

  145.         double[] adblTreasuryYield = new double[] {
  146.             0.0083, //  1Y
  147.             0.0122, //  2Y
  148.             0.0149, //  3Y
  149.             0.0193, //  5Y
  150.             0.0227, //  7Y
  151.             0.0248, // 10Y
  152.             0.0280, // 20Y
  153.             0.0308  // 30Y
  154.         };

  155.         int iNumTreasury = adblTreasuryYield.length;
  156.         GovvieCurve[] aGC = new GovvieCurve[iNumPath + 1];

  157.         aGC[0] = GovvieCurve (
  158.             dtSpot,
  159.             strTreasuryCode,
  160.             astrTenor,
  161.             adblTreasuryCoupon,
  162.             adblTreasuryYield
  163.         );

  164.         double[] adblWanderer = SequenceGenerator.Gaussian (iNumPath);

  165.         for (int iPath = 0; iPath < iNumPath; ++iPath) {
  166.             double[] adblPathTreasuryYield = new double[iNumTreasury];
  167.             double dblWanderFactor = (1. + dblVolatility * adblWanderer[iPath]);

  168.             for (int iTreasury = 0; iTreasury < iNumTreasury; ++iTreasury)
  169.                 adblPathTreasuryYield[iTreasury] = adblTreasuryYield[iTreasury] * dblWanderFactor;

  170.             aGC[iPath + 1] = GovvieCurve (
  171.                 dtSpot,
  172.                 strTreasuryCode,
  173.                 astrTenor,
  174.                 adblTreasuryCoupon,
  175.                 adblPathTreasuryYield
  176.             );
  177.         }

  178.         for (int iPath = 0; iPath <= iNumPath; ++iPath) {
  179.             String strDump = "\t[" + FormatUtil.FormatDouble (iPath, 3, 0, 1.) + "] => ";

  180.             for (int iTreasury = 0; iTreasury < iNumTreasury; ++iTreasury)
  181.                 strDump = strDump + FormatUtil.FormatDouble (aGC[iPath].yield (astrTenor[iTreasury]), 1, 3, 100.) + "% |";

  182.             System.out.println (strDump + "|");
  183.         }

  184.         EnvManager.TerminateEnv();
  185.     }
  186. }