CrossFloatCrossFloatAnalysis.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>CrossFloatCrossFloatAnalysis</i> demonstrates the impact of Funding Volatility, Forward Volatility, and
  96.  * Funding/Forward, Funding/FX, and Forward/FX Correlation for each of the FRI's on the Valuation of a
  97.  * float-float swap with a 3M EUR Floater leg that pays in USD, and a 6M EUR Floater leg that pays in USD.
  98.  * Comparison is done across MTM and non-MTM fixed Leg Counterparts.
  99.  *  
  100.  * <br><br>
  101.  *  <ul>
  102.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ProductCore.md">Product Core Module</a></li>
  103.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/FixedIncomeAnalyticsLibrary.md">Fixed Income Analytics</a></li>
  104.  *      <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>
  105.  *      <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>
  106.  *  </ul>
  107.  * <br><br>
  108.  *
  109.  * @author Lakshmi Krishnamurthy
  110.  */

  111. public class CrossFloatCrossFloatAnalysis {

  112.     private static final FloatFloatComponent MakeFloatFloatSwap (
  113.         final JulianDate dtEffective,
  114.         final boolean bFXMTM,
  115.         final String strPayCurrency,
  116.         final String strCouponCurrency,
  117.         final String strMaturityTenor,
  118.         final int iTenorInMonthsReference,
  119.         final int iTenorInMonthsDerived)
  120.         throws Exception
  121.     {
  122.         ComposableFloatingUnitSetting cfusReference = new ComposableFloatingUnitSetting (
  123.             iTenorInMonthsReference + "M",
  124.             CompositePeriodBuilder.EDGE_DATE_SEQUENCE_REGULAR,
  125.             null,
  126.             ForwardLabel.Create (
  127.                 strCouponCurrency,
  128.                 iTenorInMonthsReference + "M"
  129.             ),
  130.             CompositePeriodBuilder.REFERENCE_PERIOD_IN_ADVANCE,
  131.             0.
  132.         );

  133.         ComposableFloatingUnitSetting cfusDerived = new ComposableFloatingUnitSetting (
  134.             iTenorInMonthsDerived + "M",
  135.             CompositePeriodBuilder.EDGE_DATE_SEQUENCE_REGULAR,
  136.             null,
  137.             ForwardLabel.Create (
  138.                 strCouponCurrency,
  139.                 iTenorInMonthsDerived + "M"
  140.             ),
  141.             CompositePeriodBuilder.REFERENCE_PERIOD_IN_ADVANCE,
  142.             0.
  143.         );

  144.         CompositePeriodSetting cpsReference = new CompositePeriodSetting (
  145.             12 / iTenorInMonthsReference,
  146.             iTenorInMonthsReference + "M",
  147.             strPayCurrency,
  148.             null,
  149.             -1.,
  150.             null,
  151.             null,
  152.             bFXMTM ? null : new FixingSetting (
  153.                 FixingSetting.FIXING_PRESET_STATIC,
  154.                 null,
  155.                 dtEffective.julian()
  156.             ),
  157.             null
  158.         );

  159.         CompositePeriodSetting cpsDerived = new CompositePeriodSetting (
  160.             12 / iTenorInMonthsDerived,
  161.             iTenorInMonthsDerived + "M",
  162.             strPayCurrency,
  163.             null,
  164.             1.,
  165.             null,
  166.             null,
  167.             bFXMTM ? null : new FixingSetting (
  168.                 FixingSetting.FIXING_PRESET_STATIC,
  169.                 null,
  170.                 dtEffective.julian()
  171.             ),
  172.             null
  173.         );

  174.         List<Integer> lsReferenceStreamEdgeDate = CompositePeriodBuilder.RegularEdgeDates (
  175.             dtEffective,
  176.             iTenorInMonthsReference + "M",
  177.             strMaturityTenor,
  178.             null
  179.         );

  180.         List<Integer> lsDerivedStreamEdgeDate = CompositePeriodBuilder.RegularEdgeDates (
  181.             dtEffective,
  182.             iTenorInMonthsDerived + "M",
  183.             strMaturityTenor,
  184.             null
  185.         );

  186.         Stream referenceStream = new Stream (
  187.             CompositePeriodBuilder.FloatingCompositeUnit (
  188.                 lsReferenceStreamEdgeDate,
  189.                 cpsReference,
  190.                 cfusReference
  191.             )
  192.         );

  193.         Stream derivedStream = new Stream (
  194.             CompositePeriodBuilder.FloatingCompositeUnit (
  195.                 lsDerivedStreamEdgeDate,
  196.                 cpsDerived,
  197.                 cfusDerived
  198.             )
  199.         );

  200.         CashSettleParams csp = new CashSettleParams (
  201.             0,
  202.             strPayCurrency,
  203.             0
  204.         );

  205.         return new FloatFloatComponent (
  206.             referenceStream,
  207.             derivedStream,
  208.             csp
  209.         );
  210.     }

  211.     private static final void SetMarketParams (
  212.         final int iValueDate,
  213.         final CurveSurfaceQuoteContainer mktParams,
  214.         final ForwardLabel forwardLabel1,
  215.         final ForwardLabel forwardLabel2,
  216.         final FundingLabel fundingLabel,
  217.         final FXLabel fxLabel,
  218.         final double dblForward1Vol,
  219.         final double dblForward2Vol,
  220.         final double dblFundingVol,
  221.         final double dblFXVol,
  222.         final double dblForward1FundingCorr,
  223.         final double dblForward2FundingCorr,
  224.         final double dblForward1FXCorr,
  225.         final double dblForward2FXCorr,
  226.         final double dblFundingFXCorr)
  227.         throws Exception
  228.     {
  229.         mktParams.setForwardVolatility (
  230.             ScenarioDeterministicVolatilityBuilder.FlatForward (
  231.                 iValueDate,
  232.                 VolatilityLabel.Standard (forwardLabel1),
  233.                 forwardLabel1.currency(),
  234.                 dblForward1Vol
  235.             )
  236.         );

  237.         mktParams.setForwardVolatility (
  238.             ScenarioDeterministicVolatilityBuilder.FlatForward (
  239.                 iValueDate,
  240.                 VolatilityLabel.Standard (forwardLabel2),
  241.                 forwardLabel2.currency(),
  242.                 dblForward2Vol
  243.             )
  244.         );

  245.         mktParams.setFundingVolatility (
  246.             ScenarioDeterministicVolatilityBuilder.FlatForward (
  247.                 iValueDate,
  248.                 VolatilityLabel.Standard (fundingLabel),
  249.                 forwardLabel1.currency(),
  250.                 dblFundingVol
  251.             )
  252.         );

  253.         mktParams.setFXVolatility (
  254.             ScenarioDeterministicVolatilityBuilder.FlatForward (
  255.                 iValueDate,
  256.                 VolatilityLabel.Standard (fxLabel),
  257.                 forwardLabel1.currency(),
  258.                 dblFXVol
  259.             )
  260.         );

  261.         mktParams.setForwardFundingCorrelation (
  262.             forwardLabel1,
  263.             fundingLabel,
  264.             new FlatUnivariate (dblForward1FundingCorr)
  265.         );

  266.         mktParams.setForwardFundingCorrelation (
  267.             forwardLabel2,
  268.             fundingLabel,
  269.             new FlatUnivariate (dblForward2FundingCorr)
  270.         );

  271.         mktParams.setForwardFXCorrelation (
  272.             forwardLabel1,
  273.             fxLabel,
  274.             new FlatUnivariate (dblForward1FXCorr)
  275.         );

  276.         mktParams.setForwardFXCorrelation (
  277.             forwardLabel2,
  278.             fxLabel,
  279.             new FlatUnivariate (dblForward2FXCorr)
  280.         );

  281.         mktParams.setFundingFXCorrelation (
  282.             fundingLabel,
  283.             fxLabel,
  284.             new FlatUnivariate (dblFundingFXCorr)
  285.         );
  286.     }

  287.     private static final void VolCorrScenario (
  288.         final FloatFloatComponent[] aFloatFloat,
  289.         final ValuationParams valParams,
  290.         final CurveSurfaceQuoteContainer mktParams,
  291.         final ForwardLabel forwardLabel1,
  292.         final ForwardLabel forwardLabel2,
  293.         final FundingLabel fundingLabel,
  294.         final FXLabel fxLabel,
  295.         final double dblForward1Vol,
  296.         final double dblForward2Vol,
  297.         final double dblFundingVol,
  298.         final double dblFXVol,
  299.         final double dblForward1FundingCorr,
  300.         final double dblForward2FundingCorr,
  301.         final double dblForward1FXCorr,
  302.         final double dblForward2FXCorr,
  303.         final double dblFundingFXCorr)
  304.         throws Exception
  305.     {
  306.         SetMarketParams (
  307.             valParams.valueDate(),
  308.             mktParams,
  309.             forwardLabel1,
  310.             forwardLabel2,
  311.             fundingLabel,
  312.             fxLabel,
  313.             dblForward1Vol,
  314.             dblForward2Vol,
  315.             dblFundingVol,
  316.             dblFXVol,
  317.             dblForward1FundingCorr,
  318.             dblForward2FundingCorr,
  319.             dblForward1FXCorr,
  320.             dblForward2FXCorr,
  321.             dblFundingFXCorr
  322.         );

  323.         String strDump = "\t[" +
  324.             FormatUtil.FormatDouble (dblForward1Vol, 2, 0, 100.) + "%," +
  325.             FormatUtil.FormatDouble (dblForward2Vol, 2, 0, 100.) + "%," +
  326.             FormatUtil.FormatDouble (dblFundingVol, 2, 0, 100.) + "%," +
  327.             FormatUtil.FormatDouble (dblFXVol, 2, 0, 100.) + "%," +
  328.             FormatUtil.FormatDouble (dblForward1FundingCorr, 2, 0, 100.) + "%," +
  329.             FormatUtil.FormatDouble (dblForward2FundingCorr, 2, 0, 100.) + "%," +
  330.             FormatUtil.FormatDouble (dblForward1FXCorr, 2, 0, 100.) + "%," +
  331.             FormatUtil.FormatDouble (dblForward2FXCorr, 2, 0, 100.) + "%," +
  332.             FormatUtil.FormatDouble (dblFundingFXCorr, 2, 0, 100.) + "%] = ";

  333.         for (int i = 0; i < aFloatFloat.length; ++i) {
  334.             CaseInsensitiveTreeMap<Double> mapOutput = aFloatFloat[i].value (
  335.                 valParams,
  336.                 null,
  337.                 mktParams,
  338.                 null
  339.             );

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

  341.             strDump +=
  342.                 FormatUtil.FormatDouble (mapOutput.get ("ReferenceCumulativeConvexityAdjustmentPremium"), 2, 0, 10000.) + " | " +
  343.                 FormatUtil.FormatDouble (mapOutput.get ("DerivedCumulativeConvexityAdjustmentPremium"), 2, 0, 10000.) + " | " +
  344.                 FormatUtil.FormatDouble (mapOutput.get ("CumulativeConvexityAdjustmentPremium"), 2, 0, 10000.);
  345.         }

  346.         System.out.println (strDump);
  347.     }

  348.     public static final void main (
  349.         final String[] astrArgs)
  350.         throws Exception
  351.     {
  352.         double dblEUR3MForwardRate = 0.02;
  353.         double dblEUR6MForwardRate = 0.025;
  354.         double dblUSDFundingRate = 0.02;
  355.         double dblUSDEURFXRate = 1. / 1.35;

  356.         /*
  357.          * Initialize the Credit Analytics Library
  358.          */

  359.         EnvManager.InitEnv ("");

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

  361.         ValuationParams valParams = new ValuationParams (
  362.             dtToday,
  363.             dtToday,
  364.             "EUR"
  365.         );

  366.         MergedDiscountForwardCurve dcUSDFunding = ScenarioDiscountCurveBuilder.ExponentiallyCompoundedFlatRate (
  367.             dtToday,
  368.             "USD",
  369.             dblUSDFundingRate
  370.         );

  371.         ForwardLabel friEUR3M = ForwardLabel.Create (
  372.             "EUR",
  373.             "3M"
  374.         );

  375.         ForwardCurve fcEUR3M = ScenarioForwardCurveBuilder.FlatForwardForwardCurve (
  376.             dtToday,
  377.             friEUR3M,
  378.             dblEUR3MForwardRate
  379.         );

  380.         ForwardLabel friEUR6M = ForwardLabel.Create (
  381.             "EUR",
  382.             "6M"
  383.         );

  384.         ForwardCurve fcEUR6M = ScenarioForwardCurveBuilder.FlatForwardForwardCurve (
  385.             dtToday,
  386.             friEUR6M,
  387.             dblEUR6MForwardRate
  388.         );

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

  390.         FloatFloatComponent floatFloatMTM = MakeFloatFloatSwap (
  391.             dtToday,
  392.             true,
  393.             "USD",
  394.             "EUR",
  395.             "2Y",
  396.             6,
  397.             3
  398.         );

  399.         floatFloatMTM.setPrimaryCode ("EUR__USD__MTM::FLOAT::3M::6M::2Y");

  400.         FloatFloatComponent floatFloatNonMTM = MakeFloatFloatSwap (
  401.             dtToday,
  402.             false,
  403.             "USD",
  404.             "EUR",
  405.             "2Y",
  406.             6,
  407.             3
  408.         );

  409.         floatFloatNonMTM.setPrimaryCode ("EUR__USD__NONMTM::FLOAT::3M::6M::2Y");

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

  411.         CurveSurfaceQuoteContainer mktParams = new CurveSurfaceQuoteContainer();

  412.         mktParams.setFixing (
  413.             dtToday,
  414.             fxLabel,
  415.             dblUSDEURFXRate
  416.         );

  417.         mktParams.setForwardState (fcEUR3M);

  418.         mktParams.setForwardState (fcEUR6M);

  419.         mktParams.setFundingState (dcUSDFunding);

  420.         mktParams.setFXState (
  421.             ScenarioFXCurveBuilder.CubicPolynomialCurve (
  422.                 fxLabel.fullyQualifiedName(),
  423.                 dtToday,
  424.                 cp,
  425.                 new String[] {"10Y"},
  426.                 new double[] {dblUSDEURFXRate},
  427.                 dblUSDEURFXRate
  428.             )
  429.         );

  430.         double[] adblEURForward3MVol = new double[] {
  431.             0.1, 0.3, 0.5
  432.         };

  433.         double[] adblEURForward6MVol = new double[] {
  434.             0.1, 0.3, 0.5
  435.         };

  436.         double[] adblUSDFundingVol = new double[] {
  437.             0.1, 0.3, 0.5
  438.         };

  439.         double[] adblUSDEURFXVol = new double[] {
  440.             0.1, 0.3, 0.5
  441.         };

  442.         double[] adblEUR3MUSDFundingCorr = new double[] {
  443.             -0.2, 0.25
  444.         };

  445.         double[] adblEUR6MUSDFundingCorr = new double[] {
  446.             -0.2, 0.25
  447.         };

  448.         double[] adblEUR3MUSDEURFXCorr = new double[] {
  449.             -0.2, 0.25
  450.         };

  451.         double[] adblEUR6MUSDEURFXCorr = new double[] {
  452.             -0.2, 0.25
  453.         };

  454.         double[] adblUSDFundingUSDEURFXCorr = new double[] {
  455.             -0.2, 0.25
  456.         };

  457.         for (double dblEURForward3MVol : adblEURForward3MVol) {
  458.             for (double dblEURForward6MVol : adblEURForward6MVol) {
  459.                 for (double dblUSDFundingVol : adblUSDFundingVol) {
  460.                     for (double dblUSDEURFXVol : adblUSDEURFXVol) {
  461.                         for (double dblEUR3MUSDFundingCorr : adblEUR3MUSDFundingCorr) {
  462.                             for (double dblEUR6MUSDFundingCorr : adblEUR6MUSDFundingCorr) {
  463.                                 for (double dblEUR3MUSDEURFXCorr : adblEUR3MUSDEURFXCorr) {
  464.                                     for (double dblEUR6MUSDEURFXCorr : adblEUR6MUSDEURFXCorr) {
  465.                                         for (double dblUSDFundingUSDEURFXCorr : adblUSDFundingUSDEURFXCorr)
  466.                                             VolCorrScenario (
  467.                                                 new FloatFloatComponent[] {
  468.                                                     floatFloatMTM,
  469.                                                     floatFloatNonMTM
  470.                                                 },
  471.                                                 valParams,
  472.                                                 mktParams,
  473.                                                 friEUR3M,
  474.                                                 friEUR6M,
  475.                                                 FundingLabel.Standard ("USD"),
  476.                                                 fxLabel,
  477.                                                 dblEURForward3MVol,
  478.                                                 dblEURForward6MVol,
  479.                                                 dblUSDFundingVol,
  480.                                                 dblUSDEURFXVol,
  481.                                                 dblEUR3MUSDFundingCorr,
  482.                                                 dblEUR6MUSDFundingCorr,
  483.                                                 dblEUR3MUSDEURFXCorr,
  484.                                                 dblEUR6MUSDEURFXCorr,
  485.                                                 dblUSDFundingUSDEURFXCorr
  486.                                             );
  487.                                     }
  488.                                 }
  489.                             }
  490.                         }
  491.                     }
  492.                 }
  493.             }
  494.         }

  495.         EnvManager.TerminateEnv();
  496.     }
  497. }