Rajahmundry.java

  1. package org.drip.sample.bondmetrics;

  2. import org.drip.analytics.cashflow.CompositePeriod;
  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.product.params.EmbeddedOptionSchedule;
  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>Rajahmundry</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 Rajahmundry {

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

  111.         if (null != eosCall) bond.setEmbeddedCallSchedule (eosCall);
  112.     }

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

  121.         JulianDate dtSpot = DateUtil.CreateFromYMD (
  122.             2017,
  123.             DateUtil.JULY,
  124.             10
  125.         );

  126.         String[] astrDepositTenor = new String[] {
  127.             "2D"
  128.         };

  129.         double[] adblDepositQuote = new double[] {
  130.             0.0130411 // 2D
  131.         };

  132.         double[] adblFuturesQuote = new double[] {
  133.             0.01345,    // 98.655
  134.             0.01470,    // 98.530
  135.             0.01575,    // 98.425
  136.             0.01660,    // 98.340
  137.             0.01745,    // 98.255
  138.             0.01845     // 98.155
  139.         };

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

  159.         String[] astrGovvieTenor = new String[] {
  160.             "1Y",
  161.             "2Y",
  162.             "3Y",
  163.             "5Y",
  164.             "7Y",
  165.             "10Y",
  166.             "20Y",
  167.             "30Y"
  168.         };

  169.         double[] adblFixFloatQuote = new double[] {
  170.             0.016410, //  2Y
  171.             0.017863, //  3Y
  172.             0.019030, //  4Y
  173.             0.020035, //  5Y
  174.             0.020902, //  6Y
  175.             0.021660, //  7Y
  176.             0.022307, //  8Y
  177.             0.022879, //  9Y
  178.             0.023363, // 10Y
  179.             0.023820, // 11Y
  180.             0.024172, // 12Y
  181.             0.024934, // 15Y
  182.             0.025581, // 20Y
  183.             0.025906, // 25Y
  184.             0.025973, // 30Y
  185.             0.025838, // 40Y
  186.             0.025560  // 50Y
  187.         };

  188.         double[] adblGovvieYield = new double[] {
  189.             0.01219, //  1Y
  190.             0.01391, //  2Y
  191.             0.01590, //  3Y
  192.             0.01937, //  5Y
  193.             0.02200, //  7Y
  194.             0.02378, // 10Y
  195.             0.02677, // 20Y
  196.             0.02927  // 30Y
  197.         };

  198.         String[] astrCreditTenor = new String[] {
  199.             "06M",
  200.             "01Y",
  201.             "02Y",
  202.             "03Y",
  203.             "04Y",
  204.             "05Y",
  205.             "07Y",
  206.             "10Y"
  207.         };

  208.         double[] adblCreditQuote = new double[] {
  209.              60.,   //  6M
  210.              68.,   //  1Y
  211.              88.,   //  2Y
  212.             102.,   //  3Y
  213.             121.,   //  4Y
  214.             138.,   //  5Y
  215.             168.,   //  7Y
  216.             188.    // 10Y
  217.         };

  218.         double dblFX = 1.;
  219.         int iSettleLag = 3;
  220.         int iCouponFreq = 2;
  221.         String strName = "Rajahmundry";
  222.         double dblCleanPrice = 0.97;
  223.         double dblIssuePrice = 1.0;
  224.         String strCurrency = "USD";
  225.         double dblSpreadBump = 20.;
  226.         double dblCouponRate = 0.07750;
  227.         double dblIssueAmount = 5.25e08;
  228.         String strTreasuryCode = "UST";
  229.         String strCouponDayCount = "30/360";
  230.         double dblSpreadDurationMultiplier = 5.;

  231.         JulianDate dtEffective = DateUtil.CreateFromYMD (
  232.             2017,
  233.             6,
  234.             30
  235.         );

  236.         JulianDate dtMaturity = DateUtil.CreateFromYMD (
  237.             2025,
  238.             6,
  239.             15
  240.         );

  241.         BondComponent bond = BondBuilder.CreateSimpleFixedFP (
  242.             strName,
  243.             strCurrency,
  244.             strName,
  245.             dblCouponRate,
  246.             iCouponFreq,
  247.             strCouponDayCount,
  248.             dtEffective,
  249.             dtMaturity,
  250.             DateUtil.CreateFromYMD (
  251.                 2017,
  252.                 12,
  253.                 15
  254.             ).julian(),
  255.             DateUtil.CreateFromYMD (
  256.                 2024,
  257.                 12,
  258.                 15
  259.             ).julian(),
  260.             null,
  261.             null,
  262.             null,
  263.             null,
  264.             null,
  265.             null,
  266.             null,
  267.             null,
  268.             null,
  269.             null
  270.         );

  271.         SetEOS (
  272.             bond,
  273.             new EmbeddedOptionSchedule (
  274.                 new int[] {
  275.                     DateUtil.CreateFromYMD (2020, 06, 15).julian(),
  276.                     DateUtil.CreateFromYMD (2021, 06, 15).julian(),
  277.                     DateUtil.CreateFromYMD (2022, 06, 15).julian(),
  278.                     DateUtil.CreateFromYMD (2023, 06, 15).julian(),
  279.                 },
  280.                 new double[] {
  281.                     1.03875,
  282.                     1.01938,
  283.                     1.00969,
  284.                     1.00000,
  285.                 },
  286.                 false,
  287.                 15,
  288.                 false,
  289.                 Double.NaN,
  290.                 "",
  291.                 Double.NaN
  292.             ),
  293.             null
  294.         );

  295.         BondReplicator abr = BondReplicator.CorporateSenior (
  296.             dblCleanPrice,
  297.             dblIssuePrice,
  298.             dblIssueAmount,
  299.             dtSpot,
  300.             astrDepositTenor,
  301.             adblDepositQuote,
  302.             adblFuturesQuote,
  303.             astrFixFloatTenor,
  304.             adblFixFloatQuote,
  305.             dblSpreadBump,
  306.             dblSpreadDurationMultiplier,
  307.             strTreasuryCode,
  308.             astrGovvieTenor,
  309.             adblGovvieYield,
  310.             astrCreditTenor,
  311.             adblCreditQuote,
  312.             dblFX,
  313.             Double.NaN,
  314.             iSettleLag,
  315.             bond
  316.         );

  317.         BondReplicationRun abrr = abr.generateRun();

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

  319.         MergedDiscountForwardCurve mdfc = LatentMarketStateBuilder.SmoothFundingCurve (
  320.             dtSpot,
  321.             strCurrency,
  322.             astrDepositTenor,
  323.             adblDepositQuote,
  324.             "ForwardRate",
  325.             adblFuturesQuote,
  326.             "ForwardRate",
  327.             astrFixFloatTenor,
  328.             adblFixFloatQuote,
  329.             "SwapRate"
  330.         );

  331.         CurveSurfaceQuoteContainer csqc = MarketParamsBuilder.Create (
  332.             mdfc,
  333.             null,
  334.             null,
  335.             null,
  336.             null,
  337.             null,
  338.             null
  339.         );

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

  341.         System.out.println();

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  358.         for (CompositePeriod p : bond.couponPeriods()) {
  359.             int iEndDate = p.endDate();

  360.             int iPayDate = p.payDate();

  361.             int iStartDate = p.startDate();

  362.             dblCouponRate = bond.couponMetrics (
  363.                 iPayDate,
  364.                 valParams,
  365.                 csqc
  366.             ).rate();

  367.             double dblCouponDCF = p.couponDCF();

  368.             System.out.println ("\t|| " +
  369.                 DateUtil.YYYYMMDD (iStartDate) + " => " +
  370.                 DateUtil.YYYYMMDD (iEndDate) + " | ? | " +
  371.                 p.fundingLabel().fullyQualifiedName() + " | NONE | " +
  372.                 FormatUtil.FormatDouble (dblCouponRate, 1, 2, 100.) + "% | " +
  373.                 FormatUtil.FormatDouble (dblCouponDCF, 1, 4, 1.) + " | " +
  374.                 FormatUtil.FormatDouble (dblCouponRate * dblCouponDCF * p.notional (iEndDate) * p.couponFactor (iEndDate), 1, 4, 1.) + " | " +
  375.                 FormatUtil.FormatDouble (p.notional (iStartDate) - p.notional (iEndDate), 1, 4, 1.) + " | " +
  376.                 FormatUtil.FormatDouble (1., 1, 4, 1.) + " | " +
  377.                 FormatUtil.FormatDouble (1., 2, 2, 100.) + "% ||"
  378.             );
  379.         }

  380.         System.out.println ("\t|| " +
  381.             DateUtil.YYYYMMDD (dtEffective.julian()) + " => " +
  382.             DateUtil.YYYYMMDD (dtMaturity.julian()) + " | ? | " +
  383.             bond.fundingLabel().fullyQualifiedName() + " | NONE | " +
  384.             FormatUtil.FormatDouble (0., 1, 2, 100.) + "% | " +
  385.             FormatUtil.FormatDouble (0., 1, 4, 1.) + " | " +
  386.             FormatUtil.FormatDouble (0., 1, 4, 1.) + " | " +
  387.             FormatUtil.FormatDouble (bond.notional (dtMaturity.julian()), 1, 4, 1.) + " | " +
  388.             FormatUtil.FormatDouble (mdfc.df (dtMaturity), 1, 4, 1.) + " | " +
  389.             FormatUtil.FormatDouble (1., 1, 4, 1.) + " | " +
  390.             FormatUtil.FormatDouble (1., 2, 2, 100.) + "% ||"
  391.         );

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

  393.         EnvManager.TerminateEnv();
  394.     }
  395. }