YAS_GGB.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_GGB contains the sample demonstrating the replication of Bloomberg's Greek Govvie EUR Bond YAS
  62.  *  Functionality.
  63.  *
  64.  * @author Lakshmi Krishnamurthy
  65.  */

  66. public class YAS_GGB {

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

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

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

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

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

  130.         // Cash Calibration

  131.         JulianDate dtCashEffective = dtStart.addBusDays (
  132.             1,
  133.             strCurrency
  134.         );

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

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

  148.         // IRS Calibration

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

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

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

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

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

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

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

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

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

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

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

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

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

  247.         JulianDate dtPreviousCouponDate = tsyBond.previousCouponDate (dtSettle);

  248.         System.out.println();

  249.         System.out.println ("\t\t" + tsyBond.name());

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

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

  252.         System.out.println ("\tSettle            :  " + dtSettle);

  253.         System.out.println();

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

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

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

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

  258.         System.out.println();

  259.         System.out.println ("\tPrevious Coupon Date :  " + dtPreviousCouponDate);

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

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

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

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

  264.         System.out.println ("\tAccrual Days         : " + FormatUtil.FormatDouble (dtSettle.julian() - dtPreviousCouponDate.julian(), 1, 0, 1.));
  265.     }

  266.     public static final void main (
  267.         final String astrArgs[])
  268.         throws Exception
  269.     {
  270.         EnvManager.InitEnv ("");

  271.         JulianDate dtSpot = DateUtil.CreateFromYMD (
  272.             2015,
  273.             DateUtil.MAY,
  274.             5
  275.         );

  276.         JulianDate dtEffective = DateUtil.CreateFromYMD (
  277.             2014,
  278.             DateUtil.JULY,
  279.             17
  280.         );

  281.         JulianDate dtMaturity = DateUtil.CreateFromYMD (
  282.             2017,
  283.             DateUtil.JULY,
  284.             17
  285.         );

  286.         int iFreq = 1;
  287.         String strDayCount = "DCAct_Act_UST";
  288.         String strCurrency = "EUR";
  289.         double dblCoupon = 0.03375;
  290.         double dblNotional = 1000000.;
  291.         double dblCleanPrice = 0.7578;

  292.         BondComponent tsyBond = TSYBond (
  293.             dtEffective,
  294.             dtMaturity,
  295.             iFreq,
  296.             strDayCount,
  297.             strCurrency,
  298.             dblCoupon
  299.         );

  300.         System.out.println();

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

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

  303.         System.out.println();

  304.         MergedDiscountForwardCurve dc = FundingCurve (
  305.             dtSpot,
  306.             strCurrency
  307.         );

  308.         TSYMetrics (
  309.             tsyBond,
  310.             dblNotional,
  311.             dtSpot,
  312.             MarketParamsBuilder.Create (
  313.                 dc,
  314.                 null,
  315.                 null,
  316.                 null,
  317.                 null,
  318.                 null,
  319.                 null
  320.             ),
  321.             dblCleanPrice
  322.         );

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

  324.         for (CompositePeriod p : tsyBond.couponPeriods())
  325.             System.out.println ("\t\t" +
  326.                 DateUtil.YYYYMMDD (p.startDate()) + " | " +
  327.                 DateUtil.YYYYMMDD (p.endDate()) + " | " +
  328.                 DateUtil.YYYYMMDD (p.payDate()) + " | " +
  329.                 FormatUtil.FormatDouble (p.couponDCF(), 1, 4, 1.) + " ||"
  330.             );
  331.     }
  332. }