FundingStateShifted.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.MergedDiscountForwardCurve;

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

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

  87. /**
  88.  * <i>FundingStateShifted</i> generates a Sequence of Tenor Bumped Funding Curves.
  89.  *
  90.  *  <br><br>
  91.  *  <ul>
  92.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ProductCore.md">Product Core Module</a></li>
  93.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/FixedIncomeAnalyticsLibrary.md">Fixed Income Analytics</a></li>
  94.  *      <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>
  95.  *      <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>
  96.  *  </ul>
  97.  * <br><br>
  98.  *
  99.  * @author Lakshmi Krishnamurthy
  100.  */

  101. public class FundingStateShifted {

  102.     public static final void main (
  103.         final String[] astrArgs)
  104.         throws Exception
  105.     {
  106.         EnvManager.InitEnv ("");

  107.         String strCurrency = "USD";
  108.         double dblBump = 0.0001;
  109.         boolean bIsBumpProportional = false;

  110.         JulianDate dtSpot = DateUtil.CreateFromYMD (
  111.             2017,
  112.             DateUtil.DECEMBER,
  113.             21
  114.         );

  115.         String[] astrDepositMaturityTenor = new String[] {
  116.             "01D",
  117.             "02D",
  118.             "07D",
  119.             "14D",
  120.             "30D",
  121.             "60D"
  122.         };

  123.         double[] adblDepositQuote = new double[] {
  124.             0.0013,     //  1D
  125.             0.0017,     //  2D
  126.             0.0017,     //  7D
  127.             0.0018,     // 14D
  128.             0.0020,     // 30D
  129.             0.0023      // 60D
  130.         };

  131.         double[] adblFuturesQuote = new double[] {
  132.             0.0027,
  133.             0.0032,
  134.             0.0041,
  135.             0.0054,
  136.             0.0077,
  137.             0.0104,
  138.             0.0134,
  139.             0.0160
  140.         };

  141.         String[] astrFixFloatMaturityTenor = new String[] {
  142.             "04Y",
  143.             "05Y",
  144.             "06Y",
  145.             "07Y",
  146.             "08Y",
  147.             "09Y",
  148.             "10Y",
  149.             "11Y",
  150.             "12Y",
  151.             "15Y",
  152.             "20Y",
  153.             "25Y",
  154.             "30Y",
  155.             "40Y",
  156.             "50Y"
  157.         };

  158.         double[] adblFixFloatQuote = new double[] {
  159.             0.0166,     //   4Y
  160.             0.0206,     //   5Y
  161.             0.0241,     //   6Y
  162.             0.0269,     //   7Y
  163.             0.0292,     //   8Y
  164.             0.0311,     //   9Y
  165.             0.0326,     //  10Y
  166.             0.0340,     //  11Y
  167.             0.0351,     //  12Y
  168.             0.0375,     //  15Y
  169.             0.0393,     //  20Y
  170.             0.0402,     //  25Y
  171.             0.0407,     //  30Y
  172.             0.0409,     //  40Y
  173.             0.0409      //  50Y
  174.         };

  175.         Map<String, MergedDiscountForwardCurve> mapFundingCurve = LatentMarketStateBuilder.BumpedFundingCurve (
  176.             dtSpot,
  177.             strCurrency,
  178.             astrDepositMaturityTenor,
  179.             adblDepositQuote,
  180.             "ForwardRate",
  181.             adblFuturesQuote,
  182.             "ForwardRate",
  183.             astrFixFloatMaturityTenor,
  184.             adblFixFloatQuote,
  185.             "SwapRate",
  186.             LatentMarketStateBuilder.SMOOTH,
  187.             dblBump,
  188.             bIsBumpProportional
  189.         );

  190.         Component[] aDepositComp = OTCInstrumentBuilder.FundingDeposit (
  191.             dtSpot,
  192.             strCurrency,
  193.             astrDepositMaturityTenor
  194.         );

  195.         Component[] aFuturesComp = ExchangeInstrumentBuilder.ForwardRateFuturesPack (
  196.             dtSpot,
  197.             adblFuturesQuote.length,
  198.             strCurrency
  199.         );

  200.         Component[] aFixFloatComp = OTCInstrumentBuilder.FixFloatStandard (
  201.             dtSpot,
  202.             strCurrency,
  203.             "ALL",
  204.             astrFixFloatMaturityTenor,
  205.             "MAIN",
  206.             0.
  207.         );

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

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

  210.         for (Map.Entry<String, MergedDiscountForwardCurve> meFunding : mapFundingCurve.entrySet()) {
  211.             String strKey = meFunding.getKey();

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

  213.             CurveSurfaceQuoteContainer csqc = new CurveSurfaceQuoteContainer();

  214.             csqc.setFundingState (meFunding.getValue());

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

  216.             for (Component comp : aDepositComp)
  217.                 System.out.print (FormatUtil.FormatDouble (
  218.                     comp.measureValue (
  219.                         valParams,
  220.                         null,
  221.                         csqc,
  222.                         null,
  223.                         "ForwardRate"
  224.                     ), 1, 4, 1.) + " |");

  225.             System.out.print ("|\n");
  226.         }

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

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

  229.         for (Map.Entry<String, MergedDiscountForwardCurve> meFunding : mapFundingCurve.entrySet()) {
  230.             String strKey = meFunding.getKey();

  231.             if (!strKey.startsWith ("futures")) continue;

  232.             CurveSurfaceQuoteContainer csqc = new CurveSurfaceQuoteContainer();

  233.             csqc.setFundingState (meFunding.getValue());

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

  235.             for (Component comp : aFuturesComp)
  236.                 System.out.print (FormatUtil.FormatDouble (
  237.                     comp.measureValue (
  238.                         valParams,
  239.                         null,
  240.                         csqc,
  241.                         null,
  242.                         "ForwardRate"
  243.                     ), 1, 4, 1.) + " |");

  244.             System.out.print ("|\n");
  245.         }

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

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

  248.         for (Map.Entry<String, MergedDiscountForwardCurve> meFunding : mapFundingCurve.entrySet()) {
  249.             String strKey = meFunding.getKey();

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

  251.             CurveSurfaceQuoteContainer csqc = new CurveSurfaceQuoteContainer();

  252.             csqc.setFundingState (meFunding.getValue());

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

  254.             for (Component comp : aFixFloatComp)
  255.                 System.out.print (FormatUtil.FormatDouble (
  256.                     comp.measureValue (
  257.                         valParams,
  258.                         null,
  259.                         csqc,
  260.                         null,
  261.                         "SwapRate"
  262.                     ), 1, 4, 1.) + " |");

  263.             System.out.print ("|\n");
  264.         }

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

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

  267.         CurveSurfaceQuoteContainer csqcBase = new CurveSurfaceQuoteContainer();

  268.         csqcBase.setFundingState (mapFundingCurve.get ("Base"));

  269.         CurveSurfaceQuoteContainer csqcBump = new CurveSurfaceQuoteContainer();

  270.         csqcBump.setFundingState (mapFundingCurve.get ("Bump"));

  271.         for (Component comp : aDepositComp)
  272.             System.out.println (
  273.                 "\t| FORWARD RATE  => " +
  274.                 comp.maturityDate() + " | " +
  275.                 FormatUtil.FormatDouble (comp.measureValue (
  276.                     valParams,
  277.                     null,
  278.                     csqcBase,
  279.                     null,
  280.                     "ForwardRate"
  281.                 ), 1, 6, 1.) + " | " +
  282.                 FormatUtil.FormatDouble (comp.measureValue (
  283.                     valParams,
  284.                     null,
  285.                     csqcBump,
  286.                     null,
  287.                     "ForwardRate"
  288.                 ), 1, 6, 1.) + " ||"
  289.             );

  290.         for (Component comp : aFuturesComp)
  291.             System.out.println (
  292.                 "\t| FORWARD RATE  => " +
  293.                 comp.maturityDate() + " | " +
  294.                 FormatUtil.FormatDouble (comp.measureValue (
  295.                     valParams,
  296.                     null,
  297.                     csqcBase,
  298.                     null,
  299.                     "ForwardRate"
  300.                 ), 1, 6, 1.) + " | " +
  301.                 FormatUtil.FormatDouble (comp.measureValue (
  302.                     valParams,
  303.                     null,
  304.                     csqcBump,
  305.                     null,
  306.                     "ForwardRate"
  307.                 ), 1, 6, 1.) + " ||"
  308.             );

  309.         for (Component comp : aFixFloatComp)
  310.             System.out.println (
  311.                 "\t|  SWAP   RATE  => " +
  312.                 comp.maturityDate() + " | " +
  313.                 FormatUtil.FormatDouble (comp.measureValue (
  314.                     valParams,
  315.                     null,
  316.                     csqcBase,
  317.                     null,
  318.                     "SwapRate"
  319.                 ), 1, 6, 1.) + " | " +
  320.                 FormatUtil.FormatDouble (comp.measureValue (
  321.                     valParams,
  322.                     null,
  323.                     csqcBump,
  324.                     null,
  325.                     "SwapRate"
  326.                 ), 1, 6, 1.) + " ||"
  327.             );

  328.         System.out.println ("\t|-----------------------------------------------------||");
  329.     }
  330. }