FundingNativeForwardReconciler.java

  1. package org.drip.sample.multicurve;

  2. import org.drip.analytics.date.*;
  3. import org.drip.market.otc.*;
  4. import org.drip.numerical.common.FormatUtil;
  5. import org.drip.param.valuation.ValuationParams;
  6. import org.drip.product.creator.SingleStreamComponentBuilder;
  7. import org.drip.product.definition.CalibratableComponent;
  8. import org.drip.product.rates.FixFloatComponent;
  9. import org.drip.service.env.EnvManager;
  10. import org.drip.state.creator.ScenarioDiscountCurveBuilder;
  11. import org.drip.state.discount.*;
  12. import org.drip.state.forward.ForwardCurve;
  13. import org.drip.state.identifier.ForwardLabel;

  14. /*
  15.  * -*- mode: java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  16.  */

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

  60. /**
  61.  * FundingNativeForwardReconciler demonstrates the Construction of the Forward Curve Native to the Discount
  62.  *  Curve across different Tenors, and display their Reconciliation.
  63.  *
  64.  * @author Lakshmi Krishnamurthy
  65.  */

  66. public class FundingNativeForwardReconciler {

  67.     private static final FixFloatComponent OTCFixFloat (
  68.         final JulianDate dtSpot,
  69.         final String strCurrency,
  70.         final String strMaturityTenor,
  71.         final double dblCoupon)
  72.     {
  73.         FixedFloatSwapConvention ffConv = IBORFixedFloatContainer.ConventionFromJurisdiction (
  74.             strCurrency,
  75.             "ALL",
  76.             strMaturityTenor,
  77.             "MAIN"
  78.         );

  79.         return ffConv.createFixFloatComponent (
  80.             dtSpot,
  81.             strMaturityTenor,
  82.             dblCoupon,
  83.             0.,
  84.             1.
  85.         );
  86.     }

  87.     private static final CalibratableComponent[] DepositInstrumentsFromMaturityDays (
  88.         final JulianDate dtEffective,
  89.         final int[] aiDay,
  90.         final int iNumFuture,
  91.         final String strCurrency)
  92.         throws Exception
  93.     {
  94.         CalibratableComponent[] aCalibComp = new CalibratableComponent[aiDay.length + iNumFuture];

  95.         for (int i = 0; i < aiDay.length; ++i)
  96.             aCalibComp[i] = SingleStreamComponentBuilder.Deposit (
  97.                 dtEffective,
  98.                 dtEffective.addBusDays (
  99.                     aiDay[i],
  100.                     strCurrency
  101.                 ),
  102.                 ForwardLabel.Create (
  103.                     strCurrency,
  104.                     "3M"
  105.                 )
  106.             );

  107.         CalibratableComponent[] aEDF = SingleStreamComponentBuilder.ForwardRateFuturesPack (
  108.             dtEffective,
  109.             iNumFuture,
  110.             strCurrency
  111.         );

  112.         for (int i = aiDay.length; i < aiDay.length + iNumFuture; ++i)
  113.             aCalibComp[i] = aEDF[i - aiDay.length];

  114.         return aCalibComp;
  115.     }

  116.     private static final FixFloatComponent[] SwapInstrumentsFromMaturityTenor (
  117.         final JulianDate dtSpot,
  118.         final String strCurrency,
  119.         final String[] astrMaturityTenor,
  120.         final double[] adblCoupon)
  121.         throws Exception
  122.     {
  123.         FixFloatComponent[] aIRS = new FixFloatComponent[astrMaturityTenor.length];

  124.         for (int i = 0; i < astrMaturityTenor.length; ++i)
  125.             aIRS[i] = OTCFixFloat (
  126.                 dtSpot,
  127.                 strCurrency,
  128.                 astrMaturityTenor[i],
  129.                 adblCoupon[i]
  130.             );

  131.         return aIRS;
  132.     }

  133.     private static final MergedDiscountForwardCurve MakeDC (
  134.         final JulianDate dtSpot,
  135.         final String strCurrency)
  136.         throws Exception
  137.     {
  138.         /*
  139.          * Construct the array of Deposit instruments and their quotes.
  140.          */

  141.         CalibratableComponent[] aDepositComp = DepositInstrumentsFromMaturityDays (
  142.             dtSpot,
  143.             new int[] {
  144.                 30,
  145.                 60,
  146.                 91,
  147.                 182,
  148.                 273
  149.             },
  150.             0,
  151.             strCurrency
  152.         );

  153.         double[] adblDepositQuote = new double[] {
  154.             0.0668750,  //  30D
  155.             0.0675000,  //  60D
  156.             0.0678125,  //  91D
  157.             0.0712500,  // 182D
  158.             0.0750000   // 273D
  159.         };

  160.         String[] astrDepositManifestMeasure = new String[] {
  161.             "ForwardRate", //  30D
  162.             "ForwardRate", //  60D
  163.             "ForwardRate", //  91D
  164.             "ForwardRate", // 182D
  165.             "ForwardRate"  // 273D
  166.         };

  167.         /*
  168.          * Construct the array of Swap instruments and their quotes.
  169.          */

  170.         double[] adblSwapQuote = new double[] {
  171.             0.08265,    //  2Y
  172.             0.08550,    //  3Y
  173.             0.08655,    //  4Y
  174.             0.08770,    //  5Y
  175.             0.08910,    //  7Y
  176.             0.08920     // 10Y
  177.         };

  178.         String[] astrSwapManifestMeasure = new String[] {
  179.             "SwapRate",    //  2Y
  180.             "SwapRate",    //  3Y
  181.             "SwapRate",    //  4Y
  182.             "SwapRate",    //  5Y
  183.             "SwapRate",    //  7Y
  184.             "SwapRate"     // 10Y
  185.         };

  186.         CalibratableComponent[] aSwapComp = SwapInstrumentsFromMaturityTenor (
  187.             dtSpot,
  188.             strCurrency,
  189.             new java.lang.String[] {
  190.                 "2Y",
  191.                 "3Y",
  192.                 "4Y",
  193.                 "5Y",
  194.                 "7Y",
  195.                 "10Y"
  196.             },
  197.             adblSwapQuote
  198.         );

  199.         /*
  200.          * Construct a shape preserving and smoothing KLK Hyperbolic Spline from the cash/swap instruments.
  201.          */

  202.         return ScenarioDiscountCurveBuilder.CubicKLKHyperbolicDFRateShapePreserver (
  203.             "KLK_HYPERBOLIC_SHAPE_TEMPLATE",
  204.             new ValuationParams (
  205.                 dtSpot,
  206.                 dtSpot,
  207.                 strCurrency
  208.             ),
  209.             aDepositComp,
  210.             adblDepositQuote,
  211.             astrDepositManifestMeasure,
  212.             aSwapComp,
  213.             adblSwapQuote,
  214.             astrSwapManifestMeasure,
  215.             false
  216.         );
  217.     }

  218.     private static final void DiscountForwardReconciliation (
  219.         final JulianDate dtSpot,
  220.         final MergedDiscountForwardCurve dc,
  221.         final ForwardCurve fc,
  222.         final String strTenor)
  223.         throws Exception
  224.     {
  225.         int iNumTenor = 20;
  226.         JulianDate dtStart = dtSpot;

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

  228.         System.out.println ("\t|-------- RECONCILIATION FOR " + fc.label().fullyQualifiedName() + " ---------||");

  229.         System.out.println ("\t|--------------------------------------------------||");

  230.         System.out.println ("\t|                                                  ||");

  231.         for (int i = 0; i < iNumTenor; ++i) {
  232.             JulianDate dtEnd = dtStart.addTenor (strTenor);

  233.             System.out.println (
  234.                 "\t|   [" + dtStart + " - " + dtEnd + "]   |  " +
  235.                 FormatUtil.FormatDouble (dc.libor (dtStart, strTenor), 1, 2, 100.) + "% | " +
  236.                 FormatUtil.FormatDouble (fc.forward (dtEnd), 1, 2, 100.) + "% ||"
  237.             );

  238.             dtStart = dtEnd;
  239.         }

  240.         System.out.println ("\t|                                                  ||");

  241.         System.out.println ("\t|--------------------------------------------------||");

  242.         System.out.println ("\t|--------------------------------------------------||\n");
  243.     }

  244.     public static final void main (
  245.         final String[] astrArgs)
  246.         throws Exception
  247.     {
  248.         EnvManager.InitEnv ("");

  249.         JulianDate dtSpot = DateUtil.CreateFromYMD (
  250.             1995,
  251.             DateUtil.FEBRUARY,
  252.             3
  253.         );

  254.         String strCurrency = "GBP";
  255.         String[] astrFRATenor = {
  256.             "1M", "3M", "6M", "12M"
  257.         };

  258.         MergedDiscountForwardCurve dc = MakeDC (
  259.             dtSpot,
  260.             strCurrency
  261.         );

  262.         for (String strFRATenor : astrFRATenor) {
  263.             ForwardCurve fcNative = dc.nativeForwardCurve (strFRATenor);

  264.             DiscountForwardReconciliation (
  265.                 dtSpot,
  266.                 dc,
  267.                 fcNative,
  268.                 strFRATenor
  269.             );
  270.         }
  271.     }
  272. }