HubbaliDharwad.java

  1. package org.drip.sample.bondfixed;

  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) 2020 Lakshmi Krishnamurthy
  19.  * Copyright (C) 2019 Lakshmi Krishnamurthy
  20.  * Copyright (C) 2018 Lakshmi Krishnamurthy
  21.  * Copyright (C) 2017 Lakshmi Krishnamurthy
  22.  *
  23.  *  This file is part of DROP, an open-source library targeting analytics/risk, transaction cost analytics,
  24.  *      asset liability management analytics, capital, exposure, and margin analytics, valuation adjustment
  25.  *      analytics, and portfolio construction analytics within and across fixed income, credit, commodity,
  26.  *      equity, FX, and structured products. It also includes auxiliary libraries for algorithm support,
  27.  *      numerical analysis, numerical optimization, spline builder, model validation, statistical learning,
  28.  *      and computational support.
  29.  *  
  30.  *      https://lakshmidrip.github.io/DROP/
  31.  *  
  32.  *  DROP is composed of three modules:
  33.  *  
  34.  *  - DROP Product Core - https://lakshmidrip.github.io/DROP-Product-Core/
  35.  *  - DROP Portfolio Core - https://lakshmidrip.github.io/DROP-Portfolio-Core/
  36.  *  - DROP Computational Core - https://lakshmidrip.github.io/DROP-Computational-Core/
  37.  *
  38.  *  DROP Product Core implements libraries for the following:
  39.  *  - Fixed Income Analytics
  40.  *  - Loan Analytics
  41.  *  - Transaction Cost Analytics
  42.  *
  43.  *  DROP Portfolio Core implements libraries for the following:
  44.  *  - Asset Allocation Analytics
  45.  *  - Asset Liability Management Analytics
  46.  *  - Capital Estimation Analytics
  47.  *  - Exposure Analytics
  48.  *  - Margin Analytics
  49.  *  - XVA Analytics
  50.  *
  51.  *  DROP Computational Core implements libraries for the following:
  52.  *  - Algorithm Support
  53.  *  - Computation Support
  54.  *  - Function Analysis
  55.  *  - Model Validation
  56.  *  - Numerical Analysis
  57.  *  - Numerical Optimizer
  58.  *  - Spline Builder
  59.  *  - Statistical Learning
  60.  *
  61.  *  Documentation for DROP is Spread Over:
  62.  *
  63.  *  - Main                     => https://lakshmidrip.github.io/DROP/
  64.  *  - Wiki                     => https://github.com/lakshmiDRIP/DROP/wiki
  65.  *  - GitHub                   => https://github.com/lakshmiDRIP/DROP
  66.  *  - Repo Layout Taxonomy     => https://github.com/lakshmiDRIP/DROP/blob/master/Taxonomy.md
  67.  *  - Javadoc                  => https://lakshmidrip.github.io/DROP/Javadoc/index.html
  68.  *  - Technical Specifications => https://github.com/lakshmiDRIP/DROP/tree/master/Docs/Internal
  69.  *  - Release Versions         => https://lakshmidrip.github.io/DROP/version.html
  70.  *  - Community Credits        => https://lakshmidrip.github.io/DROP/credits.html
  71.  *  - Issues Catalog           => https://github.com/lakshmiDRIP/DROP/issues
  72.  *  - JUnit                    => https://lakshmidrip.github.io/DROP/junit/index.html
  73.  *  - Jacoco                   => https://lakshmidrip.github.io/DROP/jacoco/index.html
  74.  *
  75.  *  Licensed under the Apache License, Version 2.0 (the "License");
  76.  *      you may not use this file except in compliance with the License.
  77.  *  
  78.  *  You may obtain a copy of the License at
  79.  *      http://www.apache.org/licenses/LICENSE-2.0
  80.  *  
  81.  *  Unless required by applicable law or agreed to in writing, software
  82.  *      distributed under the License is distributed on an "AS IS" BASIS,
  83.  *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  84.  *  
  85.  *  See the License for the specific language governing permissions and
  86.  *      limitations under the License.
  87.  */

  88. /**
  89.  * <i>HubbaliDharwad</i> demonstrates EOS Fixed Coupon Multi-flavor Pricing and Relative Value Measure
  90.  * Generation for HubbaliDharwad.
  91.  *  
  92.  * <br><br>
  93.  *  <ul>
  94.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ProductCore.md">Product Core Module</a></li>
  95.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/FixedIncomeAnalyticsLibrary.md">Fixed Income Analytics</a></li>
  96.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/README.md">DROP API Construction and Usage</a></li>
  97.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/bondfixed/README.md">Fixed Coupon Agency/Corporate Bonds</a></li>
  98.  *  </ul>
  99.  * <br><br>
  100.  *
  101.  * @author Lakshmi Krishnamurthy
  102.  */

  103. public class HubbaliDharwad {

  104.     private static final MergedDiscountForwardCurve FundingCurve (
  105.         final JulianDate dtSpot,
  106.         final String strCurrency,
  107.         final double dblBump)
  108.         throws Exception
  109.     {
  110.         String[] astrDepositMaturityTenor = new String[] {
  111.             "2D"
  112.         };

  113.         double[] adblDepositQuote = new double[] {
  114.             0.0111956 + dblBump // 2D
  115.         };

  116.         double[] adblFuturesQuote = new double[] {
  117.             0.011375 + dblBump, // 98.8625
  118.             0.013350 + dblBump, // 98.6650
  119.             0.014800 + dblBump, // 98.5200
  120.             0.016450 + dblBump, // 98.3550
  121.             0.017850 + dblBump, // 98.2150
  122.             0.019300 + dblBump  // 98.0700
  123.         };

  124.         String[] astrFixFloatMaturityTenor = new String[] {
  125.             "02Y",
  126.             "03Y",
  127.             "04Y",
  128.             "05Y",
  129.             "06Y",
  130.             "07Y",
  131.             "08Y",
  132.             "09Y",
  133.             "10Y",
  134.             "11Y",
  135.             "12Y",
  136.             "15Y",
  137.             "20Y",
  138.             "25Y",
  139.             "30Y",
  140.             "40Y",
  141.             "50Y"
  142.         };

  143.         double[] adblFixFloatQuote = new double[] {
  144.             0.017029 + dblBump, //  2Y
  145.             0.019354 + dblBump, //  3Y
  146.             0.021044 + dblBump, //  4Y
  147.             0.022291 + dblBump, //  5Y
  148.             0.023240 + dblBump, //  6Y
  149.             0.024025 + dblBump, //  7Y
  150.             0.024683 + dblBump, //  8Y
  151.             0.025243 + dblBump, //  9Y
  152.             0.025720 + dblBump, // 10Y
  153.             0.026130 + dblBump, // 11Y
  154.             0.026495 + dblBump, // 12Y
  155.             0.027230 + dblBump, // 15Y
  156.             0.027855 + dblBump, // 20Y
  157.             0.028025 + dblBump, // 25Y
  158.             0.028028 + dblBump, // 30Y
  159.             0.027902 + dblBump, // 40Y
  160.             0.027655 + dblBump  // 50Y
  161.         };

  162.         MergedDiscountForwardCurve dcFunding = LatentMarketStateBuilder.SmoothFundingCurve (
  163.             dtSpot,
  164.             strCurrency,
  165.             astrDepositMaturityTenor,
  166.             adblDepositQuote,
  167.             "ForwardRate",
  168.             adblFuturesQuote,
  169.             "ForwardRate",
  170.             astrFixFloatMaturityTenor,
  171.             adblFixFloatQuote,
  172.             "SwapRate"
  173.         );

  174.         Component[] aDepositComp = OTCInstrumentBuilder.FundingDeposit (
  175.             dtSpot,
  176.             strCurrency,
  177.             astrDepositMaturityTenor
  178.         );

  179.         Component[] aFuturesComp = ExchangeInstrumentBuilder.ForwardRateFuturesPack (
  180.             dtSpot,
  181.             adblFuturesQuote.length,
  182.             strCurrency
  183.         );

  184.         Component[] aFixFloatComp = OTCInstrumentBuilder.FixFloatStandard (
  185.             dtSpot,
  186.             strCurrency,
  187.             "ALL",
  188.             astrFixFloatMaturityTenor,
  189.             "MAIN",
  190.             0.
  191.         );

  192.         ValuationParams valParams = new ValuationParams (
  193.             dtSpot,
  194.             dtSpot,
  195.             strCurrency
  196.         );

  197.         CurveSurfaceQuoteContainer csqc = MarketParamsBuilder.Create (
  198.             dcFunding,
  199.             null,
  200.             null,
  201.             null,
  202.             null,
  203.             null,
  204.             null
  205.         );

  206.         System.out.println();

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

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

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

  210.         for (int i = 0; i < aDepositComp.length; ++i)
  211.             System.out.println ("\t| [" + aDepositComp[i].maturityDate() + "] =" +
  212.                 FormatUtil.FormatDouble (aDepositComp[i].measureValue (
  213.                     valParams,
  214.                     null,
  215.                     csqc,
  216.                     null,
  217.                     "ForwardRate"
  218.                 ), 1, 6, 1.) + " |" +
  219.                 FormatUtil.FormatDouble (adblDepositQuote[i], 1, 6, 1.) + " ||"
  220.             );

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

  222.         System.out.println();

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

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

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

  226.         for (int i = 0; i < aFuturesComp.length; ++i)
  227.             System.out.println ("\t| [" + aFuturesComp[i].maturityDate() + "] =" +
  228.                 FormatUtil.FormatDouble (aFuturesComp[i].measureValue (
  229.                     valParams,
  230.                     null,
  231.                     csqc,
  232.                     null,
  233.                     "ForwardRate"
  234.                 ), 1, 6, 1.) + " |" +
  235.                 FormatUtil.FormatDouble (adblFuturesQuote[i], 1, 6, 1.) + " ||"
  236.             );

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

  238.         System.out.println();

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

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

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

  242.         for (int i = 0; i < aFixFloatComp.length; ++i)
  243.             System.out.println ("\t| [" + aFixFloatComp[i].maturityDate() + "] =" +
  244.                 FormatUtil.FormatDouble (aFixFloatComp[i].measureValue (
  245.                     valParams,
  246.                     null,
  247.                     csqc,
  248.                     null,
  249.                     "CalibSwapRate"
  250.                 ), 1, 6, 1.) + " |" +
  251.                 FormatUtil.FormatDouble (adblFixFloatQuote[i], 1, 6, 1.) + " |" +
  252.                 FormatUtil.FormatDouble (aFixFloatComp[i].measureValue (
  253.                     valParams,
  254.                     null,
  255.                     csqc,
  256.                     null,
  257.                     "FairPremium"
  258.                 ), 1, 6, 1.) + " ||"
  259.             );

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

  261.         System.out.println();

  262.         return dcFunding;
  263.     }

  264.     private static final GovvieCurve GovvieCurve (
  265.         final JulianDate dtSpot,
  266.         final String strCode,
  267.         final double[] adblCoupon,
  268.         final double[] adblYield)
  269.         throws Exception
  270.     {
  271.         JulianDate[] adtEffective = new JulianDate[] {
  272.             dtSpot,
  273.             dtSpot,
  274.             dtSpot,
  275.             dtSpot,
  276.             dtSpot,
  277.             dtSpot,
  278.             dtSpot,
  279.             dtSpot
  280.         };

  281.         JulianDate[] adtMaturity = new JulianDate[] {
  282.             dtSpot.addTenor ("1Y"),
  283.             dtSpot.addTenor ("2Y"),
  284.             dtSpot.addTenor ("3Y"),
  285.             dtSpot.addTenor ("5Y"),
  286.             dtSpot.addTenor ("7Y"),
  287.             dtSpot.addTenor ("10Y"),
  288.             dtSpot.addTenor ("20Y"),
  289.             dtSpot.addTenor ("30Y")
  290.         };

  291.         GovvieCurve gc = LatentMarketStateBuilder.GovvieCurve (
  292.             strCode,
  293.             dtSpot,
  294.             adtEffective,
  295.             adtMaturity,
  296.             adblCoupon,
  297.             adblYield,
  298.             "Yield",
  299.             LatentMarketStateBuilder.SHAPE_PRESERVING
  300.         );

  301.         BondComponent[] aComp = TreasuryBuilder.FromCode (
  302.             strCode,
  303.             adtEffective,
  304.             adtMaturity,
  305.             adblCoupon
  306.         );

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

  308.         CurveSurfaceQuoteContainer csqc = new CurveSurfaceQuoteContainer();

  309.         csqc.setGovvieState (gc);

  310.         System.out.println();

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

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

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

  314.         for (int i = 0; i < aComp.length; ++i)
  315.             System.out.println ("\t| " + aComp[i].name() + " | " +
  316.                 FormatUtil.FormatDouble (adblYield[i], 1, 3, 100.) + "% | " +
  317.                 FormatUtil.FormatDouble (aComp[i].yieldFromPrice (
  318.                     valParams,
  319.                     null,
  320.                     null,
  321.                     aComp[i].maturityDate().julian(),
  322.                     1.,
  323.                     aComp[i].priceFromYield (
  324.                         valParams,
  325.                         null,
  326.                         null,
  327.                         gc.yield (aComp[i].maturityDate().julian())
  328.                     )
  329.                 ), 1, 3, 100.) + "% ||"
  330.             );

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

  332.         return gc;
  333.     }

  334.     private static final void RVMeasures (
  335.         final BondComponent bond,
  336.         final JulianDate dtValue,
  337.         final CurveSurfaceQuoteContainer csqc,
  338.         final double dblCleanPrice)
  339.         throws Exception
  340.     {
  341.         JulianDate dtSettle = dtValue.addBusDays (
  342.             3,
  343.             bond.currency()
  344.         );

  345.         ValuationParams valParams = new ValuationParams (
  346.             dtValue,
  347.             dtSettle,
  348.             bond.currency()
  349.         );

  350.         System.out.println();

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

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

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

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

  355.         System.out.println();

  356.         double dblYTM = Double.NaN;
  357.         double dblYTW = Double.NaN;
  358.         double dblOASTW = Double.NaN;
  359.         double dblWALTM = Double.NaN;
  360.         double dblWALTW = Double.NaN;
  361.         double dblZSpreadTW = Double.NaN;
  362.         double dblModifiedDurationTW = Double.NaN;

  363.         WorkoutInfo wi = bond.exerciseYieldFromPrice (
  364.             valParams,
  365.             csqc,
  366.             null,
  367.             dblCleanPrice
  368.         );

  369.         try {
  370.             dblYTW = wi.yield();

  371.             dblYTM = bond.yieldFromPrice (
  372.                 valParams,
  373.                 csqc,
  374.                 null,
  375.                 bond.maturityDate().julian(),
  376.                 1.,
  377.                 dblCleanPrice
  378.             );

  379.             dblWALTW = bond.weightedAverageLife (
  380.                 valParams,
  381.                 csqc,
  382.                 wi.date(),
  383.                 wi.factor()
  384.             );

  385.             dblWALTM = bond.weightedAverageLife (
  386.                 valParams,
  387.                 csqc,
  388.                 bond.maturityDate().julian(),
  389.                 1.
  390.             );

  391.             dblZSpreadTW = bond.zSpreadFromYield (
  392.                 valParams,
  393.                 csqc,
  394.                 null,
  395.                 wi.date(),
  396.                 wi.factor(),
  397.                 wi.yield()
  398.             );

  399.             dblOASTW = bond.oasFromYield (
  400.                 valParams,
  401.                 csqc,
  402.                 null,
  403.                 wi.date(),
  404.                 wi.factor(),
  405.                 wi.yield()
  406.             );

  407.             dblModifiedDurationTW = bond.modifiedDurationFromPrice (
  408.                 valParams,
  409.                 csqc,
  410.                 null,
  411.                 wi.date(),
  412.                 wi.factor(),
  413.                 dblCleanPrice
  414.             );
  415.         } catch (Exception e) {
  416.             // e.printStackTrace();
  417.         }

  418.         System.out.println ("\t Bond Name                 => " + bond.name());

  419.         System.out.println ("\t Effective Date            => " + bond.effectiveDate());

  420.         System.out.println ("\t Maturity Date             => " + bond.maturityDate());

  421.         System.out.println ("\t Exercise Date             => " + new JulianDate (wi.date()));

  422.         System.out.println ("\t Price                     => " + FormatUtil.FormatDouble (dblCleanPrice, 1, 5, 100.));

  423.         System.out.println ("\t Bond Accrued              => " + FormatUtil.FormatDouble (bond.accrued (dtSettle.julian(), csqc), 1, 4, 100.));

  424.         System.out.println ("\t Bond YTW                  => " + FormatUtil.FormatDouble (dblYTW, 1, 3, 100.) + "%");

  425.         System.out.println ("\t Bond YTM                  => " + FormatUtil.FormatDouble (dblYTM, 1, 3, 100.) + "%");

  426.         System.out.println ("\t Bond WAL TW               => " + FormatUtil.FormatDouble (dblWALTW, 1, 3, 1.));

  427.         System.out.println ("\t Bond WAL TM               => " + FormatUtil.FormatDouble (dblWALTM, 1, 3, 1.));

  428.         System.out.println ("\t Bond Modified Duration TW => " + FormatUtil.FormatDouble (dblModifiedDurationTW, 1, 4, 10000.));

  429.         System.out.println ("\t Bond Z Spread TW          => " + FormatUtil.FormatDouble (dblZSpreadTW, 1, 1, 10000.));

  430.         System.out.println ("\t Bond OAS TW               => " + FormatUtil.FormatDouble (dblOASTW, 1, 1, 10000.));
  431.     }

  432.     public static final void main (
  433.         final String[] astrArgs)
  434.         throws Exception
  435.     {
  436.         EnvManager.InitEnv (
  437.             "",
  438.             true
  439.         );

  440.         JulianDate dtSpot = DateUtil.CreateFromYMD (
  441.             2017,
  442.             DateUtil.MARCH,
  443.             10
  444.         );

  445.         String strCurrency = "USD";
  446.         String strTreasuryCode = "UST";

  447.         double[] adblTreasuryCoupon = new double[] {
  448.             0.0100,
  449.             0.0100,
  450.             0.0125,
  451.             0.0150,
  452.             0.0200,
  453.             0.0225,
  454.             0.0250,
  455.             0.0300
  456.         };

  457.         double[] adblTreasuryYield = new double[] {
  458.             0.0083, //  1Y
  459.             0.0122, //  2Y
  460.             0.0149, //  3Y
  461.             0.0193, //  5Y
  462.             0.0227, //  7Y
  463.             0.0248, // 10Y
  464.             0.0280, // 20Y
  465.             0.0308  // 30Y
  466.         };

  467.         JulianDate dtEffective = DateUtil.CreateFromYMD (2011,  1, 25);
  468.         JulianDate dtMaturity  = DateUtil.CreateFromYMD (2021,  1, 31);
  469.         double dblCoupon = 0.0525;
  470.         double dblCleanPrice = 1.075498;
  471.         int iFreq = 2;
  472.         String strCUSIP = "HubbaliDharwad";
  473.         String strDayCount = "30/360";

  474.         BondComponent bond = BondBuilder.CreateSimpleFixed (
  475.             strCUSIP,
  476.             strCurrency,
  477.             "",
  478.             dblCoupon,
  479.             iFreq,
  480.             strDayCount,
  481.             dtEffective,
  482.             dtMaturity,
  483.             null,
  484.             null
  485.         );

  486.         RVMeasures (
  487.             bond,
  488.             dtSpot,
  489.             MarketParamsBuilder.Create (
  490.                 FundingCurve (
  491.                     dtSpot,
  492.                     strCurrency,
  493.                     0.
  494.                 ),
  495.                 GovvieCurve (
  496.                     dtSpot,
  497.                     strTreasuryCode,
  498.                     adblTreasuryCoupon,
  499.                     adblTreasuryYield
  500.                 ),
  501.                 null,
  502.                 null,
  503.                 null,
  504.                 null,
  505.                 null
  506.             ),
  507.             dblCleanPrice
  508.         );

  509.         EnvManager.TerminateEnv();
  510.     }
  511. }