Agartala.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.service.env.EnvManager;
  13. import org.drip.service.template.LatentMarketStateBuilder;
  14. import org.drip.state.discount.MergedDiscountForwardCurve;
  15. import org.drip.state.identifier.FloaterLabel;

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

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

  90. /**
  91.  * <i>Agartala</i> demonstrates the Analytics Calculation/Reconciliation for the Bond Agartala.
  92.  *  
  93.  * <br><br>
  94.  *  <ul>
  95.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ProductCore.md">Product Core Module</a></li>
  96.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/FixedIncomeAnalyticsLibrary.md">Fixed Income Analytics</a></li>
  97.  *      <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>
  98.  *      <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>
  99.  *  </ul>
  100.  * <br><br>
  101.  *
  102.  * @author Lakshmi Krishnamurthy
  103.  */

  104. public class Agartala {

  105.     public static final void main (
  106.         final String[] astArgs)
  107.         throws Exception
  108.     {
  109.         EnvManager.InitEnv (
  110.             "",
  111.             true
  112.         );

  113.         JulianDate dtSpot = DateUtil.CreateFromYMD (
  114.             2017,
  115.             DateUtil.SEPTEMBER,
  116.             1
  117.         );

  118.         String[] astrDepositTenor = new String[] {
  119.             "2D"
  120.         };

  121.         double[] adblDepositQuote = new double[] {
  122.             0.013161 // 2D
  123.         };

  124.         double[] adblFuturesQuote = new double[] {
  125.             0.013225,   // 98.6775
  126.             0.01425,    // 98.575
  127.             0.01475,    // 98.525
  128.             0.01525,    // 98.475
  129.             0.01575,    // 98.425
  130.             0.01650     // 98.350
  131.         };

  132.         String[] astrFixFloatTenor = new String[] {
  133.             "02Y",
  134.             "03Y",
  135.             "04Y",
  136.             "05Y",
  137.             "06Y",
  138.             "07Y",
  139.             "08Y",
  140.             "09Y",
  141.             "10Y",
  142.             "11Y",
  143.             "12Y",
  144.             "15Y",
  145.             "20Y",
  146.             "25Y",
  147.             "30Y",
  148.             "40Y",
  149.             "50Y"
  150.         };

  151.         double[] adblFixFloatQuote = new double[] {
  152.             0.015540, //  2Y
  153.             0.016423, //  3Y
  154.             0.017209, //  4Y
  155.             0.017980, //  5Y
  156.             0.018743, //  6Y
  157.             0.019455, //  7Y
  158.             0.020080, //  8Y
  159.             0.020651, //  9Y
  160.             0.021195, // 10Y
  161.             0.021651, // 11Y
  162.             0.022065, // 12Y
  163.             0.022952, // 15Y
  164.             0.023825, // 20Y
  165.             0.024175, // 25Y
  166.             0.024347, // 30Y
  167.             0.024225, // 40Y
  168.             0.023968  // 50Y
  169.         };

  170.         double dblSpread = 0.0060;
  171.         String strCurrency = "USD";
  172.         double dblCleanPrice = 1.00717;
  173.         double dblResetRate = 0.0191722 - dblSpread;

  174.         JulianDate dtEffective = DateUtil.CreateFromYMD (
  175.             2016,
  176.             6,
  177.             12
  178.         );

  179.         JulianDate dtMaturity = DateUtil.CreateFromYMD (
  180.             2019,
  181.             5,
  182.             24
  183.         );

  184.         BondComponent bond = BondBuilder.CreateSimpleFloaterFP (
  185.             "Agartala",
  186.             strCurrency,
  187.             strCurrency + "-3M",
  188.             "",
  189.             dblSpread,
  190.             4,
  191.             "Act/360",
  192.             dtEffective,
  193.             dtMaturity,
  194.             DateUtil.CreateFromYMD (
  195.                 2016,
  196.                 8,
  197.                 24
  198.             ).julian(),
  199.             DateUtil.CreateFromYMD (
  200.                 2019,
  201.                 2,
  202.                 24
  203.             ).julian(),
  204.             new DateAdjustParams (
  205.                 Convention.DATE_ROLL_FOLLOWING,
  206.                 0,
  207.                 strCurrency
  208.             ),
  209.             null,
  210.             null,
  211.             null,
  212.             null,
  213.             null,
  214.             null,
  215.             null,
  216.             null,
  217.             null
  218.         );

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

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

  222.         MergedDiscountForwardCurve mdfc = LatentMarketStateBuilder.SmoothFundingCurve (
  223.             dtSpot,
  224.             strCurrency,
  225.             astrDepositTenor,
  226.             adblDepositQuote,
  227.             "ForwardRate",
  228.             adblFuturesQuote,
  229.             "ForwardRate",
  230.             astrFixFloatTenor,
  231.             adblFixFloatQuote,
  232.             "SwapRate"
  233.         );

  234.         CurveSurfaceQuoteContainer csqc = MarketParamsBuilder.Create (
  235.             mdfc,
  236.             null,
  237.             null,
  238.             null,
  239.             null,
  240.             null,
  241.             null
  242.         );

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

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

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

  246.         double dblYield = bond.yieldFromPrice (
  247.             ValuationParams.Spot (dtSpot.julian()),
  248.             csqc,
  249.             null,
  250.             dblCleanPrice
  251.         );

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

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

  254.         System.out.println ("Price Out : " +
  255.             bond.priceFromYield (
  256.                 ValuationParams.Spot (dtSpot.julian()),
  257.                 csqc,
  258.                 null,
  259.                 dblYield
  260.             )
  261.         );

  262.         System.out.println();

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  281.         for (CompositePeriod p : bond.couponPeriods()) {
  282.             int iEndDate = p.endDate();

  283.             int iPayDate = p.payDate();

  284.             int iStartDate = p.startDate();

  285.             double dblCouponRate = bond.couponMetrics (
  286.                 iEndDate,
  287.                 valParams,
  288.                 csqc
  289.             ).rate();

  290.             double dblCouponDCF = p.couponDCF();

  291.             System.out.println ("\t|| " +
  292.                 DateUtil.YYYYMMDD (iStartDate) + " => " +
  293.                 DateUtil.YYYYMMDD (iEndDate) + " | " +
  294.                 DateUtil.YYYYMMDD (iPayDate) + " | ? | " +
  295.                 p.fundingLabel().fullyQualifiedName() + " | ? | " +
  296.                 FormatUtil.FormatDouble (dblCouponRate, 1, 3, 100.) + "% | " +
  297.                 FormatUtil.FormatDouble (dblCouponDCF, 1, 4, 1.) + " | " +
  298.                 FormatUtil.FormatDouble (dblCouponRate * dblCouponDCF * p.notional (iEndDate) * p.couponFactor (iEndDate), 1, 6, 1.) + " | " +
  299.                 FormatUtil.FormatDouble (p.notional (iStartDate) - p.notional (iEndDate), 1, 4, 1.) + " | " +
  300.                 FormatUtil.FormatDouble (1., 1, 4, 1.) + " | " +
  301.                 FormatUtil.FormatDouble (1., 2, 2, 100.) + "% ||"
  302.             );
  303.         }

  304.         System.out.println ("\t|| " +
  305.             DateUtil.YYYYMMDD (dtEffective.julian()) + " => " +
  306.             DateUtil.YYYYMMDD (bond.maturityDate().julian()) + " | " +
  307.             DateUtil.YYYYMMDD (bond.maturityDate().julian()) + " | ? | " +
  308.             bond.fundingLabel().fullyQualifiedName() + " | ? | " +
  309.             FormatUtil.FormatDouble (0., 1, 3, 100.) + "% | " +
  310.             FormatUtil.FormatDouble (0., 1, 4, 1.) + " | " +
  311.             FormatUtil.FormatDouble (0., 1, 6, 1.) + " | " +
  312.             FormatUtil.FormatDouble (bond.notional (bond.maturityDate().julian()), 1, 4, 1.) + " | " +
  313.             FormatUtil.FormatDouble (1., 1, 4, 1.) + " | " +
  314.             FormatUtil.FormatDouble (1., 2, 2, 100.) + "% ||"
  315.         );

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

  317.         EnvManager.TerminateEnv();
  318.     }
  319. }