InAdvanceIMMSwap.java

  1. package org.drip.sample.fixfloat;

  2. import java.util.List;

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

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

  25. /*!
  26.  * Copyright (C) 2019 Lakshmi Krishnamurthy
  27.  * Copyright (C) 2018 Lakshmi Krishnamurthy
  28.  * Copyright (C) 2017 Lakshmi Krishnamurthy
  29.  * Copyright (C) 2016 Lakshmi Krishnamurthy
  30.  * Copyright (C) 2015 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>InAdvanceIMMSwap</i> demonstrates the Construction and Valuation of a In-Advance IMM Swap.
  89.  *  
  90.  * <br><br>
  91.  *  <ul>
  92.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/AnalyticsCore.md">Analytics Core Module</a></li>
  93.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/FixedIncomeAnalyticsLibrary.md">Fixed Income Analytics Library</a></li>
  94.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/README.md">Sample</a></li>
  95.  *      <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>
  96.  *  </ul>
  97.  * <br><br>
  98.  *
  99.  * @author Lakshmi Krishnamurthy
  100.  */

  101. public class InAdvanceIMMSwap {

  102.     /*
  103.      * Construct the Array of Deposit Instruments from the given set of parameters
  104.      *
  105.      *      USE WITH CARE: This sample ignores errors and does not handle exceptions.
  106.      */

  107.     private static final SingleStreamComponent[] DepositInstrumentsFromMaturityDays (
  108.         final JulianDate dtEffective,
  109.         final String strCurrency,
  110.         final int[] aiDay)
  111.         throws Exception
  112.     {
  113.         SingleStreamComponent[] aDeposit = new SingleStreamComponent[aiDay.length];

  114.         ComposableFloatingUnitSetting cfus = new ComposableFloatingUnitSetting (
  115.             "3M",
  116.             CompositePeriodBuilder.EDGE_DATE_SEQUENCE_SINGLE,
  117.             null,
  118.             ForwardLabel.Create (
  119.                 strCurrency,
  120.                 "3M"
  121.             ),
  122.             CompositePeriodBuilder.REFERENCE_PERIOD_IN_ADVANCE,
  123.             0.
  124.         );

  125.         CompositePeriodSetting cps = new CompositePeriodSetting (
  126.             4,
  127.             "3M",
  128.             strCurrency,
  129.             null,
  130.             1.,
  131.             null,
  132.             null,
  133.             null,
  134.             null
  135.         );

  136.         CashSettleParams csp = new CashSettleParams (
  137.             0,
  138.             strCurrency,
  139.             0
  140.         );

  141.         for (int i = 0; i < aiDay.length; ++i) {
  142.             aDeposit[i] = new SingleStreamComponent (
  143.                 "DEPOSIT_" + aiDay[i],
  144.                 new Stream (
  145.                     CompositePeriodBuilder.FloatingCompositeUnit (
  146.                         CompositePeriodBuilder.EdgePair (
  147.                             dtEffective,
  148.                             dtEffective.addBusDays (
  149.                                 aiDay[i],
  150.                                 strCurrency
  151.                             )
  152.                         ),
  153.                         cps,
  154.                         cfus
  155.                     )
  156.                 ),
  157.                 csp
  158.             );

  159.             aDeposit[i].setPrimaryCode (aiDay[i] + "D");
  160.         }

  161.         return aDeposit;
  162.     }

  163.     /*
  164.      * Construct the Array of Swap Instruments from the given set of parameters
  165.      *
  166.      *      USE WITH CARE: This sample ignores errors and does not handle exceptions.
  167.      */

  168.     private static final FixFloatComponent[] SwapInstrumentsFromMaturityTenor (
  169.         final JulianDate dtEffective,
  170.         final String strCurrency,
  171.         final String[] astrMaturityTenor,
  172.         final boolean bIsIMM)
  173.         throws Exception
  174.     {
  175.         FixFloatComponent[] aIRS = new FixFloatComponent[astrMaturityTenor.length];

  176.         UnitCouponAccrualSetting ucasFixed = new UnitCouponAccrualSetting (
  177.             2,
  178.             "Act/360",
  179.             false,
  180.             "Act/360",
  181.             false,
  182.             strCurrency,
  183.             true,
  184.             CompositePeriodBuilder.ACCRUAL_COMPOUNDING_RULE_GEOMETRIC
  185.         );

  186.         ComposableFloatingUnitSetting cfusFloating = new ComposableFloatingUnitSetting (
  187.             "6M",
  188.             CompositePeriodBuilder.EDGE_DATE_SEQUENCE_REGULAR,
  189.             null,
  190.             ForwardLabel.Create (
  191.                 strCurrency,
  192.                 "6M"
  193.             ),
  194.             CompositePeriodBuilder.REFERENCE_PERIOD_IN_ADVANCE,
  195.             0.
  196.         );

  197.         ComposableFixedUnitSetting cfusFixed = new ComposableFixedUnitSetting (
  198.             "6M",
  199.             CompositePeriodBuilder.EDGE_DATE_SEQUENCE_REGULAR,
  200.             null,
  201.             0.,
  202.             0.,
  203.             strCurrency
  204.         );

  205.         CompositePeriodSetting cpsFloating = new CompositePeriodSetting (
  206.             2,
  207.             "6M",
  208.             strCurrency,
  209.             null,
  210.             -1.,
  211.             null,
  212.             null,
  213.             null,
  214.             null
  215.         );

  216.         CompositePeriodSetting cpsFixed = new CompositePeriodSetting (
  217.             2,
  218.             "6M",
  219.             strCurrency,
  220.             null,
  221.             1.,
  222.             null,
  223.             null,
  224.             null,
  225.             null
  226.         );

  227.         CashSettleParams csp = new CashSettleParams (
  228.             0,
  229.             strCurrency,
  230.             0
  231.         );

  232.         for (int i = 0; i < astrMaturityTenor.length; ++i) {
  233.             List<Integer> lsFixedStreamEdgeDate = !bIsIMM ? CompositePeriodBuilder.RegularEdgeDates (
  234.                 dtEffective,
  235.                 "6M",
  236.                 astrMaturityTenor[i],
  237.                 null
  238.             ) : CompositePeriodBuilder.IMMEdgeDates (
  239.                 dtEffective,
  240.                 3,
  241.                 "6M",
  242.                 astrMaturityTenor[i],
  243.                 null
  244.             );

  245.             List<Integer> lsFloatingStreamEdgeDate = !bIsIMM ? CompositePeriodBuilder.RegularEdgeDates (
  246.                 dtEffective,
  247.                 "6M",
  248.                 astrMaturityTenor[i],
  249.                 null
  250.             ) : CompositePeriodBuilder.IMMEdgeDates (
  251.                 dtEffective,
  252.                 3,
  253.                 "6M",
  254.                 astrMaturityTenor[i],
  255.                 null
  256.             );

  257.             Stream floatingStream = new Stream (
  258.                 CompositePeriodBuilder.FloatingCompositeUnit (
  259.                     lsFloatingStreamEdgeDate,
  260.                     cpsFloating,
  261.                     cfusFloating
  262.                 )
  263.             );

  264.             Stream fixedStream = new Stream (
  265.                 CompositePeriodBuilder.FixedCompositeUnit (
  266.                     lsFixedStreamEdgeDate,
  267.                     cpsFixed,
  268.                     ucasFixed,
  269.                     cfusFixed
  270.                 )
  271.             );

  272.             FixFloatComponent irs = new FixFloatComponent (
  273.                 fixedStream,
  274.                 floatingStream,
  275.                 csp
  276.             );

  277.             irs.setPrimaryCode ("IRS." + astrMaturityTenor[i] + "." + strCurrency);

  278.             aIRS[i] = irs;
  279.         }

  280.         return aIRS;
  281.     }

  282.     /*
  283.      * This sample demonstrates discount curve calibration and input instrument calibration quote recovery.
  284.      *  It shows the following:
  285.      *  - Construct the Array of Cash/Swap Instruments and their Quotes from the given set of parameters.
  286.      *  - Construct the Cash/Swap Instrument Set Stretch Builder.
  287.      *  - Set up the Linear Curve Calibrator using the following parameters:
  288.      *      - Cubic Exponential Mixture Basis Spline Set
  289.      *      - Ck = 2, Segment Curvature Penalty = 2
  290.      *      - Quadratic Rational Shape Controller
  291.      *      - Natural Boundary Setting
  292.      *  - Construct the Shape Preserving Discount Curve by applying the linear curve calibrator to the array
  293.      *      of Cash and Swap Stretches.
  294.      *  - Cross-Comparison of the Cash/Swap Calibration Instrument "Rate" metric across the different curve
  295.      *      construction methodologies.
  296.      *
  297.      *      USE WITH CARE: This sample ignores errors and does not handle exceptions.
  298.      */

  299.     private static final void CustomDiscountCurveBuilderSample (
  300.         final JulianDate dtSpot,
  301.         final String strCurrency)
  302.         throws Exception
  303.     {
  304.         /*
  305.          * Construct the Array of Deposit Instruments and their Quotes from the given set of parameters
  306.          */

  307.         SingleStreamComponent[] aDepositComp = DepositInstrumentsFromMaturityDays (
  308.             dtSpot,
  309.             strCurrency,
  310.             new int[] {
  311.                 1, 2, 7, 14, 30, 60
  312.             }
  313.         );

  314.         double[] adblDepositQuote = new double[] {
  315.             0.0013, 0.0017, 0.0017, 0.0018, 0.0020, 0.0023
  316.         };

  317.         /*
  318.          * Construct the Deposit Instrument Set Stretch Builder
  319.          */

  320.         LatentStateStretchSpec depositStretch = LatentStateStretchBuilder.ForwardFundingStretchSpec (
  321.             "DEPOSIT",
  322.             aDepositComp,
  323.             "ForwardRate",
  324.             adblDepositQuote
  325.         );

  326.         /*
  327.          * Construct the Array of EDF Instruments and their Quotes from the given set of parameters
  328.          */

  329.         SingleStreamComponent[] aEDFComp = SingleStreamComponentBuilder.ForwardRateFuturesPack (
  330.             dtSpot,
  331.             8,
  332.             strCurrency
  333.         );

  334.         double[] adblEDFQuote = new double[] {
  335.             0.0027, 0.0032, 0.0041, 0.0054, 0.0077, 0.0104, 0.0134, 0.0160
  336.         };

  337.         /*
  338.          * Construct the EDF Instrument Set Stretch Builder
  339.          */

  340.         LatentStateStretchSpec edfStretch = LatentStateStretchBuilder.ForwardFundingStretchSpec (
  341.             "EDF",
  342.             aEDFComp,
  343.             "ForwardRate",
  344.             adblEDFQuote
  345.         );

  346.         /*
  347.          * Construct the Array of Swap Instruments and their Quotes from the given set of parameters
  348.          */

  349.         FixFloatComponent[] aSwapInAdvance = SwapInstrumentsFromMaturityTenor (
  350.             dtSpot,
  351.             strCurrency,
  352.             new java.lang.String[] {
  353.                 "4Y", "5Y", "6Y", "7Y", "8Y", "9Y", "10Y", "11Y", "12Y", "15Y", "20Y", "25Y", "30Y", "40Y", "50Y"
  354.             },
  355.             false
  356.         );

  357.         FixFloatComponent[] aSwapInAdvanceIMM = SwapInstrumentsFromMaturityTenor (
  358.             dtSpot,
  359.             strCurrency,
  360.             new java.lang.String[] {
  361.                 "4Y", "5Y", "6Y", "7Y", "8Y", "9Y", "10Y", "11Y", "12Y", "15Y", "20Y", "25Y", "30Y", "40Y", "50Y"
  362.             },
  363.             true
  364.         );

  365.         double[] adblSwapQuote = new double[] {
  366.             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
  367.         };

  368.         /*
  369.          * Construct the Swap Instrument Set Stretch Builder
  370.          */

  371.         LatentStateStretchSpec swapStretch = LatentStateStretchBuilder.ForwardFundingStretchSpec (
  372.             "SWAP",
  373.             aSwapInAdvance,
  374.             "SwapRate",
  375.             adblSwapQuote
  376.         );

  377.         LatentStateStretchSpec[] aStretchSpec = new LatentStateStretchSpec[] {
  378.             depositStretch,
  379.             edfStretch,
  380.             swapStretch
  381.         };

  382.         /*
  383.          * Set up the Linear Curve Calibrator using the following parameters:
  384.          *  - Cubic Exponential Mixture Basis Spline Set
  385.          *  - Ck = 2, Segment Curvature Penalty = 2
  386.          *  - Quadratic Rational Shape Controller
  387.          *  - Natural Boundary Setting
  388.          */

  389.         LinearLatentStateCalibrator lcc = new LinearLatentStateCalibrator (
  390.             new SegmentCustomBuilderControl (
  391.                 MultiSegmentSequenceBuilder.BASIS_SPLINE_POLYNOMIAL,
  392.                 new PolynomialFunctionSetParams (4),
  393.                 SegmentInelasticDesignControl.Create (
  394.                     2,
  395.                     2
  396.                 ),
  397.                 new ResponseScalingShapeControl (
  398.                     true,
  399.                     new QuadraticRationalShapeControl (0.)
  400.                 ),
  401.                 null
  402.             ),
  403.             BoundarySettings.NaturalStandard(),
  404.             MultiSegmentSequence.CALIBRATE,
  405.             null,
  406.             null
  407.         );

  408.         ValuationParams valParams = new ValuationParams (
  409.             dtSpot,
  410.             dtSpot,
  411.             strCurrency
  412.         );

  413.         /*
  414.          * Construct the Shape Preserving Discount Curve by applying the linear curve calibrator to the array
  415.          *  of Deposit, Futures, and Swap Stretches.
  416.          */

  417.         MergedDiscountForwardCurve dc = ScenarioDiscountCurveBuilder.ShapePreservingDFBuild (
  418.             strCurrency,
  419.             lcc,
  420.             aStretchSpec,
  421.             valParams,
  422.             null,
  423.             null,
  424.             null,
  425.             1.
  426.         );

  427.         CurveSurfaceQuoteContainer csqs = MarketParamsBuilder.Create (
  428.             dc,
  429.             null,
  430.             null,
  431.             null,
  432.             null,
  433.             null,
  434.             null
  435.         );

  436.         /*
  437.          * Cross-Comparison of the In-Advance/Arrears Swap "Rate" metric across the different curve
  438.          *  construction methodologies.
  439.          */

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

  441.         System.out.println ("\t     IN-ADVANCE W+W/O IMM SWAP INSTRUMENTS METRIC COMPARISON");

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

  443.         System.out.println ("\t\tL -> R:");

  444.         System.out.println ("\t\t\t - Swap Maturity");

  445.         System.out.println ("\t\t\t - In Advance Calibration Quote");

  446.         System.out.println ("\t\t\t - In Advance Fair Premium");

  447.         System.out.println ("\t\t\t - In Advance Swap Rate");

  448.         System.out.println ("\t\t\t - In Advance IMM Swap Rate");

  449.         System.out.println ("\t\t\t - In Advance IMM Swap Rate Shift");

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

  451.         for (int i = 0; i < aSwapInAdvance.length; ++i) {
  452.             double dblInAdvanceIMMFairPremium = aSwapInAdvanceIMM[i].measureValue (
  453.                 valParams,
  454.                 null,
  455.                 csqs,
  456.                 null,
  457.                 "FairPremium"
  458.             );

  459.             System.out.println ("\t[" + aSwapInAdvance[i].maturityDate() + "] = " +
  460.                 FormatUtil.FormatDouble (aSwapInAdvance[i].measureValue (valParams, null, csqs, null, "CalibSwapRate"), 1, 4, 100.) + "% | " +
  461.                 FormatUtil.FormatDouble (adblSwapQuote[i], 1, 4, 100.) + "% | " +
  462.                 FormatUtil.FormatDouble (aSwapInAdvance[i].measureValue (valParams, null, csqs, null, "FairPremium"), 1, 4, 100.) + "% | " +
  463.                 FormatUtil.FormatDouble (dblInAdvanceIMMFairPremium, 1, 4, 100.) + "% | " +
  464.                 FormatUtil.FormatDouble (dblInAdvanceIMMFairPremium - adblSwapQuote[i], 1, 0, 10000.)
  465.             );
  466.         }

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

  468.         System.out.println ("\t     IN-ADVANCE W+W/O IMM SWAP INSTRUMENTS DV01 COMPARISON");

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

  470.         System.out.println ("\t\tL -> R:");

  471.         System.out.println ("\t\t\t - Swap Maturity");

  472.         System.out.println ("\t\t\t - In Advance Swap DV01");

  473.         System.out.println ("\t\t\t - In Advance IMM Swap DV01");

  474.         System.out.println ("\t\t\t - In Advance IMM Swap DV01 Shift");

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

  476.         for (int i = 0; i < aSwapInAdvance.length; ++i) {
  477.             double dblInAdvanceDV01 = aSwapInAdvance[i].measureValue (
  478.                 valParams,
  479.                 null,
  480.                 csqs,
  481.                 null,
  482.                 "FixedDV01"
  483.             );

  484.             double dblInAdvanceIMMDV01 = aSwapInAdvanceIMM[i].measureValue (
  485.                 valParams,
  486.                 null,
  487.                 csqs,
  488.                 null,
  489.                 "FixedDV01"
  490.             );

  491.             System.out.println ("\t[" + aSwapInAdvance[i].maturityDate() + "] = " +
  492.                 FormatUtil.FormatDouble (dblInAdvanceDV01, 2, 1, 10000.) + " | " +
  493.                 FormatUtil.FormatDouble (dblInAdvanceIMMDV01, 2, 1, 10000.) + " | " +
  494.                 FormatUtil.FormatDouble (dblInAdvanceIMMDV01 - dblInAdvanceDV01, 1, 2, 10000.)
  495.             );
  496.         }

  497.         System.out.println ("\t-------------------------------------------------------------------------------");
  498.     }

  499.     public static final void main (
  500.         final String[] astrArgs)
  501.         throws Exception
  502.     {
  503.         /*
  504.          * Initialize the Credit Analytics Library
  505.          */

  506.         EnvManager.InitEnv ("");

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

  508.         String strCurrency = "USD";

  509.         CustomDiscountCurveBuilderSample (
  510.             dtToday,
  511.             strCurrency
  512.         );

  513.         EnvManager.TerminateEnv();
  514.     }
  515. }