Shivamogga.java

  1. package org.drip.sample.loan;

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

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

  91. public class Shivamogga {

  92.     public static final void main (
  93.         final String[] astArgs)
  94.         throws Exception
  95.     {
  96.         EnvManager.InitEnv ("");

  97.         JulianDate dtSpot = DateUtil.CreateFromYMD (
  98.             2017,
  99.             DateUtil.OCTOBER,
  100.             10
  101.         );

  102.         String[] astrDepositTenor = new String[] {
  103.             "2D"
  104.         };

  105.         double[] adblDepositQuote = new double[] {
  106.             0.0130411 // 2D
  107.         };

  108.         double[] adblFuturesQuote = new double[] {
  109.             0.01345,    // 98.655
  110.             0.01470,    // 98.530
  111.             0.01575,    // 98.425
  112.             0.01660,    // 98.340
  113.             0.01745,    // 98.255
  114.             0.01845     // 98.155
  115.         };

  116.         String[] astrFixFloatTenor = new String[] {
  117.             "02Y",
  118.             "03Y",
  119.             "04Y",
  120.             "05Y",
  121.             "06Y",
  122.             "07Y",
  123.             "08Y",
  124.             "09Y",
  125.             "10Y",
  126.             "11Y",
  127.             "12Y",
  128.             "15Y",
  129.             "20Y",
  130.             "25Y",
  131.             "30Y",
  132.             "40Y",
  133.             "50Y"
  134.         };

  135.         double[] adblFixFloatQuote = new double[] {
  136.             0.016410, //  2Y
  137.             0.017863, //  3Y
  138.             0.019030, //  4Y
  139.             0.020035, //  5Y
  140.             0.020902, //  6Y
  141.             0.021660, //  7Y
  142.             0.022307, //  8Y
  143.             0.022879, //  9Y
  144.             0.023363, // 10Y
  145.             0.023820, // 11Y
  146.             0.024172, // 12Y
  147.             0.024934, // 15Y
  148.             0.025581, // 20Y
  149.             0.025906, // 25Y
  150.             0.025973, // 30Y
  151.             0.025838, // 40Y
  152.             0.025560  // 50Y
  153.         };

  154.         String[] astrGovvieTenor = new String[] {
  155.             "1Y",
  156.             "2Y",
  157.             "3Y",
  158.             "5Y",
  159.             "7Y",
  160.             "10Y",
  161.             "20Y",
  162.             "30Y"
  163.         };

  164.         double[] adblGovvieYield = new double[] {
  165.             0.01219, //  1Y
  166.             0.01391, //  2Y
  167.             0.01590, //  3Y
  168.             0.01937, //  5Y
  169.             0.02200, //  7Y
  170.             0.02378, // 10Y
  171.             0.02677, // 20Y
  172.             0.02927  // 30Y
  173.         };

  174.         String[] astrCreditTenor = new String[] {
  175.             "06M",
  176.             "01Y",
  177.             "02Y",
  178.             "03Y",
  179.             "04Y",
  180.             "05Y",
  181.             "07Y",
  182.             "10Y"
  183.         };

  184.         double[] adblCreditQuote = new double[] {
  185.              10.,   //  6M
  186.              12.,   //  1Y
  187.              15.,   //  2Y
  188.              19.,   //  3Y
  189.              24.,   //  4Y
  190.              28.,   //  5Y
  191.              38.,   //  7Y
  192.              51.    // 10Y
  193.         };

  194.         double dblFX = 1;
  195.         int iSettleLag = 3;
  196.         double dblSpread = 0.0375;
  197.         String strCurrency = "USD";
  198.         double dblCleanPrice = 1.0;
  199.         double dblIssuePrice = 0.995;
  200.         double dblSpreadBump = 20.;
  201.         String strTreasuryCode = "UST";
  202.         double dblIssueAmount = 75.0e6;
  203.         double dblSpreadDurationMultiplier = 5.;
  204.         double dblResetRate = adblDepositQuote[0];

  205.         JulianDate dtEffective = DateUtil.CreateFromYMD (
  206.             2015,
  207.             5,
  208.             21
  209.         );

  210.         JulianDate dtMaturity = DateUtil.CreateFromYMD (
  211.             2020,
  212.             6,
  213.             1
  214.         );

  215.         BondComponent bond = BondBuilder.CreateSimpleFloater (
  216.             "Shivamogga",
  217.             "USD",
  218.             "USD-3M",
  219.             "Shivamogga",
  220.             dblSpread,
  221.             4,
  222.             "Act/360",
  223.             dtEffective,
  224.             dtMaturity,
  225.             null,
  226.             null
  227.         );

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

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

  231.         MergedDiscountForwardCurve mdfc = LatentMarketStateBuilder.SmoothFundingCurve (
  232.             dtSpot,
  233.             strCurrency,
  234.             astrDepositTenor,
  235.             adblDepositQuote,
  236.             "ForwardRate",
  237.             adblFuturesQuote,
  238.             "ForwardRate",
  239.             astrFixFloatTenor,
  240.             adblFixFloatQuote,
  241.             "SwapRate"
  242.         );

  243.         BondReplicator abr = BondReplicator.CorporateLoan (
  244.             dblCleanPrice,
  245.             dblIssuePrice,
  246.             dblIssueAmount,
  247.             dtSpot,
  248.             astrDepositTenor,
  249.             adblDepositQuote,
  250.             adblFuturesQuote,
  251.             astrFixFloatTenor,
  252.             adblFixFloatQuote,
  253.             dblSpreadBump,
  254.             dblSpreadDurationMultiplier,
  255.             strTreasuryCode,
  256.             astrGovvieTenor,
  257.             adblGovvieYield,
  258.             astrCreditTenor,
  259.             adblCreditQuote,
  260.             dblFX,
  261.             dblResetRate,
  262.             iSettleLag,
  263.             bond
  264.         );

  265.         BondReplicationRun abrr = abr.generateRun();

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

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

  268.         System.out.println();

  269.         CurveSurfaceQuoteContainer csqc = abr.creditBaseCSQC();

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

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

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

  273.         double dblYield = bond.yieldFromPrice (
  274.             ValuationParams.Spot (dtSpot.julian()),
  275.             csqc,
  276.             null,
  277.             dblCleanPrice
  278.         );

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

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

  281.         System.out.println ("Price Out : " +
  282.             bond.priceFromYield (
  283.                 ValuationParams.Spot (dtSpot.julian()),
  284.                 csqc,
  285.                 null,
  286.                 dblYield
  287.             )
  288.         );

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  305.         for (CompositePeriod p : bond.couponPeriods()) {
  306.             int iEndDate = p.endDate();

  307.             int iPayDate = p.payDate();

  308.             int iStartDate = p.startDate();

  309.             double dblCouponRate = bond.couponMetrics (
  310.                 iPayDate,
  311.                 valParams,
  312.                 csqc
  313.             ).rate();

  314.             double dblCouponDCF = p.couponDCF();

  315.             System.out.println ("\t|| " +
  316.                 DateUtil.YYYYMMDD (iStartDate) + " => " +
  317.                 DateUtil.YYYYMMDD (iEndDate) + " | ? | " +
  318.                 p.fundingLabel().fullyQualifiedName() + " | " +
  319.                 p.floaterLabel().fullyQualifiedName() + " | " +
  320.                 FormatUtil.FormatDouble (dblCouponRate, 1, 2, 100.) + "% | " +
  321.                 FormatUtil.FormatDouble (dblCouponDCF, 1, 4, 1.) + " | " +
  322.                 FormatUtil.FormatDouble (dblCouponRate * dblCouponDCF * p.notional (iEndDate) * p.couponFactor (iEndDate), 1, 4, 1.) + " | " +
  323.                 FormatUtil.FormatDouble (p.notional (iStartDate) - p.notional (iEndDate), 1, 4, 1.) + " | " +
  324.                 FormatUtil.FormatDouble (p.df (csqc), 1, 4, 1.) + " | " +
  325.                 FormatUtil.FormatDouble (p.survival (csqc), 1, 4, 1.) + " | " +
  326.                 FormatUtil.FormatDouble (p.recovery (csqc), 2, 0, 100.) + "% ||"
  327.             );
  328.         }

  329.         System.out.println ("\t|| " +
  330.             DateUtil.YYYYMMDD (dtEffective.julian()) + " => " +
  331.             DateUtil.YYYYMMDD (dtMaturity.julian()) + " | ? | " +
  332.             bond.fundingLabel().fullyQualifiedName() + " | " +
  333.             bond.forwardLabel().get (bond.name()).fullyQualifiedName() + " | " +
  334.             FormatUtil.FormatDouble (0., 1, 2, 100.) + "% | " +
  335.             FormatUtil.FormatDouble (0., 1, 4, 1.) + " | " +
  336.             FormatUtil.FormatDouble (0., 1, 4, 1.) + " | " +
  337.             FormatUtil.FormatDouble (bond.notional (dtMaturity.julian()), 1, 4, 1.) + " | " +
  338.             FormatUtil.FormatDouble (mdfc.df (dtMaturity), 1, 4, 1.) + " | " +
  339.             FormatUtil.FormatDouble (1., 1, 4, 1.) + " | " +
  340.             FormatUtil.FormatDouble (1., 2, 0, 100.) + "% ||"
  341.         );

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

  343.         System.out.println();

  344.         EnvManager.TerminateEnv();
  345.     }
  346. }