MultiStreamSwapMeasures.java

  1. package org.drip.sample.funding;

  2. /*
  3.  * Credit Analytics Imports
  4.  */

  5. import org.drip.analytics.date.*;
  6. import org.drip.analytics.daycount.*;
  7. import org.drip.analytics.support.*;
  8. import org.drip.market.otc.*;
  9. import org.drip.param.market.CurveSurfaceQuoteContainer;
  10. import org.drip.param.period.*;
  11. import org.drip.param.valuation.*;
  12. import org.drip.product.definition.CalibratableComponent;
  13. import org.drip.product.params.CurrencyPair;
  14. import org.drip.product.rates.*;
  15. import org.drip.service.env.EnvManager;
  16. import org.drip.state.creator.*;
  17. import org.drip.state.discount.MergedDiscountForwardCurve;
  18. import org.drip.state.identifier.*;

  19. /*
  20.  * -*- mode: java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  21.  */

  22. /*!
  23.  * Copyright (C) 2019 Lakshmi Krishnamurthy
  24.  * Copyright (C) 2018 Lakshmi Krishnamurthy
  25.  * Copyright (C) 2017 Lakshmi Krishnamurthy
  26.  * Copyright (C) 2016 Lakshmi Krishnamurthy
  27.  * Copyright (C) 2015 Lakshmi Krishnamurthy
  28.  * Copyright (C) 2014 Lakshmi Krishnamurthy
  29.  * Copyright (C) 2013 Lakshmi Krishnamurthy
  30.  * Copyright (C) 2012 Lakshmi Krishnamurthy
  31.  *
  32.  *  This file is part of DROP, an open-source library targeting risk, transaction costs, exposure, margin
  33.  *      calculations, valuation adjustment, and portfolio construction within and across fixed income,
  34.  *      credit, commodity, equity, FX, and structured products.
  35.  *  
  36.  *      https://lakshmidrip.github.io/DROP/
  37.  *  
  38.  *  DROP is composed of three modules:
  39.  *  
  40.  *  - DROP Analytics Core - https://lakshmidrip.github.io/DROP-Analytics-Core/
  41.  *  - DROP Portfolio Core - https://lakshmidrip.github.io/DROP-Portfolio-Core/
  42.  *  - DROP Numerical Core - https://lakshmidrip.github.io/DROP-Numerical-Core/
  43.  *
  44.  *  DROP Analytics Core implements libraries for the following:
  45.  *  - Fixed Income Analytics
  46.  *  - Asset Backed Analytics
  47.  *  - XVA Analytics
  48.  *  - Exposure and Margin Analytics
  49.  *
  50.  *  DROP Portfolio Core implements libraries for the following:
  51.  *  - Asset Allocation Analytics
  52.  *  - Transaction Cost Analytics
  53.  *
  54.  *  DROP Numerical Core implements libraries for the following:
  55.  *  - Statistical Learning
  56.  *  - Numerical Optimizer
  57.  *  - Spline Builder
  58.  *  - Algorithm Support
  59.  *
  60.  *  Documentation for DROP is Spread Over:
  61.  *
  62.  *  - Main                     => https://lakshmidrip.github.io/DROP/
  63.  *  - Wiki                     => https://github.com/lakshmiDRIP/DROP/wiki
  64.  *  - GitHub                   => https://github.com/lakshmiDRIP/DROP
  65.  *  - Repo Layout Taxonomy     => https://github.com/lakshmiDRIP/DROP/blob/master/Taxonomy.md
  66.  *  - Javadoc                  => https://lakshmidrip.github.io/DROP/Javadoc/index.html
  67.  *  - Technical Specifications => https://github.com/lakshmiDRIP/DROP/tree/master/Docs/Internal
  68.  *  - Release Versions         => https://lakshmidrip.github.io/DROP/version.html
  69.  *  - Community Credits        => https://lakshmidrip.github.io/DROP/credits.html
  70.  *  - Issues Catalog           => https://github.com/lakshmiDRIP/DROP/issues
  71.  *  - JUnit                    => https://lakshmidrip.github.io/DROP/junit/index.html
  72.  *  - Jacoco                   => https://lakshmidrip.github.io/DROP/jacoco/index.html
  73.  *
  74.  *  Licensed under the Apache License, Version 2.0 (the "License");
  75.  *      you may not use this file except in compliance with the License.
  76.  *  
  77.  *  You may obtain a copy of the License at
  78.  *      http://www.apache.org/licenses/LICENSE-2.0
  79.  *  
  80.  *  Unless required by applicable law or agreed to in writing, software
  81.  *      distributed under the License is distributed on an "AS IS" BASIS,
  82.  *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  83.  *  
  84.  *  See the License for the specific language governing permissions and
  85.  *      limitations under the License.
  86.  */

  87. /**
  88.  * <i>MultiStreamSwapMeasures</i> illustrates the creation, invocation, and usage of the MultiStreamSwap. It
  89.  * shows how to:
  90.  *  
  91.  * <br><br>
  92.  *  <ul>
  93.  *      <li>
  94.  *          Create the Discount Curve from the rates instruments.
  95.  *      </li>
  96.  *      <li>
  97.  *          Set up the valuation and the market parameters.
  98.  *      </li>
  99.  *      <li>
  100.  *          Create the Rates Basket from the fixed/float streams.
  101.  *      </li>
  102.  *      <li>
  103.  *          Value the Rates Basket.
  104.  *      </li>
  105.  *  </ul>
  106.  *  
  107.  * <br><br>
  108.  *  <ul>
  109.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/AnalyticsCore.md">Analytics Core Module</a></li>
  110.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/FixedIncomeAnalyticsLibrary.md">Fixed Income Analytics Library</a></li>
  111.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/README.md">Sample</a></li>
  112.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/funding/README.md">Funding Curve Builder</a></li>
  113.  *  </ul>
  114.  * <br><br>
  115.  *
  116.  * @author Lakshmi Krishnamurthy
  117.  */

  118. public class MultiStreamSwapMeasures {

  119.     private static final FixFloatComponent OTCIRS (
  120.         final JulianDate dtSpot,
  121.         final String strCurrency,
  122.         final String strMaturityTenor,
  123.         final double dblCoupon)
  124.     {
  125.         FixedFloatSwapConvention ffConv = IBORFixedFloatContainer.ConventionFromJurisdiction (
  126.             strCurrency,
  127.             "ALL",
  128.             strMaturityTenor,
  129.             "MAIN"
  130.         );

  131.         return ffConv.createFixFloatComponent (
  132.             dtSpot,
  133.             strMaturityTenor,
  134.             dblCoupon,
  135.             0.,
  136.             1.
  137.         );
  138.     }

  139.     /*
  140.      * Sample demonstrating building of rates curve from deposit/future/swaps
  141.      *
  142.      *      USE WITH CARE: This sample ignores errors and does not handle exceptions.
  143.      */

  144.     private static MergedDiscountForwardCurve BuildRatesCurveFromInstruments (
  145.         final JulianDate dtStart,
  146.         final String[] astrDepositTenor,
  147.         final double[] adblDepositRate,
  148.         final String[] astrIRSTenor,
  149.         final double[] adblIRSRate,
  150.         final double dblBump,
  151.         final String strCurrency)
  152.         throws Exception
  153.     {
  154.         int iNumDCInstruments = astrDepositTenor.length + adblIRSRate.length;
  155.         int aiDate[] = new int[iNumDCInstruments];
  156.         double adblRate[] = new double[iNumDCInstruments];
  157.         String astrCalibMeasure[] = new String[iNumDCInstruments];
  158.         double adblCompCalibValue[] = new double[iNumDCInstruments];
  159.         CalibratableComponent aCompCalib[] = new CalibratableComponent[iNumDCInstruments];

  160.         // Deposit Calibration

  161.         ComposableFloatingUnitSetting cfusDeposit = new ComposableFloatingUnitSetting (
  162.             "3M",
  163.             CompositePeriodBuilder.EDGE_DATE_SEQUENCE_SINGLE,
  164.             null,
  165.             ForwardLabel.Create (
  166.                 strCurrency,
  167.                 "3M"
  168.             ),
  169.             CompositePeriodBuilder.REFERENCE_PERIOD_IN_ADVANCE,
  170.             0.
  171.         );

  172.         CompositePeriodSetting cpsDeposit = new CompositePeriodSetting (
  173.             4,
  174.             "3M",
  175.             strCurrency,
  176.             null,
  177.             1.,
  178.             null,
  179.             null,
  180.             null,
  181.             null
  182.         );

  183.         CashSettleParams csp = new CashSettleParams (
  184.             0,
  185.             strCurrency,
  186.             0
  187.         );

  188.         for (int i = 0; i < astrDepositTenor.length; ++i) {
  189.             astrCalibMeasure[i] = "Rate";
  190.             adblRate[i] = java.lang.Double.NaN;
  191.             adblCompCalibValue[i] = adblDepositRate[i] + dblBump;

  192.             aCompCalib[i] = new SingleStreamComponent (
  193.                 "DEPOSIT_" + astrDepositTenor[i],
  194.                 new Stream (
  195.                     CompositePeriodBuilder.FloatingCompositeUnit (
  196.                         CompositePeriodBuilder.EdgePair (
  197.                             dtStart,
  198.                             new JulianDate (aiDate[i] = dtStart.addTenor (astrDepositTenor[i]).julian())
  199.                         ),
  200.                         cpsDeposit,
  201.                         cfusDeposit
  202.                     )
  203.                 ),
  204.                 csp
  205.             );

  206.             aCompCalib[i].setPrimaryCode (astrDepositTenor[i]);
  207.         }

  208.         // IRS Calibration

  209.         for (int i = 0; i < astrIRSTenor.length; ++i) {
  210.             astrCalibMeasure[i + astrDepositTenor.length] = "Rate";
  211.             adblRate[i + astrDepositTenor.length] = java.lang.Double.NaN;
  212.             adblCompCalibValue[i + astrDepositTenor.length] = adblIRSRate[i] + dblBump;

  213.             FixFloatComponent irs = OTCIRS (
  214.                 dtStart,
  215.                 strCurrency,
  216.                 astrIRSTenor[i],
  217.                 adblIRSRate[i] + dblBump
  218.             );

  219.             irs.setPrimaryCode ("IRS." + astrIRSTenor[i] + "." + strCurrency);

  220.             aCompCalib[i + astrDepositTenor.length] = irs;
  221.         }

  222.         /*
  223.          * Build the IR curve from the components, their calibration measures, and their calibration quotes.
  224.          */

  225.         return ScenarioDiscountCurveBuilder.NonlinearBuild (
  226.             dtStart,
  227.             strCurrency,
  228.             aCompCalib,
  229.             adblCompCalibValue,
  230.             astrCalibMeasure,
  231.             null
  232.         );
  233.     }

  234.     /*
  235.      * Sample demonstrating creation of a rates basket instance from component fixed and floating streams
  236.      *
  237.      *      USE WITH CARE: This sample ignores errors and does not handle exceptions.
  238.      */

  239.     private static final RatesBasket MakeRatesBasket (
  240.         final JulianDate dtEffective)
  241.         throws Exception
  242.     {
  243.         /*
  244.          * Create a sequence of Fixed Streams
  245.          */

  246.         Stream[] aFixedStream = new Stream[3];

  247.         UnitCouponAccrualSetting ucasFixed = new UnitCouponAccrualSetting (
  248.             2,
  249.             "Act/360",
  250.             false,
  251.             "Act/360",
  252.             false,
  253.             "USD",
  254.             false,
  255.             CompositePeriodBuilder.ACCRUAL_COMPOUNDING_RULE_GEOMETRIC
  256.         );

  257.         ComposableFixedUnitSetting cfusFixed3Y = new ComposableFixedUnitSetting (
  258.             "6M",
  259.             CompositePeriodBuilder.EDGE_DATE_SEQUENCE_REGULAR,
  260.             null,
  261.             0.03,
  262.             0.,
  263.             "USD"
  264.         );

  265.         ComposableFixedUnitSetting cfusFixed5Y = new ComposableFixedUnitSetting (
  266.             "6M",
  267.             CompositePeriodBuilder.EDGE_DATE_SEQUENCE_REGULAR,
  268.             null,
  269.             0.05,
  270.             0.,
  271.             "USD"
  272.         );

  273.         ComposableFixedUnitSetting cfusFixed7Y = new ComposableFixedUnitSetting (
  274.             "6M",
  275.             CompositePeriodBuilder.EDGE_DATE_SEQUENCE_REGULAR,
  276.             null,
  277.             0.07,
  278.             0.,
  279.             "USD"
  280.         );

  281.         CompositePeriodSetting cpsFixed = new CompositePeriodSetting (
  282.             2,
  283.             "6M",
  284.             "USD",
  285.             null,
  286.             1.,
  287.             null,
  288.             null,
  289.             null,
  290.             null
  291.         );

  292.         aFixedStream[0] = new Stream (
  293.             CompositePeriodBuilder.FixedCompositeUnit (
  294.                 CompositePeriodBuilder.RegularEdgeDates (
  295.                     dtEffective,
  296.                     "6M",
  297.                     "3Y",
  298.                     null
  299.                 ),
  300.                 cpsFixed,
  301.                 ucasFixed,
  302.                 cfusFixed3Y
  303.             )
  304.         );

  305.         aFixedStream[1] = new Stream (
  306.             CompositePeriodBuilder.FixedCompositeUnit (
  307.                 CompositePeriodBuilder.RegularEdgeDates (
  308.                     dtEffective,
  309.                     "6M",
  310.                     "5Y",
  311.                     null
  312.                 ),
  313.                 cpsFixed,
  314.                 ucasFixed,
  315.                 cfusFixed5Y
  316.             )
  317.         );

  318.         aFixedStream[2] = new Stream (
  319.             CompositePeriodBuilder.FixedCompositeUnit (
  320.                 CompositePeriodBuilder.RegularEdgeDates (
  321.                     dtEffective,
  322.                     "6M",
  323.                     "7Y",
  324.                     null
  325.                 ),
  326.                 cpsFixed,
  327.                 ucasFixed,
  328.                 cfusFixed7Y
  329.             )
  330.         );

  331.         /*
  332.          * Create a sequence of Float Streams
  333.          */

  334.         Stream[] aFloatStream = new Stream[3];

  335.         ComposableFloatingUnitSetting cfusFloat3Y = new ComposableFloatingUnitSetting (
  336.             "3M",
  337.             CompositePeriodBuilder.EDGE_DATE_SEQUENCE_SINGLE,
  338.             null,
  339.             ForwardLabel.Create (
  340.                 "USD",
  341.                 "3M"
  342.             ),
  343.             CompositePeriodBuilder.REFERENCE_PERIOD_IN_ADVANCE,
  344.             0.03
  345.         );

  346.         ComposableFloatingUnitSetting cfusFloat5Y = new ComposableFloatingUnitSetting (
  347.             "3M",
  348.             CompositePeriodBuilder.EDGE_DATE_SEQUENCE_SINGLE,
  349.             null,
  350.             ForwardLabel.Create (
  351.                 "USD",
  352.                 "3M"
  353.             ),
  354.             CompositePeriodBuilder.REFERENCE_PERIOD_IN_ADVANCE,
  355.             0.05
  356.         );

  357.         ComposableFloatingUnitSetting cfusFloat7Y = new ComposableFloatingUnitSetting (
  358.             "3M",
  359.             CompositePeriodBuilder.EDGE_DATE_SEQUENCE_SINGLE,
  360.             null,
  361.             ForwardLabel.Create (
  362.                 "USD",
  363.                 "3M"
  364.             ),
  365.             CompositePeriodBuilder.REFERENCE_PERIOD_IN_ADVANCE,
  366.             0.07
  367.         );

  368.         CompositePeriodSetting cpsFloat = new CompositePeriodSetting (
  369.             4,
  370.             "3M",
  371.             "USD",
  372.             null,
  373.             1.,
  374.             null,
  375.             null,
  376.             null,
  377.             null
  378.         );

  379.         aFloatStream[0] = new Stream (
  380.             CompositePeriodBuilder.FloatingCompositeUnit (
  381.                 CompositePeriodBuilder.RegularEdgeDates (
  382.                     dtEffective,
  383.                     "6M",
  384.                     "3Y",
  385.                     null
  386.                 ),
  387.                 cpsFloat,
  388.                 cfusFloat3Y
  389.             )
  390.         );

  391.         aFloatStream[1] = new Stream (
  392.             CompositePeriodBuilder.FloatingCompositeUnit (
  393.                 CompositePeriodBuilder.RegularEdgeDates (
  394.                     dtEffective,
  395.                     "6M",
  396.                     "5Y",
  397.                     null
  398.                 ),
  399.                 cpsFloat,
  400.                 cfusFloat5Y
  401.             )
  402.         );

  403.         aFloatStream[2] = new Stream (
  404.             CompositePeriodBuilder.FloatingCompositeUnit (
  405.                 CompositePeriodBuilder.RegularEdgeDates (
  406.                     dtEffective,
  407.                     "6M",
  408.                     "7Y",
  409.                     null
  410.                 ),
  411.                 cpsFloat,
  412.                 cfusFloat7Y
  413.             )
  414.         );

  415.         /*
  416.          * Create a Rates Basket instance containing the fixed and floating streams
  417.          */

  418.         return new RatesBasket (
  419.             "RATESBASKET",
  420.             aFixedStream,
  421.             aFloatStream
  422.         );
  423.     }

  424.     /*
  425.      * Sample demonstrating creation of discount curve from cash/futures/swaps
  426.      *
  427.      *      USE WITH CARE: This sample ignores errors and does not handle exceptions.
  428.      */

  429.     private static final void MultiLegSwapSample()
  430.         throws Exception
  431.     {
  432.         JulianDate dtValue = DateUtil.Today();

  433.         /*
  434.          * Create the Discount Curve from the rates instruments
  435.          */

  436.         String[] astrCashTenor = new String[] {"3M"};
  437.         double[] adblCashRate = new double[] {0.00276};
  438.         String[] astrIRSTenor = new String[] {   "1Y",    "2Y",    "3Y",    "4Y",    "5Y",    "6Y",    "7Y",
  439.            "8Y",    "9Y",   "10Y",   "11Y",   "12Y",   "15Y",   "20Y",   "25Y",   "30Y",   "40Y",   "50Y"};
  440.         double[] adblIRSRate = new double[]  {0.00367, 0.00533, 0.00843, 0.01238, 0.01609, 0.01926, 0.02191,
  441.             0.02406, 0.02588, 0.02741, 0.02870, 0.02982, 0.03208, 0.03372, 0.03445, 0.03484, 0.03501, 0.03484};

  442.         MergedDiscountForwardCurve dc = BuildRatesCurveFromInstruments (
  443.             dtValue,
  444.             astrCashTenor,
  445.             adblCashRate,
  446.             astrIRSTenor,
  447.             adblIRSRate,
  448.             0.,
  449.             "USD"
  450.         );

  451.         /*
  452.          * Set up the valuation and the market parameters
  453.          */

  454.         ValuationParams valParams = ValuationParams.Spot (
  455.             dtValue,
  456.             0,
  457.             "",
  458.             Convention.DATE_ROLL_ACTUAL
  459.         );

  460.         double dblUSDABCFXRate = 1.;

  461.         CurveSurfaceQuoteContainer mktParams = new CurveSurfaceQuoteContainer();

  462.         mktParams.setFundingState (dc);

  463.         CurrencyPair cp = CurrencyPair.FromCode ("USD/ABC");

  464.         mktParams.setFXState (
  465.             ScenarioFXCurveBuilder.CubicPolynomialCurve (
  466.                 "FX::" + cp.code(),
  467.                 dtValue,
  468.                 cp,
  469.                 new String[] {"10Y"},
  470.                 new double[] {dblUSDABCFXRate},
  471.                 dblUSDABCFXRate
  472.             )
  473.         );

  474.         /*
  475.          * Create the Rates Basket from the streams
  476.          */

  477.         RatesBasket rb = MakeRatesBasket (dtValue);

  478.         /*
  479.          * Value the Rates Basket
  480.          */

  481.         CaseInsensitiveTreeMap<Double> mapRBResults = rb.value (
  482.             valParams,
  483.             null,
  484.             mktParams,
  485.             null
  486.         );

  487.         System.out.println (mapRBResults);
  488.     }

  489.     public static final void main (
  490.         final String[] astrArgs)
  491.         throws Exception
  492.     {
  493.         // String strConfig = "c:\\Lakshmi\\BondAnal\\Config.xml";

  494.         String strConfig = "";

  495.         EnvManager.InitEnv (strConfig);

  496.         MultiLegSwapSample();

  497.         EnvManager.TerminateEnv();
  498.     }
  499. }