PathForwardPrice.java

  1. package org.drip.sample.govviemc;

  2. import org.drip.analytics.date.*;
  3. import org.drip.numerical.common.FormatUtil;
  4. import org.drip.param.creator.MarketParamsBuilder;
  5. import org.drip.param.market.CurveSurfaceQuoteContainer;
  6. import org.drip.param.valuation.ValuationParams;
  7. import org.drip.product.creator.BondBuilder;
  8. import org.drip.product.credit.BondComponent;
  9. import org.drip.product.params.EmbeddedOptionSchedule;
  10. import org.drip.service.env.EnvManager;
  11. import org.drip.service.template.LatentMarketStateBuilder;
  12. import org.drip.state.discount.MergedDiscountForwardCurve;
  13. import org.drip.state.govvie.GovvieCurve;
  14. import org.drip.state.sequence.*;

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

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

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

  93. public class PathForwardPrice {

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

  103.         double[] adblDepositQuote = new double[] {
  104.             0.0111956 + dblBump // 2D
  105.         };

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

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

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

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

  165.     private static final PathGovvie PathGovvieCurves (
  166.         final JulianDate dtSpot,
  167.         final int iNumPath)
  168.         throws Exception
  169.     {
  170.         double dblVolatility = 0.10;
  171.         String strTreasuryCode = "UST";

  172.         String[] astrTenor = new String[] {
  173.             "01Y",
  174.             "02Y",
  175.             "03Y",
  176.             "05Y",
  177.             "07Y",
  178.             "10Y",
  179.             "20Y",
  180.             "30Y"
  181.         };

  182.         double[] adblTreasuryCoupon = new double[] {
  183.             0.0100,
  184.             0.0100,
  185.             0.0125,
  186.             0.0150,
  187.             0.0200,
  188.             0.0225,
  189.             0.0250,
  190.             0.0300
  191.         };

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

  202.         return new PathGovvie (
  203.             new GovvieBuilderSettings (
  204.                 dtSpot,
  205.                 strTreasuryCode,
  206.                 astrTenor,
  207.                 adblTreasuryCoupon,
  208.                 adblTreasuryYield
  209.             ),
  210.             dblVolatility,
  211.             true
  212.         );
  213.     }

  214.     private static final BondComponent Callable (
  215.         final EmbeddedOptionSchedule eos)
  216.         throws Exception
  217.     {
  218.         JulianDate dtEffective = DateUtil.CreateFromYMD (
  219.             2009,
  220.             12,
  221.             3
  222.         );

  223.         JulianDate dtMaturity  = DateUtil.CreateFromYMD (
  224.             2039,
  225.             12,
  226.             1
  227.         );

  228.         double dblCoupon = 0.06558;
  229.         int iFreq = 2;
  230.         String strCUSIP = "033177XV3";
  231.         String strDayCount = "30/360";

  232.         BondComponent bond = BondBuilder.CreateSimpleFixed (
  233.             strCUSIP,
  234.             "USD",
  235.             "",
  236.             dblCoupon,
  237.             iFreq,
  238.             strDayCount,
  239.             dtEffective,
  240.             dtMaturity,
  241.             null,
  242.             null
  243.         );

  244.         bond.setEmbeddedCallSchedule (eos);

  245.         return bond;
  246.     }

  247.     public static final void main (
  248.         final String[] astrArgs)
  249.         throws Exception
  250.     {
  251.         EnvManager.InitEnv ("");

  252.         JulianDate dtSpot = DateUtil.CreateFromYMD (
  253.             2017,
  254.             DateUtil.MARCH,
  255.             24
  256.         );

  257.         int iNumPath = 50;
  258.         double dblCleanPrice = 1.08641;
  259.         int[] aiExerciseDate = new int[] {
  260.             DateUtil.CreateFromYMD (2019, 12,  1).julian(),
  261.             DateUtil.CreateFromYMD (2020, 12,  1).julian(),
  262.             DateUtil.CreateFromYMD (2021, 12,  1).julian(),
  263.             DateUtil.CreateFromYMD (2022, 12,  1).julian(),
  264.             DateUtil.CreateFromYMD (2023, 12,  1).julian(),
  265.             DateUtil.CreateFromYMD (2024, 12,  1).julian(),
  266.             DateUtil.CreateFromYMD (2025, 12,  1).julian(),
  267.             DateUtil.CreateFromYMD (2026, 12,  1).julian(),
  268.             DateUtil.CreateFromYMD (2027, 12,  1).julian(),
  269.             DateUtil.CreateFromYMD (2028, 12,  1).julian(),
  270.             DateUtil.CreateFromYMD (2029, 12,  1).julian(),
  271.             DateUtil.CreateFromYMD (2030, 12,  1).julian(),
  272.             DateUtil.CreateFromYMD (2031, 12,  1).julian(),
  273.             DateUtil.CreateFromYMD (2032, 12,  1).julian(),
  274.             DateUtil.CreateFromYMD (2033, 12,  1).julian(),
  275.             DateUtil.CreateFromYMD (2034, 12,  1).julian(),
  276.             DateUtil.CreateFromYMD (2035, 12,  1).julian(),
  277.             DateUtil.CreateFromYMD (2036, 12,  1).julian(),
  278.             DateUtil.CreateFromYMD (2037, 12,  1).julian(),
  279.             DateUtil.CreateFromYMD (2038, 12,  1).julian(),
  280.         };
  281.         double[] adblExercisePrice = new double[] {
  282.             1.,
  283.             1.,
  284.             1.,
  285.             1.,
  286.             1.,
  287.             1.,
  288.             1.,
  289.             1.,
  290.             1.,
  291.             1.,
  292.             1.,
  293.             1.,
  294.             1.,
  295.             1.,
  296.             1.,
  297.             1.,
  298.             1.,
  299.             1.,
  300.             1.,
  301.             1.,
  302.         };

  303.         int iNumVertex = aiExerciseDate.length;
  304.         double[][] aadblForwardPrice = new double[iNumPath][iNumVertex];
  305.         ValuationParams[] aValParamsEvent = new ValuationParams[iNumVertex];

  306.         BondComponent bond = Callable (
  307.             new EmbeddedOptionSchedule (
  308.                 aiExerciseDate,
  309.                 adblExercisePrice,
  310.                 false,
  311.                 30,
  312.                 false,
  313.                 Double.NaN,
  314.                 "",
  315.                 Double.NaN
  316.             )
  317.         );

  318.         PathGovvie pg = PathGovvieCurves (
  319.             dtSpot,
  320.             iNumPath
  321.         );

  322.         GovvieCurve[] aGCPath = pg.curveSequence (iNumPath);

  323.         MergedDiscountForwardCurve mdfc = FundingCurve (
  324.             dtSpot,
  325.             "USD",
  326.             0.
  327.         );

  328.         CurveSurfaceQuoteContainer csqcBase = MarketParamsBuilder.Create (
  329.             mdfc,
  330.             pg.govvieBuilderSettings().groundState(),
  331.             null,
  332.             null,
  333.             null,
  334.             null,
  335.             null
  336.         );

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

  338.         double dblOASSpot = bond.oasFromPrice (
  339.             valParamsSpot,
  340.             csqcBase,
  341.             null,
  342.             dblCleanPrice
  343.         );

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

  346.         System.out.println();

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

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

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

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

  352.             CurveSurfaceQuoteContainer csqcPath = MarketParamsBuilder.Create (
  353.                 mdfc,
  354.                 aGCPath[iPath],
  355.                 null,
  356.                 null,
  357.                 null,
  358.                 null,
  359.                 null
  360.             );

  361.             for (int iVertex = 0; iVertex < iNumVertex; ++iVertex) {
  362.                 strDump = strDump + FormatUtil.FormatDouble (
  363.                     aadblForwardPrice[iPath][iVertex] = bond.priceFromOAS (
  364.                         aValParamsEvent[iVertex],
  365.                         csqcPath,
  366.                         null,
  367.                         dblOASSpot
  368.                     ), 3, 2, 100) + " |";
  369.             }

  370.             System.out.println (strDump + "|");
  371.         }

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

  373.         System.out.println();

  374.         EnvManager.TerminateEnv();
  375.     }
  376. }