AmortizingBondPeriods.java

  1. package org.drip.sample.cashflow;

  2. import org.drip.analytics.cashflow.CompositePeriod;
  3. import org.drip.analytics.date.*;
  4. import org.drip.analytics.output.ConvexityAdjustment;
  5. import org.drip.numerical.common.*;
  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>AmortizingBondPeriods</i> demonstrates the Cash Flow Period Details for an Amortizing Fixed Coupon
  93.  * Bond.
  94.  *  
  95.  * <br><br>
  96.  *  <ul>
  97.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ProductCore.md">Product Core Module</a></li>
  98.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/FixedIncomeAnalyticsLibrary.md">Fixed Income Analytics</a></li>
  99.  *      <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>
  100.  *      <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>
  101.  *  </ul>
  102.  * <br><br>
  103.  *
  104.  * @author Lakshmi Krishnamurthy
  105.  */

  106. public class AmortizingBondPeriods {

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

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

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

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

  146.         double[] adblFixFloatQuote = new double[] {
  147.             0.017029 + dblBump, //  2Y
  148.             0.019354 + dblBump, //  3Y
  149.             0.021044 + dblBump, //  4Y
  150.             0.022291 + dblBump, //  5Y
  151.             0.023240 + dblBump, //  6Y
  152.             0.024025 + dblBump, //  7Y
  153.             0.024683 + dblBump, //  8Y
  154.             0.025243 + dblBump, //  9Y
  155.             0.025720 + dblBump, // 10Y
  156.             0.026130 + dblBump, // 11Y
  157.             0.026495 + dblBump, // 12Y
  158.             0.027230 + dblBump, // 15Y
  159.             0.027855 + dblBump, // 20Y
  160.             0.028025 + dblBump, // 25Y
  161.             0.028028 + dblBump, // 30Y
  162.             0.027902 + dblBump, // 40Y
  163.             0.027655 + dblBump  // 50Y
  164.         };

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

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

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

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

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

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

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

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

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

  248.         JulianDate dtEffective = DateUtil.CreateFromYMD (2014,  8, 11);
  249.         JulianDate dtMaturity  = DateUtil.CreateFromYMD (2026,  9,  3);
  250.         double dblCoupon = 0.0375;
  251.         int iFreq = 2;
  252.         String strCUSIP = "90932QAA4";
  253.         String strDayCount = "30/360";
  254.         String strCreditCurve = "CC";
  255.         String strDateFactor = "8/11/2014;1;3/3/2016;0.970903241;9/3/2016;0.942322722;3/3/2017;0.913786792;9/3/2017;0.885297621;3/3/2018;0.857293406;9/3/2018;0.829340734;3/3/2019;0.801442245;9/3/2019;0.77360076;3/3/2020;0.7458193;9/3/2020;0.718101103;3/3/2021;0.690449645;9/3/2021;0.66286866;3/3/2022;0.635362168;9/3/2022;0.607934498;3/3/2023;0.580590323;9/3/2023;0.553334692;3/3/2024;0.526173071;9/3/2024;0.499111382;3/3/2025;0.472156058;9/3/2025;0.445314095;3/3/2026;0.418593118";

  256.         BondComponent bond = BondBuilder.CreateSimpleFixed (
  257.             strCUSIP,
  258.             strCurrency,
  259.             strCreditCurve,
  260.             dblCoupon,
  261.             iFreq,
  262.             strDayCount,
  263.             dtEffective,
  264.             dtMaturity,
  265.             Array2D.FromDateFactorVertex (
  266.                 strDateFactor,
  267.                 dtMaturity.julian()
  268.             ),
  269.             null
  270.         );

  271.         MergedDiscountForwardCurve mdfc = FundingCurve (
  272.             dtSpot,
  273.             strCurrency,
  274.             0.
  275.         );

  276.         CreditCurve cc = ScenarioCreditCurveBuilder.FlatHazard (
  277.             dtSpot.julian(),
  278.             strCreditCurve,
  279.             "USD",
  280.             0.01,
  281.             0.4
  282.         );

  283.         CurveSurfaceQuoteContainer csqc = MarketParamsBuilder.Create (
  284.             mdfc,
  285.             GovvieCurve (
  286.                 dtSpot,
  287.                 strTreasuryCode,
  288.                 adblTreasuryCoupon,
  289.                 adblTreasuryYield
  290.             ),
  291.             cc,
  292.             null,
  293.             null,
  294.             null,
  295.             null
  296.         );

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

  298.         System.out.println();

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  317.         for (CompositePeriod p : bond.couponPeriods()) {
  318.             int iEndDate = p.endDate();

  319.             System.out.println ("\t|| " +
  320.                 DateUtil.YYYYMMDD (p.startDate()) + " => " +
  321.                 DateUtil.YYYYMMDD (iEndDate) + " | " +
  322.                 DateUtil.YYYYMMDD (p.payDate()) + " | " +
  323.                 DateUtil.YYYYMMDD (p.fxFixingDate()) + " | " +
  324.                 p.isFXMTM() + " | " +
  325.                 p.tenor() + " | " +
  326.                 p.freq() + " | " +
  327.                 p.payCurrency() + " | " +
  328.                 p.couponCurrency() + " | " +
  329.                 FormatUtil.FormatDouble (p.basis(), 1, 0, 10000.) + " | " +
  330.                 FormatUtil.FormatDouble (p.baseNotional(), 1, 4, 1.) + " | " +
  331.                 FormatUtil.FormatDouble (bond.notional (iEndDate), 1, 4, 1.) + " | " +
  332.                 FormatUtil.FormatDouble (p.couponFactor (iEndDate), 1, 4, 1.) + " ||"
  333.             );
  334.         }

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

  336.         System.out.println();

  337.         double dblPreviousPeriodNotional = bond.notional (dtEffective.julian());

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  354.         for (CompositePeriod p : bond.couponPeriods()) {
  355.             int iEndDate = p.endDate();

  356.             int iPayDate = p.payDate();

  357.             int iStartDate = p.startDate();

  358.             double dblCouponRate = bond.couponMetrics (
  359.                 iPayDate,
  360.                 valParams,
  361.                 csqc
  362.             ).rate();

  363.             double dblCouponDCF = p.couponDCF();

  364.             double dblCurrentPeriodNotional = bond.notional (iEndDate);

  365.             System.out.println ("\t|| " +
  366.                 DateUtil.YYYYMMDD (iStartDate) + " => " +
  367.                 DateUtil.YYYYMMDD (iEndDate) + " | " +
  368.                 p.creditLabel().fullyQualifiedName() + " | " +
  369.                 p.fundingLabel().fullyQualifiedName() + " | " +
  370.                 FormatUtil.FormatDouble (dblCouponRate, 1, 2, 100.) + "% | " +
  371.                 FormatUtil.FormatDouble (dblCouponDCF, 1, 4, 1.) + " | " +
  372.                 FormatUtil.FormatDouble (dblCouponRate * dblCouponDCF * dblCurrentPeriodNotional * p.couponFactor (iEndDate), 1, 4, 1.) + " | " +
  373.                 FormatUtil.FormatDouble (dblPreviousPeriodNotional - dblCurrentPeriodNotional, 1, 4, 1.) + " | " +
  374.                 FormatUtil.FormatDouble (p.df (csqc), 1, 4, 1.) + " | " +
  375.                 FormatUtil.FormatDouble (p.survival (csqc), 1, 4, 1.) + " | " +
  376.                 FormatUtil.FormatDouble (p.recovery (csqc), 2, 0, 100.) + "% ||"
  377.             );

  378.             dblPreviousPeriodNotional = dblCurrentPeriodNotional;
  379.         }

  380.         System.out.println ("\t|| " +
  381.             DateUtil.YYYYMMDD (dtEffective.julian()) + " => " +
  382.             DateUtil.YYYYMMDD (dtMaturity.julian()) + " | " +
  383.             bond.creditLabel().fullyQualifiedName() + " | " +
  384.             bond.fundingLabel().fullyQualifiedName() + " | " +
  385.             FormatUtil.FormatDouble (0., 1, 2, 100.) + "% | " +
  386.             FormatUtil.FormatDouble (0., 1, 4, 1.) + " | " +
  387.             FormatUtil.FormatDouble (0., 1, 4, 1.) + " | " +
  388.             FormatUtil.FormatDouble (bond.notional (dtMaturity.julian()), 1, 4, 1.) + " | " +
  389.             FormatUtil.FormatDouble (mdfc.df (dtMaturity), 1, 4, 1.) + " | " +
  390.             FormatUtil.FormatDouble (cc.survival (dtMaturity), 1, 4, 1.) + " | " +
  391.             FormatUtil.FormatDouble (cc.recovery (dtMaturity), 2, 0, 100.) + "% ||"
  392.         );

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

  394.         System.out.println();

  395.         System.out.println();

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  411.         for (CompositePeriod p : bond.couponPeriods()) {
  412.             ConvexityAdjustment ca = p.terminalConvexityAdjustment (
  413.                 dtSpot.julian(),
  414.                 csqc
  415.             );

  416.             System.out.println ("\t|| " +
  417.                 DateUtil.YYYYMMDD (p.startDate()) + " => " +
  418.                 DateUtil.YYYYMMDD (p.endDate()) + " | " +
  419.                 FormatUtil.FormatDouble (ca.collateralCredit(), 1, 3, 1.) + " | " +
  420.                 FormatUtil.FormatDouble (ca.collateralForward(), 1, 3, 1.) + " | " +
  421.                 FormatUtil.FormatDouble (ca.collateralFunding(), 1, 3, 1.) + " | " +
  422.                 FormatUtil.FormatDouble (ca.collateralFX(), 1, 3, 1.) + " | " +
  423.                 FormatUtil.FormatDouble (ca.creditForward(), 1, 3, 1.) + " | " +
  424.                 FormatUtil.FormatDouble (ca.creditFunding(), 1, 3, 1.) + " | " +
  425.                 FormatUtil.FormatDouble (ca.creditFX(), 1, 3, 1.) + " | " +
  426.                 FormatUtil.FormatDouble (ca.forwardFunding(), 1, 3, 1.) + " | " +
  427.                 FormatUtil.FormatDouble (ca.forwardFX(), 1, 3, 1.) + " | " +
  428.                 FormatUtil.FormatDouble (ca.fundingFX(), 1, 3, 1.) + " ||"
  429.             );
  430.         }

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

  432.         System.out.println();

  433.         EnvManager.TerminateEnv();
  434.     }
  435. }