PathVertexExerciseIndicator.java

  1. package org.drip.sample.govviemc;

  2. import org.drip.analytics.date.*;
  3. import org.drip.measure.crng.RandomNumberGenerator;
  4. import org.drip.measure.discrete.CorrelatedPathVertexDimension;
  5. import org.drip.measure.dynamics.DiffusionEvaluatorLogarithmic;
  6. import org.drip.measure.process.DiffusionEvolver;
  7. import org.drip.param.creator.MarketParamsBuilder;
  8. import org.drip.param.market.CurveSurfaceQuoteContainer;
  9. import org.drip.param.valuation.ValuationParams;
  10. import org.drip.product.creator.BondBuilder;
  11. import org.drip.product.credit.BondComponent;
  12. import org.drip.product.params.EmbeddedOptionSchedule;
  13. import org.drip.service.env.EnvManager;
  14. import org.drip.service.template.LatentMarketStateBuilder;
  15. import org.drip.state.discount.MergedDiscountForwardCurve;
  16. import org.drip.state.govvie.GovvieCurve;
  17. import org.drip.state.sequence.*;

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

  21. /*!
  22.  * Copyright (C) 2019 Lakshmi Krishnamurthy
  23.  * Copyright (C) 2018 Lakshmi Krishnamurthy
  24.  * Copyright (C) 2017 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>PathVertexExerciseIndicator</i> demonstrates the Simulations of the Per-Path Callable Bond Forward
  83.  * Price Based Exercise Indicator.
  84.  *  
  85.  * <br><br>
  86.  *  <ul>
  87.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/AnalyticsCore.md">Analytics Core Module</a></li>
  88.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/FixedIncomeAnalyticsLibrary.md">Fixed Income Analytics Library</a></li>
  89.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/README.md">Sample</a></li>
  90.  *      <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>
  91.  *  </ul>
  92.  * <br><br>
  93.  *
  94.  * @author Lakshmi Krishnamurthy
  95.  */

  96. public class PathVertexExerciseIndicator {

  97.     private static final MergedDiscountForwardCurve FundingCurve (
  98.         final JulianDate dtSpot,
  99.         final String strCurrency,
  100.         final double dblBump)
  101.         throws Exception
  102.     {
  103.         String[] astrDepositMaturityTenor = new String[] {
  104.             "2D"
  105.         };

  106.         double[] adblDepositQuote = new double[] {
  107.             0.0111956 + dblBump // 2D
  108.         };

  109.         double[] adblFuturesQuote = new double[] {
  110.             0.011375 + dblBump, // 98.8625
  111.             0.013350 + dblBump, // 98.6650
  112.             0.014800 + dblBump, // 98.5200
  113.             0.016450 + dblBump, // 98.3550
  114.             0.017850 + dblBump, // 98.2150
  115.             0.019300 + dblBump  // 98.0700
  116.         };

  117.         String[] astrFixFloatMaturityTenor = new String[] {
  118.             "02Y",
  119.             "03Y",
  120.             "04Y",
  121.             "05Y",
  122.             "06Y",
  123.             "07Y",
  124.             "08Y",
  125.             "09Y",
  126.             "10Y",
  127.             "11Y",
  128.             "12Y",
  129.             "15Y",
  130.             "20Y",
  131.             "25Y",
  132.             "30Y",
  133.             "40Y",
  134.             "50Y"
  135.         };

  136.         double[] adblFixFloatQuote = new double[] {
  137.             0.017029 + dblBump, //  2Y
  138.             0.019354 + dblBump, //  3Y
  139.             0.021044 + dblBump, //  4Y
  140.             0.022291 + dblBump, //  5Y
  141.             0.023240 + dblBump, //  6Y
  142.             0.024025 + dblBump, //  7Y
  143.             0.024683 + dblBump, //  8Y
  144.             0.025243 + dblBump, //  9Y
  145.             0.025720 + dblBump, // 10Y
  146.             0.026130 + dblBump, // 11Y
  147.             0.026495 + dblBump, // 12Y
  148.             0.027230 + dblBump, // 15Y
  149.             0.027855 + dblBump, // 20Y
  150.             0.028025 + dblBump, // 25Y
  151.             0.028028 + dblBump, // 30Y
  152.             0.027902 + dblBump, // 40Y
  153.             0.027655 + dblBump  // 50Y
  154.         };

  155.         return LatentMarketStateBuilder.SmoothFundingCurve (
  156.             dtSpot,
  157.             strCurrency,
  158.             astrDepositMaturityTenor,
  159.             adblDepositQuote,
  160.             "ForwardRate",
  161.             adblFuturesQuote,
  162.             "ForwardRate",
  163.             astrFixFloatMaturityTenor,
  164.             adblFixFloatQuote,
  165.             "SwapRate"
  166.         );
  167.     }

  168.     private static final PathVertexGovvie ScenarioGovvieCurves (
  169.         final JulianDate dtSpot,
  170.         final int iNumPath,
  171.         final int iNumVertex)
  172.         throws Exception
  173.     {
  174.         double dblVolatility = 0.10;
  175.         String strTreasuryCode = "UST";

  176.         String[] astrTenor = new String[] {
  177.             "01Y",
  178.             "02Y",
  179.             "03Y",
  180.             "05Y",
  181.             "07Y",
  182.             "10Y",
  183.             "20Y",
  184.             "30Y"
  185.         };

  186.         double[] adblTreasuryCoupon = new double[] {
  187.             0.0100,
  188.             0.0100,
  189.             0.0125,
  190.             0.0150,
  191.             0.0200,
  192.             0.0225,
  193.             0.0250,
  194.             0.0300
  195.         };

  196.         double[] adblTreasuryYield = new double[] {
  197.             0.0083, //  1Y
  198.             0.0122, //  2Y
  199.             0.0149, //  3Y
  200.             0.0193, //  5Y
  201.             0.0227, //  7Y
  202.             0.0248, // 10Y
  203.             0.0280, // 20Y
  204.             0.0308  // 30Y
  205.         };

  206.         int iNumDimension = astrTenor.length;
  207.         double[][] aadblCorrelation = new double[iNumDimension][iNumDimension];

  208.         for (int i = 0; i < iNumDimension; ++i) {
  209.             for (int j = 0; j < iNumDimension; ++j)
  210.                 aadblCorrelation[i][j] = i == j ? 1. : 0.;
  211.         }

  212.         GovvieBuilderSettings gbs = new GovvieBuilderSettings (
  213.             dtSpot,
  214.             strTreasuryCode,
  215.             astrTenor,
  216.             adblTreasuryCoupon,
  217.             adblTreasuryYield
  218.         );

  219.         return PathVertexGovvie.Standard (
  220.             gbs,
  221.             new CorrelatedPathVertexDimension (
  222.                 new RandomNumberGenerator(),
  223.                 aadblCorrelation,
  224.                 iNumVertex,
  225.                 iNumPath,
  226.                 false,
  227.                 null
  228.             ),
  229.             new DiffusionEvolver (
  230.                 DiffusionEvaluatorLogarithmic.Standard (
  231.                     0.,
  232.                     dblVolatility
  233.                 )
  234.             )
  235.         );
  236.     }

  237.     private static final BondComponent Callable (
  238.         final EmbeddedOptionSchedule eos)
  239.         throws Exception
  240.     {
  241.         JulianDate dtEffective = DateUtil.CreateFromYMD (
  242.             2009,
  243.             12,
  244.             3
  245.         );

  246.         JulianDate dtMaturity  = DateUtil.CreateFromYMD (
  247.             2039,
  248.             12,
  249.             1
  250.         );

  251.         double dblCoupon = 0.06558;
  252.         int iFreq = 2;
  253.         String strCUSIP = "033177XV3";
  254.         String strDayCount = "30/360";

  255.         BondComponent bond = BondBuilder.CreateSimpleFixed (
  256.             strCUSIP,
  257.             "USD",
  258.             "",
  259.             dblCoupon,
  260.             iFreq,
  261.             strDayCount,
  262.             dtEffective,
  263.             dtMaturity,
  264.             null,
  265.             null
  266.         );

  267.         bond.setEmbeddedCallSchedule (eos);

  268.         return bond;
  269.     }

  270.     public static final void main (
  271.         final String[] astrArgs)
  272.         throws Exception
  273.     {
  274.         EnvManager.InitEnv ("");

  275.         JulianDate dtSpot = DateUtil.CreateFromYMD (
  276.             2017,
  277.             DateUtil.MARCH,
  278.             24
  279.         );

  280.         int iNumPath = 50;
  281.         double dblCleanPrice = 1.08641;
  282.         int[] aiExerciseDate = new int[] {
  283.             DateUtil.CreateFromYMD (2019, 12,  1).julian(),
  284.             DateUtil.CreateFromYMD (2020, 12,  1).julian(),
  285.             DateUtil.CreateFromYMD (2021, 12,  1).julian(),
  286.             DateUtil.CreateFromYMD (2022, 12,  1).julian(),
  287.             DateUtil.CreateFromYMD (2023, 12,  1).julian(),
  288.             DateUtil.CreateFromYMD (2024, 12,  1).julian(),
  289.             DateUtil.CreateFromYMD (2025, 12,  1).julian(),
  290.             DateUtil.CreateFromYMD (2026, 12,  1).julian(),
  291.             DateUtil.CreateFromYMD (2027, 12,  1).julian(),
  292.             DateUtil.CreateFromYMD (2028, 12,  1).julian(),
  293.             DateUtil.CreateFromYMD (2029, 12,  1).julian(),
  294.             DateUtil.CreateFromYMD (2030, 12,  1).julian(),
  295.             DateUtil.CreateFromYMD (2031, 12,  1).julian(),
  296.             DateUtil.CreateFromYMD (2032, 12,  1).julian(),
  297.             DateUtil.CreateFromYMD (2033, 12,  1).julian(),
  298.             DateUtil.CreateFromYMD (2034, 12,  1).julian(),
  299.             DateUtil.CreateFromYMD (2035, 12,  1).julian(),
  300.             DateUtil.CreateFromYMD (2036, 12,  1).julian(),
  301.             DateUtil.CreateFromYMD (2037, 12,  1).julian(),
  302.             DateUtil.CreateFromYMD (2038, 12,  1).julian(),
  303.         };
  304.         double[] adblExercisePrice = new double[] {
  305.             1.,
  306.             1.,
  307.             1.,
  308.             1.,
  309.             1.,
  310.             1.,
  311.             1.,
  312.             1.,
  313.             1.,
  314.             1.,
  315.             1.,
  316.             1.,
  317.             1.,
  318.             1.,
  319.             1.,
  320.             1.,
  321.             1.,
  322.             1.,
  323.             1.,
  324.             1.,
  325.         };

  326.         int iNumVertex = aiExerciseDate.length;
  327.         double[][] aadblForwardPrice = new double[iNumPath][iNumVertex];
  328.         ValuationParams[] aValParamsEvent = new ValuationParams[iNumVertex];

  329.         BondComponent bond = Callable (
  330.             new EmbeddedOptionSchedule (
  331.                 aiExerciseDate,
  332.                 adblExercisePrice,
  333.                 false,
  334.                 30,
  335.                 false,
  336.                 Double.NaN,
  337.                 "",
  338.                 Double.NaN
  339.             )
  340.         );

  341.         PathVertexGovvie mcrg = ScenarioGovvieCurves (
  342.             dtSpot,
  343.             iNumPath,
  344.             iNumVertex
  345.         );

  346.         GovvieCurve[][] aaGCPathEvent = mcrg.pathVertex (aiExerciseDate);

  347.         MergedDiscountForwardCurve mdfc = FundingCurve (
  348.             dtSpot,
  349.             "USD",
  350.             0.
  351.         );

  352.         CurveSurfaceQuoteContainer csqcBase = MarketParamsBuilder.Create (
  353.             mdfc,
  354.             mcrg.govvieBuilderSettings().groundState(),
  355.             null,
  356.             null,
  357.             null,
  358.             null,
  359.             null
  360.         );

  361.         ValuationParams valParamsSpot = ValuationParams.Spot (dtSpot.julian());

  362.         double dblOASSpot = bond.oasFromPrice (
  363.             valParamsSpot,
  364.             csqcBase,
  365.             null,
  366.             dblCleanPrice
  367.         );

  368.         for (int iVertex = 0; iVertex < iNumVertex; ++iVertex)
  369.             aValParamsEvent[iVertex] = ValuationParams.Spot (aiExerciseDate[iVertex]);

  370.         for (int iPath = 0; iPath < iNumPath; ++iPath) {
  371.             for (int iVertex = 0; iVertex < iNumVertex; ++iVertex) {
  372.                 CurveSurfaceQuoteContainer csqcEvent = MarketParamsBuilder.Create (
  373.                     mdfc,
  374.                     aaGCPathEvent[iPath][iVertex],
  375.                     null,
  376.                     null,
  377.                     null,
  378.                     null,
  379.                     null
  380.                 );

  381.                 aadblForwardPrice[iPath][iVertex] = bond.priceFromOAS (
  382.                     aValParamsEvent[iVertex],
  383.                     csqcEvent,
  384.                     null,
  385.                     dblOASSpot
  386.                 );
  387.             }
  388.         }

  389.         System.out.println();

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

  391.         System.out.println ("\t||                          FORWARD EXERCISE INDICATOR                           ||");

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

  393.         for (int iPath = 0; iPath < iNumPath; ++iPath) {
  394.             String strDump = "\t||";

  395.             for (int iVertex = 0; iVertex < iNumVertex; ++iVertex)
  396.                 strDump = strDump + (aadblForwardPrice[iPath][iVertex] > adblExercisePrice[iVertex] ? " Y" : " N") + " |";

  397.             System.out.println (strDump + "|");
  398.         }

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

  400.         System.out.println();

  401.         EnvManager.TerminateEnv();
  402.     }
  403. }