Belgaum.java

  1. package org.drip.sample.bondmetrics;

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

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

  102. public class Belgaum {

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

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

  116.         String[] astrDepositTenor = new String[] {
  117.             "2D"
  118.         };

  119.         double[] adblDepositQuote = new double[] {
  120.             0.013161 // 2D
  121.         };

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

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

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

  168.         double dblSpread = 0.0066;
  169.         String strCurrency = "USD";
  170.         double dblCleanPrice = 1.00717;
  171.         double dblResetRate = 0.0190167 - dblSpread;

  172.         JulianDate dtEffective = DateUtil.CreateFromYMD (
  173.             2016,
  174.             6,
  175.             14
  176.         );

  177.         JulianDate dtMaturity = DateUtil.CreateFromYMD (
  178.             2019,
  179.             6,
  180.             14
  181.         );

  182.         BondComponent bond = BondBuilder.CreateSimpleFloater (
  183.             "Belgaum",
  184.             "USD",
  185.             "USD-3M",
  186.             "",
  187.             0.0066,
  188.             4,
  189.             "30/360",
  190.             dtEffective,
  191.             dtMaturity,
  192.             null,
  193.             null
  194.         );

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

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

  198.         MergedDiscountForwardCurve mdfc = LatentMarketStateBuilder.SmoothFundingCurve (
  199.             dtSpot,
  200.             strCurrency,
  201.             astrDepositTenor,
  202.             adblDepositQuote,
  203.             "ForwardRate",
  204.             adblFuturesQuote,
  205.             "ForwardRate",
  206.             astrFixFloatTenor,
  207.             adblFixFloatQuote,
  208.             "SwapRate"
  209.         );

  210.         CurveSurfaceQuoteContainer csqc = MarketParamsBuilder.Create (
  211.             mdfc,
  212.             null,
  213.             null,
  214.             null,
  215.             null,
  216.             null,
  217.             null
  218.         );

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

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

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

  222.         double dblYield = bond.yieldFromPrice (
  223.             ValuationParams.Spot (dtSpot.julian()),
  224.             csqc,
  225.             null,
  226.             dblCleanPrice
  227.         );

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

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

  230.         System.out.println ("Price Out : " +
  231.             bond.priceFromYield (
  232.                 ValuationParams.Spot (dtSpot.julian()),
  233.                 csqc,
  234.                 null,
  235.                 dblYield
  236.             )
  237.         );

  238.         System.out.println();

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  255.         for (CompositePeriod p : bond.couponPeriods()) {
  256.             int iEndDate = p.endDate();

  257.             int iPayDate = p.payDate();

  258.             int iStartDate = p.startDate();

  259.             double dblCouponRate = bond.couponMetrics (
  260.                 iPayDate,
  261.                 valParams,
  262.                 csqc
  263.             ).rate();

  264.             double dblCouponDCF = p.couponDCF();

  265.             System.out.println ("\t|| " +
  266.                 DateUtil.YYYYMMDD (iStartDate) + " => " +
  267.                 DateUtil.YYYYMMDD (iEndDate) + " | ? | " +
  268.                 p.fundingLabel().fullyQualifiedName() + " | " +
  269.                 p.floaterLabel().fullyQualifiedName() + " | " +
  270.                 FormatUtil.FormatDouble (dblCouponRate, 1, 2, 100.) + "% | " +
  271.                 FormatUtil.FormatDouble (dblCouponDCF, 1, 4, 1.) + " | " +
  272.                 FormatUtil.FormatDouble (dblCouponRate * dblCouponDCF * p.notional (iEndDate) * p.couponFactor (iEndDate), 1, 4, 1.) + " | " +
  273.                 FormatUtil.FormatDouble (p.notional (iStartDate) - p.notional (iEndDate), 1, 4, 1.) + " | " +
  274.                 FormatUtil.FormatDouble (p.df (csqc), 1, 4, 1.) + " | " +
  275.                 FormatUtil.FormatDouble (p.survival (csqc), 1, 4, 1.) + " | " +
  276.                 FormatUtil.FormatDouble (p.recovery (csqc), 2, 0, 100.) + "% ||"
  277.             );
  278.         }

  279.         System.out.println ("\t|| " +
  280.             DateUtil.YYYYMMDD (dtEffective.julian()) + " => " +
  281.             DateUtil.YYYYMMDD (dtMaturity.julian()) + " | ? | " +
  282.             bond.fundingLabel().fullyQualifiedName() + " | " +
  283.             bond.forwardLabel().get (bond.name()).fullyQualifiedName() + " | " +
  284.             FormatUtil.FormatDouble (0., 1, 2, 100.) + "% | " +
  285.             FormatUtil.FormatDouble (0., 1, 4, 1.) + " | " +
  286.             FormatUtil.FormatDouble (0., 1, 4, 1.) + " | " +
  287.             FormatUtil.FormatDouble (bond.notional (dtMaturity.julian()), 1, 4, 1.) + " | " +
  288.             FormatUtil.FormatDouble (mdfc.df (dtMaturity), 1, 4, 1.) + " | " +
  289.             FormatUtil.FormatDouble (1., 1, 4, 1.) + " | " +
  290.             FormatUtil.FormatDouble (1., 2, 0, 100.) + "% ||"
  291.         );

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

  293.         EnvManager.TerminateEnv();
  294.     }
  295. }