JurisdictionOTCIndexSwaps.java

  1. package org.drip.sample.fixfloat;

  2. import java.util.Map;

  3. import org.drip.analytics.date.*;
  4. import org.drip.analytics.support.*;
  5. import org.drip.function.r1tor1.QuadraticRationalShapeControl;
  6. import org.drip.market.otc.*;
  7. import org.drip.numerical.common.FormatUtil;
  8. import org.drip.param.creator.*;
  9. import org.drip.param.market.CurveSurfaceQuoteContainer;
  10. import org.drip.param.period.*;
  11. import org.drip.param.valuation.*;
  12. import org.drip.product.creator.*;
  13. import org.drip.product.rates.*;
  14. import org.drip.service.env.EnvManager;
  15. import org.drip.spline.basis.PolynomialFunctionSetParams;
  16. import org.drip.spline.params.*;
  17. import org.drip.spline.stretch.*;
  18. import org.drip.state.creator.ScenarioDiscountCurveBuilder;
  19. import org.drip.state.discount.*;
  20. import org.drip.state.estimator.LatentStateStretchBuilder;
  21. import org.drip.state.identifier.*;
  22. import org.drip.state.inference.*;

  23. /*
  24.  * -*- mode: java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  25.  */

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

  88. /**
  89.  * <i>JurisdictionOTCIndexSwaps</i> contains curve construction and valuation of the common
  90.  * Jurisdiction-specific OTC IRS.
  91.  *  
  92.  * <br><br>
  93.  *  <ul>
  94.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/AnalyticsCore.md">Analytics Core Module</a></li>
  95.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/FixedIncomeAnalyticsLibrary.md">Fixed Income Analytics Library</a></li>
  96.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/README.md">Sample</a></li>
  97.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/fixfloat/README.md">Fix Float Swap Analytics</a></li>
  98.  *  </ul>
  99.  * <br><br>
  100.  *
  101.  * @author Lakshmi Krishnamurthy
  102.  */

  103. public class JurisdictionOTCIndexSwaps {

  104.     private static final FixFloatComponent OTCIRS (
  105.         final JulianDate dtSpot,
  106.         final String strCurrency,
  107.         final String strLocation,
  108.         final String strMaturityTenor,
  109.         final String strIndex,
  110.         final double dblCoupon)
  111.     {
  112.         FixedFloatSwapConvention ffConv = IBORFixedFloatContainer.ConventionFromJurisdiction (
  113.             strCurrency,
  114.             strLocation,
  115.             strMaturityTenor,
  116.             strIndex
  117.         );

  118.         return ffConv.createFixFloatComponent (
  119.             dtSpot,
  120.             strMaturityTenor,
  121.             dblCoupon,
  122.             0.,
  123.             1.
  124.         );
  125.     }

  126.     /*
  127.      * Construct the Array of Deposit Instruments from the given set of parameters
  128.      *
  129.      *      USE WITH CARE: This sample ignores errors and does not handle exceptions.
  130.      */

  131.     private static final SingleStreamComponent[] DepositInstrumentsFromMaturityDays (
  132.         final JulianDate dtEffective,
  133.         final String strCurrency,
  134.         final int[] aiDay)
  135.         throws Exception
  136.     {
  137.         SingleStreamComponent[] aDeposit = new SingleStreamComponent[aiDay.length];

  138.         ComposableFloatingUnitSetting cfus = new ComposableFloatingUnitSetting (
  139.             "3M",
  140.             CompositePeriodBuilder.EDGE_DATE_SEQUENCE_SINGLE,
  141.             null,
  142.             ForwardLabel.Create (
  143.                 strCurrency,
  144.                 "3M"
  145.             ),
  146.             CompositePeriodBuilder.REFERENCE_PERIOD_IN_ADVANCE,
  147.             0.
  148.         );

  149.         CompositePeriodSetting cps = new CompositePeriodSetting (
  150.             4,
  151.             "3M",
  152.             strCurrency,
  153.             null,
  154.             1.,
  155.             null,
  156.             null,
  157.             null,
  158.             null
  159.         );

  160.         CashSettleParams csp = new CashSettleParams (
  161.             0,
  162.             strCurrency,
  163.             0
  164.         );

  165.         for (int i = 0; i < aiDay.length; ++i) {
  166.             aDeposit[i] = new SingleStreamComponent (
  167.                 "DEPOSIT_" + aiDay[i],
  168.                 new Stream (
  169.                     CompositePeriodBuilder.FloatingCompositeUnit (
  170.                         CompositePeriodBuilder.EdgePair (
  171.                             dtEffective,
  172.                             dtEffective.addBusDays (
  173.                                 aiDay[i],
  174.                                 strCurrency
  175.                             )
  176.                         ),
  177.                         cps,
  178.                         cfus
  179.                     )
  180.                 ),
  181.                 csp
  182.             );

  183.             aDeposit[i].setPrimaryCode (aiDay[i] + "D");
  184.         }

  185.         return aDeposit;
  186.     }

  187.     /*
  188.      * Construct the Array of Swap Instruments from the given set of parameters
  189.      *
  190.      *      USE WITH CARE: This sample ignores errors and does not handle exceptions.
  191.      */

  192.     private static final FixFloatComponent[] SwapInstrumentsFromMaturityTenor (
  193.         final JulianDate dtSpot,
  194.         final String strCurrency,
  195.         final String strLocation,
  196.         final String strIndex,
  197.         final String[] astrMaturityTenor)
  198.         throws Exception
  199.     {
  200.         FixFloatComponent[] aIRS = new FixFloatComponent[astrMaturityTenor.length];

  201.         for (int i = 0; i < astrMaturityTenor.length; ++i)
  202.             aIRS[i] = OTCIRS (
  203.                 dtSpot,
  204.                 strCurrency,
  205.                 strLocation,
  206.                 astrMaturityTenor[i],
  207.                 strIndex,
  208.                 0.
  209.             );

  210.         return aIRS;
  211.     }

  212.     /*
  213.      * This sample demonstrates discount curve calibration and input instrument calibration quote recovery.
  214.      *  It shows the following:
  215.      *  - Construct the Array of Cash/Swap Instruments and their Quotes from the given set of parameters.
  216.      *  - Construct the Cash/Swap Instrument Set Stretch Builder.
  217.      *  - Set up the Linear Curve Calibrator using the following parameters:
  218.      *      - Cubic Exponential Mixture Basis Spline Set
  219.      *      - Ck = 2, Segment Curvature Penalty = 2
  220.      *      - Quadratic Rational Shape Controller
  221.      *      - Natural Boundary Setting
  222.      *  - Construct the Shape Preserving Discount Curve by applying the linear curve calibrator to the array
  223.      *      of Cash and Swap Stretches.
  224.      *  - Cross-Comparison of the Cash/Swap Calibration Instrument "Rate" metric across the different curve
  225.      *      construction methodologies.
  226.      *
  227.      *      USE WITH CARE: This sample ignores errors and does not handle exceptions.
  228.      */

  229.     private static final void OTCRun (
  230.         final JulianDate dtToday,
  231.         final String strCurrency,
  232.         final String strLocation,
  233.         final String[] astrOTCMaturityTenor,
  234.         final String strIndex)
  235.         throws Exception
  236.     {
  237.         JulianDate dtSpot = dtToday.addTenorAndAdjust (
  238.             "0D",
  239.             strCurrency
  240.         );

  241.         /*
  242.          * Construct the Array of Deposit Instruments and their Quotes from the given set of parameters
  243.          */

  244.         SingleStreamComponent[] aDepositComp = DepositInstrumentsFromMaturityDays (
  245.             dtSpot,
  246.             strCurrency,
  247.             new int[] {
  248.                 1, 7, 14, 30, 60
  249.             }
  250.         );

  251.         double[] adblDepositQuote = new double[] {
  252.             0.0013, 0.0017, 0.0018, 0.0020, 0.0023
  253.         };

  254.         /*
  255.          * Construct the Deposit Instrument Set Stretch Builder
  256.          */

  257.         LatentStateStretchSpec depositStretch = LatentStateStretchBuilder.ForwardFundingStretchSpec (
  258.             "DEPOSIT",
  259.             aDepositComp,
  260.             "ForwardRate",
  261.             adblDepositQuote
  262.         );

  263.         /*
  264.          * Construct the Array of EDF Instruments and their Quotes from the given set of parameters
  265.          */

  266.         SingleStreamComponent[] aEDFComp = SingleStreamComponentBuilder.ForwardRateFuturesPack (
  267.             dtSpot,
  268.             4,
  269.             strCurrency
  270.         );

  271.         double[] adblEDFQuote = new double[] {
  272.             0.0027, 0.0032, 0.0041, 0.0054, 0.0077, 0.0104, 0.0134, 0.0160
  273.         };

  274.         /*
  275.          * Construct the EDF Instrument Set Stretch Builder
  276.          */

  277.         LatentStateStretchSpec edfStretch = LatentStateStretchBuilder.ForwardFundingStretchSpec (
  278.             "EDF",
  279.             aEDFComp,
  280.             "ForwardRate",
  281.             adblEDFQuote
  282.         );

  283.         /*
  284.          * Construct the Array of Swap Instruments and their Quotes from the given set of parameters
  285.          */

  286.         FixFloatComponent[] aSwapComp = SwapInstrumentsFromMaturityTenor (
  287.             dtSpot,
  288.             strCurrency,
  289.             strLocation,
  290.             strIndex,
  291.             new java.lang.String[] {
  292.                 "4Y", "5Y", "6Y", "7Y", "8Y", "9Y", "10Y", "11Y", "12Y", "15Y", "20Y", "25Y", "30Y", "40Y", "50Y"
  293.             }
  294.         );

  295.         double[] adblSwapQuote = new double[] {
  296.             0.0166, 0.0206, 0.0241, 0.0269, 0.0292, 0.0311, 0.0326, 0.0340, 0.0351, 0.0375, 0.0393, 0.0402, 0.0407, 0.0409, 0.0409
  297.         };

  298.         /*
  299.          * Construct the Swap Instrument Set Stretch Builder
  300.          */

  301.         LatentStateStretchSpec swapStretch = LatentStateStretchBuilder.ForwardFundingStretchSpec (
  302.             "SWAP",
  303.             aSwapComp,
  304.             "SwapRate",
  305.             adblSwapQuote
  306.         );

  307.         LatentStateStretchSpec[] aStretchSpec = new LatentStateStretchSpec[] {
  308.             depositStretch,
  309.             edfStretch,
  310.             swapStretch
  311.         };

  312.         /*
  313.          * Set up the Linear Curve Calibrator using the following parameters:
  314.          *  - Cubic Exponential Mixture Basis Spline Set
  315.          *  - Ck = 2, Segment Curvature Penalty = 2
  316.          *  - Quadratic Rational Shape Controller
  317.          *  - Natural Boundary Setting
  318.          */

  319.         LinearLatentStateCalibrator lcc = new LinearLatentStateCalibrator (
  320.             new SegmentCustomBuilderControl (
  321.                 MultiSegmentSequenceBuilder.BASIS_SPLINE_POLYNOMIAL,
  322.                 new PolynomialFunctionSetParams (4),
  323.                 SegmentInelasticDesignControl.Create (
  324.                     2,
  325.                     2
  326.                 ),
  327.                 new ResponseScalingShapeControl (
  328.                     true,
  329.                     new QuadraticRationalShapeControl (0.)
  330.                 ),
  331.                 null
  332.             ),
  333.             BoundarySettings.NaturalStandard(),
  334.             MultiSegmentSequence.CALIBRATE,
  335.             null,
  336.             null
  337.         );

  338.         ValuationParams valParams = new ValuationParams (
  339.             dtSpot,
  340.             dtSpot,
  341.             strCurrency
  342.         );

  343.         /*
  344.          * Construct the Shape Preserving Discount Curve by applying the linear curve calibrator to the array
  345.          *  of Deposit, Futures, and Swap Stretches.
  346.          */

  347.         MergedDiscountForwardCurve dc = ScenarioDiscountCurveBuilder.ShapePreservingDFBuild (
  348.             strCurrency,
  349.             lcc,
  350.             aStretchSpec,
  351.             valParams,
  352.             null,
  353.             null,
  354.             null,
  355.             1.
  356.         );

  357.         CurveSurfaceQuoteContainer csqs = MarketParamsBuilder.Create (
  358.             dc,
  359.             null,
  360.             null,
  361.             null,
  362.             null,
  363.             null,
  364.             null
  365.         );

  366.         System.out.print ("\t[" + strCurrency + " | " + strLocation + "] = ");

  367.         for (int i = 0; i < astrOTCMaturityTenor.length; ++i) {
  368.             FixFloatComponent swap = OTCIRS (
  369.                 dtSpot,
  370.                 strCurrency,
  371.                 strLocation,
  372.                 astrOTCMaturityTenor[i],
  373.                 strIndex,
  374.                 0.
  375.             );

  376.             Map<String, Double> mapOutput = swap.value (
  377.                 valParams,
  378.                 null,
  379.                 csqs,
  380.                 null
  381.             );

  382.             System.out.print (
  383.                 FormatUtil.FormatDouble (mapOutput.get ("CalibSwapRate"), 1, 4, 100.) + "% (" +
  384.                 FormatUtil.FormatDouble (mapOutput.get ("FairPremium"), 1, 4, 100.) + "%) || "
  385.             );
  386.         }

  387.         System.out.println();
  388.     }

  389.     public static final void main (
  390.         final String[] astrArgs)
  391.         throws Exception
  392.     {
  393.         /*
  394.          * Initialize the Credit Analytics Library
  395.          */

  396.         EnvManager.InitEnv ("");

  397.         JulianDate dtToday = DateUtil.Today();

  398.         String[] astrOTCMaturityTenor = new String[] {
  399.             "1Y",
  400.             "3Y",
  401.             "5Y",
  402.             "7Y",
  403.             "10Y"
  404.         };

  405.         System.out.println ("\n\t--------------------------------------------------------------------------------------------------------------------------------");

  406.         System.out.println ("\t JURISDICTION             1Y      ||          3Y         ||          5Y         ||          7Y         ||         10Y         ||");

  407.         System.out.println ("\t--------------------------------------------------------------------------------------------------------------------------------");

  408.         OTCRun (dtToday, "AUD", "ALL", astrOTCMaturityTenor, "MAIN");

  409.         OTCRun (dtToday, "CAD", "ALL", astrOTCMaturityTenor, "MAIN");

  410.         OTCRun (dtToday, "CHF", "ALL", astrOTCMaturityTenor, "MAIN");

  411.         OTCRun (dtToday, "CNY", "ALL", astrOTCMaturityTenor, "MAIN");

  412.         OTCRun (dtToday, "DKK", "ALL", astrOTCMaturityTenor, "MAIN");

  413.         OTCRun (dtToday, "EUR", "ALL", astrOTCMaturityTenor, "MAIN");

  414.         OTCRun (dtToday, "GBP", "ALL", astrOTCMaturityTenor, "MAIN");

  415.         OTCRun (dtToday, "HKD", "ALL", astrOTCMaturityTenor, "MAIN");

  416.         OTCRun (dtToday, "INR", "ALL", astrOTCMaturityTenor, "MAIN");

  417.         OTCRun (dtToday, "JPY", "ALL", astrOTCMaturityTenor, "MAIN");

  418.         OTCRun (dtToday, "JPY", "ALL", astrOTCMaturityTenor, "TIBOR");

  419.         OTCRun (dtToday, "KRW", "ALL", astrOTCMaturityTenor, "MAIN");

  420.         OTCRun (dtToday, "MYR", "ALL", astrOTCMaturityTenor, "MAIN");

  421.         OTCRun (dtToday, "NOK", "ALL", astrOTCMaturityTenor, "MAIN");

  422.         OTCRun (dtToday, "NZD", "ALL", astrOTCMaturityTenor, "MAIN");

  423.         OTCRun (dtToday, "PLN", "ALL", astrOTCMaturityTenor, "MAIN");

  424.         OTCRun (dtToday, "SEK", "ALL", astrOTCMaturityTenor, "MAIN");

  425.         OTCRun (dtToday, "SGD", "ALL", astrOTCMaturityTenor, "MAIN");

  426.         OTCRun (dtToday, "THB", "ALL", astrOTCMaturityTenor, "MAIN");

  427.         OTCRun (dtToday, "TWD", "ALL", astrOTCMaturityTenor, "MAIN");

  428.         OTCRun (dtToday, "USD", "LON", astrOTCMaturityTenor, "MAIN");

  429.         OTCRun (dtToday, "USD", "NYC", astrOTCMaturityTenor, "MAIN");

  430.         OTCRun (dtToday, "ZAR", "ALL", astrOTCMaturityTenor, "MAIN");

  431.         System.out.println ("\t--------------------------------------------------------------------------------------------------------------------------------");

  432.         EnvManager.TerminateEnv();
  433.     }
  434. }