FixFloatFixFloat.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>FixFloatFixFloat</i> demonstrates the construction, the usage, and the eventual valuation of the Cross
  95.  * Currency Basis Swap built out of a pair of fix-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 FixFloatFixFloat {

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

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

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

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

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

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

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

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

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

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

  209.         return fixFloat;
  210.     }

  211.     public static final void main (
  212.         final String[] astrArgs)
  213.         throws Exception
  214.     {
  215.         double dblUSD3MForwardRate = 0.0275;
  216.         double dblEUR3MForwardRate = 0.0175;
  217.         double dblUSDFundingRate = 0.03;
  218.         double dblUSDEURFXRate = 1. / 1.34;

  219.         double dblUSD3MForwardVol = 0.3;
  220.         double dblEUR3MForwardVol = 0.3;
  221.         double dblUSDFundingVol = 0.3;
  222.         double dblUSDEURFXVol = 0.3;

  223.         double dblUSD3MForwardUSDFundingCorr = 0.15;
  224.         double dblEUR3MForwardUSDFundingCorr = 0.15;
  225.         double dblEUR3MForwardUSDEURFXCorr = 0.15;
  226.         double dblUSDFundingUSDEURFXCorr = 0.15;

  227.         /*
  228.          * Initialize the Credit Analytics Library
  229.          */

  230.         EnvManager.InitEnv ("");

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

  232.         ValuationParams valParams = new ValuationParams (
  233.             dtToday,
  234.             dtToday,
  235.             "USD"
  236.         );

  237.         ForwardLabel fri3MUSD = ForwardLabel.Create (
  238.             "USD",
  239.             "3M"
  240.         );

  241.         ForwardLabel fri3MEUR = ForwardLabel.Create (
  242.             "EUR",
  243.             "3M"
  244.         );

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

  246.         FundingLabel fundingLabelUSD = FundingLabel.Standard ("USD");

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

  248.         FixFloatComponent fixFloatUSD = MakeFixFloatSwap (
  249.             dtToday,
  250.             false,
  251.             "USD",
  252.             "USD",
  253.             "2Y",
  254.             3
  255.         );

  256.         FixFloatComponent fixFloatEURMTM = MakeFixFloatSwap (
  257.             dtToday,
  258.             true,
  259.             "USD",
  260.             "EUR",
  261.             "2Y",
  262.             3
  263.         );

  264.         ComponentPair cpMTM = new ComponentPair (
  265.             "FFFF_MTM",
  266.             fixFloatUSD,
  267.             fixFloatEURMTM,
  268.             null
  269.         );

  270.         FixFloatComponent fixFloatEURNonMTM = MakeFixFloatSwap (
  271.             dtToday,
  272.             false,
  273.             "USD",
  274.             "EUR",
  275.             "2Y",
  276.             3
  277.         );

  278.         ComponentPair cpNonMTM = new ComponentPair (
  279.             "FFFF_Non_MTM",
  280.             fixFloatUSD,
  281.             fixFloatEURNonMTM,
  282.             null
  283.         );

  284.         CurveSurfaceQuoteContainer mktParams = new CurveSurfaceQuoteContainer();

  285.         mktParams.setFixing (
  286.             dtToday,
  287.             fxLabel,
  288.             dblUSDEURFXRate
  289.         );

  290.         mktParams.setForwardState (
  291.             ScenarioForwardCurveBuilder.FlatForwardForwardCurve (
  292.                 dtToday,
  293.                 fri3MUSD,
  294.                 dblUSD3MForwardRate
  295.             )
  296.         );

  297.         mktParams.setForwardState (
  298.             ScenarioForwardCurveBuilder.FlatForwardForwardCurve (
  299.                 dtToday,
  300.                 fri3MEUR,
  301.                 dblEUR3MForwardRate
  302.             )
  303.         );

  304.         mktParams.setFundingState (
  305.             ScenarioDiscountCurveBuilder.ExponentiallyCompoundedFlatRate (
  306.                 dtToday,
  307.                 "USD",
  308.                 dblUSDFundingRate
  309.             )
  310.         );

  311.         mktParams.setFXState (
  312.             ScenarioFXCurveBuilder.CubicPolynomialCurve (
  313.                 fxLabel.fullyQualifiedName(),
  314.                 dtToday,
  315.                 cp,
  316.                 new String[] {"10Y"},
  317.                 new double[] {dblUSDEURFXRate},
  318.                 dblUSDEURFXRate
  319.             )
  320.         );

  321.         mktParams.setForwardVolatility (
  322.             ScenarioDeterministicVolatilityBuilder.FlatForward (
  323.                 dtToday.julian(),
  324.                 VolatilityLabel.Standard (fri3MUSD),
  325.                 "USD",
  326.                 dblUSD3MForwardVol
  327.             )
  328.         );

  329.         mktParams.setForwardVolatility (
  330.             ScenarioDeterministicVolatilityBuilder.FlatForward (
  331.                 dtToday.julian(),
  332.                 VolatilityLabel.Standard (fri3MEUR),
  333.                 "EUR",
  334.                 dblEUR3MForwardVol
  335.             )
  336.         );

  337.         mktParams.setFundingVolatility (
  338.             ScenarioDeterministicVolatilityBuilder.FlatForward (
  339.                 dtToday.julian(),
  340.                 VolatilityLabel.Standard (fundingLabelUSD),
  341.                 "USD",
  342.                 dblUSDFundingVol
  343.             )
  344.         );

  345.         mktParams.setFXVolatility (
  346.             ScenarioDeterministicVolatilityBuilder.FlatForward (
  347.                 dtToday.julian(),
  348.                 VolatilityLabel.Standard (fxLabel),
  349.                 "USD",
  350.                 dblUSDEURFXVol
  351.             )
  352.         );

  353.         mktParams.setForwardFundingCorrelation (
  354.             fri3MUSD,
  355.             fundingLabelUSD,
  356.             new FlatUnivariate (dblUSD3MForwardUSDFundingCorr)
  357.         );

  358.         mktParams.setForwardFundingCorrelation (
  359.             fri3MEUR,
  360.             fundingLabelUSD,
  361.             new FlatUnivariate (dblEUR3MForwardUSDFundingCorr)
  362.         );

  363.         mktParams.setForwardFXCorrelation (
  364.             fri3MEUR,
  365.             fxLabel,
  366.             new FlatUnivariate (dblEUR3MForwardUSDEURFXCorr)
  367.         );

  368.         mktParams.setFundingFXCorrelation (
  369.             fundingLabelUSD,
  370.             fxLabel,
  371.             new FlatUnivariate (dblUSDFundingUSDEURFXCorr)
  372.         );

  373.         CaseInsensitiveTreeMap<Double> mapMTMOutput = cpMTM.value (
  374.             valParams,
  375.             null,
  376.             mktParams,
  377.             null
  378.         );

  379.         CaseInsensitiveTreeMap<Double> mapNonMTMOutput = cpNonMTM.value (
  380.             valParams,
  381.             null,
  382.             mktParams,
  383.             null
  384.         );

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

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

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

  390.                 String strReconcile = NumberUtil.WithinTolerance (
  391.                     dblMTMMeasure,
  392.                     dblNonMTMMeasure,
  393.                     1.e-08,
  394.                     1.e-04
  395.                 ) ? "RECONCILES" : "DOES NOT RECONCILE";

  396.                 System.out.println ("\t" +
  397.                     FormatUtil.FormatDouble (dblMTMMeasure, 1, 8, 1.) + " | " +
  398.                     FormatUtil.FormatDouble (dblNonMTMMeasure, 1, 8, 1.) + " | " +
  399.                     strReconcile + " <= " + strKey);
  400.             }
  401.         }

  402.         EnvManager.TerminateEnv();
  403.     }
  404. }