FixFloatFixFloatAnalysis.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.fx.ComponentPair;
  11. import org.drip.product.params.*;
  12. import org.drip.product.rates.*;
  13. import org.drip.service.env.EnvManager;
  14. import org.drip.state.creator.*;
  15. import org.drip.state.identifier.*;

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

  19. /*!
  20.  * Copyright (C) 2020 Lakshmi Krishnamurthy
  21.  * Copyright (C) 2019 Lakshmi Krishnamurthy
  22.  * Copyright (C) 2018 Lakshmi Krishnamurthy
  23.  * Copyright (C) 2017 Lakshmi Krishnamurthy
  24.  * Copyright (C) 2016 Lakshmi Krishnamurthy
  25.  * Copyright (C) 2015 Lakshmi Krishnamurthy
  26.  * Copyright (C) 2014 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>FixFloatFixFloatAnalysis</i> demonstrates the Funding Volatility, Forward Volatility, FX Volatility,
  95.  * Funding/Forward Correlation, Funding/FX Correlation, and Forward/FX Correlation across the 2 currencies
  96.  * (USD and EUR) on the Valuation of the Cross Currency Basis Swap built out of a pair of fix-float swaps.
  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/cross/README.md">Single/Dual Stream XCCY Component</a></li>
  104.  *  </ul>
  105.  * <br><br>
  106.  *
  107.  * @author Lakshmi Krishnamurthy
  108.  */

  109. public class FixFloatFixFloatAnalysis {

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

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

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

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

  159.         CompositePeriodSetting cpsFixed = new CompositePeriodSetting (
  160.             2,
  161.             "6M",
  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> lsFloatingStreamEdgeDate = CompositePeriodBuilder.RegularEdgeDates (
  175.             dtEffective,
  176.             iTenorInMonths + "M",
  177.             strMaturityTenor,
  178.             null
  179.         );

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

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

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

  201.         FixFloatComponent fixFloat = new FixFloatComponent (
  202.             fixedStream,
  203.             floatingStream,
  204.             new CashSettleParams (
  205.                 0,
  206.                 strPayCurrency,
  207.                 0
  208.             )
  209.         );

  210.         return fixFloat;
  211.         }

  212.     private static final void SetMarketParams (
  213.         final int iValueDate,
  214.         final CurveSurfaceQuoteContainer mktParams,
  215.         final ForwardLabel forwardLabel1,
  216.         final ForwardLabel forwardLabel2,
  217.         final FundingLabel fundingLabel,
  218.         final FXLabel fxLabel,
  219.         final double dblForward1Vol,
  220.         final double dblForward2Vol,
  221.         final double dblFundingVol,
  222.         final double dblFXVol,
  223.         final double dblForward1FundingCorr,
  224.         final double dblForward2FundingCorr,
  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.             forwardLabel2,
  273.             fxLabel,
  274.             new FlatUnivariate (dblForward2FXCorr)
  275.         );

  276.         mktParams.setFundingFXCorrelation (
  277.             fundingLabel,
  278.             fxLabel,
  279.             new FlatUnivariate (dblFundingFXCorr)
  280.         );
  281.     }

  282.     private static final void VolCorrScenario (
  283.         final ComponentPair[] aCP,
  284.         final ValuationParams valParams,
  285.         final CurveSurfaceQuoteContainer mktParams,
  286.         final ForwardLabel forwardLabel1,
  287.         final ForwardLabel forwardLabel2,
  288.         final FundingLabel fundingLabel,
  289.         final FXLabel fxLabel,
  290.         final double dblForward1Vol,
  291.         final double dblForward2Vol,
  292.         final double dblFundingVol,
  293.         final double dblFXVol,
  294.         final double dblForward1FundingCorr,
  295.         final double dblForward2FundingCorr,
  296.         final double dblForward2FXCorr,
  297.         final double dblFundingFXCorr)
  298.         throws Exception
  299.     {
  300.         SetMarketParams (
  301.             valParams.valueDate(),
  302.             mktParams,
  303.             forwardLabel1,
  304.             forwardLabel2,
  305.             fundingLabel,
  306.             fxLabel,
  307.             dblForward1Vol,
  308.             dblForward2Vol,
  309.             dblFundingVol,
  310.             dblFXVol,
  311.             dblForward1FundingCorr,
  312.             dblForward2FundingCorr,
  313.             dblForward2FXCorr,
  314.             dblFundingFXCorr
  315.         );

  316.         String strDump = "\t[" +
  317.             FormatUtil.FormatDouble (dblForward1Vol, 2, 0, 100.) + "%," +
  318.             FormatUtil.FormatDouble (dblForward2Vol, 2, 0, 100.) + "%," +
  319.             FormatUtil.FormatDouble (dblFundingVol, 2, 0, 100.) + "%," +
  320.             FormatUtil.FormatDouble (dblFXVol, 2, 0, 100.) + "%," +
  321.             FormatUtil.FormatDouble (dblForward1FundingCorr, 2, 0, 100.) + "%," +
  322.             FormatUtil.FormatDouble (dblForward2FundingCorr, 2, 0, 100.) + "%," +
  323.             FormatUtil.FormatDouble (dblForward2FXCorr, 2, 0, 100.) + "%," +
  324.             FormatUtil.FormatDouble (dblFundingFXCorr, 2, 0, 100.) + "%] = ";

  325.         for (int i = 0; i < aCP.length; ++i) {
  326.             CaseInsensitiveTreeMap<Double> mapOutput = aCP[i].value (
  327.                 valParams,
  328.                 null,
  329.                 mktParams,
  330.                 null
  331.             );

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

  333.             strDump +=
  334.                 FormatUtil.FormatDouble (mapOutput.get ("ReferenceCumulativeConvexityAdjustmentPremium"), 2, 0, 10000.) + " | " +
  335.                 FormatUtil.FormatDouble (mapOutput.get ("DerivedCumulativeConvexityAdjustmentPremium"), 2, 0, 10000.) + " | " +
  336.                 FormatUtil.FormatDouble (mapOutput.get ("CumulativeConvexityAdjustmentPremium"), 2, 0, 10000.);
  337.         }

  338.         System.out.println (strDump);
  339.     }

  340.     public static final void main (
  341.         final String[] astrArgs)
  342.         throws Exception
  343.     {
  344.         double dblUSD3MForwardRate = 0.0275;
  345.         double dblEUR3MForwardRate = 0.0175;
  346.         double dblUSDFundingRate = 0.03;
  347.         double dblUSDEURFXRate = 1. / 1.34;

  348.         /*
  349.          * Initialize the Credit Analytics Library
  350.          */

  351.         EnvManager.InitEnv ("");

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

  353.         ValuationParams valParams = new ValuationParams (
  354.             dtToday,
  355.             dtToday,
  356.             "USD"
  357.         );

  358.         ForwardLabel fri3MUSD = ForwardLabel.Create (
  359.             "USD",
  360.             "3M"
  361.         );

  362.         ForwardLabel fri3MEUR = ForwardLabel.Create (
  363.             "EUR",
  364.             "3M"
  365.         );

  366.         FundingLabel fundingLabel = FundingLabel.Standard ("USD");

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

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

  369.         FixFloatComponent fixFloatUSD = MakeFixFloatSwap (
  370.             dtToday,
  371.             false,
  372.             "USD",
  373.             "USD",
  374.             "2Y",
  375.             3
  376.         );

  377.         FixFloatComponent fixFloatEURMTM = MakeFixFloatSwap (
  378.             dtToday,
  379.             true,
  380.             "USD",
  381.             "EUR",
  382.             "2Y",
  383.             3
  384.         );

  385.         ComponentPair cpMTM = new ComponentPair (
  386.             "FFFF_MTM",
  387.             fixFloatUSD,
  388.             fixFloatEURMTM,
  389.             null
  390.         );

  391.         FixFloatComponent fixFloatEURNonMTM = MakeFixFloatSwap (
  392.             dtToday,
  393.             false,
  394.             "USD",
  395.             "EUR",
  396.             "2Y",
  397.             3
  398.         );

  399.         ComponentPair cpNonMTM = new ComponentPair (
  400.             "FFFF_Non_MTM",
  401.             fixFloatUSD,
  402.             fixFloatEURNonMTM,
  403.             null
  404.         );

  405.         CurveSurfaceQuoteContainer mktParams = new CurveSurfaceQuoteContainer();

  406.         mktParams.setFixing (
  407.             dtToday,
  408.             fxLabel,
  409.             dblUSDEURFXRate
  410.         );

  411.         mktParams.setForwardState (
  412.             ScenarioForwardCurveBuilder.FlatForwardForwardCurve (
  413.                 dtToday,
  414.                 fri3MUSD,
  415.                 dblUSD3MForwardRate
  416.             )
  417.         );

  418.         mktParams.setForwardState (
  419.             ScenarioForwardCurveBuilder.FlatForwardForwardCurve (
  420.                 dtToday,
  421.                 fri3MEUR,
  422.                 dblEUR3MForwardRate
  423.             )
  424.         );

  425.         mktParams.setFundingState (
  426.             ScenarioDiscountCurveBuilder.ExponentiallyCompoundedFlatRate (
  427.                 dtToday,
  428.                 "USD",
  429.                 dblUSDFundingRate
  430.             )
  431.         );

  432.         mktParams.setFXState (
  433.             ScenarioFXCurveBuilder.CubicPolynomialCurve (
  434.                 fxLabel.fullyQualifiedName(),
  435.                 dtToday,
  436.                 cp,
  437.                 new String[] {"10Y"},
  438.                 new double[] {dblUSDEURFXRate},
  439.                 dblUSDEURFXRate
  440.             )
  441.         );

  442.         double[] adblUSD3MForwardVol = new double[] {
  443.             0.1, 0.4
  444.         };
  445.         double[] adblEUR3MForwardVol = new double[] {
  446.             0.1, 0.4
  447.         };
  448.         double[] adblUSDFundingVol = new double[] {
  449.             0.1, 0.4
  450.         };
  451.         double[] adblUSDEURFXVol = new double[] {
  452.             0.1, 0.4
  453.         };
  454.         double[] adblUSD3MForwardUSDFundingCorr = new double[] {
  455.             -0.1, 0.2
  456.         };
  457.         double[] adblEUR3MForwardUSDFundingCorr = new double[] {
  458.             -0.1, 0.2
  459.         };
  460.         double[] adblEUR3MForwardUSDEURFXCorr = new double[] {
  461.             -0.1, 0.2
  462.         };
  463.         double[] adblUSDFundingUSDEURFXCorr = new double[] {
  464.             -0.1, 0.2
  465.         };

  466.         for (double dblUSD3MForwardVol : adblUSD3MForwardVol) {
  467.             for (double dblEUR3MForwardVol : adblEUR3MForwardVol) {
  468.                 for (double dblUSDFundingVol : adblUSDFundingVol) {
  469.                     for (double dblUSDEURFXVol : adblUSDEURFXVol) {
  470.                         for (double dblUSD3MForwardUSDFundingCorr : adblUSD3MForwardUSDFundingCorr) {
  471.                             for (double dblEUR3MForwardUSDFundingCorr : adblEUR3MForwardUSDFundingCorr) {
  472.                                 for (double dblEUR3MForwardUSDEURFXCorr : adblEUR3MForwardUSDEURFXCorr) {
  473.                                     for (double dblUSDFundingUSDEURFXCorr : adblUSDFundingUSDEURFXCorr)
  474.                                         VolCorrScenario (
  475.                                             new ComponentPair[] {
  476.                                                 cpMTM,
  477.                                                 cpNonMTM
  478.                                             },
  479.                                             valParams,
  480.                                             mktParams,
  481.                                             fri3MUSD,
  482.                                             fri3MEUR,
  483.                                             fundingLabel,
  484.                                             fxLabel,
  485.                                             dblUSD3MForwardVol,
  486.                                             dblEUR3MForwardVol,
  487.                                             dblUSDFundingVol,
  488.                                             dblUSDEURFXVol,
  489.                                             dblUSD3MForwardUSDFundingCorr,
  490.                                             dblEUR3MForwardUSDFundingCorr,
  491.                                             dblEUR3MForwardUSDEURFXCorr,
  492.                                             dblUSDFundingUSDEURFXCorr
  493.                                         );
  494.                                 }
  495.                             }
  496.                         }
  497.                     }
  498.                 }
  499.             }
  500.         }

  501.         EnvManager.TerminateEnv();
  502.     }
  503. }