Bhagalpur.java

  1. package org.drip.sample.bondmetrics;

  2. import org.drip.analytics.cashflow.*;
  3. import org.drip.analytics.date.*;
  4. import org.drip.analytics.daycount.Convention;
  5. import org.drip.analytics.daycount.DateAdjustParams;
  6. import org.drip.numerical.common.FormatUtil;
  7. import org.drip.param.creator.MarketParamsBuilder;
  8. import org.drip.param.market.CurveSurfaceQuoteContainer;
  9. import org.drip.param.valuation.ValuationParams;
  10. import org.drip.product.creator.BondBuilder;
  11. import org.drip.product.credit.BondComponent;
  12. import org.drip.product.params.EmbeddedOptionSchedule;
  13. import org.drip.service.env.EnvManager;
  14. import org.drip.service.template.LatentMarketStateBuilder;
  15. import org.drip.state.discount.MergedDiscountForwardCurve;
  16. import org.drip.state.identifier.FloaterLabel;

  17. /*
  18.  * -*- mode: java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  19.  */

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

  91. /**
  92.  * <i>Bhagalpur</i> demonstrates the Analytics Calculation/Reconciliation for the Bond Bhagalpur.
  93.  *  
  94.  * <br><br>
  95.  *  <ul>
  96.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ProductCore.md">Product Core Module</a></li>
  97.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/FixedIncomeAnalyticsLibrary.md">Fixed Income Analytics</a></li>
  98.  *      <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>
  99.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/bondmetrics/README.md">Bond Relative Value Replication Demonstration</a></li>
  100.  *  </ul>
  101.  * <br><br>
  102.  *
  103.  * @author Lakshmi Krishnamurthy
  104.  */

  105. public class Bhagalpur {

  106.     private static final void SetEOS (
  107.         final BondComponent bond,
  108.         final EmbeddedOptionSchedule eosCall,
  109.         final EmbeddedOptionSchedule eosPut)
  110.         throws java.lang.Exception
  111.     {
  112.         if (null != eosPut) bond.setEmbeddedPutSchedule (eosPut);

  113.         if (null != eosCall) bond.setEmbeddedCallSchedule (eosCall);
  114.     }

  115.     public static final void main (
  116.         final String[] astArgs)
  117.         throws Exception
  118.     {
  119.         EnvManager.InitEnv (
  120.             "",
  121.             true
  122.         );

  123.         JulianDate dtSpot = DateUtil.CreateFromYMD (
  124.             2017,
  125.             DateUtil.SEPTEMBER,
  126.             1
  127.         );

  128.         String[] astrDepositTenor = new String[] {
  129.             "2D"
  130.         };

  131.         double[] adblDepositQuote = new double[] {
  132.             0.013161 // 2D
  133.         };

  134.         double[] adblFuturesQuote = new double[] {
  135.             0.013225,   // 98.6775
  136.             0.01425,    // 98.575
  137.             0.01475,    // 98.525
  138.             0.01525,    // 98.475
  139.             0.01575,    // 98.425
  140.             0.01650     // 98.350
  141.         };

  142.         String[] astrFixFloatTenor = new String[] {
  143.             "02Y",
  144.             "03Y",
  145.             "04Y",
  146.             "05Y",
  147.             "06Y",
  148.             "07Y",
  149.             "08Y",
  150.             "09Y",
  151.             "10Y",
  152.             "11Y",
  153.             "12Y",
  154.             "15Y",
  155.             "20Y",
  156.             "25Y",
  157.             "30Y",
  158.             "40Y",
  159.             "50Y"
  160.         };

  161.         double[] adblFixFloatQuote = new double[] {
  162.             0.015540, //  2Y
  163.             0.016423, //  3Y
  164.             0.017209, //  4Y
  165.             0.017980, //  5Y
  166.             0.018743, //  6Y
  167.             0.019455, //  7Y
  168.             0.020080, //  8Y
  169.             0.020651, //  9Y
  170.             0.021195, // 10Y
  171.             0.021651, // 11Y
  172.             0.022065, // 12Y
  173.             0.022952, // 15Y
  174.             0.023825, // 20Y
  175.             0.024175, // 25Y
  176.             0.024347, // 30Y
  177.             0.024225, // 40Y
  178.             0.023968  // 50Y
  179.         };

  180.         double dblSpread = 0.0030;
  181.         String strCurrency = "USD";
  182.         double dblCleanPrice = 1.0;
  183.         double dblResetRate = 0.0431611 - dblSpread;

  184.         JulianDate dtEffective = DateUtil.CreateFromYMD (
  185.             2014,
  186.             5,
  187.             20
  188.         );

  189.         JulianDate dtMaturity = DateUtil.CreateFromYMD (
  190.             2019,
  191.             6,
  192.             1
  193.         );

  194.         BondComponent bond = BondBuilder.CreateSimpleFloaterFP (
  195.             "Bhagalpur",
  196.             strCurrency,
  197.             strCurrency + "-3M",
  198.             "",
  199.             dblSpread,
  200.             4,
  201.             "Act/360",
  202.             dtEffective,
  203.             dtMaturity,
  204.             DateUtil.CreateFromYMD (
  205.                 2014,
  206.                 9,
  207.                 1
  208.             ).julian(),
  209.             DateUtil.CreateFromYMD (
  210.                 2019,
  211.                 3,
  212.                 1
  213.             ).julian(),
  214.             new DateAdjustParams (
  215.                 Convention.DATE_ROLL_FOLLOWING,
  216.                 0,
  217.                 strCurrency
  218.             ),
  219.             null,
  220.             null,
  221.             null,
  222.             null,
  223.             null,
  224.             null,
  225.             null,
  226.             null,
  227.             null
  228.         );

  229.         EmbeddedOptionSchedule eosCall = EmbeddedOptionSchedule.FromAmerican (
  230.             dtSpot.julian(),
  231.             new int[] {
  232.                 DateUtil.CreateFromYMD (2015, 6, 1).julian(),
  233.                 DateUtil.CreateFromYMD (2016, 6, 1).julian(),
  234.                 DateUtil.CreateFromYMD (2019, 3, 1).julian(),
  235.             },
  236.             new double[] {
  237.                 1.01,
  238.                 1.00,
  239.                 1.00,
  240.             },
  241.             false,
  242.             15,
  243.             15,
  244.             false,
  245.             Double.NaN,
  246.             "",
  247.             Double.NaN
  248.         );

  249.         SetEOS (
  250.             bond,
  251.             eosCall,
  252.             null
  253.         );

  254.         CompositeFloatingPeriod cfp = (CompositeFloatingPeriod) bond.stream().containingPeriod (dtSpot.julian());

  255.         int iResetDate = ((org.drip.analytics.cashflow.ComposableUnitFloatingPeriod) (cfp.periods().get
  256.             (0))).referenceIndexPeriod().fixingDate();

  257.         MergedDiscountForwardCurve mdfc = LatentMarketStateBuilder.SmoothFundingCurve (
  258.             dtSpot,
  259.             strCurrency,
  260.             astrDepositTenor,
  261.             adblDepositQuote,
  262.             "ForwardRate",
  263.             adblFuturesQuote,
  264.             "ForwardRate",
  265.             astrFixFloatTenor,
  266.             adblFixFloatQuote,
  267.             "SwapRate"
  268.         );

  269.         CurveSurfaceQuoteContainer csqc = MarketParamsBuilder.Create (
  270.             mdfc,
  271.             null,
  272.             null,
  273.             null,
  274.             null,
  275.             null,
  276.             null
  277.         );

  278.         FloaterLabel fl = bond.floaterSetting().fri();

  279.         csqc.setFixing (iResetDate, fl, dblResetRate);

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

  281.         double dblYield = bond.yieldFromPrice (
  282.             ValuationParams.Spot (dtSpot.julian()),
  283.             csqc,
  284.             null,
  285.             dblCleanPrice
  286.         );

  287.         System.out.println ("Price In  : " + dblCleanPrice);

  288.         System.out.println ("Yield Out : " + dblYield);

  289.         System.out.println ("Price Out : " +
  290.             bond.priceFromYield (
  291.                 ValuationParams.Spot (dtSpot.julian()),
  292.                 csqc,
  293.                 null,
  294.                 dblYield
  295.             )
  296.         );

  297.         System.out.println();

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

  299.         System.out.println ("\t||                                           PERIOD LABELS AND CURVE FACTORS                                          ||");

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

  301.         System.out.println ("\t||   L -> R:                                                                                                          ||");

  302.         System.out.println ("\t||           - Period Start Date                                                                                      ||");

  303.         System.out.println ("\t||           - Period End Date                                                                                        ||");

  304.         System.out.println ("\t||           - Period Pay Date                                                                                        ||");

  305.         System.out.println ("\t||           - Period Credit Label                                                                                    ||");

  306.         System.out.println ("\t||           - Period Funding Label                                                                                   ||");

  307.         System.out.println ("\t||           - Period Forward Label                                                                                   ||");

  308.         System.out.println ("\t||           - Period Coupon Rate (%)                                                                                 ||");

  309.         System.out.println ("\t||           - Period Coupon Year Fraction                                                                            ||");

  310.         System.out.println ("\t||           - Period Coupon Amount                                                                                   ||");

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

  312.         System.out.println ("\t||           - Period Discount Factor                                                                                 ||");

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

  314.         System.out.println ("\t||           - Period Recovery                                                                                        ||");

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

  316.         for (CompositePeriod p : bond.couponPeriods()) {
  317.             int iEndDate = p.endDate();

  318.             int iPayDate = p.payDate();

  319.             int iStartDate = p.startDate();

  320.             double dblCouponRate = bond.couponMetrics (
  321.                 iEndDate,
  322.                 valParams,
  323.                 csqc
  324.             ).rate();

  325.             double dblCouponDCF = p.couponDCF();

  326.             System.out.println ("\t|| " +
  327.                 DateUtil.YYYYMMDD (iStartDate) + " => " +
  328.                 DateUtil.YYYYMMDD (iEndDate) + " | " +
  329.                 DateUtil.YYYYMMDD (iPayDate) + " | ? | " +
  330.                 p.fundingLabel().fullyQualifiedName() + " | ? | " +
  331.                 FormatUtil.FormatDouble (dblCouponRate, 1, 3, 100.) + "% | " +
  332.                 FormatUtil.FormatDouble (dblCouponDCF, 1, 4, 1.) + " | " +
  333.                 FormatUtil.FormatDouble (dblCouponRate * dblCouponDCF * p.notional (iEndDate) * p.couponFactor (iEndDate), 1, 6, 1.) + " | " +
  334.                 FormatUtil.FormatDouble (p.notional (iStartDate) - p.notional (iEndDate), 1, 4, 1.) + " | " +
  335.                 FormatUtil.FormatDouble (1., 1, 4, 1.) + " | " +
  336.                 FormatUtil.FormatDouble (1., 2, 2, 100.) + "% ||"
  337.             );
  338.         }

  339.         System.out.println ("\t|| " +
  340.             DateUtil.YYYYMMDD (dtEffective.julian()) + " => " +
  341.             DateUtil.YYYYMMDD (bond.maturityDate().julian()) + " | " +
  342.             DateUtil.YYYYMMDD (bond.maturityDate().julian()) + " | ? | " +
  343.             bond.fundingLabel().fullyQualifiedName() + " | ? | " +
  344.             FormatUtil.FormatDouble (0., 1, 3, 100.) + "% | " +
  345.             FormatUtil.FormatDouble (0., 1, 4, 1.) + " | " +
  346.             FormatUtil.FormatDouble (0., 1, 6, 1.) + " | " +
  347.             FormatUtil.FormatDouble (bond.notional (bond.maturityDate().julian()), 1, 4, 1.) + " | " +
  348.             FormatUtil.FormatDouble (1., 1, 4, 1.) + " | " +
  349.             FormatUtil.FormatDouble (1., 2, 2, 100.) + "% ||"
  350.         );

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

  352.         System.out.println();

  353.         int[] aiCallDate = eosCall.dates();

  354.         double[] adblCallFactor = eosCall.factors();

  355.         for (int i = 0; i < aiCallDate.length; ++i) {
  356.             System.out.println ("\t||" +
  357.                 new JulianDate (aiCallDate[i]) + " -> " +
  358.                 adblCallFactor[i]
  359.             );
  360.         }

  361.         JulianDate dtForward = DateUtil.CreateFromYMD (2018, 7, 1);

  362.         double[] adblExerciseFactor = eosCall.exerciseFactors (dtForward.julian());

  363.         System.out.println ("\tForward Exercise Date => " +
  364.             dtForward + " | " +
  365.             adblExerciseFactor[0]
  366.         );

  367.         EnvManager.TerminateEnv();
  368.     }
  369. }