Muzaffarpur.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.market.CurveSurfaceQuoteContainer;
  6. import org.drip.param.valuation.ValuationParams;
  7. import org.drip.product.creator.BondBuilder;
  8. import org.drip.product.credit.BondComponent;
  9. import org.drip.product.params.EmbeddedOptionSchedule;
  10. import org.drip.service.env.EnvManager;
  11. import org.drip.service.scenario.*;
  12. import org.drip.service.template.LatentMarketStateBuilder;
  13. import org.drip.state.discount.MergedDiscountForwardCurve;
  14. import org.drip.state.identifier.FloaterLabel;

  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>Muzaffarpur</i> demonstrates the Analytics Calculation/Reconciliation for the Bond Muzaffarpur.
  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 Muzaffarpur {

  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.SEPTEMBER,
  124.             21
  125.         );

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

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

  132.         double[] adblFuturesQuote = new double[] {
  133.             0.01495,
  134.             0.0159,
  135.             0.01675,
  136.             0.01745,
  137.             0.0183,
  138.             0.01875
  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.             "1M",
  161.             "3M",
  162.             "6M",
  163.             "1Y",
  164.             "2Y",
  165.             "3Y",
  166.             "5Y",
  167.             "7Y",
  168.             "10Y",
  169.             "15Y",
  170.             "20Y",
  171.             "30Y"
  172.         };

  173.         double[] adblFixFloatQuote = new double[] {
  174.             0.017013,
  175.             0.018047,
  176.             0.018867,
  177.             0.019587,
  178.             0.02024,
  179.             0.020881,
  180.             0.021436,
  181.             0.021904,
  182.             0.022332,
  183.             0.022728,
  184.             0.023076,
  185.             0.023804,
  186.             0.024512,
  187.             0.024828,
  188.             0.024873,
  189.             0.024775,
  190.             0.024462
  191.         };

  192.         double[] adblGovvieYield = new double[] {
  193.             0.0099,
  194.             0.0104,
  195.             0.0119,
  196.             0.0131,
  197.             0.0145,
  198.             0.0159,
  199.             0.0189,
  200.             0.0211,
  201.             0.0227,
  202.             0.02397,
  203.             0.0257,
  204.             0.028
  205.         };

  206.         String[] astrCreditTenor = new String[] {
  207.             "06M",
  208.             "01Y",
  209.             "02Y",
  210.             "03Y",
  211.             "04Y",
  212.             "05Y",
  213.             "07Y",
  214.             "10Y"
  215.         };

  216.         double[] adblCreditQuote = new double[] {
  217.              60.,   //  6M
  218.              68.,   //  1Y
  219.              88.,   //  2Y
  220.             102.,   //  3Y
  221.             121.,   //  4Y
  222.             138.,   //  5Y
  223.             168.,   //  7Y
  224.             188.    // 10Y
  225.         };

  226.         double dblFX = 1;
  227.         int iSettleLag = 3;
  228.         double dblSpread = 0.01;
  229.         String strCurrency = "USD";
  230.         double dblCleanPrice = 0.91734;
  231.         double dblIssuePrice = 1.0;
  232.         double dblSpreadBump = 20.;
  233.         String strTreasuryCode = "UST";
  234.         double dblIssueAmount = 7.50e8;
  235.         double dblSpreadDurationMultiplier = 5.;
  236.         double dblResetRate = 0.0232 - dblSpread;

  237.         JulianDate dtEffective = DateUtil.CreateFromYMD (
  238.             2007,
  239.             4,
  240.             30
  241.         );

  242.         JulianDate dtMaturity = DateUtil.CreateFromYMD (
  243.             2047,
  244.             6,
  245.             15
  246.         );

  247.         BondComponent bond = BondBuilder.CreateSimpleFloater (
  248.             "Muzaffarpur",
  249.             "USD",
  250.             "USD-3M",
  251.             "Muzaffarpur",
  252.             dblSpread,
  253.             4,
  254.             "Act/360",
  255.             dtEffective,
  256.             dtMaturity,
  257.             null,
  258.             null
  259.         );

  260.         SetEOS (
  261.             bond,
  262.             EmbeddedOptionSchedule.FromAmerican (
  263.                 dtSpot.julian(),
  264.                 new int[] {
  265.                     DateUtil.CreateFromYMD (2012, 06, 15).julian(),
  266.                     DateUtil.CreateFromYMD (2047, 06, 15).julian(),
  267.                 },
  268.                 new double[] {
  269.                     1.00000,
  270.                     1.00000,
  271.                 },
  272.                 false,
  273.                 30,
  274.                 30,
  275.                 false,
  276.                 Double.NaN,
  277.                 "",
  278.                 Double.NaN
  279.             ),
  280.             null
  281.         );

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

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

  285.         MergedDiscountForwardCurve mdfc = LatentMarketStateBuilder.SmoothFundingCurve (
  286.             dtSpot,
  287.             strCurrency,
  288.             astrDepositTenor,
  289.             adblDepositQuote,
  290.             "ForwardRate",
  291.             adblFuturesQuote,
  292.             "ForwardRate",
  293.             astrFixFloatTenor,
  294.             adblFixFloatQuote,
  295.             "SwapRate"
  296.         );

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

  319.         BondReplicationRun abrr = abr.generateRun();

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

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

  322.         System.out.println();

  323.         CurveSurfaceQuoteContainer csqc = abr.creditBaseCSQC();

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

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

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

  327.         double dblYield = bond.yieldFromPrice (
  328.             ValuationParams.Spot (dtSpot.julian()),
  329.             csqc,
  330.             null,
  331.             dblCleanPrice
  332.         );

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

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

  335.         System.out.println ("Price Out : " +
  336.             bond.priceFromYield (
  337.                 ValuationParams.Spot (dtSpot.julian()),
  338.                 csqc,
  339.                 null,
  340.                 dblYield
  341.             )
  342.         );

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  361.             int iPayDate = p.payDate();

  362.             int iStartDate = p.startDate();

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

  368.             double dblCouponDCF = p.couponDCF();

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

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

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

  397.         System.out.println();

  398.         EnvManager.TerminateEnv();
  399.     }
  400. }