Gopalpur.java

  1. package org.drip.sample.bondmetrics;

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

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

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

  89. /**
  90.  * <i>Gopalpur</i> generates the Full Suite of Replication Metrics for a Sample Bond.
  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/bondmetrics/README.md">Bond Relative Value Replication Demonstration</a></li>
  98.  *  </ul>
  99.  * <br><br>
  100.  *
  101.  * @author Lakshmi Krishnamurthy
  102.  */

  103. public class Gopalpur {

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

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

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

  120.         double[] adblDepositQuote = new double[] {
  121.             0.0130411 // 2D
  122.         };

  123.         double[] adblFuturesQuote = new double[] {
  124.             0.01345,    // 98.655
  125.             0.01470,    // 98.530
  126.             0.01575,    // 98.425
  127.             0.01660,    // 98.340
  128.             0.01745,    // 98.255
  129.             0.01845     // 98.155
  130.         };

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

  150.         String[] astrGovvieTenor = new String[] {
  151.             "1Y",
  152.             "2Y",
  153.             "3Y",
  154.             "5Y",
  155.             "7Y",
  156.             "10Y",
  157.             "20Y",
  158.             "30Y"
  159.         };

  160.         double[] adblFixFloatQuote = new double[] {
  161.             0.016410, //  2Y
  162.             0.017863, //  3Y
  163.             0.019030, //  4Y
  164.             0.020035, //  5Y
  165.             0.020902, //  6Y
  166.             0.021660, //  7Y
  167.             0.022307, //  8Y
  168.             0.022879, //  9Y
  169.             0.023363, // 10Y
  170.             0.023820, // 11Y
  171.             0.024172, // 12Y
  172.             0.024934, // 15Y
  173.             0.025581, // 20Y
  174.             0.025906, // 25Y
  175.             0.025973, // 30Y
  176.             0.025838, // 40Y
  177.             0.025560  // 50Y
  178.         };

  179.         double[] adblGovvieYield = new double[] {
  180.             0.01219, //  1Y
  181.             0.01391, //  2Y
  182.             0.01590, //  3Y
  183.             0.01937, //  5Y
  184.             0.02200, //  7Y
  185.             0.02378, // 10Y
  186.             0.02677, // 20Y
  187.             0.02927  // 30Y
  188.         };

  189.         String[] astrCreditTenor = new String[] {
  190.             "06M",
  191.             "01Y",
  192.             "02Y",
  193.             "03Y",
  194.             "04Y",
  195.             "05Y",
  196.             "07Y",
  197.             "10Y"
  198.         };

  199.         double[] adblCreditQuote = new double[] {
  200.              60.,   //  6M
  201.              68.,   //  1Y
  202.              88.,   //  2Y
  203.             102.,   //  3Y
  204.             121.,   //  4Y
  205.             138.,   //  5Y
  206.             168.,   //  7Y
  207.             188.    // 10Y
  208.         };

  209.         double dblFX = 1.;
  210.         int iSettleLag = 3;
  211.         int iCouponFreq = 2;
  212.         String strName = "Gopalpur";
  213.         double dblCleanPrice = 1.0;
  214.         double dblIssuePrice = 1.0;
  215.         String strCurrency = "USD";
  216.         double dblSpreadBump = 20.;
  217.         double dblCouponRate = 0.06875;
  218.         double dblIssueAmount = 5.25e08;
  219.         String strTreasuryCode = "UST";
  220.         String strCouponDayCount = "30/360";
  221.         double dblSpreadDurationMultiplier = 5.;

  222.         JulianDate dtEffective = DateUtil.CreateFromYMD (
  223.             2017,
  224.             2,
  225.             16
  226.         );

  227.         JulianDate dtMaturity = DateUtil.CreateFromYMD (
  228.             2025,
  229.             2,
  230.             15
  231.         );

  232.         BondComponent bond = BondBuilder.CreateSimpleFixedFP (
  233.             strName,
  234.             strCurrency,
  235.             strName,
  236.             dblCouponRate,
  237.             iCouponFreq,
  238.             strCouponDayCount,
  239.             dtEffective,
  240.             dtMaturity,
  241.             DateUtil.CreateFromYMD (
  242.                 2017,
  243.                 8,
  244.                 15
  245.             ).julian(),
  246.             DateUtil.CreateFromYMD (
  247.                 2024,
  248.                 8,
  249.                 15
  250.             ).julian(),
  251.             new DateAdjustParams (
  252.                 Convention.DATE_ROLL_FOLLOWING,
  253.                 0,
  254.                 strCurrency
  255.             ),
  256.             null,
  257.             null,
  258.             null,
  259.             null,
  260.             null,
  261.             null,
  262.             null,
  263.             null,
  264.             null
  265.         );

  266.         BondReplicator abr = BondReplicator.CorporateSenior (
  267.             dblCleanPrice,
  268.             dblIssuePrice,
  269.             dblIssueAmount,
  270.             dtSpot,
  271.             astrDepositTenor,
  272.             adblDepositQuote,
  273.             adblFuturesQuote,
  274.             astrFixFloatTenor,
  275.             adblFixFloatQuote,
  276.             dblSpreadBump,
  277.             dblSpreadDurationMultiplier,
  278.             strTreasuryCode,
  279.             astrGovvieTenor,
  280.             adblGovvieYield,
  281.             astrCreditTenor,
  282.             adblCreditQuote,
  283.             dblFX,
  284.             Double.NaN,
  285.             iSettleLag,
  286.             bond
  287.         );

  288.         BondReplicationRun abrr = abr.generateRun();

  289.         System.out.println (abrr.display());

  290.         MergedDiscountForwardCurve mdfc = LatentMarketStateBuilder.SmoothFundingCurve (
  291.             dtSpot,
  292.             strCurrency,
  293.             astrDepositTenor,
  294.             adblDepositQuote,
  295.             "ForwardRate",
  296.             adblFuturesQuote,
  297.             "ForwardRate",
  298.             astrFixFloatTenor,
  299.             adblFixFloatQuote,
  300.             "SwapRate"
  301.         );

  302.         CurveSurfaceQuoteContainer csqc = MarketParamsBuilder.Create (
  303.             mdfc,
  304.             null,
  305.             null,
  306.             null,
  307.             null,
  308.             null,
  309.             null
  310.         );

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

  312.         System.out.println();

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  331.         for (CompositePeriod p : bond.couponPeriods()) {
  332.             int iEndDate = p.endDate();

  333.             int iPayDate = p.payDate();

  334.             int iStartDate = p.startDate();

  335.             dblCouponRate = bond.couponMetrics (
  336.                 iEndDate,
  337.                 valParams,
  338.                 csqc
  339.             ).rate();

  340.             double dblCouponDCF = p.couponDCF();

  341.             System.out.println ("\t|| " +
  342.                 DateUtil.YYYYMMDD (iStartDate) + " => " +
  343.                 DateUtil.YYYYMMDD (iEndDate) + " | " +
  344.                 DateUtil.YYYYMMDD (iPayDate) + " | ? | " +
  345.                 p.fundingLabel().fullyQualifiedName() + " | ? | " +
  346.                 FormatUtil.FormatDouble (dblCouponRate, 1, 3, 100.) + "% | " +
  347.                 FormatUtil.FormatDouble (dblCouponDCF, 1, 4, 1.) + " | " +
  348.                 FormatUtil.FormatDouble (dblCouponRate * dblCouponDCF * p.notional (iEndDate) * p.couponFactor (iEndDate), 1, 6, 1.) + " | " +
  349.                 FormatUtil.FormatDouble (p.notional (iStartDate) - p.notional (iEndDate), 1, 4, 1.) + " | " +
  350.                 FormatUtil.FormatDouble (1., 1, 4, 1.) + " | " +
  351.                 FormatUtil.FormatDouble (1., 2, 2, 100.) + "% ||"
  352.             );
  353.         }

  354.         System.out.println ("\t|| " +
  355.             DateUtil.YYYYMMDD (dtEffective.julian()) + " => " +
  356.             DateUtil.YYYYMMDD (bond.maturityDate().julian()) + " | " +
  357.             DateUtil.YYYYMMDD (bond.maturityPayDate().julian()) + " | ? | " +
  358.             bond.fundingLabel().fullyQualifiedName() + " | ? | " +
  359.             FormatUtil.FormatDouble (0., 1, 3, 100.) + "% | " +
  360.             FormatUtil.FormatDouble (0., 1, 4, 1.) + " | " +
  361.             FormatUtil.FormatDouble (0., 1, 6, 1.) + " | " +
  362.             FormatUtil.FormatDouble (bond.notional (bond.maturityDate().julian()), 1, 4, 1.) + " | " +
  363.             FormatUtil.FormatDouble (1., 1, 4, 1.) + " | " +
  364.             FormatUtil.FormatDouble (1., 2, 2, 100.) + "% ||"
  365.         );

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

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