ZeroCouponBullet1.java

  1. package org.drip.sample.sovereign;

  2. import org.drip.analytics.date.*;
  3. import org.drip.numerical.common.FormatUtil;
  4. import org.drip.param.creator.MarketParamsBuilder;
  5. import org.drip.param.market.CurveSurfaceQuoteContainer;
  6. import org.drip.param.valuation.*;
  7. import org.drip.product.creator.BondBuilder;
  8. import org.drip.product.credit.BondComponent;
  9. import org.drip.product.definition.*;
  10. import org.drip.service.env.EnvManager;
  11. import org.drip.service.template.*;
  12. import org.drip.state.discount.MergedDiscountForwardCurve;
  13. import org.drip.state.govvie.GovvieCurve;

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

  58. /**
  59.  * ZeroCouponBullet1 demonstrates Non-EOS Zero Coupon Multi-flavor Bond Pricing and Relative Value Measure
  60.  *  Generation Functionality.
  61.  *
  62.  * @author Lakshmi Krishnamurthy
  63.  */

  64. public class ZeroCouponBullet1 {

  65.     private static final MergedDiscountForwardCurve FundingCurve (
  66.         final JulianDate dtSpot,
  67.         final String strCurrency,
  68.         final double dblBump)
  69.         throws Exception
  70.     {
  71.         String[] astrDepositMaturityTenor = new String[] {
  72.             "2D"
  73.         };

  74.         double[] adblDepositQuote = new double[] {
  75.             0.0111956 + dblBump // 2D
  76.         };

  77.         double[] adblFuturesQuote = new double[] {
  78.             0.011375 + dblBump, // 98.8625
  79.             0.013350 + dblBump, // 98.6650
  80.             0.014800 + dblBump, // 98.5200
  81.             0.016450 + dblBump, // 98.3550
  82.             0.017850 + dblBump, // 98.2150
  83.             0.019300 + dblBump  // 98.0700
  84.         };

  85.         String[] astrFixFloatMaturityTenor = new String[] {
  86.             "02Y",
  87.             "03Y",
  88.             "04Y",
  89.             "05Y",
  90.             "06Y",
  91.             "07Y",
  92.             "08Y",
  93.             "09Y",
  94.             "10Y",
  95.             "11Y",
  96.             "12Y",
  97.             "15Y",
  98.             "20Y",
  99.             "25Y",
  100.             "30Y",
  101.             "40Y",
  102.             "50Y"
  103.         };

  104.         double[] adblFixFloatQuote = new double[] {
  105.             0.017029 + dblBump, //  2Y
  106.             0.019354 + dblBump, //  3Y
  107.             0.021044 + dblBump, //  4Y
  108.             0.022291 + dblBump, //  5Y
  109.             0.023240 + dblBump, //  6Y
  110.             0.024025 + dblBump, //  7Y
  111.             0.024683 + dblBump, //  8Y
  112.             0.025243 + dblBump, //  9Y
  113.             0.025720 + dblBump, // 10Y
  114.             0.026130 + dblBump, // 11Y
  115.             0.026495 + dblBump, // 12Y
  116.             0.027230 + dblBump, // 15Y
  117.             0.027855 + dblBump, // 20Y
  118.             0.028025 + dblBump, // 25Y
  119.             0.028028 + dblBump, // 30Y
  120.             0.027902 + dblBump, // 40Y
  121.             0.027655 + dblBump  // 50Y
  122.         };

  123.         MergedDiscountForwardCurve dcFunding = LatentMarketStateBuilder.SmoothFundingCurve (
  124.             dtSpot,
  125.             strCurrency,
  126.             astrDepositMaturityTenor,
  127.             adblDepositQuote,
  128.             "ForwardRate",
  129.             adblFuturesQuote,
  130.             "ForwardRate",
  131.             astrFixFloatMaturityTenor,
  132.             adblFixFloatQuote,
  133.             "SwapRate"
  134.         );

  135.         Component[] aDepositComp = OTCInstrumentBuilder.FundingDeposit (
  136.             dtSpot,
  137.             strCurrency,
  138.             astrDepositMaturityTenor
  139.         );

  140.         Component[] aFuturesComp = ExchangeInstrumentBuilder.ForwardRateFuturesPack (
  141.             dtSpot,
  142.             adblFuturesQuote.length,
  143.             strCurrency
  144.         );

  145.         Component[] aFixFloatComp = OTCInstrumentBuilder.FixFloatStandard (
  146.             dtSpot,
  147.             strCurrency,
  148.             "ALL",
  149.             astrFixFloatMaturityTenor,
  150.             "MAIN",
  151.             0.
  152.         );

  153.         ValuationParams valParams = new ValuationParams (
  154.             dtSpot,
  155.             dtSpot,
  156.             strCurrency
  157.         );

  158.         CurveSurfaceQuoteContainer csqc = MarketParamsBuilder.Create (
  159.             dcFunding,
  160.             null,
  161.             null,
  162.             null,
  163.             null,
  164.             null,
  165.             null
  166.         );

  167.         System.out.println();

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

  169.         System.out.println ("\t|        DEPOSIT INPUT vs. CALC       ||");

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

  171.         for (int i = 0; i < aDepositComp.length; ++i)
  172.             System.out.println ("\t| [" + aDepositComp[i].maturityDate() + "] =" +
  173.                 FormatUtil.FormatDouble (aDepositComp[i].measureValue (
  174.                     valParams,
  175.                     null,
  176.                     csqc,
  177.                     null,
  178.                     "ForwardRate"
  179.                 ), 1, 6, 1.) + " |" +
  180.                 FormatUtil.FormatDouble (adblDepositQuote[i], 1, 6, 1.) + " ||"
  181.             );

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

  183.         System.out.println();

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

  185.         System.out.println ("\t|        FUTURES INPUT vs. CALC       ||");

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

  187.         for (int i = 0; i < aFuturesComp.length; ++i)
  188.             System.out.println ("\t| [" + aFuturesComp[i].maturityDate() + "] =" +
  189.                 FormatUtil.FormatDouble (aFuturesComp[i].measureValue (
  190.                     valParams,
  191.                     null,
  192.                     csqc,
  193.                     null,
  194.                     "ForwardRate"
  195.                 ), 1, 6, 1.) + " |" +
  196.                 FormatUtil.FormatDouble (adblFuturesQuote[i], 1, 6, 1.) + " ||"
  197.             );

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

  199.         System.out.println();

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

  201.         System.out.println ("\t|          FIX-FLOAT INPUTS vs CALIB             ||");

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

  203.         for (int i = 0; i < aFixFloatComp.length; ++i)
  204.             System.out.println ("\t| [" + aFixFloatComp[i].maturityDate() + "] =" +
  205.                 FormatUtil.FormatDouble (aFixFloatComp[i].measureValue (
  206.                     valParams,
  207.                     null,
  208.                     csqc,
  209.                     null,
  210.                     "CalibSwapRate"
  211.                 ), 1, 6, 1.) + " |" +
  212.                 FormatUtil.FormatDouble (adblFixFloatQuote[i], 1, 6, 1.) + " |" +
  213.                 FormatUtil.FormatDouble (aFixFloatComp[i].measureValue (
  214.                     valParams,
  215.                     null,
  216.                     csqc,
  217.                     null,
  218.                     "FairPremium"
  219.                 ), 1, 6, 1.) + " ||"
  220.             );

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

  222.         System.out.println();

  223.         return dcFunding;
  224.     }

  225.     private static final GovvieCurve GovvieCurve (
  226.         final JulianDate dtSpot,
  227.         final String strCode,
  228.         final double[] adblCoupon,
  229.         final double[] adblYield)
  230.         throws Exception
  231.     {
  232.         JulianDate[] adtEffective = new JulianDate[] {
  233.             dtSpot,
  234.             dtSpot,
  235.             dtSpot,
  236.             dtSpot,
  237.             dtSpot,
  238.             dtSpot,
  239.             dtSpot,
  240.             dtSpot
  241.         };

  242.         JulianDate[] adtMaturity = new JulianDate[] {
  243.             dtSpot.addTenor ("1Y"),
  244.             dtSpot.addTenor ("2Y"),
  245.             dtSpot.addTenor ("3Y"),
  246.             dtSpot.addTenor ("5Y"),
  247.             dtSpot.addTenor ("7Y"),
  248.             dtSpot.addTenor ("10Y"),
  249.             dtSpot.addTenor ("20Y"),
  250.             dtSpot.addTenor ("30Y")
  251.         };

  252.         GovvieCurve gc = LatentMarketStateBuilder.GovvieCurve (
  253.             strCode,
  254.             dtSpot,
  255.             adtEffective,
  256.             adtMaturity,
  257.             adblCoupon,
  258.             adblYield,
  259.             "Yield",
  260.             LatentMarketStateBuilder.SHAPE_PRESERVING
  261.         );

  262.         BondComponent[] aComp = TreasuryBuilder.FromCode (
  263.             strCode,
  264.             adtEffective,
  265.             adtMaturity,
  266.             adblCoupon
  267.         );

  268.         ValuationParams valParams = ValuationParams.Spot (dtSpot.julian());

  269.         CurveSurfaceQuoteContainer csqc = new CurveSurfaceQuoteContainer();

  270.         csqc.setGovvieState (gc);

  271.         System.out.println();

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

  273.         System.out.println ("\t|       TREASURY INPUT vs CALIB YIELD       ||");

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

  275.         for (int i = 0; i < aComp.length; ++i)
  276.             System.out.println ("\t| " + aComp[i].name() + " | " +
  277.                 FormatUtil.FormatDouble (adblYield[i], 1, 3, 100.) + "% | " +
  278.                 FormatUtil.FormatDouble (aComp[i].yieldFromPrice (
  279.                     valParams,
  280.                     null,
  281.                     null,
  282.                     aComp[i].maturityDate().julian(),
  283.                     1.,
  284.                     aComp[i].priceFromYield (
  285.                         valParams,
  286.                         null,
  287.                         null,
  288.                         gc.yield (aComp[i].maturityDate().julian())
  289.                     )
  290.                 ), 1, 3, 100.) + "% ||"
  291.             );

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

  293.         return gc;
  294.     }

  295.     private static final BondComponent Zero (
  296.         final String strCUSIP,
  297.         final JulianDate dtEffective,
  298.         final JulianDate dtMaturity,
  299.         final String strDayCount)
  300.         throws Exception
  301.     {
  302.         return BondBuilder.CreateSimpleFixed (
  303.             strCUSIP,
  304.             "USD",
  305.             "",
  306.             0.,
  307.             2,
  308.             strDayCount,
  309.             dtEffective,
  310.             dtMaturity,
  311.             null,
  312.             null
  313.         );
  314.     }

  315.     private static final void RVMeasures (
  316.         final BondComponent[] aBond,
  317.         final JulianDate dtValue,
  318.         final CurveSurfaceQuoteContainer csqc,
  319.         final double[] adblCleanPrice)
  320.         throws Exception
  321.     {
  322.         JulianDate dtSettle = dtValue.addBusDays (
  323.             3,
  324.             aBond[0].currency()
  325.         );

  326.         ValuationParams valParams = new ValuationParams (
  327.             dtValue,
  328.             dtSettle,
  329.             aBond[0].currency()
  330.         );

  331.         System.out.println();

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

  333.         System.out.println ("\t| Trade Date       : " + dtValue + " ||");

  334.         System.out.println ("\t| Cash Settle Date : " + dtSettle + " ||");

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

  336.         System.out.println();

  337.         String strSecularMetrics = "";

  338.         for (int i = 0; i < aBond.length; ++i) {
  339.             double dblOAS = Double.NaN;
  340.             double dblYTM = Double.NaN;
  341.             double dblYTW = Double.NaN;
  342.             double dblWALTM = Double.NaN;
  343.             double dblWALTW = Double.NaN;
  344.             double dblDiscountMargin = Double.NaN;
  345.             double dblModifiedDurationTW = Double.NaN;

  346.             try {
  347.                 WorkoutInfo wi = aBond[i].exerciseYieldFromPrice (
  348.                     valParams,
  349.                     csqc,
  350.                     null,
  351.                     adblCleanPrice[i]
  352.                 );

  353.                 dblYTW = wi.yield();

  354.                 dblYTM = aBond[i].yieldFromPrice (
  355.                     valParams,
  356.                     csqc,
  357.                     null,
  358.                     aBond[i].maturityDate().julian(),
  359.                     1.,
  360.                     adblCleanPrice[i]
  361.                 );

  362.                 dblWALTW = aBond[i].weightedAverageLife (
  363.                     valParams,
  364.                     csqc,
  365.                     wi.date(),
  366.                     wi.factor()
  367.                 );

  368.                 dblWALTM = aBond[i].weightedAverageLife (
  369.                     valParams,
  370.                     csqc,
  371.                     aBond[i].maturityDate().julian(),
  372.                     1.
  373.                 );

  374.                 dblDiscountMargin = aBond[i].discountMarginFromYield (
  375.                     valParams,
  376.                     csqc,
  377.                     null,
  378.                     wi.date(),
  379.                     wi.factor(),
  380.                     wi.yield()
  381.                 );

  382.                 dblOAS = aBond[i].oasFromYield (
  383.                     valParams,
  384.                     csqc,
  385.                     null,
  386.                     wi.date(),
  387.                     wi.factor(),
  388.                     wi.yield()
  389.                 );

  390.                 dblModifiedDurationTW = aBond[i].modifiedDurationFromPrice (
  391.                     valParams,
  392.                     csqc,
  393.                     null,
  394.                     wi.date(),
  395.                     wi.factor(),
  396.                     adblCleanPrice[i]
  397.                 );
  398.             } catch (Exception e) {
  399.                 // e.printStackTrace();
  400.             }

  401.             strSecularMetrics +=
  402.                 aBond[i].name() + "," +
  403.                 aBond[i].effectiveDate() + "," +
  404.                 aBond[i].maturityDate() + "," +
  405.                 FormatUtil.FormatDouble (adblCleanPrice[i], 3, 3, 100.) + "," +
  406.                 FormatUtil.FormatDouble (0., 1, 4, 100.) + "," +
  407.                 FormatUtil.FormatDouble (dblYTW, 1, 3, 100.) + "%," +
  408.                 FormatUtil.FormatDouble (dblYTM, 1, 3, 100.) + "%," +
  409.                 FormatUtil.FormatDouble (dblWALTW, 1, 3, 1.) + "," +
  410.                 FormatUtil.FormatDouble (dblWALTM, 1, 3, 1.) + "," +
  411.                 FormatUtil.FormatDouble (dblModifiedDurationTW, 1, 4, 10000.) + "," +
  412.                 FormatUtil.FormatDouble (dblDiscountMargin, 1, 3, 10000.) + "," +
  413.                 FormatUtil.FormatDouble (dblOAS, 1, 3, 10000.) + "\n";
  414.         }

  415.         System.out.println
  416.             ("Bond, Issue, Maturity, Clean Price, Accrued, Yield TW, Yield TM, WAL TW, WAL TM, Duration TW, Discount Margin TW, OAS TW");

  417.         System.out.print (strSecularMetrics);
  418.     }

  419.     public static final void main (
  420.         final String[] astrArgs)
  421.         throws Exception
  422.     {
  423.         EnvManager.InitEnv ("");

  424.         JulianDate dtSpot = DateUtil.CreateFromYMD (
  425.             2017,
  426.             DateUtil.MARCH,
  427.             10
  428.         );

  429.         String strCurrency = "USD";
  430.         String strTreasuryCode = "UST";

  431.         double[] adblTreasuryCoupon = new double[] {
  432.             0.0100,
  433.             0.0100,
  434.             0.0125,
  435.             0.0150,
  436.             0.0200,
  437.             0.0225,
  438.             0.0250,
  439.             0.0300
  440.         };

  441.         double[] adblTreasuryYield = new double[] {
  442.             0.0083, //  1Y
  443.             0.0122, //  2Y
  444.             0.0149, //  3Y
  445.             0.0193, //  5Y
  446.             0.0227, //  7Y
  447.             0.0248, // 10Y
  448.             0.0280, // 20Y
  449.             0.0308  // 30Y
  450.         };

  451.         BondComponent[] aZeroBond = new BondComponent[] {
  452.             Zero ("167484QW7", DateUtil.CreateFromYMD (1999, 11, 30), DateUtil.CreateFromYMD (2031,  1,  1), "US MUNI: 30/360"),
  453.             Zero ("232723HH2", DateUtil.CreateFromYMD (2011,  4, 21), DateUtil.CreateFromYMD (2033,  8,  1), "US MUNI: 30/360"),
  454.             Zero ("891381G58", DateUtil.CreateFromYMD (2009, 10, 29), DateUtil.CreateFromYMD (2034,  8,  1), "US MUNI: 30/360"),
  455.             Zero ("738850QZ2", DateUtil.CreateFromYMD (2011,  8, 11), DateUtil.CreateFromYMD (2034,  8,  1), "US MUNI: 30/360"),
  456.             Zero ("010869AV7", DateUtil.CreateFromYMD (1999,  2, 29), DateUtil.CreateFromYMD (2034, 10,  1), "US MUNI: 30/360"),
  457.             Zero ("010824JC5", DateUtil.CreateFromYMD (2005,  6, 30), DateUtil.CreateFromYMD (2036,  8,  1), "US MUNI: 30/360"),
  458.             Zero ("779631EP1", DateUtil.CreateFromYMD (2009, 10, 26), DateUtil.CreateFromYMD (2039,  8,  1), "US MUNI: 30/360"),
  459.             Zero ("796720GG9", DateUtil.CreateFromYMD (2009,  6, 18), DateUtil.CreateFromYMD (2044,  8,  1), "US MUNI: 30/360"),
  460.             Zero ("59333HCA2", DateUtil.CreateFromYMD (2009,  7, 14), DateUtil.CreateFromYMD (2044, 10,  1), "US MUNI: 30/360"),
  461.             Zero ("59333HBH8", DateUtil.CreateFromYMD (2009,  7, 14), DateUtil.CreateFromYMD (2045, 10,  1), "US MUNI: 30/360"),
  462.             Zero ("70870EDQ9", DateUtil.CreateFromYMD (2013, 12, 23), DateUtil.CreateFromYMD (2053,  7,  1), "US MUNI: 30/360"),
  463.         };

  464.         double[] adblCleanPrice = new double[] {
  465.             0.4916090,  // (2031,  1,  1)
  466.             0.5183290,  // (2033,  8,  1)
  467.             0.4450485,  // (2034,  8,  1)
  468.             0.4823590,  // (2034,  8,  1)
  469.             0.4703460,  // (2034, 10,  1)
  470.             0.4458700,  // (2036,  8,  1)
  471.             0.3508900,  // (2039,  8,  1)
  472.             0.2728300,  // (2044,  8,  1)
  473.             0.2619985,  // (2044, 10,  1)
  474.             0.2450195,  // (2045, 10,  1)
  475.             0.1614775,  // (2053,  7,  1)
  476.         };

  477.         RVMeasures (
  478.             aZeroBond,
  479.             dtSpot,
  480.             MarketParamsBuilder.Create (
  481.                 FundingCurve (
  482.                     dtSpot,
  483.                     strCurrency,
  484.                     0.
  485.                 ),
  486.                 GovvieCurve (
  487.                     dtSpot,
  488.                     strTreasuryCode,
  489.                     adblTreasuryCoupon,
  490.                     adblTreasuryYield
  491.                 ),
  492.                 null,
  493.                 null,
  494.                 null,
  495.                 null,
  496.                 null
  497.             ),
  498.             adblCleanPrice
  499.         );

  500.         System.out.println();
  501.     }
  502. }