Ozhukarai.java

  1. package org.drip.sample.bondswap;

  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>Ozhukarai</i> demonstrates the Analytics Calculation/Reconciliation for the OTC Fix-Float Index Based
  90.  * Bond Ozhukarai.
  91.  *  
  92.  * <br><br>
  93.  *  <ul>
  94.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ProductCore.md">Product Core Module</a></li>
  95.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/FixedIncomeAnalyticsLibrary.md">Fixed Income Analytics</a></li>
  96.  *      <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>
  97.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/bondswap/README.md">Swap Index Floater Bond Analytics</a></li>
  98.  *  </ul>
  99.  * <br><br>
  100.  *
  101.  * @author Lakshmi Krishnamurthy
  102.  */

  103. public class Ozhukarai
  104. {

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

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

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

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

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

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

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

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

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

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

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

  207.         double dblFX = 1;
  208.         int iSettleLag = 3;
  209.         double dblSpread = 0.04230;
  210.         String strCurrency = "USD";
  211.         double dblCleanPrice = 1.0;
  212.         double dblIssuePrice = 0.995;
  213.         double dblSpreadBump = 20.;
  214.         double dblResetRate = 0.00970;
  215.         String strTreasuryCode = "UST";
  216.         String strFixFloatMaturity = "5Y";
  217.         double dblIssueAmount = 180523703.64;
  218.         double dblSpreadDurationMultiplier = 5.;

  219.         JulianDate dtEffective = DateUtil.CreateFromYMD (
  220.             2015,
  221.             10,
  222.             20
  223.         );

  224.         JulianDate dtMaturity = DateUtil.CreateFromYMD (
  225.             2045,
  226.             10,
  227.             20
  228.         );

  229.         BondComponent bond = BondBuilder.CreateSimpleOTCIRSFloater (
  230.             "Ozhukarai",
  231.             "USD",
  232.             "USD-3M-" + strFixFloatMaturity,
  233.             "Ozhukarai",
  234.             dblSpread,
  235.             4,
  236.             "Act/360",
  237.             dtEffective,
  238.             dtMaturity,
  239.             null,
  240.             null
  241.         );

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

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

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

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

  279.         BondReplicationRun abrr = abr.generateRun();

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

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

  282.         System.out.println();

  283.         CurveSurfaceQuoteContainer csqc = abr.creditBaseCSQC();

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

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

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

  287.         double dblYield = bond.yieldFromPrice (
  288.             ValuationParams.Spot (dtSpot.julian()),
  289.             csqc,
  290.             null,
  291.             dblCleanPrice
  292.         );

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  319.         for (CompositePeriod p : bond.couponPeriods()) {
  320.             int iEndDate = p.endDate();

  321.             int iPayDate = p.payDate();

  322.             int iStartDate = p.startDate();

  323.             double dblCouponRate = bond.couponMetrics (
  324.                 iPayDate,
  325.                 valParams,
  326.                 csqc
  327.             ).rate();

  328.             double dblCouponDCF = p.couponDCF();

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

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

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

  357.         System.out.println();

  358.         EnvManager.TerminateEnv();
  359.     }
  360. }