JurisdictionOTCInstrumentMeasures.java

  1. package org.drip.sample.ois;

  2. import java.util.Map;

  3. import org.drip.analytics.date.JulianDate;
  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.market.CurveSurfaceQuoteContainer;
  9. import org.drip.param.valuation.ValuationParams;
  10. import org.drip.product.creator.SingleStreamComponentBuilder;
  11. import org.drip.product.definition.CalibratableComponent;
  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.MergedDiscountForwardCurve;
  19. import org.drip.state.estimator.LatentStateStretchBuilder;
  20. import org.drip.state.identifier.OvernightLabel;
  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) 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 DRIP, a free-software/open-source library for buy/side financial/trading model
  32.  *      libraries targeting analysts and developers
  33.  *      https://lakshmidrip.github.io/DRIP/
  34.  *  
  35.  *  DRIP is composed of four main libraries:
  36.  *  
  37.  *  - DRIP Fixed Income - https://lakshmidrip.github.io/DRIP-Fixed-Income/
  38.  *  - DRIP Asset Allocation - https://lakshmidrip.github.io/DRIP-Asset-Allocation/
  39.  *  - DRIP Numerical Optimizer - https://lakshmidrip.github.io/DRIP-Numerical-Optimizer/
  40.  *  - DRIP Statistical Learning - https://lakshmidrip.github.io/DRIP-Statistical-Learning/
  41.  *
  42.  *  - DRIP Fixed Income: Library for Instrument/Trading Conventions, Treasury Futures/Options,
  43.  *      Funding/Forward/Overnight Curves, Multi-Curve Construction/Valuation, Collateral Valuation and XVA
  44.  *      Metric Generation, Calibration and Hedge Attributions, Statistical Curve Construction, Bond RV
  45.  *      Metrics, Stochastic Evolution and Option Pricing, Interest Rate Dynamics and Option Pricing, LMM
  46.  *      Extensions/Calibrations/Greeks, Algorithmic Differentiation, and Asset Backed Models and Analytics.
  47.  *
  48.  *  - DRIP Asset Allocation: Library for model libraries for MPT framework, Black Litterman Strategy
  49.  *      Incorporator, Holdings Constraint, and Transaction Costs.
  50.  *
  51.  *  - DRIP Numerical Optimizer: Library for Numerical Optimization and Spline Functionality.
  52.  *
  53.  *  - DRIP Statistical Learning: Library for Statistical Evaluation and Machine Learning.
  54.  *
  55.  *  Licensed under the Apache License, Version 2.0 (the "License");
  56.  *      you may not use this file except in compliance with the License.
  57.  *  
  58.  *  You may obtain a copy of the License at
  59.  *      http://www.apache.org/licenses/LICENSE-2.0
  60.  *  
  61.  *  Unless required by applicable law or agreed to in writing, software
  62.  *      distributed under the License is distributed on an "AS IS" BASIS,
  63.  *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  64.  *  
  65.  *  See the License for the specific language governing permissions and
  66.  *      limitations under the License.
  67.  */

  68. /**
  69.  * JurisdictionOTCInstrumentMeasures contains the Curve Construction and Valuation Functionality of the OTC
  70.  *  OIS Instruments across Multiple Jurisdictions.
  71.  *
  72.  * @author Lakshmi Krishnamurthy
  73.  */

  74. public class JurisdictionOTCInstrumentMeasures {

  75.     private static final FixFloatComponent OTCOISFixFloat (
  76.         final JulianDate dtSpot,
  77.         final String strCurrency,
  78.         final String strMaturityTenor,
  79.         final double dblCoupon)
  80.     {
  81.         FixedFloatSwapConvention ffConv = OvernightFixedFloatContainer.FundConventionFromJurisdiction (
  82.             strCurrency
  83.         );

  84.         return ffConv.createFixFloatComponent (
  85.             dtSpot,
  86.             strMaturityTenor,
  87.             dblCoupon,
  88.             0.,
  89.             1.
  90.         );
  91.     }

  92.     /*
  93.      * Construct the Array of Deposit Instruments from the given set of parameters
  94.      *
  95.      *      USE WITH CARE: This sample ignores errors and does not handle exceptions.
  96.      */

  97.     private static final SingleStreamComponent[] DepositInstrumentsFromMaturityDays (
  98.         final JulianDate dtEffective,
  99.         final String strCurrency,
  100.         final int[] aiDay)
  101.         throws Exception
  102.     {
  103.         SingleStreamComponent[] aDeposit = new SingleStreamComponent[aiDay.length];

  104.         for (int i = 0; i < aiDay.length; ++i)
  105.             aDeposit[i] = SingleStreamComponentBuilder.Deposit (
  106.                 dtEffective,
  107.                 dtEffective.addBusDays (
  108.                     aiDay[i],
  109.                     strCurrency
  110.                 ),
  111.                 OvernightLabel.Create (
  112.                     strCurrency
  113.                 )
  114.             );

  115.         return aDeposit;
  116.     }

  117.     /*
  118.      * Construct the Array of Swap Instruments from the given set of parameters
  119.      *
  120.      *      USE WITH CARE: This sample ignores errors and does not handle exceptions.
  121.      */

  122.     private static final FixFloatComponent[] OISFromMaturityTenor (
  123.         final JulianDate dtSpot,
  124.         final String strCurrency,
  125.         final String[] astrMaturityTenor,
  126.         final double[] adblCoupon)
  127.         throws Exception
  128.     {
  129.         FixFloatComponent[] aOIS = new FixFloatComponent[astrMaturityTenor.length];

  130.         for (int i = 0; i < astrMaturityTenor.length; ++i)
  131.             aOIS[i] = OTCOISFixFloat (
  132.                 dtSpot,
  133.                 strCurrency,
  134.                 astrMaturityTenor[i],
  135.                 adblCoupon[i]
  136.             );

  137.         return aOIS;
  138.     }

  139.     /*
  140.      * Construct the Array of Swap Instruments from the given set of parameters
  141.      *
  142.      *      USE WITH CARE: This sample ignores errors and does not handle exceptions.
  143.      */

  144.     private static final FixFloatComponent[] OISFuturesFromMaturityTenor (
  145.         final JulianDate dtSpot,
  146.         final String strCurrency,
  147.         final String[] astrStartTenor,
  148.         final String[] astrMaturityTenor,
  149.         final double[] adblCoupon)
  150.         throws Exception
  151.     {
  152.         FixFloatComponent[] aOISFutures = new FixFloatComponent[astrMaturityTenor.length];

  153.         for (int i = 0; i < astrMaturityTenor.length; ++i)
  154.             aOISFutures[i] = OTCOISFixFloat (
  155.                 dtSpot.addTenor (astrStartTenor[i]),
  156.                 strCurrency,
  157.                 astrMaturityTenor[i],
  158.                 adblCoupon[i]
  159.             );

  160.         return aOISFutures;
  161.     }

  162.     private static final void OTCOISRun (
  163.         final JulianDate dtSpot,
  164.         final String strCurrency,
  165.         final String[] astrOTCMaturityTenor,
  166.         final boolean bCalibMetricDisplay)
  167.         throws Exception
  168.     {
  169.         if (bCalibMetricDisplay) {
  170.             System.out.println ("\n\t----------------------------------------------------------------");

  171.             System.out.println ("\t--------- DISCOUNT CURVE WITH OVERNIGHT INDEX ------------------");

  172.             System.out.println ("\t----------------------------------------------------------------");
  173.         }

  174.         /*
  175.          * Construct the Array of Deposit Instruments and their Quotes from the given set of parameters
  176.          */

  177.         SingleStreamComponent[] aDepositComp = DepositInstrumentsFromMaturityDays (
  178.             dtSpot,
  179.             strCurrency,
  180.             new int[] {
  181.                 1, 2, 3
  182.             }
  183.         );

  184.         double[] adblDepositQuote = new double[] {
  185.             0.0004, 0.0004, 0.0004       // Deposit
  186.         };

  187.         /*
  188.          * Construct the Deposit Instrument Set Stretch Builder
  189.          */

  190.         LatentStateStretchSpec depositStretch = LatentStateStretchBuilder.ForwardFundingStretchSpec (
  191.             "   DEPOSIT   ",
  192.             aDepositComp,
  193.             "ForwardRate",
  194.             adblDepositQuote
  195.         );

  196.         /*
  197.          * Construct the Array of Short End OIS Instruments and their Quotes from the given set of parameters
  198.          */

  199.         double[] adblShortEndOISQuote = new double[] {
  200.             0.00070,    //   1W
  201.             0.00069,    //   2W
  202.             0.00078,    //   3W
  203.             0.00074     //   1M
  204.         };

  205.         CalibratableComponent[] aShortEndOISComp = OISFromMaturityTenor (
  206.             dtSpot,
  207.             strCurrency,
  208.             new java.lang.String[] {
  209.                 "1W", "2W", "3W", "1M"
  210.             },
  211.             adblShortEndOISQuote
  212.         );

  213.         /*
  214.          * Construct the Short End OIS Instrument Set Stretch Builder
  215.          */

  216.         LatentStateStretchSpec oisShortEndStretch = LatentStateStretchBuilder.ForwardFundingStretchSpec (
  217.             "SHORT END OIS",
  218.             aShortEndOISComp,
  219.             "SwapRate",
  220.             adblShortEndOISQuote
  221.         );

  222.         /*
  223.          * Construct the Array of OIS Futures Instruments and their Quotes from the given set of parameters
  224.          */

  225.         double[] adblOISFutureQuote = new double[] {
  226.              0.00046,    //   1M x 1M
  227.              0.00016,    //   2M x 1M
  228.             -0.00007,    //   3M x 1M
  229.             -0.00013,    //   4M x 1M
  230.             -0.00014     //   5M x 1M
  231.         };

  232.         CalibratableComponent[] aOISFutureComp = OISFuturesFromMaturityTenor (
  233.             dtSpot,
  234.             strCurrency,
  235.             new java.lang.String[] {
  236.                 "1M", "2M", "3M", "4M", "5M"
  237.             },
  238.             new java.lang.String[] {
  239.                 "1M", "1M", "1M", "1M", "1M"
  240.             },
  241.             adblOISFutureQuote
  242.         );

  243.         /*
  244.          * Construct the OIS Future Instrument Set Stretch Builder
  245.          */

  246.         LatentStateStretchSpec oisFutureStretch = LatentStateStretchBuilder.ForwardFundingStretchSpec (
  247.             " OIS FUTURE  ",
  248.             aOISFutureComp,
  249.             "SwapRate",
  250.             adblOISFutureQuote
  251.         );

  252.         /*
  253.          * Construct the Array of Long End OIS Instruments and their Quotes from the given set of parameters
  254.          */

  255.         double[] adblLongEndOISQuote = new double[] {
  256.             0.00002,    //  15M
  257.             0.00008,    //  18M
  258.             0.00021,    //  21M
  259.             0.00036,    //   2Y
  260.             0.00127,    //   3Y
  261.             0.00274,    //   4Y
  262.             0.00456,    //   5Y
  263.             0.00647,    //   6Y
  264.             0.00827,    //   7Y
  265.             0.00996,    //   8Y
  266.             0.01147,    //   9Y
  267.             0.01280,    //  10Y
  268.             0.01404,    //  11Y
  269.             0.01516,    //  12Y
  270.             0.01764,    //  15Y
  271.             0.01939,    //  20Y
  272.             0.02003,    //  25Y
  273.             0.02038     //  30Y
  274.         };

  275.         CalibratableComponent[] aLongEndOISComp = OISFromMaturityTenor (
  276.             dtSpot,
  277.             strCurrency,
  278.             new java.lang.String[] {
  279.                 "15M", "18M", "21M", "2Y", "3Y", "4Y", "5Y", "6Y", "7Y", "8Y", "9Y", "10Y", "11Y", "12Y", "15Y", "20Y", "25Y", "30Y"
  280.             },
  281.             adblLongEndOISQuote
  282.         );

  283.         /*
  284.          * Construct the Long End OIS Instrument Set Stretch Builder
  285.          */

  286.         LatentStateStretchSpec oisLongEndStretch = LatentStateStretchBuilder.ForwardFundingStretchSpec (
  287.             "LONG END OIS ",
  288.             aLongEndOISComp,
  289.             "SwapRate",
  290.             adblLongEndOISQuote
  291.         );

  292.         LatentStateStretchSpec[] aStretchSpec = new LatentStateStretchSpec[] {
  293.             depositStretch,
  294.             oisShortEndStretch,
  295.             oisFutureStretch,
  296.             oisLongEndStretch
  297.         };

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

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

  324.         ValuationParams valParams = new ValuationParams (
  325.             dtSpot,
  326.             dtSpot,
  327.             strCurrency
  328.         );

  329.         /*
  330.          * Construct the Shape Preserving Discount Curve by applying the linear curve calibrator to the array
  331.          *  of Deposit and Swap Stretches.
  332.          */

  333.         MergedDiscountForwardCurve dc = ScenarioDiscountCurveBuilder.ShapePreservingDFBuild (
  334.             strCurrency,
  335.             lcc,
  336.             aStretchSpec,
  337.             valParams,
  338.             null,
  339.             null,
  340.             null,
  341.             1.
  342.         );

  343.         CurveSurfaceQuoteContainer mktParams = MarketParamsBuilder.Create (
  344.             dc,
  345.             null,
  346.             null,
  347.             null,
  348.             null,
  349.             null,
  350.             null
  351.         );

  352.         if (bCalibMetricDisplay) {

  353.             /*
  354.              * Cross-Comparison of the Deposit Calibration Instrument "Rate" metric across the different curve
  355.              *  construction methodologies.
  356.              */

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

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

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

  360.             for (int i = 0; i < aDepositComp.length; ++i)
  361.                 System.out.println ("\t[" + aDepositComp[i].effectiveDate() + " => " + aDepositComp[i].maturityDate() + "] = " +
  362.                     FormatUtil.FormatDouble (aDepositComp[i].measureValue (valParams, null, mktParams, null, "Rate"), 1, 6, 1.) + " | " +
  363.                         FormatUtil.FormatDouble (adblDepositQuote[i], 1, 6, 1.));

  364.             /*
  365.              * Cross-Comparison of the Short End OIS Calibration Instrument "Rate" metric across the different curve
  366.              *  construction methodologies.
  367.              */

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

  369.             System.out.println ("\t     OIS SHORT END INSTRUMENTS CALIBRATION RECOVERY");

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

  371.             for (int i = 0; i < aShortEndOISComp.length; ++i) {
  372.                 Map<String, Double> mapShortEndOISComp = aShortEndOISComp[i].value (valParams, null, mktParams, null);

  373.                 System.out.println ("\t[" + aShortEndOISComp[i].effectiveDate() + " => " + aShortEndOISComp[i].maturityDate() + "] = " +
  374.                     FormatUtil.FormatDouble (mapShortEndOISComp.get ("CalibSwapRate"), 1, 6, 1.) + " | " +
  375.                     FormatUtil.FormatDouble (adblShortEndOISQuote[i], 1, 6, 1.) + " | " +
  376.                     FormatUtil.FormatDouble (mapShortEndOISComp.get ("FairPremium"), 1, 6, 1.)
  377.                 );
  378.             }

  379.             /*
  380.              * Cross-Comparison of the OIS Future Calibration Instrument "Rate" metric across the different curve
  381.              *  construction methodologies.
  382.              */

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

  384.             System.out.println ("\t     OIS FUTURE INSTRUMENTS CALIBRATION RECOVERY");

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

  386.             for (int i = 0; i < aOISFutureComp.length; ++i) {
  387.                 Map<String, Double> mapOISFutureComp = aOISFutureComp[i].value (valParams, null, mktParams, null);

  388.                 System.out.println ("\t[" + aOISFutureComp[i].effectiveDate() + " => " + aOISFutureComp[i].maturityDate() + "] = " +
  389.                     FormatUtil.FormatDouble (mapOISFutureComp.get ("SwapRate"), 1, 6, 1.) + " | " +
  390.                     FormatUtil.FormatDouble (adblOISFutureQuote[i], 1, 6, 1.) + " | " +
  391.                     FormatUtil.FormatDouble (mapOISFutureComp.get ("FairPremium"), 1, 6, 1.)
  392.                 );
  393.             }

  394.             /*
  395.              * Cross-Comparison of the Long End OIS Calibration Instrument "Rate" metric across the different curve
  396.              *  construction methodologies.
  397.              */

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

  399.             System.out.println ("\t     OIS LONG END INSTRUMENTS CALIBRATION RECOVERY");

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

  401.             for (int i = 0; i < aLongEndOISComp.length; ++i) {
  402.                 Map<String, Double> mapLongEndOISComp = aLongEndOISComp[i].value (valParams, null, mktParams, null);

  403.                 System.out.println ("\t[" + aLongEndOISComp[i].effectiveDate() + " => " + aLongEndOISComp[i].maturityDate() + "] = " +
  404.                     FormatUtil.FormatDouble (mapLongEndOISComp.get ("CalibSwapRate"), 1, 6, 1.) + " | " +
  405.                     FormatUtil.FormatDouble (adblLongEndOISQuote[i], 1, 6, 1.) + " | " +
  406.                     FormatUtil.FormatDouble (mapLongEndOISComp.get ("FairPremium"), 1, 6, 1.)
  407.                 );
  408.             }

  409.             System.out.println ("\t----------------------------------------------------------------");
  410.         }

  411.         System.out.print ("\t[" + strCurrency + "] = ");

  412.         for (int i = 0; i < astrOTCMaturityTenor.length; ++i) {
  413.             FixFloatComponent swap = OTCOISFixFloat (
  414.                 dtSpot,
  415.                 strCurrency,
  416.                 astrOTCMaturityTenor[i],
  417.                 0.
  418.             );

  419.             Map<String, Double> mapOutput = swap.value (
  420.                 valParams,
  421.                 null,
  422.                 mktParams,
  423.                 null
  424.             );

  425.             System.out.print (
  426.                 FormatUtil.FormatDouble (mapOutput.get ("SwapRate"), 1, 4, 100.) + "% (" +
  427.                 FormatUtil.FormatDouble (mapOutput.get ("FairPremium"), 1, 4, 100.) + "%) || "
  428.             );
  429.         }

  430.         System.out.println();
  431.     }

  432.     public static final void main (
  433.         final String[] astrArgs)
  434.         throws Exception
  435.     {
  436.         /*
  437.          * Initialize the Credit Analytics Library
  438.          */

  439.         EnvManager.InitEnv ("");

  440.         JulianDate dtToday = org.drip.analytics.date.DateUtil.Today();

  441.         String[] astrOTCMaturityTenor = new String[] {
  442.             "1Y", "3Y", "5Y", "7Y", "10Y"
  443.         };

  444.         OTCOISRun (dtToday, "AUD", astrOTCMaturityTenor, true);

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

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

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

  448.         OTCOISRun (dtToday, "AUD", astrOTCMaturityTenor, false);

  449.         OTCOISRun (dtToday, "CAD", astrOTCMaturityTenor, false);

  450.         OTCOISRun (dtToday, "EUR", astrOTCMaturityTenor, false);

  451.         OTCOISRun (dtToday, "GBP", astrOTCMaturityTenor, false);

  452.         OTCOISRun (dtToday, "INR", astrOTCMaturityTenor, false);

  453.         OTCOISRun (dtToday, "JPY", astrOTCMaturityTenor, false);

  454.         OTCOISRun (dtToday, "SGD", astrOTCMaturityTenor, false);

  455.         OTCOISRun (dtToday, "USD", astrOTCMaturityTenor, false);
  456.     }
  457. }