FRAStandardPeriods.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.FRAStandardComponent;
  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>FRAStandardPeriods</i> demonstrates the Cash Flow Period Details for a Standard 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 FRAStandardPeriods {

  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.         System.out.println();

  195.         MergedDiscountForwardCurve mdfc = FundingCurve (
  196.             dtSpot,
  197.             strCurrency,
  198.             0.
  199.         );

  200.         CurveSurfaceQuoteContainer csqc = MarketParamsBuilder.Create (
  201.             mdfc,
  202.             null,
  203.             null,
  204.             null,
  205.             null,
  206.             null,
  207.             null
  208.         );

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  231.         for (CompositePeriod p : fraStandard.couponPeriods()) {
  232.             int iEndDate = p.endDate();

  233.             CompositeFloatingPeriod cfp = (CompositeFloatingPeriod) p;

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

  235.             ReferenceIndexPeriod rip = cufp.referenceIndexPeriod();

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

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

  256.         System.out.println();

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

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

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

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

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

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

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

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

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

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

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

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

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

  270.         for (CompositePeriod p : fraStandard.couponPeriods()) {
  271.             int iEndDate = p.endDate();

  272.             int iPayDate = p.payDate();

  273.             int iStartDate = p.startDate();

  274.             double dblCouponRate = fraStandard.couponMetrics (
  275.                 iPayDate,
  276.                 valParams,
  277.                 csqc
  278.             ).rate();

  279.             double dblCouponDCF = p.couponDCF();

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

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

  293.         System.out.println();

  294.         System.out.println();

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  310.         for (CompositePeriod p : fraStandard.couponPeriods()) {
  311.             ConvexityAdjustment ca = p.terminalConvexityAdjustment (
  312.                 dtSpot.julian(),
  313.                 csqc
  314.             );

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

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

  331.         System.out.println();

  332.         EnvManager.TerminateEnv();
  333.     }
  334. }