FloatFloatFloatFloat.java

  1. package org.drip.sample.cross;

  2. import java.util.*;

  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.*;
  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>FloatFloatFloatFloat</i> demonstrates the construction, the usage, and the eventual valuation of the
  95.  * Cross Currency Basis Swap built out of a pair of float-float swaps.
  96.  *  
  97.  * <br><br>
  98.  *  <ul>
  99.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ProductCore.md">Product Core Module</a></li>
  100.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/FixedIncomeAnalyticsLibrary.md">Fixed Income Analytics</a></li>
  101.  *      <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>
  102.  *      <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>
  103.  *  </ul>
  104.  * <br><br>
  105.  *
  106.  * @author Lakshmi Krishnamurthy
  107.  */

  108. public class FloatFloatFloatFloat {

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

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

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

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

  171.         List<Integer> lsReferenceStreamEdgeDate = CompositePeriodBuilder.RegularEdgeDates (
  172.             dtEffective,
  173.             iTenorInMonthsReference + "M",
  174.             strMaturityTenor,
  175.             null
  176.         );

  177.         List<Integer> lsDerivedStreamEdgeDate = CompositePeriodBuilder.RegularEdgeDates (
  178.             dtEffective,
  179.             iTenorInMonthsDerived + "M",
  180.             strMaturityTenor,
  181.             null
  182.         );

  183.         Stream referenceStream = new Stream (
  184.             CompositePeriodBuilder.FloatingCompositeUnit (
  185.                 lsReferenceStreamEdgeDate,
  186.                 cpsReference,
  187.                 cfusReference
  188.             )
  189.         );

  190.         Stream derivedStream = new Stream (
  191.             CompositePeriodBuilder.FloatingCompositeUnit (
  192.                 lsDerivedStreamEdgeDate,
  193.                 cpsDerived,
  194.                 cfusDerived
  195.             )
  196.         );

  197.         CashSettleParams csp = new CashSettleParams (
  198.             0,
  199.             strPayCurrency,
  200.             0
  201.         );

  202.         return new FloatFloatComponent (
  203.             referenceStream,
  204.             derivedStream,
  205.             csp
  206.         );
  207.     }

  208.     public static final void main (
  209.         final String[] astrArgs)
  210.         throws Exception
  211.     {
  212.         String strReferenceCurrency = "USD";
  213.         String strDerivedCurrency = "EUR";

  214.         double dblReference3MForwardRate = 0.00750;
  215.         double dblReference6MForwardRate = 0.01000;
  216.         double dblDerived3MForwardRate = 0.00375;
  217.         double dblDerived6MForwardRate = 0.00625;
  218.         double dblReferenceFundingRate = 0.02;
  219.         double dblReferenceDerivedFXRate = 1. / 1.28;

  220.         double dblReference3MForwardVol = 0.3;
  221.         double dblReference6MForwardVol = 0.3;
  222.         double dblDerived3MForwardVol = 0.3;
  223.         double dblDerived6MForwardVol = 0.3;
  224.         double dblReferenceFundingVol = 0.3;
  225.         double dblReferenceDerivedFXVol = 0.3;

  226.         double dblReference3MForwardFundingCorr = 0.15;
  227.         double dblReference6MForwardFundingCorr = 0.15;
  228.         double dblDerived3MForwardFundingCorr = 0.15;
  229.         double dblDerived6MForwardFundingCorr = 0.15;

  230.         double dblReference3MForwardFXCorr = 0.15;
  231.         double dblReference6MForwardFXCorr = 0.15;
  232.         double dblDerived3MForwardFXCorr = 0.15;
  233.         double dblDerived6MForwardFXCorr = 0.15;

  234.         double dblFundingFXCorr = 0.15;

  235.         /*
  236.          * Initialize the Credit Analytics Library
  237.          */

  238.         EnvManager.InitEnv ("");

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

  240.         ValuationParams valParams = new ValuationParams (
  241.             dtToday,
  242.             dtToday,
  243.             strReferenceCurrency
  244.         );

  245.         ForwardLabel fri3MReference = ForwardLabel.Create (
  246.             strReferenceCurrency,
  247.             "3M"
  248.         );

  249.         ForwardLabel fri6MReference = ForwardLabel.Create (
  250.             strReferenceCurrency,
  251.             "6M"
  252.         );

  253.         ForwardLabel fri3MDerived = ForwardLabel.Create (
  254.             strDerivedCurrency,
  255.             "3M"
  256.         );

  257.         ForwardLabel fri6MDerived = ForwardLabel.Create (
  258.             strDerivedCurrency,
  259.             "6M"
  260.         );

  261.         FundingLabel fundingLabelReference = FundingLabel.Standard (strReferenceCurrency);

  262.         CurrencyPair cp = CurrencyPair.FromCode (strReferenceCurrency + "/" + strDerivedCurrency);

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

  264.         FloatFloatComponent floatFloatReference = MakeFloatFloatSwap (
  265.             dtToday,
  266.             false,
  267.             strReferenceCurrency,
  268.             strReferenceCurrency,
  269.             "2Y",
  270.             6,
  271.             3
  272.         );

  273.         floatFloatReference.setPrimaryCode (
  274.             "FLOAT::FLOAT::" + strReferenceCurrency + "::" + strReferenceCurrency + "_3M::" + strReferenceCurrency + "_6M::2Y"
  275.         );

  276.         FloatFloatComponent floatFloatDerivedMTM = MakeFloatFloatSwap (
  277.             dtToday,
  278.             true,
  279.             strReferenceCurrency,
  280.             strDerivedCurrency,
  281.             "2Y",
  282.             6,
  283.             3
  284.         );

  285.         floatFloatDerivedMTM.setPrimaryCode (
  286.             "FLOAT::FLOAT::MTM::" + strReferenceCurrency + "::" + strDerivedCurrency + "_3M::" + strDerivedCurrency + "_6M::2Y"
  287.         );

  288.         ComponentPair cpMTM = new ComponentPair (
  289.             "FFFF_MTM",
  290.             floatFloatReference,
  291.             floatFloatDerivedMTM,
  292.             null
  293.         );

  294.         FloatFloatComponent floatFloatDerivedNonMTM = MakeFloatFloatSwap (
  295.             dtToday,
  296.             false,
  297.             strReferenceCurrency,
  298.             strDerivedCurrency,
  299.             "2Y",
  300.             6,
  301.             3
  302.         );

  303.         floatFloatDerivedNonMTM.setPrimaryCode (
  304.             "FLOAT::FLOAT::NONMTM::" + strReferenceCurrency + "::" + strDerivedCurrency + "_3M::" + strDerivedCurrency + "_6M::2Y"
  305.         );

  306.         ComponentPair cpNonMTM = new ComponentPair (
  307.             "FFFF_NonMTM",
  308.             floatFloatReference,
  309.             floatFloatDerivedNonMTM,
  310.             null
  311.         );

  312.         CurveSurfaceQuoteContainer mktParams = new CurveSurfaceQuoteContainer();

  313.         mktParams.setFixing (
  314.             dtToday,
  315.             fxLabel,
  316.             dblReferenceDerivedFXRate
  317.         );

  318.         mktParams.setForwardState (
  319.             ScenarioForwardCurveBuilder.FlatForwardForwardCurve (
  320.                 dtToday,
  321.                 fri3MReference,
  322.                 dblReference3MForwardRate
  323.             )
  324.         );

  325.         mktParams.setForwardState (
  326.             ScenarioForwardCurveBuilder.FlatForwardForwardCurve (
  327.                 dtToday,
  328.                 fri6MReference,
  329.                 dblReference6MForwardRate
  330.             )
  331.         );

  332.         mktParams.setForwardState (
  333.             ScenarioForwardCurveBuilder.FlatForwardForwardCurve (
  334.                 dtToday,
  335.                 fri3MDerived,
  336.                 dblDerived3MForwardRate
  337.             )
  338.         );

  339.         mktParams.setForwardState (
  340.             ScenarioForwardCurveBuilder.FlatForwardForwardCurve (
  341.                 dtToday,
  342.                 fri6MDerived,
  343.                 dblDerived6MForwardRate
  344.             )
  345.         );

  346.         mktParams.setFundingState (
  347.             ScenarioDiscountCurveBuilder.ExponentiallyCompoundedFlatRate (
  348.                 dtToday,
  349.                 strReferenceCurrency,
  350.                 dblReferenceFundingRate
  351.             )
  352.         );

  353.         mktParams.setFXState (
  354.             ScenarioFXCurveBuilder.CubicPolynomialCurve (
  355.                 fxLabel.fullyQualifiedName(),
  356.                 dtToday,
  357.                 cp,
  358.                 new String[] {"10Y"},
  359.                 new double[] {dblReferenceDerivedFXRate},
  360.                 dblReferenceDerivedFXRate
  361.             )
  362.         );

  363.         mktParams.setForwardVolatility (
  364.             ScenarioDeterministicVolatilityBuilder.FlatForward (
  365.                 dtToday.julian(),
  366.                 VolatilityLabel.Standard (fri3MReference),
  367.                 fri3MReference.currency(),
  368.                 dblReference3MForwardVol
  369.             )
  370.         );

  371.         mktParams.setForwardVolatility (
  372.             ScenarioDeterministicVolatilityBuilder.FlatForward (
  373.                 dtToday.julian(),
  374.                 VolatilityLabel.Standard (fri6MReference),
  375.                 fri6MReference.currency(),
  376.                 dblReference6MForwardVol
  377.             )
  378.         );

  379.         mktParams.setForwardVolatility (
  380.             ScenarioDeterministicVolatilityBuilder.FlatForward (
  381.                 dtToday.julian(),
  382.                 VolatilityLabel.Standard (fri3MDerived),
  383.                 fri3MDerived.currency(),
  384.                 dblDerived3MForwardVol
  385.             )
  386.         );

  387.         mktParams.setForwardVolatility (
  388.             ScenarioDeterministicVolatilityBuilder.FlatForward (
  389.                 dtToday.julian(),
  390.                 VolatilityLabel.Standard (fri6MDerived),
  391.                 fri6MDerived.currency(),
  392.                 dblDerived6MForwardVol
  393.             )
  394.         );

  395.         mktParams.setFundingVolatility (
  396.             ScenarioDeterministicVolatilityBuilder.FlatForward (
  397.                 dtToday.julian(),
  398.                 VolatilityLabel.Standard (fundingLabelReference),
  399.                 strReferenceCurrency,
  400.                 dblReferenceFundingVol
  401.             )
  402.         );

  403.         mktParams.setFXVolatility (
  404.             ScenarioDeterministicVolatilityBuilder.FlatForward (
  405.                 dtToday.julian(),
  406.                 VolatilityLabel.Standard (fxLabel),
  407.                 strDerivedCurrency,
  408.                 dblReferenceDerivedFXVol
  409.             )
  410.         );

  411.         mktParams.setForwardFundingCorrelation (
  412.             fri3MReference,
  413.             fundingLabelReference,
  414.             new FlatUnivariate (dblReference3MForwardFundingCorr)
  415.         );

  416.         mktParams.setForwardFundingCorrelation (
  417.             fri6MReference,
  418.             fundingLabelReference,
  419.             new FlatUnivariate (dblReference6MForwardFundingCorr)
  420.         );

  421.         mktParams.setForwardFundingCorrelation (
  422.             fri3MDerived,
  423.             fundingLabelReference,
  424.             new FlatUnivariate (dblDerived3MForwardFundingCorr)
  425.         );

  426.         mktParams.setForwardFundingCorrelation (
  427.             fri6MDerived,
  428.             fundingLabelReference,
  429.             new FlatUnivariate (dblDerived6MForwardFundingCorr)
  430.         );

  431.         mktParams.setForwardFXCorrelation (
  432.             fri3MReference,
  433.             fxLabel,
  434.             new FlatUnivariate (dblReference3MForwardFXCorr)
  435.         );

  436.         mktParams.setForwardFXCorrelation (
  437.             fri6MReference,
  438.             fxLabel,
  439.             new FlatUnivariate (dblReference6MForwardFXCorr)
  440.         );

  441.         mktParams.setForwardFXCorrelation (
  442.             fri3MDerived,
  443.             fxLabel,
  444.             new FlatUnivariate (dblDerived3MForwardFXCorr)
  445.         );

  446.         mktParams.setForwardFXCorrelation (
  447.             fri6MDerived,
  448.             fxLabel,
  449.             new FlatUnivariate (dblDerived6MForwardFXCorr)
  450.         );

  451.         mktParams.setFundingFXCorrelation (
  452.             fundingLabelReference,
  453.             fxLabel,
  454.             new FlatUnivariate (dblFundingFXCorr)
  455.         );

  456.         CaseInsensitiveTreeMap<Double> mapMTMOutput = cpMTM.value (
  457.             valParams,
  458.             null,
  459.             mktParams,
  460.             null
  461.         );

  462.         CaseInsensitiveTreeMap<Double> mapNonMTMOutput = cpNonMTM.value (
  463.             valParams,
  464.             null,
  465.             mktParams,
  466.             null
  467.         );

  468.         for (Map.Entry<String, Double> me : mapMTMOutput.entrySet()) {
  469.             String strKey = me.getKey();

  470.             if (null != me.getValue() && null != mapNonMTMOutput.get (strKey)) {
  471.                 double dblMTMMeasure = me.getValue();

  472.                 double dblNonMTMMeasure = mapNonMTMOutput.get (strKey);

  473.                 String strReconcile = NumberUtil.WithinTolerance (
  474.                     dblMTMMeasure,
  475.                     dblNonMTMMeasure,
  476.                     1.e-08,
  477.                     1.e-04
  478.                 ) ? "RECONCILES" : "DOES NOT RECONCILE";

  479.                 System.out.println ("\t" +
  480.                     FormatUtil.FormatDouble (dblMTMMeasure, 1, 8, 1.) + " | " +
  481.                     FormatUtil.FormatDouble (dblNonMTMMeasure, 1, 8, 1.) + " | " +
  482.                     strReconcile + " <= " + strKey);
  483.             }
  484.         }

  485.         EnvManager.TerminateEnv();
  486.     }
  487. }