YAS_GILT.java

  1. package org.drip.sample.treasury;

  2. import org.drip.analytics.cashflow.CompositePeriod;
  3. import org.drip.analytics.date.*;
  4. import org.drip.market.otc.*;
  5. import org.drip.numerical.common.FormatUtil;
  6. import org.drip.param.creator.*;
  7. import org.drip.param.market.CurveSurfaceQuoteContainer;
  8. import org.drip.param.valuation.ValuationParams;
  9. import org.drip.product.creator.*;
  10. import org.drip.product.credit.BondComponent;
  11. import org.drip.product.definition.CalibratableComponent;
  12. import org.drip.product.rates.FixFloatComponent;
  13. import org.drip.service.env.EnvManager;
  14. import org.drip.state.creator.ScenarioDiscountCurveBuilder;
  15. import org.drip.state.discount.MergedDiscountForwardCurve;
  16. import org.drip.state.identifier.ForwardLabel;

  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.  * YAS_GILT contains the sample demonstrating the replication of Bloomberg's GILT YAS functionality.
  62.  *
  63.  * @author Lakshmi Krishnamurthy
  64.  */

  65. public class YAS_GILT {

  66.     private static BondComponent TSYBond (
  67.         final JulianDate dtEffective,
  68.         final JulianDate dtMaturity,
  69.         final int iFreq,
  70.         final String strCurrency,
  71.         final double dblCoupon)
  72.         throws Exception
  73.     {
  74.         return BondBuilder.CreateSimpleFixed (
  75.             "GILT " + dtMaturity,
  76.             strCurrency,
  77.             "",
  78.             dblCoupon,
  79.             iFreq,
  80.             "DCAct_Act_UST",
  81.             dtEffective,
  82.             dtMaturity,
  83.             null,
  84.             null
  85.         );
  86.     }

  87.     private static final FixFloatComponent OTCIRS (
  88.         final JulianDate dtSpot,
  89.         final String strCurrency,
  90.         final String strMaturityTenor,
  91.         final double dblCoupon)
  92.     {
  93.         FixedFloatSwapConvention ffConv = IBORFixedFloatContainer.ConventionFromJurisdiction (
  94.             strCurrency,
  95.             "ALL",
  96.             strMaturityTenor,
  97.             "MAIN"
  98.         );

  99.         return ffConv.createFixFloatComponent (
  100.             dtSpot,
  101.             strMaturityTenor,
  102.             dblCoupon,
  103.             0.,
  104.             1.
  105.         );
  106.     }

  107.     /*
  108.      * Sample demonstrating building of rates curve from cash/future/swaps
  109.      *
  110.      *      USE WITH CARE: This sample ignores errors and does not handle exceptions.
  111.      */

  112.     private static MergedDiscountForwardCurve BuildRatesCurveFromInstruments (
  113.         final JulianDate dtStart,
  114.         final String[] astrCashTenor,
  115.         final double[] adblCashRate,
  116.         final String[] astrIRSTenor,
  117.         final double[] adblIRSRate,
  118.         final double dblBump,
  119.         final String strCurrency)
  120.         throws Exception
  121.     {
  122.         int iNumDCInstruments = astrCashTenor.length + adblIRSRate.length;
  123.         int aiDate[] = new int[iNumDCInstruments];
  124.         double adblRate[] = new double[iNumDCInstruments];
  125.         String astrCalibMeasure[] = new String[iNumDCInstruments];
  126.         double adblCompCalibValue[] = new double[iNumDCInstruments];
  127.         CalibratableComponent aCompCalib[] = new CalibratableComponent[iNumDCInstruments];

  128.         // Cash Calibration

  129.         JulianDate dtCashEffective = dtStart.addBusDays (
  130.             1,
  131.             strCurrency
  132.         );

  133.         for (int i = 0; i < astrCashTenor.length; ++i) {
  134.             astrCalibMeasure[i] = "Rate";
  135.             adblRate[i] = java.lang.Double.NaN;
  136.             adblCompCalibValue[i] = adblCashRate[i] + dblBump;

  137.             aCompCalib[i] = SingleStreamComponentBuilder.Deposit (
  138.                 dtCashEffective,
  139.                 new JulianDate (aiDate[i] = dtCashEffective.addTenor (astrCashTenor[i]).julian()),
  140.                 ForwardLabel.Create (
  141.                     strCurrency,
  142.                     astrCashTenor[i]
  143.                 )
  144.             );
  145.         }

  146.         // IRS Calibration

  147.         JulianDate dtIRSEffective = dtStart.addBusDays (2, strCurrency);

  148.         for (int i = 0; i < astrIRSTenor.length; ++i) {
  149.             astrCalibMeasure[i + astrCashTenor.length] = "Rate";
  150.             adblRate[i + astrCashTenor.length] = java.lang.Double.NaN;
  151.             adblCompCalibValue[i + astrCashTenor.length] = adblIRSRate[i] + dblBump;

  152.             aiDate[i + astrCashTenor.length] = dtIRSEffective.addTenor (astrIRSTenor[i]).julian();

  153.             aCompCalib[i + astrCashTenor.length] = OTCIRS (
  154.                 dtIRSEffective,
  155.                 strCurrency,
  156.                 astrIRSTenor[i],
  157.                 0.
  158.             );
  159.         }

  160.         /*
  161.          * Build the IR curve from the components, their calibration measures, and their calibration quotes.
  162.          */

  163.         return ScenarioDiscountCurveBuilder.NonlinearBuild (
  164.             dtStart,
  165.             strCurrency,
  166.             aCompCalib,
  167.             adblCompCalibValue,
  168.             astrCalibMeasure,
  169.             null
  170.         );
  171.     }

  172.     private static final MergedDiscountForwardCurve FundingCurve (
  173.         final JulianDate dtSpot,
  174.         final String strCurrency)
  175.         throws Exception
  176.     {
  177.         String[] astrCashTenor = new String[] {"3M"};
  178.         double[] adblCashRate = new double[] {0.00276};
  179.         String[] astrIRSTenor = new String[] {   "1Y",    "2Y",    "3Y",    "4Y",    "5Y",    "6Y",    "7Y",
  180.                "8Y",    "9Y",   "10Y",   "11Y",   "12Y",   "15Y",   "20Y",   "25Y",   "30Y",   "40Y",   "50Y"};
  181.         double[] adblIRSRate = new double[]  {0.00367, 0.00533, 0.00843, 0.01238, 0.01609, 0.01926, 0.02191,
  182.             0.02406, 0.02588, 0.02741, 0.02870, 0.02982, 0.03208, 0.03372, 0.03445, 0.03484, 0.03501, 0.03484};

  183.         return BuildRatesCurveFromInstruments (
  184.             dtSpot,
  185.             astrCashTenor,
  186.             adblCashRate,
  187.             astrIRSTenor,
  188.             adblIRSRate,
  189.             0.,
  190.             strCurrency
  191.         );
  192.     }

  193.     private static final void TSYMetrics (
  194.         final BondComponent tsyBond,
  195.         final double dblNotional,
  196.         final JulianDate dtSettle,
  197.         final CurveSurfaceQuoteContainer mktParams,
  198.         final double dblCleanPrice)
  199.         throws Exception
  200.     {
  201.         double dblAccrued = tsyBond.accrued (
  202.             dtSettle.julian(),
  203.             null
  204.         );

  205.         double dblYield = tsyBond.yieldFromPrice (
  206.             new ValuationParams (
  207.                 dtSettle,
  208.                 dtSettle,
  209.                 tsyBond.currency()
  210.             ),
  211.             mktParams,
  212.             null,
  213.             dblCleanPrice
  214.         );

  215.         double dblModifiedDuration = tsyBond.modifiedDurationFromPrice (
  216.             new ValuationParams (
  217.                 dtSettle,
  218.                 dtSettle,
  219.                 tsyBond.currency()
  220.             ),
  221.             mktParams,
  222.             null,
  223.             dblCleanPrice
  224.         );

  225.         double dblRisk = tsyBond.yield01FromPrice (
  226.             new ValuationParams (
  227.                 dtSettle,
  228.                 dtSettle,
  229.                 tsyBond.currency()
  230.             ),
  231.             mktParams,
  232.             null,
  233.             dblCleanPrice
  234.         );

  235.         double dblConvexity = tsyBond.convexityFromPrice (
  236.             new ValuationParams (
  237.                 dtSettle,
  238.                 dtSettle,
  239.                 tsyBond.currency()
  240.             ),
  241.             mktParams,
  242.             null,
  243.             dblCleanPrice
  244.         );

  245.         // double dblPreviousCouponDate = tsyBond.previousCouponRate (dtSettle, mktParams);

  246.         System.out.println();

  247.         System.out.println ("\tPrice             : " + FormatUtil.FormatDouble (dblCleanPrice, 1, 4, 100.));

  248.         System.out.println ("\tYield             : " + FormatUtil.FormatDouble (dblYield, 1, 4, 100.) + "%");

  249.         System.out.println();

  250.         System.out.println ("\tModified Duration : " + FormatUtil.FormatDouble (dblModifiedDuration, 1, 4, 10000.));

  251.         System.out.println ("\tRisk              : " + FormatUtil.FormatDouble (dblRisk, 1, 4, 10000.));

  252.         System.out.println ("\tConvexity         : " + FormatUtil.FormatDouble (dblConvexity * dblNotional, 1, 4, 1.));

  253.         System.out.println ("\tDV01              : " + FormatUtil.FormatDouble (dblRisk * dblNotional, 1, 0, 1.));

  254.         System.out.println();

  255.         // System.out.println ("\tPrevious Coupon Date : " + new JulianDate (dblPreviousCouponDate));

  256.         System.out.println ("\tFace                 : " + FormatUtil.FormatDouble (dblNotional, 1, 2, 1.));

  257.         System.out.println ("\tPrincipal            : " + FormatUtil.FormatDouble (dblCleanPrice * dblNotional, 1, 2, 1.));

  258.         System.out.println ("\tAccrued              : " + FormatUtil.FormatDouble (dblAccrued * dblNotional, 1, 2, 1.));

  259.         System.out.println ("\tTotal                : " + FormatUtil.FormatDouble ((dblCleanPrice + dblAccrued) * dblNotional, 1, 2, 1.));

  260.         // System.out.println ("\tAccrual Days         : " + (dtSettle.julian() - dblPreviousCouponDate));
  261.     }

  262.     public static final void main (
  263.         final String astrArgs[])
  264.         throws Exception
  265.     {
  266.         EnvManager.InitEnv ("");

  267.         JulianDate dtSpot = DateUtil.CreateFromYMD (
  268.             2015,
  269.             DateUtil.SEPTEMBER,
  270.             8
  271.         );

  272.         JulianDate dtEffective = DateUtil.CreateFromYMD (
  273.             2001,
  274.             DateUtil.SEPTEMBER,
  275.             27
  276.         );

  277.         JulianDate dtMaturity = DateUtil.CreateFromYMD (
  278.             2025,
  279.             DateUtil.MARCH,
  280.             7
  281.         );

  282.         int iFreq = 2;
  283.         String strCurrency = "GBP";
  284.         double dblCoupon = 0.05;
  285.         double dblNotional = 1000000.;
  286.         double dblCleanPrice = 1.28;

  287.         BondComponent tsyBond = TSYBond (
  288.             dtEffective,
  289.             dtMaturity,
  290.             iFreq,
  291.             strCurrency,
  292.             dblCoupon
  293.         );

  294.         System.out.println();

  295.         System.out.println ("\tEffective : " + tsyBond.effectiveDate());

  296.         System.out.println ("\tMaturity  : " + tsyBond.maturityDate());

  297.         System.out.println();

  298.         MergedDiscountForwardCurve dc = FundingCurve (
  299.             dtSpot,
  300.             strCurrency
  301.         );

  302.         TSYMetrics (
  303.             tsyBond,
  304.             dblNotional,
  305.             dtSpot,
  306.             MarketParamsBuilder.Create (
  307.                 dc,
  308.                 null,
  309.                 null,
  310.                 null,
  311.                 null,
  312.                 null,
  313.                 null
  314.             ),
  315.             dblCleanPrice
  316.         );

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

  318.         for (CompositePeriod p : tsyBond.couponPeriods())
  319.             System.out.println ("\t\t" +
  320.                 DateUtil.YYYYMMDD (p.startDate()) + " | " +
  321.                 DateUtil.YYYYMMDD (p.endDate()) + " | " +
  322.                 DateUtil.YYYYMMDD (p.payDate()) + " | " +
  323.                 FormatUtil.FormatDouble (p.couponDCF(), 1, 4, 1.) + " ||"
  324.             );
  325.     }
  326. }