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

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

  92. public class Kollam {

  93.     private static final void SetEOS (
  94.         final BondComponent bond,
  95.         final EmbeddedOptionSchedule eosCall,
  96.         final EmbeddedOptionSchedule eosPut)
  97.         throws java.lang.Exception
  98.     {
  99.         if (null != eosPut) bond.setEmbeddedPutSchedule (eosPut);

  100.         if (null != eosCall) bond.setEmbeddedCallSchedule (eosCall);
  101.     }

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

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

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

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

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

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

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

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

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

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

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

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

  215.         JulianDate dtEffective = DateUtil.CreateFromYMD (
  216.             2014,
  217.             3,
  218.             13
  219.         );

  220.         JulianDate dtMaturity = DateUtil.CreateFromYMD (
  221.             2020,
  222.             10,
  223.             25
  224.         );

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

  238.         SetEOS (
  239.             bond,
  240.             EmbeddedOptionSchedule.FromAmerican (
  241.                 dtSpot.julian(),
  242.                 new int[] {
  243.                     DateUtil.CreateFromYMD (2014,  3, 13).julian(),
  244.                     DateUtil.CreateFromYMD (2015,  3, 13).julian(),
  245.                     DateUtil.CreateFromYMD (2020, 10, 25).julian(),
  246.                 },
  247.                 new double[] {
  248.                     1.01,
  249.                     1.00,
  250.                     1.00,
  251.                 },
  252.                 false,
  253.                 15,
  254.                 15,
  255.                 false,
  256.                 Double.NaN,
  257.                 "",
  258.                 Double.NaN
  259.             ),
  260.             null
  261.         );

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

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

  265.         MergedDiscountForwardCurve mdfc = LatentMarketStateBuilder.SmoothFundingCurve (
  266.             dtSpot,
  267.             strCurrency,
  268.             astrDepositTenor,
  269.             adblDepositQuote,
  270.             "ForwardRate",
  271.             adblFuturesQuote,
  272.             "ForwardRate",
  273.             astrFixFloatTenor,
  274.             adblFixFloatQuote,
  275.             "SwapRate"
  276.         );

  277.         BondReplicator abr = BondReplicator.CorporateLoan (
  278.             dblCleanPrice,
  279.             dblIssuePrice,
  280.             dblIssueAmount,
  281.             dtSpot,
  282.             astrDepositTenor,
  283.             adblDepositQuote,
  284.             adblFuturesQuote,
  285.             astrFixFloatTenor,
  286.             adblFixFloatQuote,
  287.             dblSpreadBump,
  288.             dblSpreadDurationMultiplier,
  289.             strTreasuryCode,
  290.             astrGovvieTenor,
  291.             adblGovvieYield,
  292.             astrCreditTenor,
  293.             adblCreditQuote,
  294.             dblFX,
  295.             dblResetRate,
  296.             iSettleLag,
  297.             bond
  298.         );

  299.         BondReplicationRun abrr = abr.generateRun();

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

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

  302.         System.out.println();

  303.         CurveSurfaceQuoteContainer csqc = abr.creditBaseCSQC();

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

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

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

  307.         double dblYield = bond.yieldFromPrice (
  308.             ValuationParams.Spot (dtSpot.julian()),
  309.             csqc,
  310.             null,
  311.             dblCleanPrice
  312.         );

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

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

  315.         System.out.println ("Price Out : " +
  316.             bond.priceFromYield (
  317.                 ValuationParams.Spot (dtSpot.julian()),
  318.                 csqc,
  319.                 null,
  320.                 dblYield
  321.             )
  322.         );

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  339.         for (CompositePeriod p : bond.couponPeriods()) {
  340.             int iEndDate = p.endDate();

  341.             int iPayDate = p.payDate();

  342.             int iStartDate = p.startDate();

  343.             double dblCouponRate = bond.couponMetrics (
  344.                 iPayDate,
  345.                 valParams,
  346.                 csqc
  347.             ).rate();

  348.             double dblCouponDCF = p.couponDCF();

  349.             System.out.println ("\t|| " +
  350.                 DateUtil.YYYYMMDD (iStartDate) + " => " +
  351.                 DateUtil.YYYYMMDD (iEndDate) + " | ? | " +
  352.                 p.fundingLabel().fullyQualifiedName() + " | " +
  353.                 p.floaterLabel().fullyQualifiedName() + " | " +
  354.                 FormatUtil.FormatDouble (dblCouponRate, 1, 2, 100.) + "% | " +
  355.                 FormatUtil.FormatDouble (dblCouponDCF, 1, 4, 1.) + " | " +
  356.                 FormatUtil.FormatDouble (dblCouponRate * dblCouponDCF * p.notional (iEndDate) * p.couponFactor (iEndDate), 1, 4, 1.) + " | " +
  357.                 FormatUtil.FormatDouble (p.notional (iStartDate) - p.notional (iEndDate), 1, 4, 1.) + " | " +
  358.                 FormatUtil.FormatDouble (p.df (csqc), 1, 4, 1.) + " | " +
  359.                 FormatUtil.FormatDouble (p.survival (csqc), 1, 4, 1.) + " | " +
  360.                 FormatUtil.FormatDouble (p.recovery (csqc), 2, 0, 100.) + "% ||"
  361.             );
  362.         }

  363.         System.out.println ("\t|| " +
  364.             DateUtil.YYYYMMDD (dtEffective.julian()) + " => " +
  365.             DateUtil.YYYYMMDD (dtMaturity.julian()) + " | ? | " +
  366.             bond.fundingLabel().fullyQualifiedName() + " | " +
  367.             bond.forwardLabel().get (bond.name()).fullyQualifiedName() + " | " +
  368.             FormatUtil.FormatDouble (0., 1, 2, 100.) + "% | " +
  369.             FormatUtil.FormatDouble (0., 1, 4, 1.) + " | " +
  370.             FormatUtil.FormatDouble (0., 1, 4, 1.) + " | " +
  371.             FormatUtil.FormatDouble (bond.notional (dtMaturity.julian()), 1, 4, 1.) + " | " +
  372.             FormatUtil.FormatDouble (mdfc.df (dtMaturity), 1, 4, 1.) + " | " +
  373.             FormatUtil.FormatDouble (1., 1, 4, 1.) + " | " +
  374.             FormatUtil.FormatDouble (1., 2, 0, 100.) + "% ||"
  375.         );

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

  377.         System.out.println();

  378.         EnvManager.TerminateEnv();
  379.     }
  380. }