MultiCallMonteCarlo.java

  1. package org.drip.sample.bond;

  2. import org.drip.analytics.date.*;
  3. import org.drip.analytics.output.BondEOSMetrics;
  4. import org.drip.measure.dynamics.DiffusionEvaluatorLogarithmic;
  5. import org.drip.measure.process.DiffusionEvolver;
  6. import org.drip.numerical.common.FormatUtil;
  7. import org.drip.param.creator.MarketParamsBuilder;
  8. import org.drip.param.valuation.ValuationParams;
  9. import org.drip.product.creator.BondBuilder;
  10. import org.drip.product.credit.BondComponent;
  11. import org.drip.product.params.EmbeddedOptionSchedule;
  12. import org.drip.service.env.EnvManager;
  13. import org.drip.service.template.LatentMarketStateBuilder;
  14. import org.drip.state.discount.MergedDiscountForwardCurve;
  15. import org.drip.state.sequence.GovvieBuilderSettings;

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

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

  90. /**
  91.  * <i>MultiCallMonteCarlo</i> demonstrates the Simulations of the Path/Vertex EOS Bond Metrics.
  92.  *  
  93.  * <br><br>
  94.  *  <ul>
  95.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ProductCore.md">Product Core Module</a></li>
  96.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/FixedIncomeAnalyticsLibrary.md">Fixed Income Analytics</a></li>
  97.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/README.md">DROP API Construction and Usage</a></li>
  98.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/bond/README.md">Bullet, EOS Bond Metrics + Curve</a></li>
  99.  *  </ul>
  100.  * <br><br>
  101.  *
  102.  * @author Lakshmi Krishnamurthy
  103.  */

  104. public class MultiCallMonteCarlo {

  105.     private static final MergedDiscountForwardCurve FundingCurve (
  106.         final JulianDate dtSpot,
  107.         final String strCurrency,
  108.         final double dblBump)
  109.         throws Exception
  110.     {
  111.         String[] astrDepositMaturityTenor = new String[] {
  112.             "2D"
  113.         };

  114.         double[] adblDepositQuote = new double[] {
  115.             0.0111956 + dblBump // 2D
  116.         };

  117.         double[] adblFuturesQuote = new double[] {
  118.             0.011375 + dblBump, // 98.8625
  119.             0.013350 + dblBump, // 98.6650
  120.             0.014800 + dblBump, // 98.5200
  121.             0.016450 + dblBump, // 98.3550
  122.             0.017850 + dblBump, // 98.2150
  123.             0.019300 + dblBump  // 98.0700
  124.         };

  125.         String[] astrFixFloatMaturityTenor = new String[] {
  126.             "02Y",
  127.             "03Y",
  128.             "04Y",
  129.             "05Y",
  130.             "06Y",
  131.             "07Y",
  132.             "08Y",
  133.             "09Y",
  134.             "10Y",
  135.             "11Y",
  136.             "12Y",
  137.             "15Y",
  138.             "20Y",
  139.             "25Y",
  140.             "30Y",
  141.             "40Y",
  142.             "50Y"
  143.         };

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

  163.         return LatentMarketStateBuilder.SmoothFundingCurve (
  164.             dtSpot,
  165.             strCurrency,
  166.             astrDepositMaturityTenor,
  167.             adblDepositQuote,
  168.             "ForwardRate",
  169.             adblFuturesQuote,
  170.             "ForwardRate",
  171.             astrFixFloatMaturityTenor,
  172.             adblFixFloatQuote,
  173.             "SwapRate"
  174.         );
  175.     }

  176.     public static final void main (
  177.         final String[] astrArgs)
  178.         throws Exception
  179.     {
  180.         EnvManager.InitEnv (
  181.             "",
  182.             true
  183.         );

  184.         JulianDate dtSpot = DateUtil.CreateFromYMD (
  185.             2017,
  186.             DateUtil.MARCH,
  187.             24
  188.         );

  189.         int iNumPath = 50;
  190.         double dblCleanPrice = 1.08641;
  191.         int[] aiExerciseDate = new int[] {
  192.             DateUtil.CreateFromYMD (2019, 12,  1).julian(),
  193.             DateUtil.CreateFromYMD (2020, 12,  1).julian(),
  194.             DateUtil.CreateFromYMD (2021, 12,  1).julian(),
  195.             DateUtil.CreateFromYMD (2022, 12,  1).julian(),
  196.             DateUtil.CreateFromYMD (2023, 12,  1).julian(),
  197.             DateUtil.CreateFromYMD (2024, 12,  1).julian(),
  198.             DateUtil.CreateFromYMD (2025, 12,  1).julian(),
  199.             DateUtil.CreateFromYMD (2026, 12,  1).julian(),
  200.             DateUtil.CreateFromYMD (2027, 12,  1).julian(),
  201.             DateUtil.CreateFromYMD (2028, 12,  1).julian(),
  202.             DateUtil.CreateFromYMD (2029, 12,  1).julian(),
  203.             DateUtil.CreateFromYMD (2030, 12,  1).julian(),
  204.             DateUtil.CreateFromYMD (2031, 12,  1).julian(),
  205.             DateUtil.CreateFromYMD (2032, 12,  1).julian(),
  206.             DateUtil.CreateFromYMD (2033, 12,  1).julian(),
  207.             DateUtil.CreateFromYMD (2034, 12,  1).julian(),
  208.             DateUtil.CreateFromYMD (2035, 12,  1).julian(),
  209.             DateUtil.CreateFromYMD (2036, 12,  1).julian(),
  210.             DateUtil.CreateFromYMD (2037, 12,  1).julian(),
  211.             DateUtil.CreateFromYMD (2038, 12,  1).julian(),
  212.         };
  213.         double[] adblExercisePrice = new double[] {
  214.             1.,
  215.             1.,
  216.             1.,
  217.             1.,
  218.             1.,
  219.             1.,
  220.             1.,
  221.             1.,
  222.             1.,
  223.             1.,
  224.             1.,
  225.             1.,
  226.             1.,
  227.             1.,
  228.             1.,
  229.             1.,
  230.             1.,
  231.             1.,
  232.             1.,
  233.             1.,
  234.         };
  235.         JulianDate dtEffective = DateUtil.CreateFromYMD (
  236.             2009,
  237.             12,
  238.             3
  239.         );

  240.         JulianDate dtMaturity  = DateUtil.CreateFromYMD (
  241.             2039,
  242.             12,
  243.             1
  244.         );

  245.         double dblCoupon = 0.06558;
  246.         int iFreq = 2;
  247.         String strCUSIP = "033177XV3";
  248.         String strDayCount = "30/360";
  249.         double dblVolatility = 0.10;
  250.         String strTreasuryCode = "UST";

  251.         String[] astrTenor = new String[] {
  252.             "01Y",
  253.             "02Y",
  254.             "03Y",
  255.             "05Y",
  256.             "07Y",
  257.             "10Y",
  258.             "20Y",
  259.             "30Y"
  260.         };

  261.         double[] adblTreasuryCoupon = new double[] {
  262.             0.0100,
  263.             0.0100,
  264.             0.0125,
  265.             0.0150,
  266.             0.0200,
  267.             0.0225,
  268.             0.0250,
  269.             0.0300
  270.         };

  271.         double[] adblTreasuryYield = new double[] {
  272.             0.0083, //  1Y
  273.             0.0122, //  2Y
  274.             0.0149, //  3Y
  275.             0.0193, //  5Y
  276.             0.0227, //  7Y
  277.             0.0248, // 10Y
  278.             0.0280, // 20Y
  279.             0.0308  // 30Y
  280.         };

  281.         BondComponent bond = BondBuilder.CreateSimpleFixed (
  282.             strCUSIP,
  283.             "USD",
  284.             "",
  285.             dblCoupon,
  286.             iFreq,
  287.             strDayCount,
  288.             dtEffective,
  289.             dtMaturity,
  290.             null,
  291.             null
  292.         );

  293.         bond.setEmbeddedCallSchedule (
  294.             new EmbeddedOptionSchedule (
  295.                 aiExerciseDate,
  296.                 adblExercisePrice,
  297.                 false,
  298.                 30,
  299.                 false,
  300.                 Double.NaN,
  301.                 "",
  302.                 Double.NaN
  303.             )
  304.         );

  305.         GovvieBuilderSettings gbs = new GovvieBuilderSettings (
  306.             dtSpot,
  307.             strTreasuryCode,
  308.             astrTenor,
  309.             adblTreasuryCoupon,
  310.             adblTreasuryYield
  311.         );

  312.         BondEOSMetrics bem = bond.callMetrics (
  313.             ValuationParams.Spot (dtSpot.julian()),
  314.             MarketParamsBuilder.Create (
  315.                 FundingCurve (
  316.                     dtSpot,
  317.                     "USD",
  318.                     0.
  319.                 ),
  320.                 gbs.groundState(),
  321.                 null,
  322.                 null,
  323.                 null,
  324.                 null,
  325.                 null
  326.             ),
  327.             null,
  328.             dblCleanPrice,
  329.             gbs,
  330.             new DiffusionEvolver (
  331.                 DiffusionEvaluatorLogarithmic.Standard (
  332.                     0.,
  333.                     dblVolatility
  334.                 )
  335.             ),
  336.             iNumPath
  337.         );

  338.         boolean[][] aabExerciseIndicator = bem.exerciseIndicator();

  339.         double[][] aadblForwardPrice = bem.forwardPrice();

  340.         int iNumVertex = aabExerciseIndicator[0].length;

  341.         System.out.println();

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

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

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

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

  347.             for (int iVertex = 0; iVertex < iNumVertex; ++iVertex)
  348.                 strDump = strDump + (aabExerciseIndicator[iPath][iVertex] ? " Y" : " N") + " |";

  349.             System.out.println (strDump + "|");
  350.         }

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

  352.         System.out.println();

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

  354.         System.out.println ("\t||                                                                                FORWARD BOND PRICE                                                                                 ||");

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

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

  358.             for (int iVertex = 0; iVertex < iNumVertex; ++iVertex)
  359.                 strDump = strDump + FormatUtil.FormatDouble (aadblForwardPrice[iPath][iVertex], 3, 2, 100) + " |";

  360.             System.out.println (strDump + "|");
  361.         }

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

  363.         System.out.println();

  364.         EnvManager.TerminateEnv();
  365.     }
  366. }