FRAMarketPeriods.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.fra.*;
  10. import org.drip.service.env.EnvManager;
  11. import org.drip.service.template.*;
  12. import org.drip.state.discount.MergedDiscountForwardCurve;
  13. import org.drip.state.identifier.ForwardLabel;

  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>FRAMarketPeriods</i> demonstrates the Cash Flow Period Details for a Market FRA.
  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 FRAMarketPeriods {

  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.         FRAStandardComponent fraStandard = OTCInstrumentBuilder.FRAStandard (
  186.             dtSpot,
  187.             ForwardLabel.Create (
  188.                 strCurrency,
  189.                 "3M"
  190.             ),
  191.             "2Y",
  192.             0.01
  193.         );

  194.         FRAMarketComponent fraMarket = new FRAMarketComponent (
  195.             "FRA_MARKET_COMPONENT",
  196.             fraStandard.stream(),
  197.             fraStandard.strike(),
  198.             null
  199.         );

  200.         System.out.println();

  201.         MergedDiscountForwardCurve mdfc = FundingCurve (
  202.             dtSpot,
  203.             strCurrency,
  204.             0.
  205.         );

  206.         CurveSurfaceQuoteContainer csqc = MarketParamsBuilder.Create (
  207.             mdfc,
  208.             null,
  209.             null,
  210.             null,
  211.             null,
  212.             null,
  213.             null
  214.         );

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  237.         for (CompositePeriod p : fraMarket.couponPeriods()) {
  238.             int iEndDate = p.endDate();

  239.             CompositeFloatingPeriod cfp = (CompositeFloatingPeriod) p;

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

  241.             ReferenceIndexPeriod rip = cufp.referenceIndexPeriod();

  242.             System.out.println ("\t|| " +
  243.                 DateUtil.YYYYMMDD (p.startDate()) + " => " +
  244.                 DateUtil.YYYYMMDD (iEndDate) + " | " +
  245.                 DateUtil.YYYYMMDD (rip.startDate()) + " | " +
  246.                 DateUtil.YYYYMMDD (rip.endDate()) + " | " +
  247.                 DateUtil.YYYYMMDD (rip.fixingDate()) + " | " +
  248.                 DateUtil.YYYYMMDD (p.payDate()) + " | " +
  249.                 DateUtil.YYYYMMDD (p.fxFixingDate()) + " | " +
  250.                 p.isFXMTM() + " | " +
  251.                 p.tenor() + " | " +
  252.                 p.freq() + " | " +
  253.                 p.payCurrency() + " | " +
  254.                 p.couponCurrency() + " | " +
  255.                 FormatUtil.FormatDouble (p.basis(), 1, 0, 10000.) + " | " +
  256.                 FormatUtil.FormatDouble (p.baseNotional(), 1, 4, 1.) + " | " +
  257.                 FormatUtil.FormatDouble (p.notional (iEndDate), 1, 4, 1.) + " | " +
  258.                 FormatUtil.FormatDouble (p.couponFactor (iEndDate), 1, 4, 1.) + " ||"
  259.             );
  260.         }

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

  262.         System.out.println();

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

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

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

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

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

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

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

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

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

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

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

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

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

  276.         for (CompositePeriod p : fraMarket.couponPeriods()) {
  277.             int iEndDate = p.endDate();

  278.             int iPayDate = p.payDate();

  279.             int iStartDate = p.startDate();

  280.             double dblCouponRate = fraMarket.couponMetrics (
  281.                 iPayDate,
  282.                 valParams,
  283.                 csqc
  284.             ).rate();

  285.             double dblCouponDCF = p.couponDCF();

  286.             System.out.println ("\t|| " +
  287.                 DateUtil.YYYYMMDD (iStartDate) + " => " +
  288.                 DateUtil.YYYYMMDD (iEndDate) + " | " +
  289.                 p.fundingLabel().fullyQualifiedName() + " | " +
  290.                 p.floaterLabel().fullyQualifiedName() + " | " +
  291.                 FormatUtil.FormatDouble (dblCouponRate, 1, 2, 100.) + "% | " +
  292.                 FormatUtil.FormatDouble (dblCouponDCF, 1, 4, 1.) + " | " +
  293.                 FormatUtil.FormatDouble (dblCouponRate * dblCouponDCF * p.notional (iEndDate) * p.couponFactor (iEndDate), 1, 4, 1.) + " | " +
  294.                 FormatUtil.FormatDouble (p.notional (iStartDate) - p.notional (iEndDate), 1, 4, 1.) + " | " +
  295.                 FormatUtil.FormatDouble (p.df (csqc), 1, 4, 1.) + " ||"
  296.             );
  297.         }

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

  299.         System.out.println();

  300.         System.out.println();

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  316.         for (CompositePeriod p : fraMarket.couponPeriods()) {
  317.             ConvexityAdjustment ca = p.terminalConvexityAdjustment (
  318.                 dtSpot.julian(),
  319.                 csqc
  320.             );

  321.             System.out.println ("\t|| " +
  322.                 DateUtil.YYYYMMDD (p.startDate()) + " => " +
  323.                 DateUtil.YYYYMMDD (p.endDate()) + " | " +
  324.                 FormatUtil.FormatDouble (ca.collateralCredit(), 1, 3, 1.) + " | " +
  325.                 FormatUtil.FormatDouble (ca.collateralForward(), 1, 3, 1.) + " | " +
  326.                 FormatUtil.FormatDouble (ca.collateralFunding(), 1, 3, 1.) + " | " +
  327.                 FormatUtil.FormatDouble (ca.collateralFX(), 1, 3, 1.) + " | " +
  328.                 FormatUtil.FormatDouble (ca.creditForward(), 1, 3, 1.) + " | " +
  329.                 FormatUtil.FormatDouble (ca.creditFunding(), 1, 3, 1.) + " | " +
  330.                 FormatUtil.FormatDouble (ca.creditFX(), 1, 3, 1.) + " | " +
  331.                 FormatUtil.FormatDouble (ca.forwardFunding(), 1, 3, 1.) + " | " +
  332.                 FormatUtil.FormatDouble (ca.forwardFX(), 1, 3, 1.) + " | " +
  333.                 FormatUtil.FormatDouble (ca.fundingFX(), 1, 3, 1.) + " ||"
  334.             );
  335.         }

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

  337.         System.out.println();

  338.         EnvManager.TerminateEnv();
  339.     }
  340. }