Patiala.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>Patiala</i> generates the Full Suite of Replication Metrics for a Sample Bond.
  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 Patiala {

  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.JULY,
  114.             10
  115.         );

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

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

  122.         double[] adblFuturesQuote = new double[] {
  123.             0.01345,    // 98.655
  124.             0.01470,    // 98.530
  125.             0.01575,    // 98.425
  126.             0.01660,    // 98.340
  127.             0.01745,    // 98.255
  128.             0.01845     // 98.155
  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.016410, //  2Y
  151.             0.017863, //  3Y
  152.             0.019030, //  4Y
  153.             0.020035, //  5Y
  154.             0.020902, //  6Y
  155.             0.021660, //  7Y
  156.             0.022307, //  8Y
  157.             0.022879, //  9Y
  158.             0.023363, // 10Y
  159.             0.023820, // 11Y
  160.             0.024172, // 12Y
  161.             0.024934, // 15Y
  162.             0.025581, // 20Y
  163.             0.025906, // 25Y
  164.             0.025973, // 30Y
  165.             0.025838, // 40Y
  166.             0.025560  // 50Y
  167.         };

  168.         String strName = "Patiala";
  169.         String strCurrency = "USD";
  170.         double dblSpread = 0.0113;
  171.         double dblResetRate = 0.0244639 - dblSpread;

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

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

  182.         BondComponent bond = BondBuilder.CreateSimpleFloaterFP (
  183.             strName,
  184.             "USD",
  185.             "USD-3M",
  186.             "",
  187.             dblSpread,
  188.             4,
  189.             "Act/360",
  190.             dtEffective,
  191.             dtMaturity,
  192.             DateUtil.CreateFromYMD (
  193.                 2016,
  194.                 11,
  195.                 21
  196.             ).julian(),
  197.             DateUtil.CreateFromYMD (
  198.                 2019,
  199.                 5,
  200.                 20
  201.             ).julian(),
  202.             null,
  203.             null,
  204.             null,
  205.             null,
  206.             null,
  207.             null,
  208.             null,
  209.             null,
  210.             null,
  211.             null
  212.         );

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

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

  216.         MergedDiscountForwardCurve mdfc = LatentMarketStateBuilder.SmoothFundingCurve (
  217.             dtSpot,
  218.             strCurrency,
  219.             astrDepositTenor,
  220.             adblDepositQuote,
  221.             "ForwardRate",
  222.             adblFuturesQuote,
  223.             "ForwardRate",
  224.             astrFixFloatTenor,
  225.             adblFixFloatQuote,
  226.             "SwapRate"
  227.         );

  228.         CurveSurfaceQuoteContainer csqc = MarketParamsBuilder.Create (
  229.             mdfc,
  230.             null,
  231.             null,
  232.             null,
  233.             null,
  234.             null,
  235.             null
  236.         );

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

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

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

  240.         System.out.println();

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  257.         for (CompositePeriod p : bond.couponPeriods()) {
  258.             int iEndDate = p.endDate();

  259.             int iPayDate = p.payDate();

  260.             int iStartDate = p.startDate();

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

  266.             double dblCouponDCF = p.couponDCF();

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

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

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

  291.         EnvManager.TerminateEnv();
  292.     }
  293. }