DerivedForwardState.java

  1. package org.drip.template.state;

  2. import org.drip.analytics.date.*;
  3. import org.drip.numerical.common.FormatUtil;
  4. import org.drip.service.env.EnvManager;
  5. import org.drip.service.template.LatentMarketStateBuilder;
  6. import org.drip.state.discount.*;
  7. import org.drip.state.forward.ForwardCurve;
  8. import org.drip.state.identifier.ForwardLabel;

  9. /*
  10.  * -*- mode: java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  11.  */

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

  85. /**
  86.  * <i>DerivedForwardState</i> sets up the Calibration of the Derived Forward Latent State and examine the
  87.  * Emitted Metrics.
  88.  *
  89.  *  <br><br>
  90.  *  <ul>
  91.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ProductCore.md">Product Core Module</a></li>
  92.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/FixedIncomeAnalyticsLibrary.md">Fixed Income Analytics</a></li>
  93.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/template/README.md">Pricing/Risk Templates for Fixed Income Component Products</a></li>
  94.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/template/state/README.md">Standard Latent State Construction Template</a></li>
  95.  *  </ul>
  96.  * <br><br>
  97.  *
  98.  * @author Lakshmi Krishnamurthy
  99.  */

  100. public class DerivedForwardState {

  101.     private static final MergedDiscountForwardCurve OvernightCurve (
  102.         final JulianDate dtSpot,
  103.         final String strCurrency)
  104.         throws Exception
  105.     {
  106.         String[] astrDepositMaturityTenor = new String[] {
  107.             "1D",
  108.             // "2D",
  109.             "3D"
  110.         };

  111.         double[] adblDepositQuote = new double[] {
  112.             0.0004,     // 1D
  113.             // 0.0004,      // 2D
  114.             0.0004      // 3D
  115.         };

  116.         String[] astrShortEndOISMaturityTenor = new String[] {
  117.             "1W",
  118.             "2W",
  119.             "3W",
  120.             "1M"
  121.         };

  122.         double[] adblShortEndOISQuote = new double[] {
  123.             0.00070,    //   1W
  124.             0.00069,    //   2W
  125.             0.00078,    //   3W
  126.             0.00074     //   1M
  127.         };

  128.         String[] astrOISFuturesEffectiveTenor = new String[] {
  129.             "1M",
  130.             "2M",
  131.             "3M",
  132.             "4M",
  133.             "5M"
  134.         };

  135.         String[] astrOISFuturesMaturityTenor = new String[] {
  136.             "1M",
  137.             "1M",
  138.             "1M",
  139.             "1M",
  140.             "1M"
  141.         };

  142.         double[] adblOISFuturesQuote = new double[] {
  143.              0.00046,    //   1M x 1M
  144.              0.00016,    //   2M x 1M
  145.             -0.00007,    //   3M x 1M
  146.             -0.00013,    //   4M x 1M
  147.             -0.00014     //   5M x 1M
  148.         };

  149.         String[] astrLongEndOISMaturityTenor = new String[] {
  150.             "15M",
  151.             "18M",
  152.             "21M",
  153.             "02Y",
  154.             "03Y",
  155.             "04Y",
  156.             "05Y",
  157.             "06Y",
  158.             "07Y",
  159.             "08Y",
  160.             "09Y",
  161.             "10Y",
  162.             "11Y",
  163.             "12Y",
  164.             "15Y",
  165.             "20Y",
  166.             "25Y",
  167.             "30Y"
  168.         };

  169.         double[] adblLongEndOISQuote = new double[] {
  170.             0.00002,    //  15M
  171.             0.00008,    //  18M
  172.             0.00021,    //  21M
  173.             0.00036,    //   2Y
  174.             0.00127,    //   3Y
  175.             0.00274,    //   4Y
  176.             0.00456,    //   5Y
  177.             0.00647,    //   6Y
  178.             0.00827,    //   7Y
  179.             0.00996,    //   8Y
  180.             0.01147,    //   9Y
  181.             0.01280,    //  10Y
  182.             0.01404,    //  11Y
  183.             0.01516,    //  12Y
  184.             0.01764,    //  15Y
  185.             0.01939,    //  20Y
  186.             0.02003,    //  25Y
  187.             0.02038     //  30Y
  188.         };

  189.         return LatentMarketStateBuilder.SmoothOvernightCurve (
  190.             dtSpot,
  191.             strCurrency,
  192.             astrDepositMaturityTenor,
  193.             adblDepositQuote,
  194.             "Rate",
  195.             astrShortEndOISMaturityTenor,
  196.             adblShortEndOISQuote,
  197.             "SwapRate",
  198.             astrOISFuturesEffectiveTenor,
  199.             astrOISFuturesMaturityTenor,
  200.             adblOISFuturesQuote,
  201.             "SwapRate",
  202.             astrLongEndOISMaturityTenor,
  203.             adblLongEndOISQuote,
  204.             "SwapRate"
  205.         );
  206.     }

  207.     private static final ForwardCurve Reference (
  208.         final JulianDate dtSpot,
  209.         final MergedDiscountForwardCurve dcOvernight,
  210.         final String strReferenceForwardTenor)
  211.         throws Exception
  212.     {
  213.         ForwardLabel forwardLabel = ForwardLabel.Create (
  214.             dcOvernight.currency(),
  215.             strReferenceForwardTenor
  216.         );

  217.         String[] astrDepositMaturityTenor = new String[] {
  218.             "1D",
  219.             "1W",
  220.             "2W",
  221.             "3W",
  222.             "1M",
  223.             "2M",
  224.             "3M",
  225.             "4M",
  226.             "5M"
  227.         };

  228.         double[] adblDepositQuote = new double[] {
  229.             0.003565,   // 1D
  230.             0.003858,   // 1W
  231.             0.003840,   // 2W
  232.             0.003922,   // 3W
  233.             0.003869,   // 1M
  234.             0.003698,   // 2M
  235.             0.003527,   // 3M
  236.             0.003342,   // 4M
  237.             0.003225    // 5M
  238.         };

  239.         String[] astrFRAMaturityTenor = new String[] {
  240.             "00D",
  241.             "01M",
  242.             "02M",
  243.             "03M",
  244.             "04M",
  245.             "05M",
  246.             "06M",
  247.             "07M",
  248.             "08M",
  249.             "09M",
  250.             "10M",
  251.             "11M",
  252.             "12M",
  253.             "13M",
  254.             "14M",
  255.             "15M",
  256.             "16M",
  257.             "17M",
  258.             "18M"
  259.         };

  260.         double[] adblFRAQuote = new double[] {
  261.             0.003120,   //  0D
  262.             0.002930,   //  1M
  263.             0.002720,   //  2M
  264.             0.002600,   //  3M
  265.             0.002560,   //  4M
  266.             0.002520,   //  5M
  267.             0.002480,   //  6M
  268.             0.002540,   //  7M
  269.             0.002610,   //  8M
  270.             0.002670,   //  9M
  271.             0.002790,   // 10M
  272.             0.002910,   // 11M
  273.             0.003030,   // 12M
  274.             0.003180,   // 13M
  275.             0.003350,   // 14M
  276.             0.003520,   // 15M
  277.             0.003710,   // 16M
  278.             0.003890,   // 17M
  279.             0.004090    // 18M
  280.         };

  281.         String[] astrFixFloatMaturityTenor = new String[] {
  282.             "03Y",
  283.             "04Y",
  284.             "05Y",
  285.             "06Y",
  286.             "07Y",
  287.             "08Y",
  288.             "09Y",
  289.             "10Y",
  290.             "12Y",
  291.             "15Y",
  292.             "20Y",
  293.             "25Y",
  294.             "30Y",
  295.             "35Y",
  296.             "40Y",
  297.             "50Y",
  298.             "60Y"
  299.         };

  300.         double[] adblFixFloatQuote = new double[] {
  301.             0.004240,   //  3Y
  302.             0.005760,   //  4Y          
  303.             0.007620,   //  5Y
  304.             0.009540,   //  6Y
  305.             0.011350,   //  7Y
  306.             0.013030,   //  8Y
  307.             0.014520,   //  9Y
  308.             0.015840,   // 10Y
  309.             0.018090,   // 12Y
  310.             0.020370,   // 15Y
  311.             0.021870,   // 20Y
  312.             0.022340,   // 25Y
  313.             0.022560,   // 30Y
  314.             0.022950,   // 35Y
  315.             0.023480,   // 40Y
  316.             0.024210,   // 50Y
  317.             0.024630    // 60Y
  318.         };

  319.         return LatentMarketStateBuilder.SmoothForwardCurve (
  320.             dtSpot,
  321.             forwardLabel,
  322.             astrDepositMaturityTenor,
  323.             adblDepositQuote,
  324.             "ForwardRate",
  325.             astrFRAMaturityTenor,
  326.             adblFRAQuote,
  327.             "ParForwardRate",
  328.             astrFixFloatMaturityTenor,
  329.             adblFixFloatQuote,
  330.             "SwapRate",
  331.             null,
  332.             null,
  333.             "DerivedParBasisSpread",
  334.             null,
  335.             null,
  336.             "DerivedParBasisSpread",
  337.             dcOvernight,
  338.             null
  339.         );
  340.     }

  341.     public static final void main (
  342.         final String[] astrArgs)
  343.         throws Exception
  344.     {
  345.         /*
  346.          * Initialize the Credit Analytics Library
  347.          */

  348.         EnvManager.InitEnv ("");

  349.         String strCurrency = "EUR";
  350.         String strDerivedForwardTenor = "3M";
  351.         String strReferenceForwardTenor = "6M";

  352.         JulianDate dtSpot = DateUtil.CreateFromYMD (
  353.             2017,
  354.             DateUtil.DECEMBER,
  355.             21
  356.         );

  357.         MergedDiscountForwardCurve dcOvernight = OvernightCurve (
  358.             dtSpot,
  359.             strCurrency
  360.         );

  361.         ForwardCurve fcReference = Reference (
  362.             dtSpot,
  363.             dcOvernight,
  364.             strReferenceForwardTenor
  365.         );

  366.         ForwardLabel forwardLabel = ForwardLabel.Create (
  367.             strCurrency,
  368.             strDerivedForwardTenor
  369.         );

  370.         String[] astrDepositMaturityTenor = new String[] {
  371.             "2W",
  372.             "3W",
  373.             "1M",
  374.             "2M"
  375.         };

  376.         double[] adblDepositQuote = new double[] {
  377.             0.001865,   // 2W
  378.             0.001969,   // 3W
  379.             0.001951,   // 1M
  380.             0.001874    // 2M
  381.         };

  382.         String[] astrFRAMaturityTenor = new String[] {
  383.             "00D",
  384.             "01M",
  385.             "03M",
  386.             "06M",
  387.             "09M",
  388.             "12M",
  389.             "15M",
  390.             "18M",
  391.             "21M"
  392.         };

  393.         double[] adblFRAQuote = new double[] {
  394.             0.001790,   //  0D
  395.             0.001775,   //  1M
  396.             0.001274,   //  3M
  397.             0.001222,   //  6M
  398.             0.001269,   //  9M
  399.             0.001565,   // 12M
  400.             0.001961,   // 15M
  401.             0.002556,   // 18M
  402.             0.003101    // 21M
  403.         };

  404.         String[] astrFixFloatMaturityTenor = new String[] {
  405.             "03Y",
  406.             "04Y",
  407.             "05Y",
  408.             "06Y",
  409.             "07Y",
  410.             "08Y",
  411.             "09Y",
  412.             "10Y",
  413.             "12Y",
  414.             "15Y",
  415.             "20Y",
  416.             "25Y",
  417.             "30Y"
  418.         };

  419.         double[] adblFixFloatQuote = new double[] {
  420.             0.002850,   //  3Y
  421.             0.004370,   //  4Y
  422.             0.006230,   //  5Y
  423.             0.008170,   //  6Y
  424.             0.010000,   //  7Y
  425.             0.011710,   //  8Y
  426.             0.013240,   //  9Y
  427.             0.014590,   // 10Y
  428.             0.016920,   // 12Y
  429.             0.019330,   // 15Y
  430.             0.020990,   // 20Y
  431.             0.021560,   // 25Y
  432.             0.021860    // 30Y
  433.         };

  434.         String[] astrSyntheticFloatFloatMaturityTenor = new String[] {
  435.             "35Y",
  436.             "40Y",
  437.             "50Y",
  438.             "60Y"
  439.         };

  440.         double[] adblSyntheticFloatFloatQuote = new double[] {
  441.             0.00065,    // 35Y
  442.             0.00060,    // 40Y
  443.             0.00054,    // 50Y
  444.             0.00050     // 60Y
  445.         };

  446.         ForwardCurve fcDerived = LatentMarketStateBuilder.SmoothForwardCurve (
  447.             dtSpot,
  448.             forwardLabel,
  449.             astrDepositMaturityTenor,
  450.             adblDepositQuote,
  451.             "ForwardRate",
  452.             astrFRAMaturityTenor,
  453.             adblFRAQuote,
  454.             "ParForwardRate",
  455.             astrFixFloatMaturityTenor,
  456.             adblFixFloatQuote,
  457.             "SwapRate",
  458.             null,
  459.             null,
  460.             "DerivedParBasisSpread",
  461.             astrSyntheticFloatFloatMaturityTenor,
  462.             adblSyntheticFloatFloatQuote,
  463.             "DerivedParBasisSpread",
  464.             dcOvernight,
  465.             fcReference
  466.         );

  467.         String strLatentStateLabel = fcDerived.label().fullyQualifiedName();

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

  469.         for (int i = 0; i < adblDepositQuote.length; ++i)
  470.             System.out.println (
  471.                 "\t||  " + strLatentStateLabel +
  472.                 " |  DEPOSIT  | " + astrDepositMaturityTenor[i] + "  | " +
  473.                 FormatUtil.FormatDouble (adblDepositQuote[i], 1, 4, 100.) +
  474.                 "% | Forward Rate | " +
  475.                 FormatUtil.FormatDouble (fcDerived.forward (astrDepositMaturityTenor[i]), 1, 4, 100.) +
  476.                 "%  ||"
  477.             );

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

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

  480.         for (int i = 0; i < adblFRAQuote.length; ++i)
  481.             System.out.println (
  482.                 "\t||  " + strLatentStateLabel +
  483.                 " |  FRA  | " + astrFRAMaturityTenor[i] + "  | " +
  484.                 FormatUtil.FormatDouble (adblFRAQuote[i], 1, 4, 100.) +
  485.                 "% | Par Forward Rate | " +
  486.                 FormatUtil.FormatDouble (fcDerived.forward (dtSpot.addTenor (astrFRAMaturityTenor[i]).addTenor (strDerivedForwardTenor)), 1, 4, 100.) +
  487.                 "%  ||"
  488.             );

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

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

  491.         for (int i = 0; i < adblFixFloatQuote.length; ++i)
  492.             System.out.println (
  493.                 "\t||  " + strLatentStateLabel +
  494.                 " |  FIX FLOAT  | " + astrFixFloatMaturityTenor[i] + "  | " +
  495.                 FormatUtil.FormatDouble (adblFixFloatQuote[i], 1, 4, 100.) +
  496.                 "% | Swap Rate | " +
  497.                 FormatUtil.FormatDouble (fcDerived.forward (astrFixFloatMaturityTenor[i]), 1, 4, 100.) +
  498.                 "%  ||"
  499.             );

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

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

  502.         for (int i = 0; i < adblSyntheticFloatFloatQuote.length; ++i)
  503.             System.out.println (
  504.                 "\t||  " + strLatentStateLabel +
  505.                 " |  SYNTHETIC FLOAT FLOAT  | " + astrSyntheticFloatFloatMaturityTenor[i] + "  | " +
  506.                 FormatUtil.FormatDouble (adblSyntheticFloatFloatQuote[i], 1, 2, 10000.) +
  507.                 " bp | Derived Par Basis Spread | " +
  508.                 FormatUtil.FormatDouble (fcDerived.forward (astrSyntheticFloatFloatMaturityTenor[i]), 1, 4, 100.) +
  509.                 "%  ||"
  510.             );

  511.         System.out.println ("\t||----------------------------------------------------------------------------------------------------||\n");
  512.     }
  513. }