FixFloatMetricComparison.java

  1. package org.drip.sample.cms;

  2. import java.util.List;

  3. import org.drip.analytics.date.*;
  4. import org.drip.analytics.support.*;
  5. import org.drip.market.otc.*;
  6. import org.drip.numerical.common.FormatUtil;
  7. import org.drip.param.market.CurveSurfaceQuoteContainer;
  8. import org.drip.param.period.*;
  9. import org.drip.param.valuation.*;
  10. import org.drip.product.creator.SingleStreamComponentBuilder;
  11. import org.drip.product.definition.CalibratableComponent;
  12. import org.drip.product.rates.*;
  13. import org.drip.service.env.EnvManager;
  14. import org.drip.state.creator.ScenarioDiscountCurveBuilder;
  15. import org.drip.state.discount.*;
  16. import org.drip.state.identifier.ForwardLabel;

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

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

  93. /**
  94.  * <i>FixFloatMetricComparison</i> demonstrates the Construction and Valuation of an In-Advance and
  95.  * In-Arrears Variants of the CMS Fix-Float Swap.
  96.  *  
  97.  * <br><br>
  98.  *  <ul>
  99.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ProductCore.md">Product Core Module</a></li>
  100.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/FixedIncomeAnalyticsLibrary.md">Fixed Income Analytics</a></li>
  101.  *      <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>
  102.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/cms/README.md">Dual Stream Constant Maturity Swap</a></li>
  103.  *  </ul>
  104.  * <br><br>
  105.  *
  106.  * @author Lakshmi Krishnamurthy
  107.  */

  108. public class FixFloatMetricComparison {

  109.     private static final FixFloatComponent OTCIRS (
  110.         final JulianDate dtSpot,
  111.         final String strCurrency,
  112.         final String strMaturityTenor,
  113.         final double dblCoupon)
  114.     {
  115.         FixedFloatSwapConvention ffConv = IBORFixedFloatContainer.ConventionFromJurisdiction (
  116.             strCurrency,
  117.             "ALL",
  118.             strMaturityTenor,
  119.             "MAIN"
  120.         );

  121.         return ffConv.createFixFloatComponent (
  122.             dtSpot,
  123.             strMaturityTenor,
  124.             dblCoupon,
  125.             0.,
  126.             1.
  127.         );
  128.     }

  129.     private static final CalibratableComponent[] DepositInstrumentsFromMaturityDays (
  130.         final JulianDate dtEffective,
  131.         final int[] aiDay,
  132.         final int iNumFutures,
  133.         final String strCurrency)
  134.         throws Exception
  135.     {
  136.         CalibratableComponent[] aCalibComp = new CalibratableComponent[aiDay.length + iNumFutures];

  137.         for (int i = 0; i < aiDay.length; ++i)
  138.             aCalibComp[i] = SingleStreamComponentBuilder.Deposit (
  139.                 dtEffective,
  140.                 dtEffective.addBusDays (
  141.                     aiDay[i],
  142.                     strCurrency
  143.                 ),
  144.                 ForwardLabel.Create (
  145.                     strCurrency,
  146.                     "3M"
  147.                 )
  148.             );

  149.         CalibratableComponent[] aEDF = SingleStreamComponentBuilder.ForwardRateFuturesPack (
  150.             dtEffective,
  151.             iNumFutures,
  152.             strCurrency
  153.         );

  154.         for (int i = aiDay.length; i < aiDay.length + iNumFutures; ++i)
  155.             aCalibComp[i] = aEDF[i - aiDay.length];

  156.         return aCalibComp;
  157.     }

  158.     private static final FixFloatComponent[] SwapInstrumentsFromMaturityTenor (
  159.         final JulianDate dtSpot,
  160.         final String strCurrency,
  161.         final String[] astrMaturityTenor,
  162.         final double[] adblCoupon)
  163.         throws Exception
  164.     {
  165.         FixFloatComponent[] aIRS = new FixFloatComponent[astrMaturityTenor.length];

  166.         for (int i = 0; i < astrMaturityTenor.length; ++i) {
  167.             FixFloatComponent irs = OTCIRS (
  168.                 dtSpot,
  169.                 strCurrency,
  170.                 astrMaturityTenor[i],
  171.                 adblCoupon[i]
  172.             );

  173.             irs.setPrimaryCode ("IRS." + astrMaturityTenor[i] + "." + strCurrency);

  174.             aIRS[i] = irs;
  175.         }

  176.         return aIRS;
  177.     }

  178.     private static final MergedDiscountForwardCurve MakeDC (
  179.         final JulianDate dtSpot,
  180.         final String strCurrency)
  181.         throws Exception
  182.     {

  183.         CalibratableComponent[] aDepositComp = DepositInstrumentsFromMaturityDays (
  184.             dtSpot,
  185.             new int[] {
  186.                 1, 2, 3, 7, 14, 21, 30, 60
  187.             },
  188.             0,
  189.             strCurrency
  190.         );

  191.         double[] adblDepositQuote = new double[] {
  192.             0.01200, 0.01200, 0.01200, 0.01450, 0.01550, 0.01600, 0.01660, 0.01850
  193.         };

  194.         String[] astrDepositManifestMeasure = new String[] {
  195.             "ForwardRate", "ForwardRate", "ForwardRate", "ForwardRate", "ForwardRate", "ForwardRate", "ForwardRate", "ForwardRate"
  196.         };

  197.         double[] adblSwapQuote = new double[] {
  198.             0.02604,    //  4Y
  199.             0.02808,    //  5Y
  200.             0.02983,    //  6Y
  201.             0.03136,    //  7Y
  202.             0.03268,    //  8Y
  203.             0.03383,    //  9Y
  204.             0.03488,    // 10Y
  205.             0.03583,    // 11Y
  206.             0.03668,    // 12Y
  207.             0.03833,    // 15Y
  208.             0.03854,    // 20Y
  209.             0.03672,    // 25Y
  210.             0.03510,    // 30Y
  211.             0.03266,    // 40Y
  212.             0.03145     // 50Y
  213.         };

  214.         String[] astrSwapManifestMeasure = new String[] {
  215.             "SwapRate",    //  4Y
  216.             "SwapRate",    //  5Y
  217.             "SwapRate",    //  6Y
  218.             "SwapRate",    //  7Y
  219.             "SwapRate",    //  8Y
  220.             "SwapRate",    //  9Y
  221.             "SwapRate",    // 10Y
  222.             "SwapRate",    // 11Y
  223.             "SwapRate",    // 12Y
  224.             "SwapRate",    // 15Y
  225.             "SwapRate",    // 20Y
  226.             "SwapRate",    // 25Y
  227.             "SwapRate",    // 30Y
  228.             "SwapRate",    // 40Y
  229.             "SwapRate"     // 50Y
  230.         };

  231.         CalibratableComponent[] aSwapComp = SwapInstrumentsFromMaturityTenor (
  232.             dtSpot,
  233.             strCurrency,
  234.             new java.lang.String[] {
  235.                 "4Y", "5Y", "6Y", "7Y", "8Y", "9Y", "10Y", "11Y", "12Y", "15Y", "20Y", "25Y", "30Y", "40Y", "50Y"
  236.             },
  237.             adblSwapQuote
  238.         );

  239.         return ScenarioDiscountCurveBuilder.CubicKLKHyperbolicDFRateShapePreserver (
  240.             "KLK_HYPERBOLIC_SHAPE_TEMPLATE",
  241.             new ValuationParams (
  242.                 dtSpot,
  243.                 dtSpot,
  244.                 strCurrency
  245.             ),
  246.             aDepositComp,
  247.             adblDepositQuote,
  248.             astrDepositManifestMeasure,
  249.             aSwapComp,
  250.             adblSwapQuote,
  251.             astrSwapManifestMeasure,
  252.             true
  253.         );
  254.     }

  255.     private static final FixFloatComponent MakeFixFloatSwap (
  256.         final JulianDate dtEffective,
  257.         final String strCurrency,
  258.         final String strFloaterTenor,
  259.         final String strMaturityTenor,
  260.         final boolean bInArrears)
  261.         throws Exception
  262.     {
  263.         UnitCouponAccrualSetting ucasFixed = new UnitCouponAccrualSetting (
  264.             4,
  265.             "Act/360",
  266.             false,
  267.             "Act/360",
  268.             false,
  269.             strCurrency,
  270.             false,
  271.             CompositePeriodBuilder.ACCRUAL_COMPOUNDING_RULE_GEOMETRIC
  272.         );

  273.         ComposableFixedUnitSetting cfusFixed = new ComposableFixedUnitSetting (
  274.             "3M",
  275.             CompositePeriodBuilder.EDGE_DATE_SEQUENCE_REGULAR,
  276.             null,
  277.             0.02,
  278.             0.,
  279.             strCurrency
  280.         );

  281.         CompositePeriodSetting cpsFixed = new CompositePeriodSetting (
  282.             4,
  283.             "3M",
  284.             strCurrency,
  285.             null,
  286.             1.,
  287.             null,
  288.             null,
  289.             null,
  290.             null
  291.         );

  292.         ComposableFloatingUnitSetting cfusFloating = new ComposableFloatingUnitSetting (
  293.             "3M",
  294.             CompositePeriodBuilder.EDGE_DATE_SEQUENCE_REGULAR,
  295.             null,
  296.             ForwardLabel.Create (
  297.                 strCurrency,
  298.                 strFloaterTenor
  299.             ),
  300.             bInArrears ? CompositePeriodBuilder.REFERENCE_PERIOD_IN_ARREARS : CompositePeriodBuilder.REFERENCE_PERIOD_IN_ADVANCE,
  301.             0.
  302.         );

  303.         CompositePeriodSetting cpsFloating = new CompositePeriodSetting (
  304.             4,
  305.             "3M",
  306.             strCurrency,
  307.             null,
  308.             -1.,
  309.             null,
  310.             null,
  311.             null,
  312.             null
  313.         );

  314.         List<Integer> lsFixedStreamEdgeDate = CompositePeriodBuilder.RegularEdgeDates (
  315.             dtEffective,
  316.             "3M",
  317.             strMaturityTenor,
  318.             null
  319.         );

  320.         List<Integer> lsFloatingStreamEdgeDate = CompositePeriodBuilder.RegularEdgeDates (
  321.             dtEffective,
  322.             "3M",
  323.             strMaturityTenor,
  324.             null
  325.         );

  326.         Stream floatingStream = new Stream (
  327.             CompositePeriodBuilder.FloatingCompositeUnit (
  328.                 lsFloatingStreamEdgeDate,
  329.                 cpsFloating,
  330.                 cfusFloating
  331.             )
  332.         );

  333.         Stream fixedStream = new Stream (
  334.             CompositePeriodBuilder.FixedCompositeUnit (
  335.                 lsFixedStreamEdgeDate,
  336.                 cpsFixed,
  337.                 ucasFixed,
  338.                 cfusFixed
  339.             )
  340.         );

  341.         FixFloatComponent fixFloat = new FixFloatComponent (
  342.             fixedStream,
  343.             floatingStream,
  344.             new CashSettleParams (
  345.                 0,
  346.                 strCurrency,
  347.                 0
  348.             )
  349.         );

  350.         return fixFloat;
  351.     }

  352.     private static final double CMSFairPremium (
  353.         final String strCurrency,
  354.         final String strCMSFloaterTenor,
  355.         final String strCMSMaturityTenor,
  356.         final boolean bInArrears,
  357.         final JulianDate dtSpot,
  358.         final CurveSurfaceQuoteContainer mktParams)
  359.         throws Exception
  360.     {
  361.         FixFloatComponent cms = MakeFixFloatSwap (
  362.             dtSpot,
  363.             strCurrency,
  364.             strCMSFloaterTenor,
  365.             strCMSMaturityTenor,
  366.             bInArrears
  367.         );

  368.         CaseInsensitiveTreeMap<Double> mapCMSOutput = cms.value (
  369.             new ValuationParams (
  370.                 dtSpot,
  371.                 dtSpot,
  372.                 strCurrency
  373.             ),
  374.             null,
  375.             mktParams,
  376.             null
  377.         );

  378.         return mapCMSOutput.get ("FairPremium");
  379.     }

  380.     public static final void main (
  381.         final String[] astrArgs)
  382.         throws Exception
  383.     {
  384.         EnvManager.InitEnv ("");

  385.         String strCurrency = "USD";

  386.         JulianDate dtSpot = DateUtil.CreateFromYMD (
  387.             2012,
  388.             DateUtil.DECEMBER,
  389.             11
  390.         );

  391.         MergedDiscountForwardCurve dc = MakeDC (
  392.             dtSpot,
  393.             strCurrency
  394.         );

  395.         CurveSurfaceQuoteContainer mktParams = new CurveSurfaceQuoteContainer();

  396.         mktParams.setFundingState (dc);

  397.         String[] astrCMSFloaterTenor = new String[] {
  398.             "3M", "6M", "12M"
  399.         };

  400.         String[] astrCMSMaturityTenor = new String[] {
  401.             "24M", "60M", "10Y", "20Y", "30Y"
  402.         };

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

  404.         System.out.println ("\t| FIX-FLOAT CMS FAIR PREMIUM: 3M ADVANCE |");

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

  406.         System.out.println ("\t|  MATURITY  |    3M   |   6M   |   12M  |");

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

  408.         for (String strCMSMaturityTenor : astrCMSMaturityTenor) {
  409.             String strDump = "\t|     " + strCMSMaturityTenor + "    |  ";

  410.             for (String strCMSFloaterTenor : astrCMSFloaterTenor) {
  411.                 double dblFairPremium = CMSFairPremium (
  412.                     strCurrency,
  413.                     strCMSFloaterTenor,
  414.                     strCMSMaturityTenor,
  415.                     false,
  416.                     dtSpot,
  417.                     mktParams
  418.                 );

  419.                 strDump += FormatUtil.FormatDouble (dblFairPremium, 1, 2, 100) + "% | ";
  420.             }

  421.             System.out.println (strDump);
  422.         }

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

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

  425.         System.out.println ("\t| FIX-FLOAT CMS FAIR PREMIUM: 3M ARREARS |");

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

  427.         System.out.println ("\t|  MATURITY  |    3M   |   6M   |   12M  |");

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

  429.         for (String strCMSMaturityTenor : astrCMSMaturityTenor) {
  430.             String strDump = "\t|     " + strCMSMaturityTenor + "    |  ";

  431.             for (String strCMSFloaterTenor : astrCMSFloaterTenor) {
  432.                 double dblFairPremium = CMSFairPremium (
  433.                     strCurrency,
  434.                     strCMSFloaterTenor,
  435.                     strCMSMaturityTenor,
  436.                     true,
  437.                     dtSpot,
  438.                     mktParams
  439.                 );

  440.                 strDump += FormatUtil.FormatDouble (dblFairPremium, 1, 2, 100) + "% | ";
  441.             }

  442.             System.out.println (strDump);
  443.         }

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

  445.         EnvManager.TerminateEnv();
  446.     }
  447. }