InAdvanceSwap.java

  1. package org.drip.sample.fixfloat;

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

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

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

  86. /**
  87.  * <i>InAdvanceSwap</i> discount curve calibration and input instrument calibration quote recovery. It shows
  88.  * the following:
  89.  *  
  90.  * <br><br>
  91.  *  <ul>
  92.  *      <li>
  93.  *          Construct the Array of Deposit/Swap Instruments and their Quotes from the given set of
  94.  *              parameters.
  95.  *      </li>
  96.  *      <li>
  97.  *          Construct the Deposit/Swap Instrument Set Stretch Builder.
  98.  *      </li>
  99.  *      <li>
  100.  *          Set up the Linear Curve Calibrator using the following parameters:
  101.  *          <ul>
  102.  *              <li>
  103.  *                  Cubic Exponential Mixture Basis Spline Set
  104.  *              </li>
  105.  *              <li>
  106.  *                  C<sub>k</sub> = 2
  107.  *                  Segment Curvature Penalty = 2
  108.  *              </li>
  109.  *              <li>
  110.  *                  Quadratic Rational Shape Controller
  111.  *              </li>
  112.  *              <li>
  113.  *                  Natural Boundary Setting
  114.  *              </li>
  115.  *          </ul>
  116.  *      </li>
  117.  *      <li>
  118.  *          Construct the Shape Preserving Discount Curve by applying the linear curve calibrator to the
  119.  *              array of Cash and Swap Stretches.
  120.  *      </li>
  121.  *      <li>
  122.  *          Cross-Comparison of the Cash/Swap Calibration Instrument "Rate" metric across the different curve
  123.  *              construction methodologies.
  124.  *      </li>
  125.  *  </ul>
  126.  *  
  127.  * <br><br>
  128.  *  <ul>
  129.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/AnalyticsCore.md">Analytics Core Module</a></li>
  130.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/FixedIncomeAnalyticsLibrary.md">Fixed Income Analytics Library</a></li>
  131.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/README.md">Sample</a></li>
  132.  *      <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>
  133.  *  </ul>
  134.  * <br><br>
  135.  *
  136.  * @author Lakshmi Krishnamurthy
  137.  */

  138. public class InAdvanceSwap {

  139.     private static final FixFloatComponent OTCIRS (
  140.         final JulianDate dtSpot,
  141.         final String strCurrency,
  142.         final String strMaturityTenor,
  143.         final double dblCoupon)
  144.     {
  145.         FixedFloatSwapConvention ffConv = IBORFixedFloatContainer.ConventionFromJurisdiction (
  146.             strCurrency,
  147.             "ALL",
  148.             strMaturityTenor,
  149.             "MAIN"
  150.         );

  151.         return ffConv.createFixFloatComponent (
  152.             dtSpot,
  153.             strMaturityTenor,
  154.             dblCoupon,
  155.             0.,
  156.             1.
  157.         );
  158.     }

  159.     /*
  160.      * Construct the Array of Deposit Instruments from the given set of parameters
  161.      *
  162.      *      USE WITH CARE: This sample ignores errors and does not handle exceptions.
  163.      */

  164.     private static final SingleStreamComponent[] DepositInstrumentsFromMaturityDays (
  165.         final JulianDate dtEffective,
  166.         final String strCurrency,
  167.         final int[] aiDay)
  168.         throws Exception
  169.     {
  170.         SingleStreamComponent[] aDeposit = new SingleStreamComponent[aiDay.length];

  171.         ComposableFloatingUnitSetting cfus = new ComposableFloatingUnitSetting (
  172.             "3M",
  173.             CompositePeriodBuilder.EDGE_DATE_SEQUENCE_SINGLE,
  174.             null,
  175.             ForwardLabel.Create (
  176.                 strCurrency,
  177.                 "3M"
  178.             ),
  179.             CompositePeriodBuilder.REFERENCE_PERIOD_IN_ADVANCE,
  180.             0.
  181.         );

  182.         CompositePeriodSetting cps = new CompositePeriodSetting (
  183.             4,
  184.             "3M",
  185.             strCurrency,
  186.             null,
  187.             1.,
  188.             null,
  189.             null,
  190.             null,
  191.             null
  192.         );

  193.         CashSettleParams csp = new CashSettleParams (
  194.             0,
  195.             strCurrency,
  196.             0
  197.         );

  198.         for (int i = 0; i < aiDay.length; ++i) {
  199.             aDeposit[i] = new SingleStreamComponent (
  200.                 "DEPOSIT_" + aiDay[i],
  201.                 new Stream (
  202.                     CompositePeriodBuilder.FloatingCompositeUnit (
  203.                         CompositePeriodBuilder.EdgePair (
  204.                             dtEffective,
  205.                             dtEffective.addBusDays (
  206.                                 aiDay[i],
  207.                                 strCurrency
  208.                             )
  209.                         ),
  210.                         cps,
  211.                         cfus
  212.                     )
  213.                 ),
  214.                 csp
  215.             );

  216.             aDeposit[i].setPrimaryCode (aiDay[i] + "D");
  217.         }

  218.         return aDeposit;
  219.     }

  220.     /*
  221.      * Construct the Array of Swap Instruments from the given set of parameters
  222.      *
  223.      *      USE WITH CARE: This sample ignores errors and does not handle exceptions.
  224.      */

  225.     private static final FixFloatComponent[] SwapInstrumentsFromMaturityTenor (
  226.         final JulianDate dtSpot,
  227.         final String strCurrency,
  228.         final String[] astrMaturityTenor)
  229.         throws Exception
  230.     {
  231.         FixFloatComponent[] aIRS = new FixFloatComponent[astrMaturityTenor.length];

  232.         for (int i = 0; i < astrMaturityTenor.length; ++i)
  233.             aIRS[i] = OTCIRS (
  234.                 dtSpot,
  235.                 strCurrency,
  236.                 astrMaturityTenor[i],
  237.                 0.
  238.             );

  239.         return aIRS;
  240.     }

  241.     /*
  242.      * This sample demonstrates discount curve calibration and input instrument calibration quote recovery.
  243.      *  It shows the following:
  244.      *  - Construct the Array of Cash/Swap Instruments and their Quotes from the given set of parameters.
  245.      *  - Construct the Cash/Swap Instrument Set Stretch Builder.
  246.      *  - Set up the Linear Curve Calibrator using the following parameters:
  247.      *      - Cubic Exponential Mixture Basis Spline Set
  248.      *      - Ck = 2, Segment Curvature Penalty = 2
  249.      *      - Quadratic Rational Shape Controller
  250.      *      - Natural Boundary Setting
  251.      *  - Construct the Shape Preserving Discount Curve by applying the linear curve calibrator to the array
  252.      *      of Cash and Swap Stretches.
  253.      *  - Cross-Comparison of the Cash/Swap Calibration Instrument "Rate" metric across the different curve
  254.      *      construction methodologies.
  255.      *
  256.      *      USE WITH CARE: This sample ignores errors and does not handle exceptions.
  257.      */

  258.     private static final void CustomDiscountCurveBuilderSample (
  259.         final JulianDate dtSpot,
  260.         final String strCurrency)
  261.         throws Exception
  262.     {
  263.         /*
  264.          * Construct the Array of Deposit Instruments and their Quotes from the given set of parameters
  265.          */

  266.         SingleStreamComponent[] aDepositComp = DepositInstrumentsFromMaturityDays (
  267.             dtSpot,
  268.             strCurrency,
  269.             new int[] {
  270.                 1, 2, 7, 14, 30, 60
  271.             }
  272.         );

  273.         double[] adblDepositQuote = new double[] {
  274.             0.0013, 0.0017, 0.0017, 0.0018, 0.0020, 0.0023
  275.         };

  276.         /*
  277.          * Construct the Deposit Instrument Set Stretch Builder
  278.          */

  279.         LatentStateStretchSpec depositStretch = LatentStateStretchBuilder.ForwardFundingStretchSpec (
  280.             "DEPOSIT",
  281.             aDepositComp,
  282.             "ForwardRate",
  283.             adblDepositQuote
  284.         );

  285.         /*
  286.          * Construct the Array of EDF Instruments and their Quotes from the given set of parameters
  287.          */

  288.         SingleStreamComponent[] aEDFComp = SingleStreamComponentBuilder.ForwardRateFuturesPack (
  289.             dtSpot,
  290.             8,
  291.             strCurrency
  292.         );

  293.         double[] adblEDFQuote = new double[] {
  294.             0.0027, 0.0032, 0.0041, 0.0054, 0.0077, 0.0104, 0.0134, 0.0160
  295.         };

  296.         /*
  297.          * Construct the EDF Instrument Set Stretch Builder
  298.          */

  299.         LatentStateStretchSpec edfStretch = LatentStateStretchBuilder.ForwardFundingStretchSpec (
  300.             "EDF",
  301.             aEDFComp,
  302.             "ForwardRate",
  303.             adblEDFQuote
  304.         );

  305.         /*
  306.          * Construct the Array of Swap Instruments and their Quotes from the given set of parameters
  307.          */

  308.         FixFloatComponent[] aSwapComp = SwapInstrumentsFromMaturityTenor (
  309.             dtSpot,
  310.             strCurrency,
  311.             new java.lang.String[] {
  312.                 "4Y", "5Y", "6Y", "7Y", "8Y", "9Y", "10Y", "11Y", "12Y", "15Y", "20Y", "25Y", "30Y", "40Y", "50Y"
  313.             }
  314.         );

  315.         double[] adblSwapQuote = new double[] {
  316.             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
  317.         };

  318.         /*
  319.          * Construct the Swap Instrument Set Stretch Builder
  320.          */

  321.         LatentStateStretchSpec swapStretch = LatentStateStretchBuilder.ForwardFundingStretchSpec (
  322.             "SWAP",
  323.             aSwapComp,
  324.             "SwapRate",
  325.             adblSwapQuote
  326.         );

  327.         LatentStateStretchSpec[] aStretchSpec = new LatentStateStretchSpec[] {
  328.             depositStretch,
  329.             edfStretch,
  330.             swapStretch
  331.         };

  332.         /*
  333.          * Set up the Linear Curve Calibrator using the following parameters:
  334.          *  - Cubic Exponential Mixture Basis Spline Set
  335.          *  - Ck = 2, Segment Curvature Penalty = 2
  336.          *  - Quadratic Rational Shape Controller
  337.          *  - Natural Boundary Setting
  338.          */

  339.         LinearLatentStateCalibrator lcc = new LinearLatentStateCalibrator (
  340.             new SegmentCustomBuilderControl (
  341.                 MultiSegmentSequenceBuilder.BASIS_SPLINE_POLYNOMIAL,
  342.                 new PolynomialFunctionSetParams (4),
  343.                 SegmentInelasticDesignControl.Create (
  344.                     2,
  345.                     2
  346.                 ),
  347.                 new ResponseScalingShapeControl (
  348.                     true,
  349.                     new QuadraticRationalShapeControl (0.)
  350.                 ),
  351.                 null
  352.             ),
  353.             BoundarySettings.NaturalStandard(),
  354.             MultiSegmentSequence.CALIBRATE,
  355.             null,
  356.             null
  357.         );

  358.         ValuationParams valParams = new ValuationParams (
  359.             dtSpot,
  360.             dtSpot,
  361.             strCurrency
  362.         );

  363.         /*
  364.          * Construct the Shape Preserving Discount Curve by applying the linear curve calibrator to the array
  365.          *  of Deposit, Futures, and Swap Stretches.
  366.          */

  367.         MergedDiscountForwardCurve dc = ScenarioDiscountCurveBuilder.ShapePreservingDFBuild (
  368.             strCurrency,
  369.             lcc,
  370.             aStretchSpec,
  371.             valParams,
  372.             null,
  373.             null,
  374.             null,
  375.             1.
  376.         );

  377.         /*
  378.          * Cross-Comparison of the Deposit Calibration Instrument "Rate" metric across the different curve
  379.          *  construction methodologies.
  380.          */

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

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

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

  384.         for (int i = 0; i < aDepositComp.length; ++i)
  385.             System.out.println ("\t[" + aDepositComp[i].maturityDate() + "] = " +
  386.                 FormatUtil.FormatDouble (aDepositComp[i].measureValue (valParams, null,
  387.                     MarketParamsBuilder.Create (dc, null, null, null, null, null, null),
  388.                         null, "Rate"), 1, 6, 1.) + " | " + FormatUtil.FormatDouble (adblDepositQuote[i], 1, 6, 1.));

  389.         /*
  390.          * Cross-Comparison of the EDF Calibration Instrument "Rate" metric across the different curve
  391.          *  construction methodologies.
  392.          */

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

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

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

  396.         for (int i = 0; i < aEDFComp.length; ++i)
  397.             System.out.println ("\t[" + aEDFComp[i].maturityDate() + "] = " +
  398.                 FormatUtil.FormatDouble (aEDFComp[i].measureValue (valParams, null,
  399.                     MarketParamsBuilder.Create (dc, null, null, null, null, null, null),
  400.                         null, "Rate"), 1, 6, 1.) + " | " + FormatUtil.FormatDouble (adblEDFQuote[i], 1, 6, 1.));

  401.         /*
  402.          * Cross-Comparison of the Swap Calibration Instrument "Rate" metric across the different curve
  403.          *  construction methodologies.
  404.          */

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

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

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

  408.         for (int i = 0; i < aSwapComp.length; ++i)
  409.             System.out.println ("\t[" + aSwapComp[i].maturityDate() + "] = " +
  410.                 FormatUtil.FormatDouble (aSwapComp[i].measureValue (valParams, null,
  411.                     MarketParamsBuilder.Create (dc, null, null, null, null, null, null),
  412.                         null, "CalibSwapRate"), 1, 6, 1.) + " | " + FormatUtil.FormatDouble (adblSwapQuote[i], 1, 6, 1.) + " | " +
  413.                             FormatUtil.FormatDouble (aSwapComp[i].measureValue (valParams, null,
  414.                                 MarketParamsBuilder.Create (dc, null, null, null, null, null, null),
  415.                                     null, "FairPremium"), 1, 6, 1.));

  416.         System.out.println ("\t----------------------------------------------------------------");
  417.     }

  418.     public static final void main (
  419.         final String[] astrArgs)
  420.         throws Exception
  421.     {
  422.         /*
  423.          * Initialize the Credit Analytics Library
  424.          */

  425.         EnvManager.InitEnv ("");

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

  427.         String strCurrency = "USD";

  428.         CustomDiscountCurveBuilderSample (
  429.             dtToday,
  430.             strCurrency
  431.         );

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