PathVertexForwardRealization.java

  1. package org.drip.sample.govviemc;

  2. import java.util.List;

  3. import org.drip.analytics.date.*;
  4. import org.drip.analytics.support.Helper;
  5. import org.drip.measure.crng.RandomNumberGenerator;
  6. import org.drip.measure.discrete.*;
  7. import org.drip.measure.dynamics.DiffusionEvaluatorLogarithmic;
  8. import org.drip.measure.process.DiffusionEvolver;
  9. import org.drip.measure.realization.*;
  10. import org.drip.numerical.common.FormatUtil;
  11. import org.drip.service.env.EnvManager;
  12. import org.drip.service.template.LatentMarketStateBuilder;
  13. import org.drip.state.curve.BasisSplineGovvieYield;
  14. import org.drip.state.govvie.GovvieCurve;
  15. import org.drip.state.nonlinear.FlatForwardGovvieCurve;

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

  79. /**
  80.  * <i>PathVertexForwardRealization</i> demonstrates the Simulations of the Per-Path Forward Vertex Govvie
  81.  * Yield Nodes.
  82.  *  
  83.  * <br><br>
  84.  *  <ul>
  85.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/AnalyticsCore.md">Analytics Core Module</a></li>
  86.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/FixedIncomeAnalyticsLibrary.md">Fixed Income Analytics Library</a></li>
  87.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/README.md">Sample</a></li>
  88.  *      <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>
  89.  *  </ul>
  90.  * <br><br>
  91.  *
  92.  * @author Lakshmi Krishnamurthy
  93.  */

  94. public class PathVertexForwardRealization {

  95.     private static final GovvieCurve GovvieCurve (
  96.         final JulianDate dtSpot,
  97.         final String strCode,
  98.         final String[] astrTenor,
  99.         final double[] adblCoupon,
  100.         final double[] adblYield)
  101.         throws Exception
  102.     {
  103.         JulianDate[] adtMaturity = new JulianDate[astrTenor.length];
  104.         JulianDate[] adtEffective = new JulianDate[astrTenor.length];

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

  107.             adtMaturity[i] = dtSpot.addTenor (astrTenor[i]);
  108.         }

  109.         return LatentMarketStateBuilder.GovvieCurve (
  110.             strCode,
  111.             dtSpot,
  112.             adtEffective,
  113.             adtMaturity,
  114.             adblCoupon,
  115.             adblYield,
  116.             "Yield",
  117.             LatentMarketStateBuilder.SHAPE_PRESERVING
  118.         );
  119.     }

  120.     public static final void main (
  121.         final String[] astrArgs)
  122.         throws Exception
  123.     {
  124.         EnvManager.InitEnv ("");

  125.         JulianDate dtSpot = DateUtil.CreateFromYMD (
  126.             2017,
  127.             DateUtil.MARCH,
  128.             24
  129.         );

  130.         int iNumPath = 50;
  131.         int iNumVertex = 1;
  132.         double dblTimeWidth = 1.0;
  133.         double dblVolatility = 0.10;
  134.         String strTreasuryCode = "UST";

  135.         String[] astrTenor = new String[] {
  136.             "01Y",
  137.             "02Y",
  138.             "03Y",
  139.             "05Y",
  140.             "07Y",
  141.             "10Y",
  142.             "20Y",
  143.             "30Y"
  144.         };

  145.         double[] adblTreasuryCoupon = new double[] {
  146.             0.0100,
  147.             0.0100,
  148.             0.0125,
  149.             0.0150,
  150.             0.0200,
  151.             0.0225,
  152.             0.0250,
  153.             0.0300
  154.         };

  155.         double[] adblTreasuryYield = new double[] {
  156.             0.0083, //  1Y
  157.             0.0122, //  2Y
  158.             0.0149, //  3Y
  159.             0.0193, //  5Y
  160.             0.0227, //  7Y
  161.             0.0248, // 10Y
  162.             0.0280, // 20Y
  163.             0.0308  // 30Y
  164.         };

  165.         BasisSplineGovvieYield bsgyGround = (BasisSplineGovvieYield) GovvieCurve (
  166.             dtSpot,
  167.             strTreasuryCode,
  168.             astrTenor,
  169.             adblTreasuryCoupon,
  170.             adblTreasuryYield
  171.         );

  172.         double[] adblInitialForward = bsgyGround.flatForward (astrTenor).nodeValues();

  173.         DiffusionEvolver de = new DiffusionEvolver (
  174.             DiffusionEvaluatorLogarithmic.Standard (
  175.                 0.,
  176.                 dblVolatility
  177.             )
  178.         );

  179.         int iNumDimension = astrTenor.length;
  180.         DiffusionEvolver[] aDE = new DiffusionEvolver[iNumDimension];
  181.         double[][] aadblCorrelation = new double[iNumDimension][iNumDimension];
  182.         double[][] aadblPathForwardYield = new double[iNumVertex][iNumDimension];

  183.         for (int i = 0; i < iNumDimension; ++i) {
  184.             aDE[i] = de;

  185.             for (int j = 0; j < iNumDimension; ++j)
  186.                 aadblCorrelation[i][j] = i == j ? 1. : 0.;
  187.         }

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

  189.         String strDump = "\t|| TENOR ";

  190.         for (int i = 0; i < iNumDimension; ++i)
  191.             strDump += " |   " + astrTenor[i] + "  ";

  192.         for (int i = 0; i < iNumDimension; ++i)
  193.             strDump += " |   " + astrTenor[i] + "  ";

  194.         System.out.println (strDump + " ||");

  195.         strDump = "\t|| " + FormatUtil.FormatDouble (0, 3, 0, 1.) + " => ";

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

  197.         for (int i = 0; i < iNumDimension; ++i)
  198.             strDump += " " + FormatUtil.FormatDouble (adblInitialForward[i], 1, 3, 100.) + "% |";

  199.         for (int iDimension = 0; iDimension < iNumDimension; ++iDimension)
  200.             strDump = strDump + " " + FormatUtil.FormatDouble (bsgyGround.yield (astrTenor[iDimension]), 1, 3, 100.) + "% |";

  201.         System.out.println (strDump + "|");

  202.         CorrelatedPathVertexDimension cpvd = new CorrelatedPathVertexDimension (
  203.             new RandomNumberGenerator(),
  204.             aadblCorrelation,
  205.             iNumVertex,
  206.             iNumPath,
  207.             false,
  208.             null
  209.         );

  210.         VertexRd[] aVertexRd = cpvd.multiPathVertexRd();

  211.         for (int iPath = 0; iPath < iNumPath; ++iPath) {
  212.             List<double[]> lsVertexRd = aVertexRd[iPath].vertexList();

  213.             JumpDiffusionEdgeUnit[][] aaUR = new JumpDiffusionEdgeUnit[iNumDimension][iNumVertex];
  214.             JumpDiffusionVertex[][] aaJDV = new JumpDiffusionVertex[iNumDimension][iNumVertex + 1];

  215.             for (int iTimeVertex = 0; iTimeVertex < iNumVertex; ++iTimeVertex) {
  216.                 double[] adblRd = lsVertexRd.get (iTimeVertex);

  217.                 for (int iDimension = 0; iDimension < iNumDimension; ++iDimension)
  218.                     aaUR[iDimension][iTimeVertex] = new JumpDiffusionEdgeUnit (
  219.                         dblTimeWidth,                          
  220.                         adblRd[iDimension],
  221.                         0.
  222.                     );
  223.             }

  224.             for (int iDimension = 0; iDimension < iNumDimension; ++iDimension)
  225.                 aaJDV[iDimension] = aDE[iDimension].vertexSequence (
  226.                     new JumpDiffusionVertex (
  227.                         0.,
  228.                         adblInitialForward[iDimension],
  229.                         0.,
  230.                         false
  231.                     ),
  232.                     aaUR[iDimension],
  233.                     dblTimeWidth
  234.                 );

  235.             for (int iTimeVertex = 0; iTimeVertex < iNumVertex; ++iTimeVertex) {
  236.                 strDump = "\t|| " + FormatUtil.FormatDouble (iTimeVertex + 1, 3, 0, 1.) + " => ";

  237.                 for (int iDimension = 0; iDimension < iNumDimension; ++iDimension)
  238.                     strDump = strDump + " " + FormatUtil.FormatDouble (
  239.                         aadblPathForwardYield[iTimeVertex][iDimension] = aaJDV[iDimension][iTimeVertex].value(), 1, 3, 100.
  240.                     ) + "% |";

  241.                 JulianDate dtEvent = dtSpot.addYears (iTimeVertex + 1);

  242.                 FlatForwardGovvieCurve ffgc = new FlatForwardGovvieCurve (
  243.                     dtEvent.julian(),
  244.                     strTreasuryCode,
  245.                     bsgyGround.currency(),
  246.                     Helper.TenorToDate (
  247.                         dtEvent,
  248.                         astrTenor
  249.                     ),
  250.                     aadblPathForwardYield[iTimeVertex]
  251.                 );

  252.                 for (int iDimension = 0; iDimension < iNumDimension; ++iDimension)
  253.                     strDump = strDump + " " + FormatUtil.FormatDouble (ffgc.yield (astrTenor[iDimension]), 1, 3, 100.) + "% |";

  254.                 System.out.println (strDump + "|");
  255.             }
  256.         }

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

  258.         EnvManager.TerminateEnv();
  259.     }
  260. }