FloatFloatVarianceAnalysis.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.function.r1tor1.FlatUnivariate;
  6. import org.drip.market.otc.*;
  7. import org.drip.numerical.common.FormatUtil;
  8. import org.drip.param.market.CurveSurfaceQuoteContainer;
  9. import org.drip.param.period.*;
  10. import org.drip.param.valuation.*;
  11. import org.drip.product.creator.SingleStreamComponentBuilder;
  12. import org.drip.product.definition.CalibratableComponent;
  13. import org.drip.product.rates.*;
  14. import org.drip.service.env.EnvManager;
  15. import org.drip.state.creator.*;
  16. import org.drip.state.discount.MergedDiscountForwardCurve;
  17. import org.drip.state.identifier.*;

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

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

  94. /**
  95.  * <i>FloatFloatVarianceAnalysis</i> demonstrates the Construction and Valuation Impact of Volatility and
  96.  * Correlation on the CMS Float-Float Swap.
  97.  *  
  98.  * <br><br>
  99.  *  <ul>
  100.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ProductCore.md">Product Core Module</a></li>
  101.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/FixedIncomeAnalyticsLibrary.md">Fixed Income Analytics</a></li>
  102.  *      <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>
  103.  *      <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>
  104.  *  </ul>
  105.  * <br><br>
  106.  *
  107.  * @author Lakshmi Krishnamurthy
  108.  */

  109. public class FloatFloatVarianceAnalysis {

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

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

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

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

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

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

  157.         return aCalibComp;
  158.     }

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

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

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

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

  177.         return aIRS;
  178.     }

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

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

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

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

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

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

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

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

  256.     private static final FloatFloatComponent MakeFloatFloatSwap (
  257.         final JulianDate dtEffective,
  258.         final String strCurrency,
  259.         final ForwardLabel forwardLabel,
  260.         final String strMaturityTenor,
  261.         final boolean bInArrears)
  262.         throws Exception
  263.     {
  264.         ComposableFloatingUnitSetting cfusReference = new ComposableFloatingUnitSetting (
  265.             "3M",
  266.             CompositePeriodBuilder.EDGE_DATE_SEQUENCE_REGULAR,
  267.             null,
  268.             ForwardLabel.Create (
  269.                 strCurrency,
  270.                 "3M"
  271.             ),
  272.             CompositePeriodBuilder.REFERENCE_PERIOD_IN_ADVANCE,
  273.             0.
  274.         );

  275.         ComposableFloatingUnitSetting cfusDerived = new ComposableFloatingUnitSetting (
  276.             "3M",
  277.             CompositePeriodBuilder.EDGE_DATE_SEQUENCE_REGULAR,
  278.             null,
  279.             forwardLabel,
  280.             bInArrears ? CompositePeriodBuilder.REFERENCE_PERIOD_IN_ARREARS : CompositePeriodBuilder.REFERENCE_PERIOD_IN_ADVANCE,
  281.             0.
  282.         );

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

  294.         CompositePeriodSetting cpsDerived = new CompositePeriodSetting (
  295.             4,
  296.             "3M",
  297.             strCurrency,
  298.             null,
  299.             -1.,
  300.             null,
  301.             null,
  302.             null,
  303.             null
  304.         );

  305.         List<Integer> lsReferenceStreamEdgeDate = CompositePeriodBuilder.RegularEdgeDates (
  306.             dtEffective,
  307.             "3M",
  308.             strMaturityTenor,
  309.             null
  310.         );

  311.         List<Integer> lsDerivedStreamEdgeDate = CompositePeriodBuilder.RegularEdgeDates (
  312.             dtEffective,
  313.             "3M",
  314.             strMaturityTenor,
  315.             null
  316.         );

  317.         Stream referenceStream = new Stream (
  318.             CompositePeriodBuilder.FloatingCompositeUnit (
  319.                 lsReferenceStreamEdgeDate,
  320.                 cpsReference,
  321.                 cfusReference
  322.             )
  323.         );

  324.         Stream derivedStream = new Stream (
  325.             CompositePeriodBuilder.FloatingCompositeUnit (
  326.                 lsDerivedStreamEdgeDate,
  327.                 cpsDerived,
  328.                 cfusDerived
  329.             )
  330.         );

  331.         FloatFloatComponent floatFloat = new FloatFloatComponent (
  332.             referenceStream,
  333.             derivedStream,
  334.             new CashSettleParams (0, strCurrency, 0)
  335.         );

  336.         return floatFloat;
  337.     }

  338.     private static final void SetMarketParams (
  339.         final int iValueDate,
  340.         final CurveSurfaceQuoteContainer mktParams,
  341.         final ForwardLabel forwardLabel,
  342.         final FundingLabel fundingLabel,
  343.         final double dblFundingVol,
  344.         final double dblForwardVol,
  345.         final double dblForwardFundingCorr)
  346.         throws Exception
  347.     {
  348.         mktParams.setForwardVolatility (
  349.             ScenarioDeterministicVolatilityBuilder.FlatForward (
  350.                 iValueDate,
  351.                 VolatilityLabel.Standard (forwardLabel),
  352.                 forwardLabel.currency(),
  353.                 dblForwardVol
  354.             )
  355.         );

  356.         mktParams.setFundingVolatility (
  357.             ScenarioDeterministicVolatilityBuilder.FlatForward (
  358.                 iValueDate,
  359.                 VolatilityLabel.Standard (fundingLabel),
  360.                 forwardLabel.currency(),
  361.                 dblFundingVol
  362.             )
  363.         );

  364.         mktParams.setForwardFundingCorrelation (
  365.             forwardLabel,
  366.             fundingLabel,
  367.             new FlatUnivariate (dblForwardFundingCorr)
  368.         );
  369.     }

  370.     private static final void VolCorrScenario (
  371.         final FloatFloatComponent[] aCMSFloatFloat,
  372.         final ValuationParams valParams,
  373.         final CurveSurfaceQuoteContainer mktParams,
  374.         final ForwardLabel forwardLabel,
  375.         final FundingLabel fundingLabel,
  376.         final double dblForwardVol,
  377.         final double dblFundingVol,
  378.         final double dblForwardFundingCorr,
  379.         final double dblBaseReferenceParBasisSpread)
  380.         throws Exception
  381.     {
  382.         SetMarketParams (
  383.             valParams.valueDate(),
  384.             mktParams,
  385.             forwardLabel,
  386.             fundingLabel,
  387.             dblForwardVol,
  388.             dblFundingVol,
  389.             dblForwardFundingCorr
  390.         );

  391.         String strDump = "\t[" +
  392.             FormatUtil.FormatDouble (dblForwardVol, 2, 0, 100.) + "%," +
  393.             FormatUtil.FormatDouble (dblFundingVol, 2, 0, 100.) + "%," +
  394.             FormatUtil.FormatDouble (dblForwardFundingCorr, 2, 0, 100.) + "%] = ";

  395.         for (int i = 0; i < aCMSFloatFloat.length; ++i) {
  396.             CaseInsensitiveTreeMap<Double> mapOutput = aCMSFloatFloat[i].value (
  397.                 valParams,
  398.                 null,
  399.                 mktParams,
  400.                 null
  401.             );

  402.             if (0 != i) strDump += " || ";

  403.             double dblReferenceParBasisSpread = mapOutput.get ("ReferenceParBasisSpread");

  404.             strDump +=
  405.                 FormatUtil.FormatDouble (dblReferenceParBasisSpread, 2, 1, 1.) + " | " +
  406.                 FormatUtil.FormatDouble (dblReferenceParBasisSpread - dblBaseReferenceParBasisSpread, 2, 1, 1.);
  407.         }

  408.         System.out.println (strDump + "  |");
  409.     }

  410.     public static final void main (
  411.         final String[] astrArgs)
  412.         throws Exception
  413.     {
  414.         EnvManager.InitEnv ("");

  415.         String strTenor = "6M";
  416.         String strCurrency = "USD";
  417.         String strMaturityTenor = "5Y";

  418.         JulianDate dtSpot = DateUtil.CreateFromYMD (
  419.             2012,
  420.             DateUtil.DECEMBER,
  421.             11
  422.         );

  423.         MergedDiscountForwardCurve dc = MakeDC (
  424.             dtSpot,
  425.             strCurrency
  426.         );

  427.         ForwardLabel forwardLabel = ForwardLabel.Create (
  428.             strCurrency,
  429.             strTenor
  430.         );

  431.         CurveSurfaceQuoteContainer mktParams = new CurveSurfaceQuoteContainer();

  432.         mktParams.setFundingState (dc);

  433.         FloatFloatComponent cmsInAdvance = MakeFloatFloatSwap (
  434.             dtSpot,
  435.             strCurrency,
  436.             forwardLabel,
  437.             strMaturityTenor,
  438.             false
  439.         );

  440.         FloatFloatComponent cmsInArrears = MakeFloatFloatSwap (
  441.             dtSpot,
  442.             strCurrency,
  443.             forwardLabel,
  444.             strMaturityTenor,
  445.             true
  446.         );

  447.         ValuationParams valParams = new ValuationParams (
  448.             dtSpot,
  449.             dtSpot,
  450.             strCurrency
  451.         );

  452.         double dblBaseReferenceParBasisSpread = cmsInAdvance.value (
  453.             valParams,
  454.             null,
  455.             mktParams,
  456.             null
  457.         ).get ("ReferenceParBasisSpread");

  458.         double[] adblForwardVol = new double[] {
  459.             0.10, 0.30, 0.50
  460.         };

  461.         double[] adblFundingVol = new double[] {
  462.             0.10, 0.30, 0.50
  463.         };

  464.         double[] adblForwardFundingCorr = new double[] {
  465.             -0.10, 0.25
  466.         };

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

  468.         System.out.println ("\t| CMS FLOAT-FLOAT IN-ADVANCE & IN-ARREARS ANALYSIS |");

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

  470.         System.out.println ("\t| INPUTS: L -> R                                   |");

  471.         System.out.println ("\t|                                                  |");

  472.         System.out.println ("\t|   Forward State Volatility                       |");

  473.         System.out.println ("\t|   Funding State Volatility                       |");

  474.         System.out.println ("\t|   Forward-Funding Correlation                    |");

  475.         System.out.println ("\t|                                                  |");

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

  477.         System.out.println ("\t| OUTPUTS: L -> R                                  |");

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

  479.         System.out.println ("\t|   In Advance Reference Par Basis Spread          |");

  480.         System.out.println ("\t|   In Advance Reference Par Basis Spread Change   |");

  481.         System.out.println ("\t|   In Arrears Reference Par Basis Spread          |");

  482.         System.out.println ("\t|   In Arrears Reference Par Basis Spread Change   |");

  483.         System.out.println ("\t|                                                  |");

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

  485.         for (double dblForwardVol : adblForwardVol) {
  486.             for (double dblFundingVol : adblFundingVol) {
  487.                 for (double dblForwardFundingCorr : adblForwardFundingCorr) {
  488.                     VolCorrScenario (
  489.                         new FloatFloatComponent[] {
  490.                             cmsInAdvance,
  491.                             cmsInArrears
  492.                         },
  493.                         valParams,
  494.                         mktParams,
  495.                         forwardLabel,
  496.                         FundingLabel.Standard (strCurrency),
  497.                         dblForwardVol,
  498.                         dblFundingVol,
  499.                         dblForwardFundingCorr,
  500.                         dblBaseReferenceParBasisSpread
  501.                     );
  502.                 }
  503.             }
  504.         }

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

  506.         EnvManager.TerminateEnv();
  507.     }
  508. }