FixFloatVarianceAnalysis.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>FixFloatVarianceAnalysis</i> demonstrates the Construction and Valuation Impact of Volatility and
  96.  * Correlation on the CMS Fix-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 FixFloatVarianceAnalysis {

  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 FixFloatComponent MakeFixFloatSwap (
  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.         UnitCouponAccrualSetting ucasFixed = new UnitCouponAccrualSetting (
  265.             4,
  266.             "Act/360",
  267.             false,
  268.             "Act/360",
  269.             false,
  270.             strCurrency,
  271.             false,
  272.             CompositePeriodBuilder.ACCRUAL_COMPOUNDING_RULE_GEOMETRIC
  273.         );

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

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

  293.         ComposableFloatingUnitSetting cfusFloating = new ComposableFloatingUnitSetting (
  294.             "3M",
  295.             CompositePeriodBuilder.EDGE_DATE_SEQUENCE_REGULAR,
  296.             null,
  297.             forwardLabel,
  298.             bInArrears ? CompositePeriodBuilder.REFERENCE_PERIOD_IN_ARREARS : CompositePeriodBuilder.REFERENCE_PERIOD_IN_ADVANCE,
  299.             0.
  300.         );

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

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

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

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

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

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

  348.         return fixFloat;
  349.     }

  350.     private static final void SetMarketParams (
  351.         final int iValueDate,
  352.         final CurveSurfaceQuoteContainer mktParams,
  353.         final ForwardLabel forwardLabel,
  354.         final FundingLabel fundingLabel,
  355.         final double dblFundingVol,
  356.         final double dblForwardVol,
  357.         final double dblForwardFundingCorr)
  358.         throws Exception
  359.     {
  360.         mktParams.setForwardVolatility (
  361.             ScenarioDeterministicVolatilityBuilder.FlatForward (
  362.                 iValueDate,
  363.                 VolatilityLabel.Standard (forwardLabel),
  364.                 forwardLabel.currency(),
  365.                 dblForwardVol
  366.             )
  367.         );

  368.         mktParams.setFundingVolatility (
  369.             ScenarioDeterministicVolatilityBuilder.FlatForward (
  370.                 iValueDate,
  371.                 VolatilityLabel.Standard (fundingLabel),
  372.                 forwardLabel.currency(),
  373.                 dblFundingVol
  374.             )
  375.         );

  376.         mktParams.setForwardFundingCorrelation (
  377.             forwardLabel,
  378.             fundingLabel,
  379.             new FlatUnivariate (dblForwardFundingCorr)
  380.         );
  381.     }

  382.     private static final void VolCorrScenario (
  383.         final FixFloatComponent[] aCMSFixFloat,
  384.         final ValuationParams valParams,
  385.         final CurveSurfaceQuoteContainer mktParams,
  386.         final ForwardLabel forwardLabel,
  387.         final FundingLabel fundingLabel,
  388.         final double dblForwardVol,
  389.         final double dblFundingVol,
  390.         final double dblForwardFundingCorr,
  391.         final double dblBaseFairPremium)
  392.         throws Exception
  393.     {
  394.         SetMarketParams (
  395.             valParams.valueDate(),
  396.             mktParams,
  397.             forwardLabel,
  398.             fundingLabel,
  399.             dblForwardVol,
  400.             dblFundingVol,
  401.             dblForwardFundingCorr
  402.         );

  403.         String strDump = "\t[" +
  404.             FormatUtil.FormatDouble (dblForwardVol, 2, 0, 100.) + "%," +
  405.             FormatUtil.FormatDouble (dblFundingVol, 2, 0, 100.) + "%," +
  406.             FormatUtil.FormatDouble (dblForwardFundingCorr, 2, 0, 100.) + "%] = ";

  407.         for (int i = 0; i < aCMSFixFloat.length; ++i) {
  408.             CaseInsensitiveTreeMap<Double> mapOutput = aCMSFixFloat[i].value (
  409.                 valParams,
  410.                 null,
  411.                 mktParams,
  412.                 null
  413.             );

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

  415.             double dblFairPremium = mapOutput.get ("FairPremium");

  416.             strDump +=
  417.                 FormatUtil.FormatDouble (dblFairPremium, 1, 2, 100.) + "% | " +
  418.                 FormatUtil.FormatDouble (dblFairPremium - dblBaseFairPremium, 2, 0, 10000.);
  419.         }

  420.         System.out.println (strDump + " |");
  421.     }

  422.     public static final void main (
  423.         final String[] astrArgs)
  424.         throws Exception
  425.     {
  426.         EnvManager.InitEnv ("");

  427.         String strTenor = "6M";
  428.         String strCurrency = "USD";
  429.         String strMaturityTenor = "5Y";

  430.         JulianDate dtSpot = DateUtil.CreateFromYMD (
  431.             2012,
  432.             DateUtil.DECEMBER,
  433.             11
  434.         );

  435.         MergedDiscountForwardCurve dc = MakeDC (
  436.             dtSpot,
  437.             strCurrency
  438.         );

  439.         ForwardLabel forwardLabel = ForwardLabel.Create (
  440.             strCurrency,
  441.             strTenor
  442.         );

  443.         CurveSurfaceQuoteContainer mktParams = new CurveSurfaceQuoteContainer();

  444.         mktParams.setFundingState (dc);

  445.         FixFloatComponent cmsInAdvance = MakeFixFloatSwap (
  446.             dtSpot,
  447.             strCurrency,
  448.             forwardLabel,
  449.             strMaturityTenor,
  450.             false
  451.         );

  452.         FixFloatComponent cmsInArrears = MakeFixFloatSwap (
  453.             dtSpot,
  454.             strCurrency,
  455.             forwardLabel,
  456.             strMaturityTenor,
  457.             true
  458.         );

  459.         ValuationParams valParams = new ValuationParams (
  460.             dtSpot,
  461.             dtSpot,
  462.             strCurrency
  463.         );

  464.         double dblBaseFairPremium = cmsInAdvance.value (
  465.             valParams,
  466.             null,
  467.             mktParams,
  468.             null
  469.         ).get ("FairPremium");

  470.         double[] adblForwardVol = new double[] {0.10, 0.30, 0.50};

  471.         double[] adblFundingVol = new double[] {0.10, 0.30, 0.50};

  472.         double[] adblForwardFundingCorr = new double[] {
  473.             -0.10, 0.25
  474.         };

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

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

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

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

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

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

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

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

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

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

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

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

  487.         System.out.println ("\t|   In Advance Fair Premium (%)                 |");

  488.         System.out.println ("\t|   In Advance Fair Premium Basis (bp)          |");

  489.         System.out.println ("\t|   In Arrears Fair Premium (%)                 |");

  490.         System.out.println ("\t|   In Arrears Fair Premium Basis (bp)          |");

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

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

  493.         for (double dblForwardVol : adblForwardVol) {
  494.             for (double dblFundingVol : adblFundingVol) {
  495.                 for (double dblForwardFundingCorr : adblForwardFundingCorr) {
  496.                     VolCorrScenario (
  497.                         new FixFloatComponent[] {
  498.                             cmsInAdvance,
  499.                             cmsInArrears
  500.                         },
  501.                         valParams,
  502.                         mktParams,
  503.                         forwardLabel,
  504.                         FundingLabel.Standard (strCurrency),
  505.                         dblForwardVol,
  506.                         dblFundingVol,
  507.                         dblForwardFundingCorr,
  508.                         dblBaseFairPremium
  509.                     );
  510.                 }
  511.             }
  512.         }

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

  514.         EnvManager.TerminateEnv();
  515.     }
  516. }