JurisdictionIRSFuturesValuation.java

  1. package org.drip.sample.forwardratefutures;

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

  89. /**
  90.  * <i>JurisdictionIRSFuturesValuation</i> contains the demonstration of the construction and the Valuation of
  91.  * the Exchange-Traded IRS Futures Contract.
  92.  *  
  93.  * <br><br>
  94.  *  <ul>
  95.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/AnalyticsCore.md">Analytics Core Module</a></li>
  96.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/FixedIncomeAnalyticsLibrary.md">Fixed Income Analytics Library</a></li>
  97.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/README.md">Sample</a></li>
  98.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/forwardratefutures/README.md">Forward Rate Futures Analytics</a></li>
  99.  *  </ul>
  100.  * <br><br>
  101.  *
  102.  * @author Lakshmi Krishnamurthy
  103.  */

  104. public class JurisdictionIRSFuturesValuation {

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

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

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

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

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

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

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

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

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

  184.         return aDeposit;
  185.     }

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

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

  198.         for (int i = 0; i < astrMaturityTenor.length; ++i)
  199.             aIRS[i] = OTCIRS (
  200.                 dtSpot,
  201.                 strCurrency,
  202.                 astrMaturityTenor[i],
  203.                 0.
  204.             );

  205.         return aIRS;
  206.     }

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

  224.     private static final void OTCInstrumentCurve (
  225.         final JulianDate dtSpot,
  226.         final String strCurrency)
  227.         throws Exception
  228.     {
  229.         /*
  230.          * Construct the Array of Deposit Instruments and their Quotes from the given set of parameters
  231.          */

  232.         SingleStreamComponent[] aDepositComp = DepositInstrumentsFromMaturityDays (
  233.             dtSpot,
  234.             strCurrency,
  235.             new int[] {
  236.                 1, 2, 7, 14, 30, 60
  237.             }
  238.         );

  239.         double[] adblDepositQuote = new double[] {
  240.             0.0013, 0.0017, 0.0017, 0.0018, 0.0020, 0.0023
  241.         };

  242.         /*
  243.          * Construct the Deposit Instrument Set Stretch Builder
  244.          */

  245.         LatentStateStretchSpec depositStretch = LatentStateStretchBuilder.ForwardFundingStretchSpec (
  246.             "DEPOSIT",
  247.             aDepositComp,
  248.             "ForwardRate",
  249.             adblDepositQuote
  250.         );

  251.         /*
  252.          * Construct the Array of EDF Instruments and their Quotes from the given set of parameters
  253.          */

  254.         SingleStreamComponent[] aEDFComp = SingleStreamComponentBuilder.ForwardRateFuturesPack (
  255.             dtSpot,
  256.             8,
  257.             strCurrency
  258.         );

  259.         double[] adblEDFQuote = new double[] {
  260.             0.0027, 0.0032, 0.0041, 0.0054, 0.0077, 0.0104, 0.0134, 0.0160
  261.         };

  262.         /*
  263.          * Construct the EDF Instrument Set Stretch Builder
  264.          */

  265.         LatentStateStretchSpec edfStretch = LatentStateStretchBuilder.ForwardFundingStretchSpec (
  266.             "EDF",
  267.             aEDFComp,
  268.             "ForwardRate",
  269.             adblEDFQuote
  270.         );

  271.         /*
  272.          * Construct the Array of Swap Instruments and their Quotes from the given set of parameters
  273.          */

  274.         FixFloatComponent[] aSwapComp = SwapInstrumentsFromMaturityTenor (
  275.             dtSpot,
  276.             strCurrency,
  277.             new java.lang.String[] {
  278.                 "4Y", "5Y", "6Y", "7Y", "8Y", "9Y", "10Y", "11Y", "12Y", "15Y", "20Y", "25Y", "30Y", "40Y", "50Y"
  279.             }
  280.         );

  281.         double[] adblSwapQuote = new double[] {
  282.             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
  283.         };

  284.         /*
  285.          * Construct the Swap Instrument Set Stretch Builder
  286.          */

  287.         LatentStateStretchSpec swapStretch = LatentStateStretchBuilder.ForwardFundingStretchSpec (
  288.             "SWAP",
  289.             aSwapComp,
  290.             "SwapRate",
  291.             adblSwapQuote
  292.         );

  293.         LatentStateStretchSpec[] aStretchSpec = new LatentStateStretchSpec[] {depositStretch, edfStretch, swapStretch};

  294.         /*
  295.          * Set up the Linear Curve Calibrator using the following parameters:
  296.          *  - Cubic Exponential Mixture Basis Spline Set
  297.          *  - Ck = 2, Segment Curvature Penalty = 2
  298.          *  - Quadratic Rational Shape Controller
  299.          *  - Natural Boundary Setting
  300.          */

  301.         LinearLatentStateCalibrator lcc = new LinearLatentStateCalibrator (
  302.             new SegmentCustomBuilderControl (
  303.                 MultiSegmentSequenceBuilder.BASIS_SPLINE_POLYNOMIAL,
  304.                 new PolynomialFunctionSetParams (4),
  305.                 SegmentInelasticDesignControl.Create (
  306.                     2,
  307.                     2
  308.                 ),
  309.                 new ResponseScalingShapeControl (
  310.                     true,
  311.                     new QuadraticRationalShapeControl (0.)
  312.                 ),
  313.                 null
  314.             ),
  315.             BoundarySettings.NaturalStandard(),
  316.             MultiSegmentSequence.CALIBRATE,
  317.             null,
  318.             null
  319.         );

  320.         ValuationParams valParams = new ValuationParams (
  321.             dtSpot,
  322.             dtSpot,
  323.             strCurrency
  324.         );

  325.         /*
  326.          * Construct the Shape Preserving Discount Curve by applying the linear curve calibrator to the array
  327.          *  of Deposit, Futures, and Swap Stretches.
  328.          */

  329.         MergedDiscountForwardCurve dc = ScenarioDiscountCurveBuilder.ShapePreservingDFBuild (
  330.             strCurrency,
  331.             lcc,
  332.             aStretchSpec,
  333.             valParams,
  334.             null,
  335.             null,
  336.             null,
  337.             1.
  338.         );

  339.         CurveSurfaceQuoteContainer csqs = MarketParamsBuilder.Create (
  340.             dc,
  341.             null,
  342.             null,
  343.             null,
  344.             null,
  345.             null,
  346.             null
  347.         );

  348.         /*
  349.          * Cross-Comparison of the Deposit Calibration Instrument "Rate" metric across the different curve
  350.          *  construction methodologies.
  351.          */

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

  353.         System.out.println ("\t     DEPOSIT INSTRUMENTS CALIBRATION RECOVERY");

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

  355.         for (int i = 0; i < aDepositComp.length; ++i)
  356.             System.out.println ("\t[" + aDepositComp[i].maturityDate() + "] = " +
  357.                 FormatUtil.FormatDouble (aDepositComp[i].measureValue (valParams, null, csqs,
  358.                     null, "Rate"), 1, 6, 1.) + " | " + FormatUtil.FormatDouble (adblDepositQuote[i], 1, 6, 1.));

  359.         /*
  360.          * Cross-Comparison of the EDF Calibration Instrument "Rate" metric across the different curve
  361.          *  construction methodologies.
  362.          */

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

  364.         System.out.println ("\t     EDF INSTRUMENTS CALIBRATION RECOVERY");

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

  366.         for (int i = 0; i < aEDFComp.length; ++i)
  367.             System.out.println ("\t[" + aEDFComp[i].maturityDate() + "] = " +
  368.                 FormatUtil.FormatDouble (aEDFComp[i].measureValue (valParams, null, csqs, null, "Rate"), 1, 6, 1.)
  369.                     + " | " + FormatUtil.FormatDouble (adblEDFQuote[i], 1, 6, 1.));

  370.         /*
  371.          * Cross-Comparison of the Swap Calibration Instrument "Rate" metric across the different curve
  372.          *  construction methodologies.
  373.          */

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

  375.         System.out.println ("\t     SWAP INSTRUMENTS CALIBRATION RECOVERY");

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

  377.         for (int i = 0; i < aSwapComp.length; ++i)
  378.             System.out.println ("\t[" + aSwapComp[i].maturityDate() + "] = " +
  379.                 FormatUtil.FormatDouble (aSwapComp[i].measureValue (valParams, null, csqs, null, "CalibSwapRate"), 1, 6, 1.)
  380.                     + " | " + FormatUtil.FormatDouble (adblSwapQuote[i], 1, 6, 1.) + " | " +
  381.                         FormatUtil.FormatDouble (aSwapComp[i].measureValue (valParams, null, csqs, null, "FairPremium"), 1, 6, 1.));

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

  383.         System.out.println ("\t     EXCHANGE-TRADED SWAP INSTRUMENTS VALUATION");

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

  385.         String[] astrExchangeTenor = new String[] {"2Y", "5Y", "10Y", "30Y"};

  386.         double[] adblCoupon = new double[] {0.0075, 0.0200, 0.0325, 0.0400};

  387.         for (int i = 0; i < astrExchangeTenor.length; ++i) {
  388.             DeliverableSwapFutures dsf = DeliverableSwapFuturesContainer.ProductInfo (
  389.                 strCurrency,
  390.                 astrExchangeTenor[i]
  391.             );

  392.             FixFloatComponent swapExchange = dsf.Create (
  393.                 dtSpot,
  394.                 adblCoupon[i]
  395.             );

  396.             System.out.println ("\t[" + swapExchange.maturityDate() + "] = " +
  397.                 FormatUtil.FormatDouble (swapExchange.measureValue (valParams, null, csqs, null, "CalibSwapRate"), 1, 6, 1.) + " | " +
  398.                 FormatUtil.FormatDouble (swapExchange.measureValue (valParams, null, csqs, null, "FairPremium"), 1, 6, 1.) + " | " +
  399.                 FormatUtil.FormatDouble (swapExchange.measureValue (valParams, null, csqs, null, "PV"), 4, 0, 1.) + " | " +
  400.                 astrExchangeTenor[i]
  401.             );
  402.         }
  403.     }

  404.     public static final void main (
  405.         final String[] astrArgs)
  406.         throws Exception
  407.     {
  408.         /*
  409.          * Initialize the Credit Analytics Library
  410.          */

  411.         EnvManager.InitEnv ("");

  412.         JulianDate dtToday = DateUtil.Today().addTenor ("0D");

  413.         String strCurrency = "USD";

  414.         OTCInstrumentCurve (
  415.             dtToday,
  416.             strCurrency
  417.         );

  418.         EnvManager.TerminateEnv();
  419.     }
  420. }