ReferenceForwardStateShifted.java

  1. package org.drip.template.statebump;

  2. import java.util.Map;

  3. import org.drip.analytics.date.*;
  4. import org.drip.numerical.common.FormatUtil;
  5. import org.drip.param.market.CurveSurfaceQuoteContainer;
  6. import org.drip.param.valuation.ValuationParams;
  7. import org.drip.product.definition.Component;
  8. import org.drip.service.env.EnvManager;
  9. import org.drip.service.template.*;
  10. import org.drip.state.discount.*;
  11. import org.drip.state.forward.ForwardCurve;
  12. import org.drip.state.identifier.ForwardLabel;

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

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

  89. /**
  90.  * <i>ReferenceForwardStateShifted</i> demonstrates the Generation of the Shifted Reference Forward Curves.
  91.  *
  92.  *  <br><br>
  93.  *  <ul>
  94.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ProductCore.md">Product Core Module</a></li>
  95.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/FixedIncomeAnalyticsLibrary.md">Fixed Income Analytics</a></li>
  96.  *      <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>
  97.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/template/statebump/README.md">Shifted Latent State Construction Template</a></li>
  98.  *  </ul>
  99.  * <br><br>
  100.  *
  101.  * @author Lakshmi Krishnamurthy
  102.  */

  103. public class ReferenceForwardStateShifted {

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

  114.         double[] adblDepositQuote = new double[] {
  115.             0.0004,     // 1D
  116.             // 0.0004,      // 2D
  117.             0.0004      // 3D
  118.         };

  119.         String[] astrShortEndOISMaturityTenor = new String[] {
  120.             "1W",
  121.             "2W",
  122.             "3W",
  123.             "1M"
  124.         };

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

  131.         String[] astrOISFuturesEffectiveTenor = new String[] {
  132.             "1M",
  133.             "2M",
  134.             "3M",
  135.             "4M",
  136.             "5M"
  137.         };

  138.         String[] astrOISFuturesMaturityTenor = new String[] {
  139.             "1M",
  140.             "1M",
  141.             "1M",
  142.             "1M",
  143.             "1M"
  144.         };

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

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

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

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

  210.     public static final void main (
  211.         final String[] astrArgs)
  212.         throws Exception
  213.     {
  214.         /*
  215.          * Initialize the Credit Analytics Library
  216.          */

  217.         EnvManager.InitEnv ("");

  218.         String strCurrency = "GBP";
  219.         String strForwardTenor = "6M";
  220.         double dblBump = 0.0001;
  221.         boolean bIsProportional = false;

  222.         JulianDate dtSpot = DateUtil.CreateFromYMD (
  223.             2017,
  224.             DateUtil.DECEMBER,
  225.             21
  226.         );

  227.         MergedDiscountForwardCurve dcOvernight = OvernightCurve (
  228.             dtSpot,
  229.             strCurrency
  230.         );

  231.         ForwardLabel forwardLabel = ForwardLabel.Create (
  232.             strCurrency,
  233.             strForwardTenor
  234.         );

  235.         String[] astrDepositMaturityTenor = new String[] {
  236.             "1D",
  237.             "1W",
  238.             "2W",
  239.             "3W",
  240.             "1M",
  241.             "2M",
  242.             "3M",
  243.             "4M",
  244.             "5M"
  245.         };

  246.         double[] adblDepositQuote = new double[] {
  247.             0.003565,   // 1D
  248.             0.003858,   // 1W
  249.             0.003840,   // 2W
  250.             0.003922,   // 3W
  251.             0.003869,   // 1M
  252.             0.003698,   // 2M
  253.             0.003527,   // 3M
  254.             0.003342,   // 4M
  255.             0.003225    // 5M
  256.         };

  257.         String[] astrFRAMaturityTenor = new String[] {
  258.             "00D",
  259.             "01M",
  260.             "02M",
  261.             "03M",
  262.             "04M",
  263.             "05M",
  264.             "06M",
  265.             "07M",
  266.             "08M",
  267.             "09M",
  268.             "10M",
  269.             "11M",
  270.             "12M",
  271.             "13M",
  272.             "14M",
  273.             "15M",
  274.             "16M",
  275.             "17M",
  276.             "18M"
  277.         };

  278.         double[] adblFRAQuote = new double[] {
  279.             0.003120,   //  0D
  280.             0.002930,   //  1M
  281.             0.002720,   //  2M
  282.             0.002600,   //  3M
  283.             0.002560,   //  4M
  284.             0.002520,   //  5M
  285.             0.002480,   //  6M
  286.             0.002540,   //  7M
  287.             0.002610,   //  8M
  288.             0.002670,   //  9M
  289.             0.002790,   // 10M
  290.             0.002910,   // 11M
  291.             0.003030,   // 12M
  292.             0.003180,   // 13M
  293.             0.003350,   // 14M
  294.             0.003520,   // 15M
  295.             0.003710,   // 16M
  296.             0.003890,   // 17M
  297.             0.004090    // 18M
  298.         };

  299.         String[] astrFixFloatMaturityTenor = new String[] {
  300.             "03Y",
  301.             "04Y",
  302.             "05Y",
  303.             "06Y",
  304.             "07Y",
  305.             "08Y",
  306.             "09Y",
  307.             "10Y",
  308.             "12Y",
  309.             "15Y",
  310.             "20Y",
  311.             "25Y",
  312.             "30Y",
  313.             "35Y",
  314.             "40Y",
  315.             "50Y",
  316.             "60Y"
  317.         };

  318.         double[] adblFixFloatQuote = new double[] {
  319.             0.004240,   //  3Y
  320.             0.005760,   //  4Y          
  321.             0.007620,   //  5Y
  322.             0.009540,   //  6Y
  323.             0.011350,   //  7Y
  324.             0.013030,   //  8Y
  325.             0.014520,   //  9Y
  326.             0.015840,   // 10Y
  327.             0.018090,   // 12Y
  328.             0.020370,   // 15Y
  329.             0.021870,   // 20Y
  330.             0.022340,   // 25Y
  331.             0.022560,   // 30Y
  332.             0.022950,   // 35Y
  333.             0.023480,   // 40Y
  334.             0.024210,   // 50Y
  335.             0.024630    // 60Y
  336.         };

  337.         Map<String, ForwardCurve> mapReferenceForwardCurve = LatentMarketStateBuilder.BumpedForwardCurve (
  338.             dtSpot,
  339.             forwardLabel,
  340.             astrDepositMaturityTenor,
  341.             adblDepositQuote,
  342.             "ForwardRate",
  343.             astrFRAMaturityTenor,
  344.             adblFRAQuote,
  345.             "ParForwardRate",
  346.             astrFixFloatMaturityTenor,
  347.             adblFixFloatQuote,
  348.             "SwapRate",
  349.             null,
  350.             null,
  351.             "DerivedParBasisSpread",
  352.             null,
  353.             null,
  354.             "DerivedParBasisSpread",
  355.             dcOvernight,
  356.             null,
  357.             LatentMarketStateBuilder.SMOOTH,
  358.             dblBump,
  359.             bIsProportional
  360.         );

  361.         Component[] aDepositComp = OTCInstrumentBuilder.ForwardRateDeposit (
  362.             dtSpot,
  363.             astrDepositMaturityTenor,
  364.             forwardLabel
  365.         );

  366.         Component[] aFRAComp = OTCInstrumentBuilder.FRAStandard (
  367.             dtSpot,
  368.             forwardLabel,
  369.             astrFRAMaturityTenor,
  370.             adblFRAQuote
  371.         );

  372.         Component[] aFixFloatComp = OTCInstrumentBuilder.FixFloatCustom (
  373.             dtSpot,
  374.             forwardLabel,
  375.             astrFixFloatMaturityTenor
  376.         );

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

  378.         ValuationParams valParams = ValuationParams.Spot (dtSpot.julian());

  379.         for (Map.Entry<String, ForwardCurve> meForward : mapReferenceForwardCurve.entrySet()) {
  380.             String strKey = meForward.getKey();

  381.             if (!strKey.startsWith ("deposit")) continue;

  382.             CurveSurfaceQuoteContainer csqc = new CurveSurfaceQuoteContainer();

  383.             csqc.setFundingState (dcOvernight);

  384.             csqc.setForwardState (meForward.getValue());

  385.             System.out.print ("\t|  [" + meForward.getKey() + "] => ");

  386.             for (Component comp : aDepositComp)
  387.                 System.out.print (FormatUtil.FormatDouble (comp.measureValue (
  388.                     valParams,
  389.                     null,
  390.                     csqc,
  391.                     null,
  392.                     "ForwardRate"
  393.                 ), 1, 6, 1.) + " |");

  394.             System.out.print ("|\n");
  395.         }

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

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

  398.         for (Map.Entry<String, ForwardCurve> meForward : mapReferenceForwardCurve.entrySet()) {
  399.             String strKey = meForward.getKey();

  400.             if (!strKey.startsWith ("fra")) continue;

  401.             CurveSurfaceQuoteContainer csqc = new CurveSurfaceQuoteContainer();

  402.             csqc.setFundingState (dcOvernight);

  403.             csqc.setForwardState (meForward.getValue());

  404.             System.out.print ("\t|  [" + meForward.getKey() + "] => ");

  405.             for (Component comp : aFRAComp)
  406.                 System.out.print (FormatUtil.FormatDouble (comp.measureValue (
  407.                     valParams,
  408.                     null,
  409.                     csqc,
  410.                     null,
  411.                     "ParForwardRate"
  412.                 ), 1, 6, 1.) + " |");

  413.             System.out.print ("|\n");
  414.         }

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

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

  417.         for (Map.Entry<String, ForwardCurve> meForward : mapReferenceForwardCurve.entrySet()) {
  418.             String strKey = meForward.getKey();

  419.             if (!strKey.startsWith ("fixfloat")) continue;

  420.             CurveSurfaceQuoteContainer csqc = new CurveSurfaceQuoteContainer();

  421.             csqc.setFundingState (dcOvernight);

  422.             csqc.setForwardState (meForward.getValue());

  423.             System.out.print ("\t|  [" + meForward.getKey() + "] => ");

  424.             for (Component comp : aFixFloatComp)
  425.                 System.out.print (FormatUtil.FormatDouble (comp.measureValue (
  426.                     valParams,
  427.                     null,
  428.                     csqc,
  429.                     null,
  430.                     "SwapRate"
  431.                 ), 1, 6, 1.) + " |");

  432.             System.out.print ("|\n");
  433.         }

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

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

  436.         CurveSurfaceQuoteContainer csqcBase = new CurveSurfaceQuoteContainer();

  437.         csqcBase.setFundingState (dcOvernight);

  438.         csqcBase.setForwardState (mapReferenceForwardCurve.get ("Base"));

  439.         CurveSurfaceQuoteContainer csqcBump = new CurveSurfaceQuoteContainer();

  440.         csqcBump.setFundingState (dcOvernight);

  441.         csqcBump.setForwardState (mapReferenceForwardCurve.get ("Bump"));

  442.         for (Component comp : aDepositComp)
  443.             System.out.println (
  444.                 "\t| FORWARD RATE  => " +
  445.                 comp.maturityDate() + " | " +
  446.                 FormatUtil.FormatDouble (comp.measureValue (
  447.                     valParams,
  448.                     null,
  449.                     csqcBase,
  450.                     null,
  451.                     "ForwardRate"
  452.                 ), 1, 6, 1.) + " | " +
  453.                 FormatUtil.FormatDouble (comp.measureValue (
  454.                     valParams,
  455.                     null,
  456.                     csqcBump,
  457.                     null,
  458.                     "ForwardRate"
  459.                 ), 1, 6, 1.) + " ||"
  460.             );

  461.         for (Component comp : aFRAComp)
  462.             System.out.println (
  463.                 "\t| FORWARD RATE  => " +
  464.                 comp.maturityDate() + " | " +
  465.                 FormatUtil.FormatDouble (comp.measureValue (
  466.                     valParams,
  467.                     null,
  468.                     csqcBase,
  469.                     null,
  470.                     "ParForwardRate"
  471.                 ), 1, 6, 1.) + " | " +
  472.                 FormatUtil.FormatDouble (comp.measureValue (
  473.                     valParams,
  474.                     null,
  475.                     csqcBump,
  476.                     null,
  477.                     "ParForwardRate"
  478.                 ), 1, 6, 1.) + " ||"
  479.             );

  480.         for (Component comp : aFixFloatComp)
  481.             System.out.println (
  482.                 "\t|  SWAP   RATE  => " +
  483.                 comp.maturityDate() + " | " +
  484.                 FormatUtil.FormatDouble (comp.measureValue (
  485.                     valParams,
  486.                     null,
  487.                     csqcBase,
  488.                     null,
  489.                     "SwapRate"
  490.                 ), 1, 6, 1.) + " | " +
  491.                 FormatUtil.FormatDouble (comp.measureValue (
  492.                     valParams,
  493.                     null,
  494.                     csqcBump,
  495.                     null,
  496.                     "SwapRate"
  497.                 ), 1, 6, 1.) + " ||"
  498.             );

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