PathVertexExerciseOptimal.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.numerical.common.FormatUtil;
  8. import org.drip.param.creator.MarketParamsBuilder;
  9. import org.drip.param.market.CurveSurfaceQuoteContainer;
  10. import org.drip.param.valuation.ValuationParams;
  11. import org.drip.product.creator.BondBuilder;
  12. import org.drip.product.credit.BondComponent;
  13. import org.drip.product.params.EmbeddedOptionSchedule;
  14. import org.drip.service.env.EnvManager;
  15. import org.drip.service.template.LatentMarketStateBuilder;
  16. import org.drip.state.discount.MergedDiscountForwardCurve;
  17. import org.drip.state.govvie.GovvieCurve;
  18. import org.drip.state.sequence.*;

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

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

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

  97. public class PathVertexExerciseOptimal {

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  268.         bond.setEmbeddedCallSchedule (eos);

  269.         return bond;
  270.     }

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

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

  281.         int iNumPath = 50;
  282.         double dblCleanPrice = 1.08641;
  283.         int[] aiExerciseDate = new int[] {
  284.             DateUtil.CreateFromYMD (2019, 12,  1).julian(),
  285.             DateUtil.CreateFromYMD (2020, 12,  1).julian(),
  286.             DateUtil.CreateFromYMD (2021, 12,  1).julian(),
  287.             DateUtil.CreateFromYMD (2022, 12,  1).julian(),
  288.             DateUtil.CreateFromYMD (2023, 12,  1).julian(),
  289.             DateUtil.CreateFromYMD (2024, 12,  1).julian(),
  290.             DateUtil.CreateFromYMD (2025, 12,  1).julian(),
  291.             DateUtil.CreateFromYMD (2026, 12,  1).julian(),
  292.             DateUtil.CreateFromYMD (2027, 12,  1).julian(),
  293.             DateUtil.CreateFromYMD (2028, 12,  1).julian(),
  294.             DateUtil.CreateFromYMD (2029, 12,  1).julian(),
  295.             DateUtil.CreateFromYMD (2030, 12,  1).julian(),
  296.             DateUtil.CreateFromYMD (2031, 12,  1).julian(),
  297.             DateUtil.CreateFromYMD (2032, 12,  1).julian(),
  298.             DateUtil.CreateFromYMD (2033, 12,  1).julian(),
  299.             DateUtil.CreateFromYMD (2034, 12,  1).julian(),
  300.             DateUtil.CreateFromYMD (2035, 12,  1).julian(),
  301.             DateUtil.CreateFromYMD (2036, 12,  1).julian(),
  302.             DateUtil.CreateFromYMD (2037, 12,  1).julian(),
  303.             DateUtil.CreateFromYMD (2038, 12,  1).julian(),
  304.         };
  305.         double[] adblExercisePrice = new double[] {
  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.             1.,
  326.         };

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

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

  343.         PathVertexGovvie mcrg = ScenarioGovvieCurves (
  344.             dtSpot,
  345.             iNumPath,
  346.             iNumVertex
  347.         );

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

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

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

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

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

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

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

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

  391.         System.out.println();

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

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

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

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

  397.             for (int iVertex = 0; iVertex < iNumVertex; ++iVertex) {
  398.                 aadblExercisePV[iPath][iVertex] = (aadblForwardPrice[iPath][iVertex] - adblExercisePrice[iVertex])
  399.                     * mdfc.df (aiExerciseDate[iVertex]);

  400.                 strDump = strDump + " " + FormatUtil.FormatDouble (aadblExercisePV[iPath][iVertex], 2, 0, 100.) + " |";
  401.             }

  402.             System.out.println (strDump + "|");
  403.         }

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

  405.         System.out.println();

  406.         EnvManager.TerminateEnv();
  407.     }
  408. }