ForwardRateFuturePeriods.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.rates.SingleStreamComponent;
  10. import org.drip.service.env.EnvManager;
  11. import org.drip.service.template.*;
  12. import org.drip.state.discount.MergedDiscountForwardCurve;

  13. /*
  14.  * -*- mode: java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  15.  */

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

  87. /**
  88.  * <i>ForwardRateFuturePeriods</i> demonstrates the Cash Flow Period Details for a Forward Rate Futures
  89.  * Instrument.
  90.  *  
  91.  * <br><br>
  92.  *  <ul>
  93.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ProductCore.md">Product Core Module</a></li>
  94.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/FixedIncomeAnalyticsLibrary.md">Fixed Income Analytics</a></li>
  95.  *      <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>
  96.  *      <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>
  97.  *  </ul>
  98.  * <br><br>
  99.  *
  100.  * @author Lakshmi Krishnamurthy
  101.  */

  102. public class ForwardRateFuturePeriods {

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

  112.         double[] adblDepositQuote = new double[] {
  113.             0.0111956 + dblBump // 2D
  114.         };

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

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

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

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

  174.     public static final void main (
  175.         final String[] astrArgs)
  176.         throws Exception
  177.     {
  178.         EnvManager.InitEnv ("");

  179.         JulianDate dtSpot = DateUtil.CreateFromYMD (
  180.             2017,
  181.             DateUtil.MARCH,
  182.             10
  183.         );

  184.         String strCurrency = "USD";

  185.         SingleStreamComponent sscForwardRateFuture = ExchangeInstrumentBuilder.ForwardRateFutures (
  186.             dtSpot.addTenor ("2Y"),
  187.             strCurrency
  188.         );

  189.         System.out.println();

  190.         MergedDiscountForwardCurve mdfc = FundingCurve (
  191.             dtSpot,
  192.             strCurrency,
  193.             0.
  194.         );

  195.         CurveSurfaceQuoteContainer csqc = MarketParamsBuilder.Create (
  196.             mdfc,
  197.             null,
  198.             null,
  199.             null,
  200.             null,
  201.             null,
  202.             null
  203.         );

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

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

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

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

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

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

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

  211.         System.out.println ("\t||           - Period Index Reference Start Date                                                                                                            ||");

  212.         System.out.println ("\t||           - Period Index Reference End Date                                                                                                              ||");

  213.         System.out.println ("\t||           - Period Reset Date                                                                                                                            ||");

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

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

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

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

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

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

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

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

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

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

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

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

  226.         for (CompositePeriod p : sscForwardRateFuture.couponPeriods()) {
  227.             int iEndDate = p.endDate();

  228.             CompositeFloatingPeriod cfp = (CompositeFloatingPeriod) p;

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

  230.             ReferenceIndexPeriod rip = cufp.referenceIndexPeriod();

  231.             System.out.println ("\t|| " +
  232.                 DateUtil.YYYYMMDD (p.startDate()) + " => " +
  233.                 DateUtil.YYYYMMDD (iEndDate) + " | " +
  234.                 DateUtil.YYYYMMDD (rip.startDate()) + " | " +
  235.                 DateUtil.YYYYMMDD (rip.endDate()) + " | " +
  236.                 DateUtil.YYYYMMDD (rip.fixingDate()) + " | " +
  237.                 DateUtil.YYYYMMDD (p.payDate()) + " | " +
  238.                 DateUtil.YYYYMMDD (p.fxFixingDate()) + " | " +
  239.                 p.isFXMTM() + " | " +
  240.                 p.tenor() + " | " +
  241.                 p.freq() + " | " +
  242.                 p.payCurrency() + " | " +
  243.                 p.couponCurrency() + " | " +
  244.                 FormatUtil.FormatDouble (p.basis(), 1, 0, 10000.) + " | " +
  245.                 FormatUtil.FormatDouble (p.baseNotional(), 1, 4, 1.) + " | " +
  246.                 FormatUtil.FormatDouble (p.notional (iEndDate), 1, 4, 1.) + " | " +
  247.                 FormatUtil.FormatDouble (p.couponFactor (iEndDate), 1, 4, 1.) + " ||"
  248.             );
  249.         }

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

  251.         System.out.println();

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

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

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

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

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

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

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

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

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

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

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

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

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

  265.         for (CompositePeriod p : sscForwardRateFuture.couponPeriods()) {
  266.             int iEndDate = p.endDate();

  267.             int iPayDate = p.payDate();

  268.             int iStartDate = p.startDate();

  269.             double dblCouponRate = sscForwardRateFuture.couponMetrics (
  270.                 iPayDate,
  271.                 valParams,
  272.                 csqc
  273.             ).rate();

  274.             double dblCouponDCF = p.couponDCF();

  275.             System.out.println ("\t|| " +
  276.                 DateUtil.YYYYMMDD (iStartDate) + " => " +
  277.                 DateUtil.YYYYMMDD (iEndDate) + " | " +
  278.                 p.fundingLabel().fullyQualifiedName() + " | " +
  279.                 p.floaterLabel().fullyQualifiedName() + " | " +
  280.                 FormatUtil.FormatDouble (dblCouponRate, 1, 2, 100.) + "% | " +
  281.                 FormatUtil.FormatDouble (dblCouponDCF, 1, 4, 1.) + " | " +
  282.                 FormatUtil.FormatDouble (dblCouponRate * dblCouponDCF * p.notional (iEndDate) * p.couponFactor (iEndDate), 1, 4, 1.) + " | " +
  283.                 FormatUtil.FormatDouble (p.notional (iStartDate) - p.notional (iEndDate), 1, 4, 1.) + " | " +
  284.                 FormatUtil.FormatDouble (p.df (csqc), 1, 4, 1.) + " ||"
  285.             );
  286.         }

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

  288.         System.out.println();

  289.         System.out.println();

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  305.         for (CompositePeriod p : sscForwardRateFuture.couponPeriods()) {
  306.             ConvexityAdjustment ca = p.terminalConvexityAdjustment (
  307.                 dtSpot.julian(),
  308.                 csqc
  309.             );

  310.             System.out.println ("\t|| " +
  311.                 DateUtil.YYYYMMDD (p.startDate()) + " => " +
  312.                 DateUtil.YYYYMMDD (p.endDate()) + " | " +
  313.                 FormatUtil.FormatDouble (ca.collateralCredit(), 1, 3, 1.) + " | " +
  314.                 FormatUtil.FormatDouble (ca.collateralForward(), 1, 3, 1.) + " | " +
  315.                 FormatUtil.FormatDouble (ca.collateralFunding(), 1, 3, 1.) + " | " +
  316.                 FormatUtil.FormatDouble (ca.collateralFX(), 1, 3, 1.) + " | " +
  317.                 FormatUtil.FormatDouble (ca.creditForward(), 1, 3, 1.) + " | " +
  318.                 FormatUtil.FormatDouble (ca.creditFunding(), 1, 3, 1.) + " | " +
  319.                 FormatUtil.FormatDouble (ca.creditFX(), 1, 3, 1.) + " | " +
  320.                 FormatUtil.FormatDouble (ca.forwardFunding(), 1, 3, 1.) + " | " +
  321.                 FormatUtil.FormatDouble (ca.forwardFX(), 1, 3, 1.) + " | " +
  322.                 FormatUtil.FormatDouble (ca.fundingFX(), 1, 3, 1.) + " ||"
  323.             );
  324.         }

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

  326.         System.out.println();

  327.         EnvManager.TerminateEnv();
  328.     }
  329. }