CrossFixedPlainFloatAnalysis.java

  1. package org.drip.sample.cross;

  2. import java.util.List;

  3. import org.drip.analytics.date.JulianDate;
  4. import org.drip.analytics.support.*;
  5. import org.drip.function.r1tor1.FlatUnivariate;
  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.params.*;
  11. import org.drip.product.rates.*;
  12. import org.drip.service.env.EnvManager;
  13. import org.drip.state.creator.*;
  14. import org.drip.state.discount.*;
  15. import org.drip.state.forward.ForwardCurve;
  16. import org.drip.state.identifier.*;

  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.  * Copyright (C) 2014 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>CrossFixedPlainFloatAnalysis</i> demonstrates the impact of Funding Volatility, Forward Volatility, and
  96.  * Funding/Forward Correlation on the Valuation of a fix-float swap with a EUR Fixed leg that pays in USD,
  97.  * and a USD Floating Leg. Comparison is done across MTM and non-MTM fixed Leg Counterparts.
  98.  *  
  99.  * <br><br>
  100.  *  <ul>
  101.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ProductCore.md">Product Core Module</a></li>
  102.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/FixedIncomeAnalyticsLibrary.md">Fixed Income Analytics</a></li>
  103.  *      <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>
  104.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/cross/README.md">Single/Dual Stream XCCY Component</a></li>
  105.  *  </ul>
  106.  * <br><br>
  107.  *
  108.  * @author Lakshmi Krishnamurthy
  109.  */

  110. public class CrossFixedPlainFloatAnalysis {

  111.     private static final FixFloatComponent MakeFixFloatSwap (
  112.         final JulianDate dtEffective,
  113.         final boolean bFXMTM,
  114.         final String strPayCurrency,
  115.         final String strFixedCouponCurrency,
  116.         final String strMaturityTenor,
  117.         final int iTenorInMonths)
  118.         throws Exception
  119.     {
  120.         UnitCouponAccrualSetting ucasFixed = new UnitCouponAccrualSetting (
  121.             2,
  122.             "Act/360",
  123.             false,
  124.             "Act/360",
  125.             false,
  126.             strFixedCouponCurrency,
  127.             false,
  128.             CompositePeriodBuilder.ACCRUAL_COMPOUNDING_RULE_GEOMETRIC
  129.         );

  130.         ComposableFloatingUnitSetting cfusFloating = new ComposableFloatingUnitSetting (
  131.             iTenorInMonths + "M",
  132.             CompositePeriodBuilder.EDGE_DATE_SEQUENCE_REGULAR,
  133.             null,
  134.             ForwardLabel.Create (
  135.                 strPayCurrency,
  136.                 iTenorInMonths + "M"
  137.             ),
  138.             CompositePeriodBuilder.REFERENCE_PERIOD_IN_ADVANCE,
  139.             0.
  140.         );

  141.         ComposableFixedUnitSetting cfusFixed = new ComposableFixedUnitSetting (
  142.             "6M",
  143.             CompositePeriodBuilder.EDGE_DATE_SEQUENCE_REGULAR,
  144.             null,
  145.             0.02,
  146.             0.,
  147.             strFixedCouponCurrency
  148.         );

  149.         CompositePeriodSetting cpsFloating = new CompositePeriodSetting (
  150.             12 / iTenorInMonths,
  151.             iTenorInMonths + "M",
  152.             strPayCurrency,
  153.             null,
  154.             -1.,
  155.             null,
  156.             null,
  157.             null,
  158.             null
  159.         );

  160.         CompositePeriodSetting cpsFixed = new CompositePeriodSetting (
  161.             2,
  162.             "6M",
  163.             strPayCurrency,
  164.             null,
  165.             1.,
  166.             null,
  167.             null,
  168.             bFXMTM ? null : new FixingSetting (
  169.                 FixingSetting.FIXING_PRESET_STATIC,
  170.                 null,
  171.                 dtEffective.julian()
  172.             ),
  173.             null
  174.         );

  175.         List<Integer> lsFloatingStreamEdgeDate = CompositePeriodBuilder.RegularEdgeDates (
  176.             dtEffective,
  177.             iTenorInMonths + "M",
  178.             strMaturityTenor,
  179.             null
  180.         );

  181.         List<Integer> lsFixedStreamEdgeDate = CompositePeriodBuilder.RegularEdgeDates (
  182.             dtEffective,
  183.             "6M",
  184.             strMaturityTenor,
  185.             null
  186.         );

  187.         Stream floatingStream = new Stream (
  188.             CompositePeriodBuilder.FloatingCompositeUnit (
  189.                 lsFloatingStreamEdgeDate,
  190.                 cpsFloating,
  191.                 cfusFloating
  192.             )
  193.         );

  194.         Stream fixedStream = new Stream (
  195.             CompositePeriodBuilder.FixedCompositeUnit (
  196.                 lsFixedStreamEdgeDate,
  197.                 cpsFixed,
  198.                 ucasFixed,
  199.                 cfusFixed
  200.             )
  201.         );

  202.         /*
  203.          * The fix-float swap instance
  204.          */

  205.         FixFloatComponent fixFloat = new FixFloatComponent (
  206.             fixedStream,
  207.             floatingStream,
  208.             new CashSettleParams (
  209.                 0,
  210.                 strPayCurrency,
  211.                 0
  212.             )
  213.         );

  214.         return fixFloat;
  215.     }

  216.     private static final void SetMarketParams (
  217.         final int iValueDate,
  218.         final CurveSurfaceQuoteContainer mktParams,
  219.         final ForwardLabel forwardLabel,
  220.         final FundingLabel fundingLabel,
  221.         final FXLabel fxLabel,
  222.         final double dblForwardVol,
  223.         final double dblFundingVol,
  224.         final double dblFXVol,
  225.         final double dblForwardFundingCorr,
  226.         final double dblForwardFXCorr,
  227.         final double dblFundingFXCorr)
  228.         throws Exception
  229.     {
  230.         mktParams.setForwardVolatility (
  231.             ScenarioDeterministicVolatilityBuilder.FlatForward (
  232.                 iValueDate,
  233.                 VolatilityLabel.Standard (forwardLabel),
  234.                 forwardLabel.currency(),
  235.                 dblForwardVol
  236.             )
  237.         );

  238.         mktParams.setFundingVolatility (
  239.             ScenarioDeterministicVolatilityBuilder.FlatForward (
  240.                 iValueDate,
  241.                 VolatilityLabel.Standard (fundingLabel),
  242.                 forwardLabel.currency(),
  243.                 dblFundingVol
  244.             )
  245.         );

  246.         mktParams.setFXVolatility (
  247.             ScenarioDeterministicVolatilityBuilder.FlatForward (
  248.                 iValueDate,
  249.                 VolatilityLabel.Standard (fxLabel),
  250.                 forwardLabel.currency(),
  251.                 dblFXVol
  252.             )
  253.         );

  254.         mktParams.setForwardFundingCorrelation (
  255.             forwardLabel,
  256.             fundingLabel,
  257.             new FlatUnivariate (dblForwardFundingCorr)
  258.         );

  259.         mktParams.setForwardFXCorrelation (
  260.             forwardLabel,
  261.             fxLabel,
  262.             new FlatUnivariate (dblForwardFXCorr)
  263.         );

  264.         mktParams.setFundingFXCorrelation (
  265.             fundingLabel,
  266.             fxLabel,
  267.             new FlatUnivariate (dblFundingFXCorr)
  268.         );
  269.     }

  270.     private static final void VolCorrScenario (
  271.         final FixFloatComponent[] aFixFloat,
  272.         final ValuationParams valParams,
  273.         final CurveSurfaceQuoteContainer mktParams,
  274.         final ForwardLabel forwardLabel,
  275.         final FundingLabel fundingLabel,
  276.         final FXLabel fxLabel,
  277.         final double dblForwardVol,
  278.         final double dblFundingVol,
  279.         final double dblFXVol,
  280.         final double dblForwardFundingCorr,
  281.         final double dblForwardFXCorr,
  282.         final double dblFundingFXCorr)
  283.         throws Exception
  284.     {
  285.         SetMarketParams (
  286.             valParams.valueDate(),
  287.             mktParams,
  288.             forwardLabel,
  289.             fundingLabel,
  290.             fxLabel,
  291.             dblForwardVol,
  292.             dblFundingVol,
  293.             dblFXVol,
  294.             dblForwardFundingCorr,
  295.             dblForwardFXCorr,
  296.             dblFundingFXCorr
  297.         );

  298.         String strDump = "\t[" +
  299.             FormatUtil.FormatDouble (dblForwardVol, 2, 0, 100.) + "%," +
  300.             FormatUtil.FormatDouble (dblFundingVol, 2, 0, 100.) + "%," +
  301.             FormatUtil.FormatDouble (dblFXVol, 2, 0, 100.) + "%," +
  302.             FormatUtil.FormatDouble (dblForwardFundingCorr, 2, 0, 100.) + "%," +
  303.             FormatUtil.FormatDouble (dblForwardFXCorr, 2, 0, 100.) + "%," +
  304.             FormatUtil.FormatDouble (dblFundingFXCorr, 2, 0, 100.) + "%] = ";

  305.         for (int i = 0; i < aFixFloat.length; ++i) {
  306.             CaseInsensitiveTreeMap<Double> mapOutput = aFixFloat[i].value (valParams, null, mktParams, null);

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

  308.             strDump +=
  309.                 FormatUtil.FormatDouble (mapOutput.get ("ReferenceCumulativeConvexityAdjustmentPremium"), 2, 0, 10000.) + " | " +
  310.                 FormatUtil.FormatDouble (mapOutput.get ("DerivedCumulativeConvexityAdjustmentPremium"), 2, 0, 10000.) + " | " +
  311.                 FormatUtil.FormatDouble (mapOutput.get ("CumulativeConvexityAdjustmentPremium"), 2, 0, 10000.);
  312.         }

  313.         System.out.println (strDump);
  314.     }

  315.     public static final void main (
  316.         final String[] astrArgs)
  317.         throws Exception
  318.     {
  319.         double dblUSDCollateralRate = 0.02;
  320.         double dblEURCollateralRate = 0.02;
  321.         double dblUSD3MForwardRate = 0.02;
  322.         double dblUSDEURFXRate = 1. / 1.35;

  323.         /*
  324.          * Initialize the Credit Analytics Library
  325.          */

  326.         EnvManager.InitEnv ("");

  327.         JulianDate dtToday = org.drip.analytics.date.DateUtil.Today();

  328.         ValuationParams valParams = new ValuationParams (
  329.             dtToday,
  330.             dtToday,
  331.             "USD"
  332.         );

  333.         ForwardLabel fri3M = ForwardLabel.Create (
  334.             "USD",
  335.             "3M"
  336.         );

  337.         MergedDiscountForwardCurve dcUSDCollatDomestic = ScenarioDiscountCurveBuilder.ExponentiallyCompoundedFlatRate (
  338.             dtToday,
  339.             "USD",
  340.             dblUSDCollateralRate
  341.         );

  342.         MergedDiscountForwardCurve dcEURCollatDomestic = ScenarioDiscountCurveBuilder.ExponentiallyCompoundedFlatRate (
  343.             dtToday,
  344.             "EUR",
  345.             dblEURCollateralRate
  346.         );

  347.         ForwardCurve fc3MUSD = ScenarioForwardCurveBuilder.FlatForwardForwardCurve (
  348.             dtToday,
  349.             fri3M,
  350.             dblUSD3MForwardRate
  351.         );

  352.         CurrencyPair cp = CurrencyPair.FromCode ("USD/EUR");

  353.         FixFloatComponent fixMTMFloat = MakeFixFloatSwap (
  354.             dtToday,
  355.             true,
  356.             "USD",
  357.             "EUR",
  358.             "2Y",
  359.             3
  360.         );

  361.         FixFloatComponent fixNonMTMFloat = MakeFixFloatSwap (
  362.             dtToday,
  363.             false,
  364.             "USD",
  365.             "EUR",
  366.             "2Y",
  367.             3
  368.         );

  369.         FXLabel fxLabel = FXLabel.Standard (cp);

  370.         CurveSurfaceQuoteContainer mktParams = new CurveSurfaceQuoteContainer();

  371.         mktParams.setFundingState (dcUSDCollatDomestic);

  372.         mktParams.setForwardState (fc3MUSD);

  373.         mktParams.setFundingState (dcEURCollatDomestic);

  374.         mktParams.setFXState (
  375.             ScenarioFXCurveBuilder.CubicPolynomialCurve (
  376.                 fxLabel.fullyQualifiedName(),
  377.                 dtToday,
  378.                 cp,
  379.                 new String[] {"10Y"},
  380.                 new double[] {dblUSDEURFXRate},
  381.                 dblUSDEURFXRate
  382.             )
  383.         );

  384.         mktParams.setFixing (
  385.             dtToday,
  386.             fxLabel,
  387.             dblUSDEURFXRate
  388.         );

  389.         double[] adblForwardVol = new double[] {
  390.             0.1, 0.35, 0.60
  391.         };

  392.         double[] adblFundingVol = new double[] {
  393.             0.1, 0.35, 0.60
  394.         };

  395.         double[] adblFXVol = new double[] {
  396.             0.1, 0.35, 0.60
  397.         };

  398.         double[] adblForwardFundingCorr = new double[] {
  399.             -0.1, 0.35
  400.         };

  401.         double[] adblForwardFXCorr = new double[] {
  402.             -0.1, 0.35
  403.         };

  404.         double[] adblFundingFXCorr = new double[] {
  405.             -0.1, 0.35
  406.         };

  407.         for (double dblForwardVol : adblForwardVol) {
  408.             for (double dblFundingVol : adblFundingVol) {
  409.                 for (double dblFXVol : adblFXVol) {
  410.                     for (double dblForwardFundingCorr : adblForwardFundingCorr) {
  411.                         for (double dblForwardFXCorr : adblForwardFXCorr) {
  412.                             for (double dblFundingFXCorr : adblFundingFXCorr)
  413.                                 VolCorrScenario (
  414.                                     new FixFloatComponent[] {
  415.                                         fixMTMFloat,
  416.                                         fixNonMTMFloat
  417.                                     },
  418.                                     valParams,
  419.                                     mktParams,
  420.                                     fri3M,
  421.                                     FundingLabel.Standard ("USD"),
  422.                                     fxLabel,
  423.                                     dblForwardVol,
  424.                                     dblFundingVol,
  425.                                     dblFXVol,
  426.                                     dblForwardFundingCorr,
  427.                                     dblForwardFXCorr,
  428.                                     dblFundingFXCorr
  429.                                 );
  430.                         }
  431.                     }
  432.                 }
  433.             }
  434.         }

  435.         EnvManager.TerminateEnv();
  436.     }
  437. }