FloatingCouponBondPeriods.java

  1. package org.drip.sample.cashflow;

  2. import org.drip.analytics.cashflow.*;
  3. import org.drip.analytics.date.*;
  4. import org.drip.analytics.output.ConvexityAdjustment;
  5. import org.drip.numerical.common.FormatUtil;
  6. import org.drip.param.creator.MarketParamsBuilder;
  7. import org.drip.param.market.CurveSurfaceQuoteContainer;
  8. import org.drip.param.valuation.ValuationParams;
  9. import org.drip.product.creator.BondBuilder;
  10. import org.drip.product.credit.BondComponent;
  11. import org.drip.service.env.EnvManager;
  12. import org.drip.service.template.LatentMarketStateBuilder;
  13. import org.drip.state.creator.ScenarioCreditCurveBuilder;
  14. import org.drip.state.credit.CreditCurve;
  15. import org.drip.state.discount.MergedDiscountForwardCurve;
  16. import org.drip.state.govvie.GovvieCurve;

  17. /*
  18.  * -*- mode: java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  19.  */

  20. /*!
  21.  * Copyright (C) 2020 Lakshmi Krishnamurthy
  22.  * Copyright (C) 2019 Lakshmi Krishnamurthy
  23.  * Copyright (C) 2018 Lakshmi Krishnamurthy
  24.  * Copyright (C) 2017 Lakshmi Krishnamurthy
  25.  *
  26.  *  This file is part of DROP, an open-source library targeting analytics/risk, transaction cost analytics,
  27.  *      asset liability management analytics, capital, exposure, and margin analytics, valuation adjustment
  28.  *      analytics, and portfolio construction analytics within and across fixed income, credit, commodity,
  29.  *      equity, FX, and structured products. It also includes auxiliary libraries for algorithm support,
  30.  *      numerical analysis, numerical optimization, spline builder, model validation, statistical learning,
  31.  *      and computational support.
  32.  *  
  33.  *      https://lakshmidrip.github.io/DROP/
  34.  *  
  35.  *  DROP is composed of three modules:
  36.  *  
  37.  *  - DROP Product Core - https://lakshmidrip.github.io/DROP-Product-Core/
  38.  *  - DROP Portfolio Core - https://lakshmidrip.github.io/DROP-Portfolio-Core/
  39.  *  - DROP Computational Core - https://lakshmidrip.github.io/DROP-Computational-Core/
  40.  *
  41.  *  DROP Product Core implements libraries for the following:
  42.  *  - Fixed Income Analytics
  43.  *  - Loan Analytics
  44.  *  - Transaction Cost Analytics
  45.  *
  46.  *  DROP Portfolio Core implements libraries for the following:
  47.  *  - Asset Allocation Analytics
  48.  *  - Asset Liability Management Analytics
  49.  *  - Capital Estimation Analytics
  50.  *  - Exposure Analytics
  51.  *  - Margin Analytics
  52.  *  - XVA Analytics
  53.  *
  54.  *  DROP Computational Core implements libraries for the following:
  55.  *  - Algorithm Support
  56.  *  - Computation Support
  57.  *  - Function Analysis
  58.  *  - Model Validation
  59.  *  - Numerical Analysis
  60.  *  - Numerical Optimizer
  61.  *  - Spline Builder
  62.  *  - Statistical Learning
  63.  *
  64.  *  Documentation for DROP is Spread Over:
  65.  *
  66.  *  - Main                     => https://lakshmidrip.github.io/DROP/
  67.  *  - Wiki                     => https://github.com/lakshmiDRIP/DROP/wiki
  68.  *  - GitHub                   => https://github.com/lakshmiDRIP/DROP
  69.  *  - Repo Layout Taxonomy     => https://github.com/lakshmiDRIP/DROP/blob/master/Taxonomy.md
  70.  *  - Javadoc                  => https://lakshmidrip.github.io/DROP/Javadoc/index.html
  71.  *  - Technical Specifications => https://github.com/lakshmiDRIP/DROP/tree/master/Docs/Internal
  72.  *  - Release Versions         => https://lakshmidrip.github.io/DROP/version.html
  73.  *  - Community Credits        => https://lakshmidrip.github.io/DROP/credits.html
  74.  *  - Issues Catalog           => https://github.com/lakshmiDRIP/DROP/issues
  75.  *  - JUnit                    => https://lakshmidrip.github.io/DROP/junit/index.html
  76.  *  - Jacoco                   => https://lakshmidrip.github.io/DROP/jacoco/index.html
  77.  *
  78.  *  Licensed under the Apache License, Version 2.0 (the "License");
  79.  *      you may not use this file except in compliance with the License.
  80.  *  
  81.  *  You may obtain a copy of the License at
  82.  *      http://www.apache.org/licenses/LICENSE-2.0
  83.  *  
  84.  *  Unless required by applicable law or agreed to in writing, software
  85.  *      distributed under the License is distributed on an "AS IS" BASIS,
  86.  *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  87.  *  
  88.  *  See the License for the specific language governing permissions and
  89.  *      limitations under the License.
  90.  */

  91. /**
  92.  * <i>FloatingCouponBondPeriods</i> demonstrates the Cash Flow Period Details for a Floating Coupon Bond.
  93.  *  
  94.  * <br><br>
  95.  *  <ul>
  96.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ProductCore.md">Product Core Module</a></li>
  97.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/FixedIncomeAnalyticsLibrary.md">Fixed Income Analytics</a></li>
  98.  *      <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>
  99.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/cashflow/README.md">Fixed Income Product Cash Flow Display</a></li>
  100.  *  </ul>
  101.  * <br><br>
  102.  *
  103.  * @author Lakshmi Krishnamurthy
  104.  */

  105. public class FloatingCouponBondPeriods {

  106.     private static final MergedDiscountForwardCurve FundingCurve (
  107.         final JulianDate dtSpot,
  108.         final String strCurrency,
  109.         final double dblBump)
  110.         throws Exception
  111.     {
  112.         String[] astrDepositMaturityTenor = new String[] {
  113.             "2D"
  114.         };

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

  118.         double[] adblFuturesQuote = new double[] {
  119.             0.011375 + dblBump, // 98.8625
  120.             0.013350 + dblBump, // 98.6650
  121.             0.014800 + dblBump, // 98.5200
  122.             0.016450 + dblBump, // 98.3550
  123.             0.017850 + dblBump, // 98.2150
  124.             0.019300 + dblBump  // 98.0700
  125.         };

  126.         String[] astrFixFloatMaturityTenor = 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.017029 + dblBump, //  2Y
  147.             0.019354 + dblBump, //  3Y
  148.             0.021044 + dblBump, //  4Y
  149.             0.022291 + dblBump, //  5Y
  150.             0.023240 + dblBump, //  6Y
  151.             0.024025 + dblBump, //  7Y
  152.             0.024683 + dblBump, //  8Y
  153.             0.025243 + dblBump, //  9Y
  154.             0.025720 + dblBump, // 10Y
  155.             0.026130 + dblBump, // 11Y
  156.             0.026495 + dblBump, // 12Y
  157.             0.027230 + dblBump, // 15Y
  158.             0.027855 + dblBump, // 20Y
  159.             0.028025 + dblBump, // 25Y
  160.             0.028028 + dblBump, // 30Y
  161.             0.027902 + dblBump, // 40Y
  162.             0.027655 + dblBump  // 50Y
  163.         };

  164.         return LatentMarketStateBuilder.SmoothFundingCurve (
  165.             dtSpot,
  166.             strCurrency,
  167.             astrDepositMaturityTenor,
  168.             adblDepositQuote,
  169.             "ForwardRate",
  170.             adblFuturesQuote,
  171.             "ForwardRate",
  172.             astrFixFloatMaturityTenor,
  173.             adblFixFloatQuote,
  174.             "SwapRate"
  175.         );
  176.     }

  177.     private static final GovvieCurve GovvieCurve (
  178.         final JulianDate dtSpot,
  179.         final String strCode,
  180.         final double[] adblCoupon,
  181.         final double[] adblYield)
  182.         throws Exception
  183.     {
  184.         JulianDate[] adtEffective = new JulianDate[] {
  185.             dtSpot,
  186.             dtSpot,
  187.             dtSpot,
  188.             dtSpot,
  189.             dtSpot,
  190.             dtSpot,
  191.             dtSpot,
  192.             dtSpot
  193.         };

  194.         JulianDate[] adtMaturity = new JulianDate[] {
  195.             dtSpot.addTenor ("1Y"),
  196.             dtSpot.addTenor ("2Y"),
  197.             dtSpot.addTenor ("3Y"),
  198.             dtSpot.addTenor ("5Y"),
  199.             dtSpot.addTenor ("7Y"),
  200.             dtSpot.addTenor ("10Y"),
  201.             dtSpot.addTenor ("20Y"),
  202.             dtSpot.addTenor ("30Y")
  203.         };

  204.         return LatentMarketStateBuilder.GovvieCurve (
  205.             strCode,
  206.             dtSpot,
  207.             adtEffective,
  208.             adtMaturity,
  209.             adblCoupon,
  210.             adblYield,
  211.             "Yield",
  212.             LatentMarketStateBuilder.SHAPE_PRESERVING
  213.         );
  214.     }

  215.     public static final void main (
  216.         final String[] astrArgs)
  217.         throws Exception
  218.     {
  219.         EnvManager.InitEnv ("");

  220.         JulianDate dtSpot = DateUtil.CreateFromYMD (
  221.             2017,
  222.             DateUtil.MARCH,
  223.             10
  224.         );

  225.         String strCurrency = "USD";
  226.         String strTreasuryCode = "UST";

  227.         double[] adblTreasuryCoupon = new double[] {
  228.             0.0100,
  229.             0.0100,
  230.             0.0125,
  231.             0.0150,
  232.             0.0200,
  233.             0.0225,
  234.             0.0250,
  235.             0.0300
  236.         };

  237.         double[] adblTreasuryYield = new double[] {
  238.             0.0083, //  1Y
  239.             0.0122, //  2Y
  240.             0.0149, //  3Y
  241.             0.0193, //  5Y
  242.             0.0227, //  7Y
  243.             0.0248, // 10Y
  244.             0.0280, // 20Y
  245.             0.0308  // 30Y
  246.         };

  247.         JulianDate dtEffective = DateUtil.CreateFromYMD (2016, 10, 18);
  248.         JulianDate dtMaturity  = DateUtil.CreateFromYMD (2019, 10, 18);
  249.         double dblSpread = 0.0091;
  250.         int iFreq = 4;
  251.         String strCUSIP = "00206RCP5";
  252.         String strDayCount = "Act/360";
  253.         String strCreditCurve = "CC";

  254.         BondComponent bond = BondBuilder.CreateSimpleFloaterFP (
  255.             strCUSIP,
  256.             "USD",
  257.             "USD-3M",
  258.             strCreditCurve,
  259.             dblSpread,
  260.             iFreq,
  261.             strDayCount,
  262.             dtEffective,
  263.             dtMaturity,
  264.             DateUtil.CreateFromYMD (2016, 12, 18).julian(),
  265.             DateUtil.CreateFromYMD (2019,  9, 18).julian(),
  266.             null,
  267.             null,
  268.             null,
  269.             null,
  270.             null,
  271.             null,
  272.             null,
  273.             null,
  274.             null,
  275.             null
  276.         );

  277.         System.out.println();

  278.         MergedDiscountForwardCurve mdfc = FundingCurve (
  279.             dtSpot,
  280.             strCurrency,
  281.             0.
  282.         );

  283.         CreditCurve cc = ScenarioCreditCurveBuilder.FlatHazard (
  284.             dtSpot.julian(),
  285.             strCreditCurve,
  286.             "USD",
  287.             0.01,
  288.             0.4
  289.         );

  290.         CurveSurfaceQuoteContainer csqc = MarketParamsBuilder.Create (
  291.             mdfc,
  292.             GovvieCurve (
  293.                 dtSpot,
  294.                 strTreasuryCode,
  295.                 adblTreasuryCoupon,
  296.                 adblTreasuryYield
  297.             ),
  298.             cc,
  299.             null,
  300.             null,
  301.             null,
  302.             null
  303.         );

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

  305.         System.out.println();

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

  307.         System.out.println ("\t||                                      BOND CASH FLOW PERIOD DATES AND FACTORS                                       ||");

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

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

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

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

  312.         System.out.println ("\t||           - Period Pay Date                                                                                                                               ||");

  313.         System.out.println ("\t||           - Period FX Fixing Date                                                                                                                         ||");

  314.         System.out.println ("\t||           - Period Is FX MTM?                                                                                                                             ||");

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

  316.         System.out.println ("\t||           - Period Coupon Frequency                                                                                                                       ||");

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

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

  319.         System.out.println ("\t||           - Period Basis                                                                                                                                  ||");

  320.         System.out.println ("\t||           - Period Base Notional                                                                                                                          ||");

  321.         System.out.println ("\t||           - Period Notional                                                                                                                               ||");

  322.         System.out.println ("\t||           - Period Coupon Factor                                                                                                                          ||");

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

  324.         for (CompositePeriod p : bond.couponPeriods()) {
  325.             int iEndDate = p.endDate();

  326.             CompositeFloatingPeriod cfp = (CompositeFloatingPeriod) p;

  327.             ComposableUnitFloatingPeriod cufp = (ComposableUnitFloatingPeriod) cfp.periods().get(0);

  328.             ReferenceIndexPeriod rip = cufp.referenceIndexPeriod();

  329.             System.out.println ("\t|| " +
  330.                 DateUtil.YYYYMMDD (p.startDate()) + " => " +
  331.                 DateUtil.YYYYMMDD (iEndDate) + " | " +
  332.                 DateUtil.YYYYMMDD (rip.startDate()) + " | " +
  333.                 DateUtil.YYYYMMDD (rip.endDate()) + " | " +
  334.                 DateUtil.YYYYMMDD (rip.fixingDate()) + " | " +
  335.                 DateUtil.YYYYMMDD (p.payDate()) + " | " +
  336.                 DateUtil.YYYYMMDD (p.fxFixingDate()) + " | " +
  337.                 p.isFXMTM() + " | " +
  338.                 p.tenor() + " | " +
  339.                 p.freq() + " | " +
  340.                 p.payCurrency() + " | " +
  341.                 p.couponCurrency() + " | " +
  342.                 FormatUtil.FormatDouble (p.basis(), 1, 0, 10000.) + " | " +
  343.                 FormatUtil.FormatDouble (p.baseNotional(), 1, 4, 1.) + " | " +
  344.                 FormatUtil.FormatDouble (p.notional (iEndDate), 1, 4, 1.) + " | " +
  345.                 FormatUtil.FormatDouble (p.couponFactor (iEndDate), 1, 4, 1.) + " ||"
  346.             );
  347.         }

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

  349.         System.out.println();

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  366.         for (CompositePeriod p : bond.couponPeriods()) {
  367.             int iEndDate = p.endDate();

  368.             int iPayDate = p.payDate();

  369.             int iStartDate = p.startDate();

  370.             double dblCouponRate = bond.couponMetrics (
  371.                 iPayDate,
  372.                 valParams,
  373.                 csqc
  374.             ).rate();

  375.             double dblCouponDCF = p.couponDCF();

  376.             System.out.println ("\t|| " +
  377.                 DateUtil.YYYYMMDD (iStartDate) + " => " +
  378.                 DateUtil.YYYYMMDD (iEndDate) + " | " +
  379.                 p.creditLabel().fullyQualifiedName() + " | " +
  380.                 p.fundingLabel().fullyQualifiedName() + " | " +
  381.                 p.floaterLabel().fullyQualifiedName() + " | " +
  382.                 FormatUtil.FormatDouble (dblCouponRate, 1, 2, 100.) + "% | " +
  383.                 FormatUtil.FormatDouble (dblCouponDCF, 1, 4, 1.) + " | " +
  384.                 FormatUtil.FormatDouble (dblCouponRate * dblCouponDCF * p.notional (iEndDate) * p.couponFactor (iEndDate), 1, 4, 1.) + " | " +
  385.                 FormatUtil.FormatDouble (p.notional (iStartDate) - p.notional (iEndDate), 1, 4, 1.) + " | " +
  386.                 FormatUtil.FormatDouble (p.df (csqc), 1, 4, 1.) + " | " +
  387.                 FormatUtil.FormatDouble (p.survival (csqc), 1, 4, 1.) + " | " +
  388.                 FormatUtil.FormatDouble (p.recovery (csqc), 2, 0, 100.) + "% ||"
  389.             );
  390.         }

  391.         System.out.println ("\t|| " +
  392.             DateUtil.YYYYMMDD (dtEffective.julian()) + " => " +
  393.             DateUtil.YYYYMMDD (dtMaturity.julian()) + " | " +
  394.             bond.creditLabel().fullyQualifiedName() + " | " +
  395.             bond.fundingLabel().fullyQualifiedName() + " | " +
  396.             bond.forwardLabel().get (bond.name()).fullyQualifiedName() + " | " +
  397.             FormatUtil.FormatDouble (0., 1, 2, 100.) + "% | " +
  398.             FormatUtil.FormatDouble (0., 1, 4, 1.) + " | " +
  399.             FormatUtil.FormatDouble (0., 1, 4, 1.) + " | " +
  400.             FormatUtil.FormatDouble (bond.notional (dtMaturity.julian()), 1, 4, 1.) + " | " +
  401.             FormatUtil.FormatDouble (mdfc.df (dtMaturity), 1, 4, 1.) + " | " +
  402.             FormatUtil.FormatDouble (cc.survival (dtMaturity), 1, 4, 1.) + " | " +
  403.             FormatUtil.FormatDouble (cc.recovery (dtMaturity), 2, 0, 100.) + "% ||"
  404.         );

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

  406.         System.out.println();

  407.         System.out.println();

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

  409.         System.out.println ("\t||                                       CASH FLOW PERIODS CONVEXITY CORRECTION                                       ||");

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

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

  412.         System.out.println ("\t||            - Collateral Credit Adjustment                                                                          ||");

  413.         System.out.println ("\t||            - Collateral Forward Adjustment                                                                         ||");

  414.         System.out.println ("\t||            - Collateral Funding Adjustment                                                                         ||");

  415.         System.out.println ("\t||            - Collateral FX Adjustment                                                                              ||");

  416.         System.out.println ("\t||            - Credit Forward Adjustment                                                                             ||");

  417.         System.out.println ("\t||            - Credit Funding Adjustment                                                                             ||");

  418.         System.out.println ("\t||            - Credit FX Adjustment                                                                                  ||");

  419.         System.out.println ("\t||            - Forward Funding Adjustment                                                                            ||");

  420.         System.out.println ("\t||            - Forward FX Adjustment                                                                                 ||");

  421.         System.out.println ("\t||            - Funding FX Adjustment                                                                                 ||");

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

  423.         for (CompositePeriod p : bond.couponPeriods()) {
  424.             ConvexityAdjustment ca = p.terminalConvexityAdjustment (
  425.                 dtSpot.julian(),
  426.                 csqc
  427.             );

  428.             System.out.println ("\t|| " +
  429.                 DateUtil.YYYYMMDD (p.startDate()) + " => " +
  430.                 DateUtil.YYYYMMDD (p.endDate()) + " | " +
  431.                 FormatUtil.FormatDouble (ca.collateralCredit(), 1, 3, 1.) + " | " +
  432.                 FormatUtil.FormatDouble (ca.collateralForward(), 1, 3, 1.) + " | " +
  433.                 FormatUtil.FormatDouble (ca.collateralFunding(), 1, 3, 1.) + " | " +
  434.                 FormatUtil.FormatDouble (ca.collateralFX(), 1, 3, 1.) + " | " +
  435.                 FormatUtil.FormatDouble (ca.creditForward(), 1, 3, 1.) + " | " +
  436.                 FormatUtil.FormatDouble (ca.creditFunding(), 1, 3, 1.) + " | " +
  437.                 FormatUtil.FormatDouble (ca.creditFX(), 1, 3, 1.) + " | " +
  438.                 FormatUtil.FormatDouble (ca.forwardFunding(), 1, 3, 1.) + " | " +
  439.                 FormatUtil.FormatDouble (ca.forwardFX(), 1, 3, 1.) + " | " +
  440.                 FormatUtil.FormatDouble (ca.fundingFX(), 1, 3, 1.) + " ||"
  441.             );
  442.         }

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

  444.         System.out.println();

  445.         EnvManager.TerminateEnv();
  446.     }
  447. }