Kakinada.java

  1. package org.drip.sample.loan;

  2. import org.drip.analytics.cashflow.*;
  3. import org.drip.analytics.date.*;
  4. import org.drip.market.definition.FloaterIndex;
  5. import org.drip.numerical.common.FormatUtil;
  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.scenario.*;

  12. /*
  13.  * -*- mode: java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  14.  */

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

  75. /**
  76.  * <i>Kakinada</i> demonstrates the Analytics Calculation/Reconciliation for the Loan Kakinada.
  77.  *  
  78.  * <br><br>
  79.  *  <ul>
  80.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/AnalyticsCore.md">Analytics Core Module</a></li>
  81.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/FixedIncomeAnalyticsLibrary.md">Fixed Income Analytics Library</a></li>
  82.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/README.md">Sample</a></li>
  83.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/loan/README.md">Loan Analytics</a></li>
  84.  *  </ul>
  85.  * <br><br>
  86.  *
  87.  * @author Lakshmi Krishnamurthy
  88.  */

  89. public class Kakinada {

  90.     private static final void PrintCashFlows (
  91.         final BondComponent bond,
  92.         final ValuationParams valParams,
  93.         final CurveSurfaceQuoteContainer csqc)
  94.         throws Exception
  95.     {
  96.         double dirtyPV = 0.;
  97.         double startDF = 1.;

  98.         FloaterIndex floaterIndex = bond.floaterSetting().fri().floaterIndex();

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

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

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

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

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

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

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

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

  107.         System.out.println ("\t||           - Period Start DF                                                                                          ||");

  108.         System.out.println ("\t||           - Period End DF                                                                                            ||");

  109.         System.out.println ("\t||           - Recalculated Rate                                                                                        ||");

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

  111.         for (CompositePeriod p : bond.couponPeriods()) {
  112.             int iEndDate = p.endDate();

  113.             int iPayDate = p.payDate();

  114.             int iStartDate = p.startDate() > valParams.valueDate() ? p.startDate() : valParams.valueDate();

  115.             double dblCouponRate = bond.couponMetrics (
  116.                 iPayDate,
  117.                 valParams,
  118.                 csqc
  119.             ).rate();

  120.             double dblCouponDCF = p.couponDCF();

  121.             double dblDCF = org.drip.analytics.daycount.Convention.YearFraction (
  122.                 iStartDate,
  123.                 iEndDate,
  124.                 floaterIndex.dayCount(),
  125.                 false,
  126.                 null,
  127.                 floaterIndex.calendar()
  128.             );

  129.             double endDF = p.df (csqc);

  130.             double dblCouponPV = dblCouponRate * dblDCF * endDF;

  131.             System.out.println ("\t|| " +
  132.                 DateUtil.YYYYMMDD (iStartDate) + " => " +
  133.                 DateUtil.YYYYMMDD (iEndDate) + " | " +
  134.                 FormatUtil.FormatDouble (dblCouponRate, 1, 4, 100.) + "% | " +
  135.                 FormatUtil.FormatDouble (dblCouponDCF, 1, 4, 1.) + " | " +
  136.                 FormatUtil.FormatDouble (startDF, 1, 6, 1.) + " | " +
  137.                 FormatUtil.FormatDouble (endDF, 1, 6, 1.) + " | " +
  138.                 FormatUtil.FormatDouble (dblDCF, 1, 4, 1.) + " | " +
  139.                 FormatUtil.FormatDouble (dblCouponPV, 1, 6, 1.) + " | " +
  140.                 FormatUtil.FormatDouble (((startDF / endDF) - 1.) / dblDCF, 1, 4, 100.) + "% | "
  141.             );

  142.             dirtyPV += dblCouponPV;
  143.             startDF = endDF;
  144.         }

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

  146.         System.out.println ("\t||                    PV : " + FormatUtil.FormatDouble (dirtyPV + startDF, 1, 6, 1.));

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

  148.         System.out.println();
  149.     }

  150.     public static final void main (
  151.         final String[] astArgs)
  152.         throws Exception
  153.     {
  154.         EnvManager.InitEnv ("");

  155.         JulianDate dtSpot = DateUtil.CreateFromYMD (
  156.             2017,
  157.             DateUtil.OCTOBER,
  158.             10
  159.         );

  160.         String[] astrDepositTenor = new String[] {
  161.             "1D"
  162.         };

  163.         double[] adblDepositQuote = new double[] {
  164.             0.01304  // 1D
  165.         };

  166.         double[] adblFuturesQuote = new double[] {
  167.             0.01345, // 98.655
  168.             0.01470, // 98.530
  169.             0.01575, // 98.425
  170.             0.01660, // 98.340
  171.             0.01745, // 98.255
  172.             0.01845  // 98.155
  173.         };

  174.         String[] astrFixFloatTenor = new String[] {
  175.             "02Y",
  176.             "03Y",
  177.             "04Y",
  178.             "05Y",
  179.             "06Y",
  180.             "07Y",
  181.             "08Y",
  182.             "09Y",
  183.             "10Y",
  184.             "11Y",
  185.             "12Y",
  186.             "15Y",
  187.             "20Y",
  188.             "25Y",
  189.             "30Y",
  190.             "40Y",
  191.             "50Y"
  192.         };

  193.         double[] adblFixFloatQuote = new double[] {
  194.             0.016410, //  2Y
  195.             0.017863, //  3Y
  196.             0.019030, //  4Y
  197.             0.020035, //  5Y
  198.             0.020902, //  6Y
  199.             0.021660, //  7Y
  200.             0.022307, //  8Y
  201.             0.022879, //  9Y
  202.             0.023363, // 10Y
  203.             0.023820, // 11Y
  204.             0.024172, // 12Y
  205.             0.024934, // 15Y
  206.             0.025581, // 20Y
  207.             0.025906, // 25Y
  208.             0.025973, // 30Y
  209.             0.025838, // 40Y
  210.             0.025560  // 50Y
  211.         };

  212.         String[] astrGovvieTenor = new String[] {
  213.             "1Y",
  214.             "2Y",
  215.             "3Y",
  216.             "5Y",
  217.             "7Y",
  218.             "10Y",
  219.             "20Y",
  220.             "30Y"
  221.         };

  222.         double[] adblGovvieYield = new double[] {
  223.             0.01219, //  1Y
  224.             0.01391, //  2Y
  225.             0.01590, //  3Y
  226.             0.01937, //  5Y
  227.             0.02200, //  7Y
  228.             0.02378, // 10Y
  229.             0.02677, // 20Y
  230.             0.02927  // 30Y
  231.         };

  232.         String[] astrCreditTenor = new String[] {
  233.             "06M",
  234.             "01Y",
  235.             "02Y",
  236.             "03Y",
  237.             "04Y",
  238.             "05Y",
  239.             "07Y",
  240.             "10Y"
  241.         };

  242.         double[] adblCreditQuote = new double[] {
  243.              10.,   //  6M
  244.              12.,   //  1Y
  245.              15.,   //  2Y
  246.              19.,   //  3Y
  247.              24.,   //  4Y
  248.              28.,   //  5Y
  249.              38.,   //  7Y
  250.              51.    // 10Y
  251.         };

  252.         double dblFX = 1;
  253.         int iSettleLag = 3;
  254.         double dblSpread = 0.0450;
  255.         double dblCleanPrice = 1.0;
  256.         double dblIssuePrice = 0.995;
  257.         double dblSpreadBump = 20.;
  258.         String strTreasuryCode = "UST";
  259.         double dblIssueAmount = 321500000.;
  260.         double dblSpreadDurationMultiplier = 5.;
  261.         double dblResetRate = adblDepositQuote[0];

  262.         JulianDate dtEffective = DateUtil.CreateFromYMD (
  263.             2017,
  264.             7,
  265.             26
  266.         );

  267.         JulianDate dtMaturity = DateUtil.CreateFromYMD (
  268.             2024,
  269.             1,
  270.             26
  271.         );

  272.         BondComponent bond = BondBuilder.CreateSimpleFloater (
  273.             "Kakinada",
  274.             "USD",
  275.             "USD-3M",
  276.             "Kakinada",
  277.             dblSpread,
  278.             4,
  279.             "Act/360",
  280.             dtEffective,
  281.             dtMaturity,
  282.             null,
  283.             null
  284.         );

  285.         BondReplicator abr = BondReplicator.CorporateLoan (
  286.             dblCleanPrice,
  287.             dblIssuePrice,
  288.             dblIssueAmount,
  289.             dtSpot,
  290.             astrDepositTenor,
  291.             adblDepositQuote,
  292.             adblFuturesQuote,
  293.             astrFixFloatTenor,
  294.             adblFixFloatQuote,
  295.             dblSpreadBump,
  296.             dblSpreadDurationMultiplier,
  297.             strTreasuryCode,
  298.             astrGovvieTenor,
  299.             adblGovvieYield,
  300.             astrCreditTenor,
  301.             adblCreditQuote,
  302.             dblFX,
  303.             dblResetRate,
  304.             iSettleLag,
  305.             bond
  306.         );

  307.         BondReplicationRun abrr = abr.generateRun();

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

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

  310.         System.out.println();

  311.         CurveSurfaceQuoteContainer csqc = abr.fundingBaseCSQC();

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

  313.         double dblYield = bond.yieldFromPrice (
  314.             ValuationParams.Spot (dtSpot.julian()),
  315.             csqc,
  316.             null,
  317.             dblCleanPrice
  318.         );

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

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

  321.         System.out.println ("Price Out : " +
  322.             bond.priceFromYield (
  323.                 valParams,
  324.                 csqc,
  325.                 null,
  326.                 dblYield
  327.             )
  328.         );

  329.         PrintCashFlows (
  330.             bond,
  331.             valParams,
  332.             csqc
  333.         );

  334.         EnvManager.TerminateEnv();
  335.     }
  336. }