Dewas.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.service.env.EnvManager;
  10. import org.drip.service.scenario.*;
  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>Dewas</i> demonstrates the Analytics Calculation/Reconciliation for the Floater Dewas.
  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 Dewas {

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

  108.         JulianDate dtSpot = DateUtil.CreateFromYMD (
  109.             2017,
  110.             DateUtil.OCTOBER,
  111.             10
  112.         );

  113.         String[] astrDepositTenor = new String[] {
  114.             "2D"
  115.         };

  116.         double[] adblDepositQuote = new double[] {
  117.             0.0130411 // 2D
  118.         };

  119.         double[] adblFuturesQuote = new double[] {
  120.             0.01345,    // 98.655
  121.             0.01470,    // 98.530
  122.             0.01575,    // 98.425
  123.             0.01660,    // 98.340
  124.             0.01745,    // 98.255
  125.             0.01845     // 98.155
  126.         };

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

  146.         double[] adblFixFloatQuote = new double[] {
  147.             0.016410, //  2Y
  148.             0.017863, //  3Y
  149.             0.019030, //  4Y
  150.             0.020035, //  5Y
  151.             0.020902, //  6Y
  152.             0.021660, //  7Y
  153.             0.022307, //  8Y
  154.             0.022879, //  9Y
  155.             0.023363, // 10Y
  156.             0.023820, // 11Y
  157.             0.024172, // 12Y
  158.             0.024934, // 15Y
  159.             0.025581, // 20Y
  160.             0.025906, // 25Y
  161.             0.025973, // 30Y
  162.             0.025838, // 40Y
  163.             0.025560  // 50Y
  164.         };

  165.         String[] astrGovvieTenor = new String[] {
  166.             "1Y",
  167.             "2Y",
  168.             "3Y",
  169.             "5Y",
  170.             "7Y",
  171.             "10Y",
  172.             "20Y",
  173.             "30Y"
  174.         };

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

  185.         String[] astrCreditTenor = new String[] {
  186.             "06M",
  187.             "01Y",
  188.             "02Y",
  189.             "03Y",
  190.             "04Y",
  191.             "05Y",
  192.             "07Y",
  193.             "10Y"
  194.         };

  195.         double[] adblCreditQuote = new double[] {
  196.              10.,   //  6M
  197.              12.,   //  1Y
  198.              15.,   //  2Y
  199.              19.,   //  3Y
  200.              24.,   //  4Y
  201.              28.,   //  5Y
  202.              38.,   //  7Y
  203.              51.    // 10Y
  204.         };

  205.         double dblFX = 1;
  206.         int iSettleLag = 3;
  207.         double dblSpread = 0.0078;
  208.         String strCurrency = "USD";
  209.         double dblCleanPrice = 1.00592;
  210.         double dblIssuePrice = 1.0;
  211.         double dblSpreadBump = 20.;
  212.         String strTreasuryCode = "UST";
  213.         double dblIssueAmount = 7.50e8;
  214.         double dblSpreadDurationMultiplier = 5.;
  215.         double dblResetRate = 0.025152 - dblSpread;

  216.         JulianDate dtEffective = DateUtil.CreateFromYMD (
  217.             2016,
  218.             1,
  219.             14
  220.         );

  221.         JulianDate dtMaturity = DateUtil.CreateFromYMD (
  222.             2019,
  223.             1,
  224.             14
  225.         );

  226.         BondComponent bond = BondBuilder.CreateSimpleFloater (
  227.             "Dewas",
  228.             "USD",
  229.             "USD-3M",
  230.             "Dewas",
  231.             dblSpread,
  232.             4,
  233.             "Act/360",
  234.             dtEffective,
  235.             dtMaturity,
  236.             null,
  237.             null
  238.         );

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

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

  242.         MergedDiscountForwardCurve mdfc = LatentMarketStateBuilder.SmoothFundingCurve (
  243.             dtSpot,
  244.             strCurrency,
  245.             astrDepositTenor,
  246.             adblDepositQuote,
  247.             "ForwardRate",
  248.             adblFuturesQuote,
  249.             "ForwardRate",
  250.             astrFixFloatTenor,
  251.             adblFixFloatQuote,
  252.             "SwapRate"
  253.         );

  254.         BondReplicator abr = BondReplicator.CorporateSenior (
  255.             dblCleanPrice,
  256.             dblIssuePrice,
  257.             dblIssueAmount,
  258.             dtSpot,
  259.             astrDepositTenor,
  260.             adblDepositQuote,
  261.             adblFuturesQuote,
  262.             astrFixFloatTenor,
  263.             adblFixFloatQuote,
  264.             dblSpreadBump,
  265.             dblSpreadDurationMultiplier,
  266.             strTreasuryCode,
  267.             astrGovvieTenor,
  268.             adblGovvieYield,
  269.             astrCreditTenor,
  270.             adblCreditQuote,
  271.             dblFX,
  272.             dblResetRate,
  273.             iSettleLag,
  274.             bond
  275.         );

  276.         BondReplicationRun abrr = abr.generateRun();

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

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

  279.         System.out.println();

  280.         CurveSurfaceQuoteContainer csqc = abr.creditBaseCSQC();

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

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

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

  284.         double dblYield = bond.yieldFromPrice (
  285.             ValuationParams.Spot (dtSpot.julian()),
  286.             csqc,
  287.             null,
  288.             dblCleanPrice
  289.         );

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

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

  292.         System.out.println ("Price Out : " +
  293.             bond.priceFromYield (
  294.                 ValuationParams.Spot (dtSpot.julian()),
  295.                 csqc,
  296.                 null,
  297.                 dblYield
  298.             )
  299.         );

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  316.         for (CompositePeriod p : bond.couponPeriods()) {
  317.             int iEndDate = p.endDate();

  318.             int iPayDate = p.payDate();

  319.             int iStartDate = p.startDate();

  320.             double dblCouponRate = bond.couponMetrics (
  321.                 iPayDate,
  322.                 valParams,
  323.                 csqc
  324.             ).rate();

  325.             double dblCouponDCF = p.couponDCF();

  326.             System.out.println ("\t|| " +
  327.                 DateUtil.YYYYMMDD (iStartDate) + " => " +
  328.                 DateUtil.YYYYMMDD (iEndDate) + " | ? | " +
  329.                 p.fundingLabel().fullyQualifiedName() + " | " +
  330.                 p.floaterLabel().fullyQualifiedName() + " | " +
  331.                 FormatUtil.FormatDouble (dblCouponRate, 1, 2, 100.) + "% | " +
  332.                 FormatUtil.FormatDouble (dblCouponDCF, 1, 4, 1.) + " | " +
  333.                 FormatUtil.FormatDouble (dblCouponRate * dblCouponDCF * p.notional (iEndDate) * p.couponFactor (iEndDate), 1, 4, 1.) + " | " +
  334.                 FormatUtil.FormatDouble (p.notional (iStartDate) - p.notional (iEndDate), 1, 4, 1.) + " | " +
  335.                 FormatUtil.FormatDouble (p.df (csqc), 1, 4, 1.) + " | " +
  336.                 FormatUtil.FormatDouble (p.survival (csqc), 1, 4, 1.) + " | " +
  337.                 FormatUtil.FormatDouble (p.recovery (csqc), 2, 0, 100.) + "% ||"
  338.             );
  339.         }

  340.         System.out.println ("\t|| " +
  341.             DateUtil.YYYYMMDD (dtEffective.julian()) + " => " +
  342.             DateUtil.YYYYMMDD (dtMaturity.julian()) + " | ? | " +
  343.             bond.fundingLabel().fullyQualifiedName() + " | " +
  344.             bond.forwardLabel().get (bond.name()).fullyQualifiedName() + " | " +
  345.             FormatUtil.FormatDouble (0., 1, 2, 100.) + "% | " +
  346.             FormatUtil.FormatDouble (0., 1, 4, 1.) + " | " +
  347.             FormatUtil.FormatDouble (0., 1, 4, 1.) + " | " +
  348.             FormatUtil.FormatDouble (bond.notional (dtMaturity.julian()), 1, 4, 1.) + " | " +
  349.             FormatUtil.FormatDouble (mdfc.df (dtMaturity), 1, 4, 1.) + " | " +
  350.             FormatUtil.FormatDouble (1., 1, 4, 1.) + " | " +
  351.             FormatUtil.FormatDouble (1., 2, 0, 100.) + "% ||"
  352.         );

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

  354.         System.out.println();
  355.     }
  356. }