CrossFixedPlainFloat.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.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>CrossFixedPlainFloat</i> demonstrates the construction, usage, and eventual valuation of a fix-float
  96.  * swap with a EUR Fixed leg that pays in USD, and a USD Floating Leg. Comparison is done across MTM and
  97.  * 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 CrossFixedPlainFloat {

  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.     public static final void main (
  217.         final String[] astrArgs)
  218.         throws Exception
  219.     {
  220.         double dblUSDCollateralRate = 0.03;
  221.         double dblEURCollateralRate = 0.02;
  222.         double dblUSD3MForwardRate = 0.02;
  223.         double dblUSDEURFXRate = 1. / 1.35;

  224.         double dblUSDFundingVol = 0.1;
  225.         double dblUSD3MVol = 0.1;
  226.         double dblUSD3MUSDFundingCorr = 0.1;

  227.         double dblEURFundingVol = 0.1;
  228.         double dblUSDEURFXVol = 0.3;
  229.         double dblUSDFundingUSDEURFXCorr = 0.3;

  230.         /*
  231.          * Initialize the Credit Analytics Library
  232.          */

  233.         EnvManager.InitEnv ("");

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

  235.         ValuationParams valParams = new ValuationParams (
  236.             dtToday,
  237.             dtToday,
  238.             "USD"
  239.         );

  240.         ForwardLabel fri3MUSD = ForwardLabel.Create (
  241.             "USD",
  242.             "3M"
  243.         );

  244.         MergedDiscountForwardCurve dcUSDCollatDomestic = ScenarioDiscountCurveBuilder.ExponentiallyCompoundedFlatRate (
  245.             dtToday,
  246.             "USD",
  247.             dblUSDCollateralRate
  248.         );

  249.         MergedDiscountForwardCurve dcEURCollatDomestic = ScenarioDiscountCurveBuilder.ExponentiallyCompoundedFlatRate (
  250.             dtToday,
  251.             "EUR",
  252.             dblEURCollateralRate
  253.         );

  254.         ForwardCurve fc3MUSD = ScenarioForwardCurveBuilder.FlatForwardForwardCurve (
  255.             dtToday,
  256.             fri3MUSD,
  257.             dblUSD3MForwardRate
  258.         );

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

  260.         FixFloatComponent fixMTMFloat = MakeFixFloatSwap (
  261.             dtToday,
  262.             true,
  263.             "USD",
  264.             "EUR",
  265.             "2Y",
  266.             3
  267.         );

  268.         FixFloatComponent fixNonMTMFloat = MakeFixFloatSwap (
  269.             dtToday,
  270.             false,
  271.             "USD",
  272.             "EUR",
  273.             "2Y",
  274.             3
  275.         );

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

  277.         FundingLabel fundingLabelUSD = org.drip.state.identifier.FundingLabel.Standard ("USD");

  278.         FundingLabel fundingLabelEUR = org.drip.state.identifier.FundingLabel.Standard ("EUR");

  279.         CurveSurfaceQuoteContainer mktParams = new CurveSurfaceQuoteContainer();

  280.         mktParams.setFundingState (dcUSDCollatDomestic);

  281.         mktParams.setForwardState (fc3MUSD);

  282.         mktParams.setForwardVolatility (
  283.             ScenarioDeterministicVolatilityBuilder.FlatForward (
  284.                 valParams.valueDate(),
  285.                 VolatilityLabel.Standard (fri3MUSD),
  286.                 "USD",
  287.                 dblUSD3MVol
  288.             )
  289.         );

  290.         mktParams.setFundingVolatility (
  291.             ScenarioDeterministicVolatilityBuilder.FlatForward (
  292.                 valParams.valueDate(),
  293.                 VolatilityLabel.Standard (fundingLabelUSD),
  294.                 "USD",
  295.                 dblUSDFundingVol
  296.             )
  297.         );

  298.         mktParams.setForwardFundingCorrelation (
  299.             fri3MUSD,
  300.             fundingLabelUSD,
  301.             new FlatUnivariate (dblUSD3MUSDFundingCorr)
  302.         );

  303.         mktParams.setFundingState (dcEURCollatDomestic);

  304.         mktParams.setFXState (
  305.             ScenarioFXCurveBuilder.CubicPolynomialCurve (
  306.                 fxLabel.fullyQualifiedName(),
  307.                 dtToday,
  308.                 cp,
  309.                 new String[] {"10Y"},
  310.                 new double[] {dblUSDEURFXRate},
  311.                 dblUSDEURFXRate
  312.             )
  313.         );

  314.         mktParams.setFixing (
  315.             dtToday,
  316.             fxLabel,
  317.             dblUSDEURFXRate
  318.         );

  319.         mktParams.setFundingVolatility (
  320.             ScenarioDeterministicVolatilityBuilder.FlatForward (
  321.                 valParams.valueDate(),
  322.                 VolatilityLabel.Standard (fundingLabelEUR),
  323.                 "EUR",
  324.                 dblEURFundingVol
  325.             )
  326.         );

  327.         mktParams.setFXVolatility (
  328.             ScenarioDeterministicVolatilityBuilder.FlatForward (
  329.                 valParams.valueDate(),
  330.                 VolatilityLabel.Standard (fxLabel),
  331.                 "USD",
  332.                 dblUSDEURFXVol
  333.             )
  334.         );

  335.         mktParams.setFundingFXCorrelation (
  336.             fundingLabelUSD,
  337.             fxLabel,
  338.             new FlatUnivariate (dblUSDFundingUSDEURFXCorr)
  339.         );

  340.         CaseInsensitiveTreeMap<Double> mapMTMOutput = fixMTMFloat.value (
  341.             valParams,
  342.             null,
  343.             mktParams,
  344.             null
  345.         );

  346.         CaseInsensitiveTreeMap<Double> mapNonMTMOutput = fixNonMTMFloat.value (
  347.             valParams,
  348.             null,
  349.             mktParams,
  350.             null
  351.         );

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

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

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

  357.                 String strReconcile = NumberUtil.WithinTolerance (
  358.                     dblMTMMeasure,
  359.                     dblNonMTMMeasure,
  360.                     1.e-08,
  361.                     1.e-04
  362.                 ) ?
  363.                 "RECONCILES" :
  364.                 "DOES NOT RECONCILE";

  365.                 System.out.println ("\t" +
  366.                     FormatUtil.FormatDouble (dblMTMMeasure, 1, 8, 1.) + " | " +
  367.                     FormatUtil.FormatDouble (dblNonMTMMeasure, 1, 8, 1.) + " | " +
  368.                     strReconcile + " <= " + strKey);
  369.             }
  370.         }

  371.         EnvManager.TerminateEnv();
  372.     }
  373. }