MultiStretchCurveBuilder.java

  1. package org.drip.sample.overnight;

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

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

  23. /*!
  24.  * Copyright (C) 2018 Lakshmi Krishnamurthy
  25.  * Copyright (C) 2017 Lakshmi Krishnamurthy
  26.  * Copyright (C) 2016 Lakshmi Krishnamurthy
  27.  * Copyright (C) 2015 Lakshmi Krishnamurthy
  28.  * Copyright (C) 2014 Lakshmi Krishnamurthy
  29.  *
  30.  *  This file is part of DRIP, a free-software/open-source library for buy/side financial/trading model
  31.  *      libraries targeting analysts and developers
  32.  *      https://lakshmidrip.github.io/DRIP/
  33.  *  
  34.  *  DRIP is composed of four main libraries:
  35.  *  
  36.  *  - DRIP Fixed Income - https://lakshmidrip.github.io/DRIP-Fixed-Income/
  37.  *  - DRIP Asset Allocation - https://lakshmidrip.github.io/DRIP-Asset-Allocation/
  38.  *  - DRIP Numerical Optimizer - https://lakshmidrip.github.io/DRIP-Numerical-Optimizer/
  39.  *  - DRIP Statistical Learning - https://lakshmidrip.github.io/DRIP-Statistical-Learning/
  40.  *
  41.  *  - DRIP Fixed Income: Library for Instrument/Trading Conventions, Treasury Futures/Options,
  42.  *      Funding/Forward/Overnight Curves, Multi-Curve Construction/Valuation, Collateral Valuation and XVA
  43.  *      Metric Generation, Calibration and Hedge Attributions, Statistical Curve Construction, Bond RV
  44.  *      Metrics, Stochastic Evolution and Option Pricing, Interest Rate Dynamics and Option Pricing, LMM
  45.  *      Extensions/Calibrations/Greeks, Algorithmic Differentiation, and Asset Backed Models and Analytics.
  46.  *
  47.  *  - DRIP Asset Allocation: Library for model libraries for MPT framework, Black Litterman Strategy
  48.  *      Incorporator, Holdings Constraint, and Transaction Costs.
  49.  *
  50.  *  - DRIP Numerical Optimizer: Library for Numerical Optimization and Spline Functionality.
  51.  *
  52.  *  - DRIP Statistical Learning: Library for Statistical Evaluation and Machine Learning.
  53.  *
  54.  *  Licensed under the Apache License, Version 2.0 (the "License");
  55.  *      you may not use this file except in compliance with the License.
  56.  *  
  57.  *  You may obtain a copy of the License at
  58.  *      http://www.apache.org/licenses/LICENSE-2.0
  59.  *  
  60.  *  Unless required by applicable law or agreed to in writing, software
  61.  *      distributed under the License is distributed on an "AS IS" BASIS,
  62.  *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  63.  *  
  64.  *  See the License for the specific language governing permissions and
  65.  *      limitations under the License.
  66.  */

  67. /**
  68.  * MultiStretchCurveBuilder contains a sample of the construction and usage of the Overnight Curve built
  69.  *  using the Overnight Indexed Swap Product Instruments in their distinct stretches. The Tenors/Quotes to
  70.  *  replicate are taken from:
  71.  *
  72.  *  - Ametrano, F., and M. Bianchetti (2013): Everything You Always Wanted to Know About Multiple Interest
  73.  *      Rate Curve Bootstrapping but Were Afraid to Ask,
  74.  *          http://papers.ssrn.com/sol3/papers.cfm?abstract_id=2219548
  75.  *
  76.  * @author Lakshmi Krishnamurthy
  77.  */

  78. public class MultiStretchCurveBuilder {

  79.     private static final FixFloatComponent OTCOISFixFloat (
  80.         final JulianDate dtSpot,
  81.         final String strCurrency,
  82.         final String strMaturityTenor,
  83.         final double dblCoupon)
  84.     {
  85.         FixedFloatSwapConvention ffConv = OvernightFixedFloatContainer.FundConventionFromJurisdiction (
  86.             strCurrency
  87.         );

  88.         return ffConv.createFixFloatComponent (
  89.             dtSpot,
  90.             strMaturityTenor,
  91.             dblCoupon,
  92.             0.,
  93.             1.
  94.         );
  95.     }

  96.     /*
  97.      * Construct the Array of Deposit Instruments from the given set of parameters
  98.      *
  99.      *      USE WITH CARE: This sample ignores errors and does not handle exceptions.
  100.      */

  101.     private static final SingleStreamComponent[] DepositInstrumentsFromMaturityDays (
  102.         final JulianDate dtEffective,
  103.         final String strCurrency,
  104.         final int[] aiDay)
  105.         throws Exception
  106.     {
  107.         SingleStreamComponent[] aDeposit = new SingleStreamComponent[aiDay.length];

  108.         for (int i = 0; i < aiDay.length; ++i)
  109.             aDeposit[i] = SingleStreamComponentBuilder.Deposit (
  110.                 dtEffective,
  111.                 dtEffective.addBusDays (
  112.                     aiDay[i],
  113.                     strCurrency
  114.                 ),
  115.                 OvernightLabel.Create (
  116.                     strCurrency
  117.                 )
  118.             );

  119.         return aDeposit;
  120.     }

  121.     /*
  122.      * Construct the Array of Overnight Index Instruments from the given set of parameters
  123.      *
  124.      *      USE WITH CARE: This sample ignores errors and does not handle exceptions.
  125.      */

  126.     private static final FixFloatComponent[] OISFromMaturityTenor (
  127.         final JulianDate dtSpot,
  128.         final String strCurrency,
  129.         final String[] astrMaturityTenor,
  130.         final double[] adblCoupon)
  131.         throws Exception
  132.     {
  133.         FixFloatComponent[] aOIS = new FixFloatComponent[astrMaturityTenor.length];

  134.         for (int i = 0; i < astrMaturityTenor.length; ++i)
  135.             aOIS[i] = OTCOISFixFloat (
  136.                 dtSpot,
  137.                 strCurrency,
  138.                 astrMaturityTenor[i],
  139.                 adblCoupon[i]
  140.             );

  141.         return aOIS;
  142.     }

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

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

  159.         return aOISFutures;
  160.     }

  161.     /*
  162.      * Construct the Array of Overnight Index Future Instruments from the given set of parameters
  163.      *
  164.      *      USE WITH CARE: This sample ignores errors and does not handle exceptions.
  165.      */

  166.     private static final void CustomOISCurveBuilderSample (
  167.         final JulianDate dtSpot,
  168.         final String strCurrency,
  169.         final String strHeaderComment)
  170.         throws Exception
  171.     {
  172.         System.out.println ("\n\t----------------------------------------------------------------");

  173.         System.out.println ("\t     " + strHeaderComment);

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

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

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

  185.         double[] adblDepositQuote = new double[] {
  186.             0.0004,     // 1D
  187.             0.0004,     // 2D
  188.             0.0004      // 3D
  189.         };

  190.         String[] astrDepositMeasure = new String[] {
  191.             "ForwardRate",      // 1D
  192.             "ForwardRate",      // 2D
  193.             "ForwardRate"       // 3D
  194.         };

  195.         /*
  196.          * Construct the Deposit Instrument Set Stretch Builder
  197.          */

  198.         LatentStateStretchSpec depositStretch = LatentStateStretchBuilder.ForwardFundingStretchSpec (
  199.             "DEPOSIT",
  200.             aDepositComp,
  201.             astrDepositMeasure,
  202.             adblDepositQuote
  203.         );

  204.         /*
  205.          * Construct the Array of Short End OIS Instruments and their Quotes from the given set of parameters
  206.          */

  207.         double[] adblShortEndOISQuote = new double[] {
  208.             0.00070,    //   1W
  209.             0.00069,    //   2W
  210.             0.00078,    //   3W
  211.             0.00074     //   1M
  212.         };

  213.         CalibratableComponent[] aShortEndOISComp = OISFromMaturityTenor (
  214.             dtSpot,
  215.             strCurrency,
  216.             new String[] {
  217.                 "1W",
  218.                 "2W",
  219.                 "3W",
  220.                 "1M"
  221.             },
  222.             adblShortEndOISQuote
  223.         );

  224.         String[] astrShortEndOISMeasure = new String[] {
  225.             "SwapRate",    //   1W
  226.             "SwapRate",    //   2W
  227.             "SwapRate",    //   3W
  228.             "SwapRate"     //   1M
  229.         };

  230.         /*
  231.          * Construct the Short End OIS Instrument Set Stretch Builder
  232.          */

  233.         LatentStateStretchSpec oisShortEndStretch = LatentStateStretchBuilder.ForwardFundingStretchSpec (
  234.             "OIS_SHORT_END",
  235.             aShortEndOISComp,
  236.             astrShortEndOISMeasure,
  237.             adblShortEndOISQuote
  238.         );

  239.         /*
  240.          * Construct the Array of OIS Futures Instruments and their Quotes from the given set of parameters
  241.          */

  242.         double[] adblOISFutureQuote = new double[] {
  243.              0.00046,    //   1M x 1M
  244.              0.00016,    //   2M x 1M
  245.             -0.00007,    //   3M x 1M
  246.             -0.00013,    //   4M x 1M
  247.             -0.00014     //   5M x 1M
  248.         };

  249.         CalibratableComponent[] aOISFutureComp = OISFuturesFromMaturityTenor (
  250.             dtSpot,
  251.             strCurrency,
  252.             new String[] {
  253.                 "1M", "2M", "3M", "4M", "5M"
  254.             },
  255.             new String[] {
  256.                 "1M", "1M", "1M", "1M", "1M"
  257.             },
  258.             adblOISFutureQuote
  259.         );

  260.         String[] astrOISFutureMeasure = new String[] {
  261.             "SwapRate",    //   1M
  262.             "SwapRate",    //   2M
  263.             "SwapRate",    //   3M
  264.             "SwapRate",    //   4M
  265.             "SwapRate"     //   5M
  266.         };

  267.         /*
  268.          * Construct the OIS Future Instrument Set Stretch Builder
  269.          */

  270.         LatentStateStretchSpec oisFutureStretch = LatentStateStretchBuilder.ForwardFundingStretchSpec (
  271.             "OIS_FUTURE",
  272.             aOISFutureComp,
  273.             astrOISFutureMeasure,
  274.             adblOISFutureQuote
  275.         );

  276.         /*
  277.          * Construct the Array of Long End OIS Instruments and their Quotes from the given set of parameters
  278.          */

  279.         double[] adblLongEndOISQuote = new double[] {
  280.             0.00002,    //  15M
  281.             0.00008,    //  18M
  282.             0.00021,    //  21M
  283.             0.00036,    //   2Y
  284.             0.00127,    //   3Y
  285.             0.00274,    //   4Y
  286.             0.00456,    //   5Y
  287.             0.00647,    //   6Y
  288.             0.00827,    //   7Y
  289.             0.00996,    //   8Y
  290.             0.01147,    //   9Y
  291.             0.01280,    //  10Y
  292.             0.01404,    //  11Y
  293.             0.01516,    //  12Y
  294.             0.01764,    //  15Y
  295.             0.01939,    //  20Y
  296.             0.02003,    //  25Y
  297.             0.02038     //  30Y
  298.         };

  299.         String[] adblLongEndOISMeasure = new String[] {
  300.             "SwapRate",    //  15M
  301.             "SwapRate",    //  18M
  302.             "SwapRate",    //  21M
  303.             "SwapRate",    //   2Y
  304.             "SwapRate",    //   3Y
  305.             "SwapRate",    //   4Y
  306.             "SwapRate",    //   5Y
  307.             "SwapRate",    //   6Y
  308.             "SwapRate",    //   7Y
  309.             "SwapRate",    //   8Y
  310.             "SwapRate",    //   9Y
  311.             "SwapRate",    //  10Y
  312.             "SwapRate",    //  11Y
  313.             "SwapRate",    //  12Y
  314.             "SwapRate",    //  15Y
  315.             "SwapRate",    //  20Y
  316.             "SwapRate",    //  25Y
  317.             "SwapRate"     //  30Y
  318.         };

  319.         CalibratableComponent[] aLongEndOISComp = OISFromMaturityTenor (
  320.             dtSpot,
  321.             strCurrency,
  322.             new String[] {
  323.                 "15M",
  324.                 "18M",
  325.                 "21M",
  326.                 "2Y",
  327.                 "3Y",
  328.                 "4Y",
  329.                 "5Y",
  330.                 "6Y",
  331.                 "7Y",
  332.                 "8Y",
  333.                 "9Y",
  334.                 "10Y",
  335.                 "11Y",
  336.                 "12Y",
  337.                 "15Y",
  338.                 "20Y",
  339.                 "25Y",
  340.                 "30Y"
  341.             },
  342.             adblLongEndOISQuote
  343.         );

  344.         /*
  345.          * Construct the Long End OIS Instrument Set Stretch Builder
  346.          */

  347.         LatentStateStretchSpec oisLongEndStretch = LatentStateStretchBuilder.ForwardFundingStretchSpec (
  348.             "OIS_LONG_END",
  349.             aLongEndOISComp,
  350.             adblLongEndOISMeasure,
  351.             adblLongEndOISQuote
  352.         );

  353.         LatentStateStretchSpec[] aStretchSpec = new LatentStateStretchSpec[] {
  354.             depositStretch,
  355.             oisShortEndStretch,
  356.             oisFutureStretch,
  357.             oisLongEndStretch
  358.         };

  359.         /*
  360.          * Set up the Linear Curve Calibrator using the following parameters:
  361.          *  - Cubic Exponential Mixture Basis Spline Set
  362.          *  - Ck = 2, Segment Curvature Penalty = 2
  363.          *  - Quadratic Rational Shape Controller
  364.          *  - Natural Boundary Setting
  365.          */

  366.         LinearLatentStateCalibrator lcc = new LinearLatentStateCalibrator (
  367.             new SegmentCustomBuilderControl (
  368.                 MultiSegmentSequenceBuilder.BASIS_SPLINE_POLYNOMIAL,
  369.                 new PolynomialFunctionSetParams (4),
  370.                 SegmentInelasticDesignControl.Create (
  371.                     2,
  372.                     2
  373.                 ),
  374.                 new ResponseScalingShapeControl (
  375.                     true,
  376.                     new QuadraticRationalShapeControl (0.)
  377.                 ),
  378.                 null
  379.             ),
  380.             BoundarySettings.NaturalStandard(),
  381.             MultiSegmentSequence.CALIBRATE,
  382.             null,
  383.             null
  384.         );

  385.         /*
  386.          * Construct the Shape Preserving Discount Curve by applying the linear curve calibrator to the array
  387.          *  of Deposit and Swap Stretches.
  388.          */

  389.         ValuationParams valParams = new ValuationParams (
  390.             dtSpot,
  391.             dtSpot,
  392.             strCurrency
  393.         );

  394.         MergedDiscountForwardCurve dc = ScenarioDiscountCurveBuilder.ShapePreservingDFBuild (
  395.             strCurrency,
  396.             lcc,
  397.             aStretchSpec,
  398.             valParams,
  399.             null,
  400.             null,
  401.             null,
  402.             1.
  403.         );

  404.         /*
  405.          * Cross-Comparison of the Deposit Calibration Instrument "Rate" metric across the different curve
  406.          *  construction methodologies.
  407.          */

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

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

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

  411.         for (int i = 0; i < aDepositComp.length; ++i)
  412.             System.out.println ("\t[" + aDepositComp[i].effectiveDate() + " => " + aDepositComp[i].maturityDate() + "] = " +
  413.                 FormatUtil.FormatDouble (aDepositComp[i].measureValue (valParams, null,
  414.                     MarketParamsBuilder.Create (dc, null, null, null, null, null, null),
  415.                         null, "Rate"), 1, 6, 1.) + " | " + FormatUtil.FormatDouble (adblDepositQuote[i], 1, 6, 1.));

  416.         /*
  417.          * Cross-Comparison of the Short End OIS Calibration Instrument "Rate" metric across the different curve
  418.          *  construction methodologies.
  419.          */

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

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

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

  423.         for (int i = 0; i < aShortEndOISComp.length; ++i)
  424.             System.out.println ("\t[" + aShortEndOISComp[i].effectiveDate() + " => " + aShortEndOISComp[i].maturityDate() + "] = " +
  425.                 FormatUtil.FormatDouble (aShortEndOISComp[i].measureValue (valParams, null,
  426.                     MarketParamsBuilder.Create (dc, null, null, null, null, null, null),
  427.                         null, "CalibSwapRate"), 1, 6, 1.) + " | " + FormatUtil.FormatDouble (adblShortEndOISQuote[i], 1, 6, 1.) + " | " +
  428.                             FormatUtil.FormatDouble (aShortEndOISComp[i].measureValue (valParams, null,
  429.                                 MarketParamsBuilder.Create (dc, null, null, null, null, null, null),
  430.                                     null, "FairPremium"), 1, 6, 1.));

  431.         /*
  432.          * Cross-Comparison of the OIS Future Calibration Instrument "Rate" metric across the different curve
  433.          *  construction methodologies.
  434.          */

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

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

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

  438.         for (int i = 0; i < aOISFutureComp.length; ++i)
  439.             System.out.println ("\t[" + aOISFutureComp[i].effectiveDate() + " => " + aOISFutureComp[i].maturityDate() + "] = " +
  440.                 FormatUtil.FormatDouble (aOISFutureComp[i].measureValue (valParams, null,
  441.                     MarketParamsBuilder.Create (dc, null, null, null, null, null, null),
  442.                         null, "SwapRate"), 1, 6, 1.) + " | " + FormatUtil.FormatDouble (adblOISFutureQuote[i], 1, 6, 1.) + " | " +
  443.                             FormatUtil.FormatDouble (aOISFutureComp[i].measureValue (valParams, null,
  444.                                 MarketParamsBuilder.Create (dc, null, null, null, null, null, null),
  445.                                     null, "FairPremium"), 1, 6, 1.));

  446.         /*
  447.          * Cross-Comparison of the Long End OIS Calibration Instrument "Rate" metric across the different curve
  448.          *  construction methodologies.
  449.          */

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

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

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

  453.         for (int i = 0; i < aLongEndOISComp.length; ++i)
  454.             System.out.println ("\t[" + aLongEndOISComp[i].effectiveDate() + " => " + aLongEndOISComp[i].maturityDate() + "] = " +
  455.                 FormatUtil.FormatDouble (aLongEndOISComp[i].measureValue (valParams, null,
  456.                     MarketParamsBuilder.Create (dc, null, null, null, null, null, null),
  457.                         null, "CalibSwapRate"), 1, 6, 1.) + " | " + FormatUtil.FormatDouble (adblLongEndOISQuote[i], 1, 6, 1.) + " | " +
  458.                             FormatUtil.FormatDouble (aLongEndOISComp[i].measureValue (valParams, null,
  459.                                 MarketParamsBuilder.Create (dc, null, null, null, null, null, null),
  460.                                     null, "FairPremium"), 1, 6, 1.));
  461.     }

  462.     public static final void main (
  463.         final String[] astrArgs)
  464.         throws Exception
  465.     {
  466.         /*
  467.          * Initialize the Credit Analytics Library
  468.          */

  469.         EnvManager.InitEnv ("");

  470.         String strCurrency = "EUR";

  471.         JulianDate dtToday = DateUtil.CreateFromYMD (
  472.             2012,
  473.             DateUtil.DECEMBER,
  474.             11
  475.         );

  476.         CustomOISCurveBuilderSample (
  477.             dtToday,
  478.             strCurrency,
  479.             "OVERNIGHT INDEX RUN RECONCILIATION"
  480.         );
  481.     }
  482. }