FRAStdCapSequence.java

  1. package org.drip.sample.capfloor;

  2. import java.util.*;

  3. import org.drip.analytics.date.*;
  4. import org.drip.analytics.support.CompositePeriodBuilder;
  5. import org.drip.market.otc.*;
  6. import org.drip.numerical.common.FormatUtil;
  7. import org.drip.param.creator.*;
  8. import org.drip.param.market.CurveSurfaceQuoteContainer;
  9. import org.drip.param.period.*;
  10. import org.drip.param.valuation.ValuationParams;
  11. import org.drip.pricer.option.BlackScholesAlgorithm;
  12. import org.drip.product.creator.SingleStreamComponentBuilder;
  13. import org.drip.product.definition.CalibratableComponent;
  14. import org.drip.product.fra.FRAStandardCapFloor;
  15. import org.drip.product.params.LastTradingDateSetting;
  16. import org.drip.product.rates.*;
  17. import org.drip.service.env.EnvManager;
  18. import org.drip.state.creator.ScenarioDiscountCurveBuilder;
  19. import org.drip.state.discount.*;
  20. import org.drip.state.forward.ForwardCurve;
  21. import org.drip.state.identifier.ForwardLabel;

  22. /*
  23.  * -*- mode: java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  24.  */

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

  98. /**
  99.  * <i>FRAStdCapSequence</i> demonstrates the Product Creation, Market Parameters Construction, and Valuation
  100.  * of a Sequence of Standard FRA Caps. The Marks and the Valuation References are sourced from the reference
  101.  * below.
  102.  *
  103.  * <br><br>
  104.  *  <ul>
  105.  *      <li>
  106.  *          Brace, A., D. Gatarek, and M. Musiela (1997): The Market Model of Interest Rate Dynamics
  107.  *              <i>Mathematical Finance</i> <b>7 (2)</b> 127-155
  108.  *      </li>
  109.  *  </ul>
  110.  *  
  111.  * <br><br>
  112.  *  <ul>
  113.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ProductCore.md">Product Core Module</a></li>
  114.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/FixedIncomeAnalyticsLibrary.md">Fixed Income Analytics</a></li>
  115.  *      <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>
  116.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/capfloor/README.md">FRA Standard Cap Floor Valuation</a></li>
  117.  *  </ul>
  118.  * <br><br>
  119.  *
  120.  * @author Lakshmi Krishnamurthy
  121.  */

  122. public class FRAStdCapSequence {

  123.     private static final FixFloatComponent OTCFixFloat (
  124.         final JulianDate dtSpot,
  125.         final String strCurrency,
  126.         final String strMaturityTenor,
  127.         final double dblCoupon)
  128.     {
  129.         FixedFloatSwapConvention ffConv = IBORFixedFloatContainer.ConventionFromJurisdiction (
  130.             strCurrency,
  131.             "ALL",
  132.             strMaturityTenor,
  133.             "MAIN"
  134.         );

  135.         return ffConv.createFixFloatComponent (
  136.             dtSpot,
  137.             strMaturityTenor,
  138.             dblCoupon,
  139.             0.,
  140.             1.
  141.         );
  142.     }

  143.     private static final CalibratableComponent[] DepositInstrumentsFromMaturityDays (
  144.         final JulianDate dtEffective,
  145.         final int[] aiDay,
  146.         final int iNumFuture,
  147.         final String strCurrency)
  148.         throws Exception
  149.     {
  150.         CalibratableComponent[] aCalibComp = new CalibratableComponent[aiDay.length + iNumFuture];

  151.         for (int i = 0; i < aiDay.length; ++i)
  152.             aCalibComp[i] = SingleStreamComponentBuilder.Deposit (
  153.                 dtEffective,
  154.                 dtEffective.addBusDays (
  155.                     aiDay[i],
  156.                     strCurrency
  157.                 ),
  158.                 ForwardLabel.Create (
  159.                     strCurrency,
  160.                     "3M"
  161.                 )
  162.             );

  163.         CalibratableComponent[] aEDF = SingleStreamComponentBuilder.ForwardRateFuturesPack (
  164.             dtEffective,
  165.             iNumFuture,
  166.             strCurrency
  167.         );

  168.         for (int i = aiDay.length; i < aiDay.length + iNumFuture; ++i)
  169.             aCalibComp[i] = aEDF[i - aiDay.length];

  170.         return aCalibComp;
  171.     }

  172.     private static final FixFloatComponent[] SwapInstrumentsFromMaturityTenor (
  173.         final JulianDate dtSpot,
  174.         final String strCurrency,
  175.         final String[] astrMaturityTenor,
  176.         final double[] adblCoupon)
  177.         throws Exception
  178.     {
  179.         FixFloatComponent[] aIRS = new FixFloatComponent[astrMaturityTenor.length];

  180.         for (int i = 0; i < astrMaturityTenor.length; ++i)
  181.             aIRS[i] = OTCFixFloat (
  182.                 dtSpot,
  183.                 strCurrency,
  184.                 astrMaturityTenor[i],
  185.                 adblCoupon[i]
  186.             );

  187.         return aIRS;
  188.     }

  189.     private static final MergedDiscountForwardCurve MakeDC (
  190.         final JulianDate dtSpot,
  191.         final String strCurrency)
  192.         throws Exception
  193.     {
  194.         /*
  195.          * Construct the array of Deposit instruments and their quotes.
  196.          */

  197.         CalibratableComponent[] aDepositComp = DepositInstrumentsFromMaturityDays (
  198.             dtSpot,
  199.             new int[] {
  200.                 30,
  201.                 60,
  202.                 91,
  203.                 182,
  204.                 273
  205.             },
  206.             0,
  207.             strCurrency
  208.         );

  209.         double[] adblDepositQuote = new double[] {
  210.             0.0668750,  //  30D
  211.             0.0675000,  //  60D
  212.             0.0678125,  //  91D
  213.             0.0712500,  // 182D
  214.             0.0750000   // 273D
  215.         };

  216.         String[] astrDepositManifestMeasure = new String[] {
  217.             "ForwardRate", //  30D
  218.             "ForwardRate", //  60D
  219.             "ForwardRate", //  91D
  220.             "ForwardRate", // 182D
  221.             "ForwardRate"  // 273D
  222.         };

  223.         /*
  224.          * Construct the array of Swap instruments and their quotes.
  225.          */

  226.         double[] adblSwapQuote = new double[] {
  227.             0.08265,    //  2Y
  228.             0.08550,    //  3Y
  229.             0.08655,    //  4Y
  230.             0.08770,    //  5Y
  231.             0.08910,    //  7Y
  232.             0.08920     // 10Y
  233.         };

  234.         String[] astrSwapManifestMeasure = new String[] {
  235.             "SwapRate",    //  2Y
  236.             "SwapRate",    //  3Y
  237.             "SwapRate",    //  4Y
  238.             "SwapRate",    //  5Y
  239.             "SwapRate",    //  7Y
  240.             "SwapRate"     // 10Y
  241.         };

  242.         CalibratableComponent[] aSwapComp = SwapInstrumentsFromMaturityTenor (
  243.             dtSpot,
  244.             strCurrency,
  245.             new java.lang.String[] {
  246.                 "2Y",
  247.                 "3Y",
  248.                 "4Y",
  249.                 "5Y",
  250.                 "7Y",
  251.                 "10Y"
  252.             },
  253.             adblSwapQuote
  254.         );

  255.         /*
  256.          * Construct a shape preserving and smoothing KLK Hyperbolic Spline from the cash/swap instruments.
  257.          */

  258.         return ScenarioDiscountCurveBuilder.CubicKLKHyperbolicDFRateShapePreserver (
  259.             "KLK_HYPERBOLIC_SHAPE_TEMPLATE",
  260.             new ValuationParams (
  261.                 dtSpot,
  262.                 dtSpot,
  263.                 strCurrency
  264.             ),
  265.             aDepositComp,
  266.             adblDepositQuote,
  267.             astrDepositManifestMeasure,
  268.             aSwapComp,
  269.             adblSwapQuote,
  270.             astrSwapManifestMeasure,
  271.             false
  272.         );
  273.     }

  274.     private static final FRAStandardCapFloor MakeCap (
  275.         final JulianDate dtEffective,
  276.         final ForwardLabel fri,
  277.         final String strMaturityTenor,
  278.         final String strManifestMeasure,
  279.         final double dblStrike)
  280.         throws Exception
  281.     {
  282.         ComposableFloatingUnitSetting cfus = new ComposableFloatingUnitSetting (
  283.             fri.tenor(),
  284.             CompositePeriodBuilder.EDGE_DATE_SEQUENCE_SINGLE,
  285.             null,
  286.             fri,
  287.             CompositePeriodBuilder.REFERENCE_PERIOD_IN_ADVANCE,
  288.             0.
  289.         );

  290.         CompositePeriodSetting cps = new CompositePeriodSetting (
  291.             4,
  292.             fri.tenor(),
  293.             fri.currency(),
  294.             null,
  295.             1.,
  296.             null,
  297.             null,
  298.             null,
  299.             null
  300.         );

  301.         Stream floatStream = new Stream (
  302.             CompositePeriodBuilder.FloatingCompositeUnit (
  303.                 CompositePeriodBuilder.RegularEdgeDates (
  304.                     dtEffective.julian(),
  305.                     fri.tenor(),
  306.                     strMaturityTenor,
  307.                     null
  308.                 ),
  309.                 cps,
  310.                 cfus
  311.             )
  312.         );

  313.         return new FRAStandardCapFloor (
  314.             "FRA_CAP",
  315.             floatStream,
  316.             strManifestMeasure,
  317.             true,
  318.             dblStrike,
  319.             new LastTradingDateSetting (
  320.                 LastTradingDateSetting.MID_CURVE_OPTION_QUARTERLY,
  321.                 "",
  322.                 Integer.MIN_VALUE
  323.             ),
  324.             null,
  325.             new BlackScholesAlgorithm()
  326.         );
  327.     }

  328.     public static final void main (
  329.         final String[] astrArgs)
  330.         throws Exception
  331.     {
  332.         EnvManager.InitEnv ("");

  333.         JulianDate dtSpot = DateUtil.CreateFromYMD (
  334.             1995,
  335.             DateUtil.FEBRUARY,
  336.             3
  337.         );

  338.         String strFRATenor = "3M";
  339.         String strCurrency = "GBP";
  340.         String strManifestMeasure = "ParForward";

  341.         ForwardLabel fri = ForwardLabel.Create (
  342.             strCurrency,
  343.             strFRATenor
  344.         );

  345.         MergedDiscountForwardCurve dc = MakeDC (
  346.             dtSpot,
  347.             strCurrency
  348.         );

  349.         ForwardCurve fcNative = dc.nativeForwardCurve (strFRATenor);

  350.         ValuationParams valParams = new ValuationParams (
  351.             dtSpot,
  352.             dtSpot,
  353.             strCurrency
  354.         );

  355.         CurveSurfaceQuoteContainer mktParams = MarketParamsBuilder.Create (
  356.             dc,
  357.             fcNative,
  358.             null,
  359.             null,
  360.             null,
  361.             null,
  362.             null,
  363.             null
  364.         );

  365.         String[] astrMaturityTenor = new String[] {
  366.              "1Y",
  367.              "2Y",
  368.              "3Y",
  369.              "4Y",
  370.              "5Y",
  371.              "7Y",
  372.             "10Y"
  373.         };

  374.         double[] adblATMStrike = new double[] {
  375.             0.0788, //  "1Y",
  376.             0.0839, //  "2Y",
  377.             0.0864, //  "3Y",
  378.             0.0869, //  "4Y",
  379.             0.0879, //  "5Y",
  380.             0.0890, //  "7Y",
  381.             0.0889  // "10Y"
  382.         };

  383.         double[] adblATMVol = new double[] {
  384.             0.1550, //  "1Y",
  385.             0.1775, //  "2Y",
  386.             0.1800, //  "3Y",
  387.             0.1775, //  "4Y",
  388.             0.1775, //  "5Y",
  389.             0.1650, //  "7Y",
  390.             0.1550  // "10Y"
  391.         };

  392.         Map<JulianDate, Double> mapDateVol = new TreeMap<JulianDate, Double>();

  393.         for (int i = 0; i < astrMaturityTenor.length; ++i) {
  394.             FRAStandardCapFloor cap = MakeCap (
  395.                 dtSpot,
  396.                 fri,
  397.                 astrMaturityTenor[i],
  398.                 strManifestMeasure,
  399.                 adblATMStrike[i]
  400.             );

  401.             Map<String, Double> mapCapStreamOutput = cap.stream().value (
  402.                 valParams,
  403.                 null,
  404.                 mktParams,
  405.                 null
  406.             );

  407.             double dblCapStreamFairPremium = mapCapStreamOutput.get ("FairPremium");

  408.             FixFloatComponent swap = OTCFixFloat (
  409.                 dtSpot,
  410.                 strCurrency,
  411.                 astrMaturityTenor[i],
  412.                 0.
  413.             );

  414.             Map<String, Double> mapSwapOutput = swap.value (
  415.                 valParams,
  416.                 null,
  417.                 mktParams,
  418.                 null
  419.             );

  420.             double dblSwapRate = mapSwapOutput.get ("FairPremium");

  421.             double dblCapPrice = cap.priceFromFlatVolatility (
  422.                 valParams,
  423.                 null,
  424.                 mktParams,
  425.                 null,
  426.                 adblATMVol[i]
  427.             );

  428.             cap.stripPiecewiseForwardVolatility (
  429.                 valParams,
  430.                 null,
  431.                 mktParams,
  432.                 null,
  433.                 adblATMVol[i],
  434.                 mapDateVol
  435.             );

  436.             System.out.println (
  437.                 "\tCap  " + cap.maturityDate() + " | " +
  438.                 FormatUtil.FormatDouble (dblCapStreamFairPremium, 1, 2, 100.) + "% |" +
  439.                 FormatUtil.FormatDouble (dblSwapRate, 1, 2, 100.) + "% |" +
  440.                 FormatUtil.FormatDouble (cap.strike(), 1, 2, 100.) + "% |" +
  441.                 FormatUtil.FormatDouble (adblATMVol[i], 2, 2, 100.) + "% |" +
  442.                 FormatUtil.FormatDouble (dblCapPrice, 1, 0, 10000.) + " ||"
  443.             );
  444.         }

  445.         System.out.println ("\n\n\t---------------------------------------------------");

  446.         System.out.println ("\t-----  CALIBRATED FORWARD VOLATILITY NODES --------");

  447.         System.out.println ("\t---------------------------------------------------\n");

  448.         for (Map.Entry<JulianDate, Double> me : mapDateVol.entrySet())
  449.             System.out.println (
  450.                 "\t" +
  451.                 me.getKey() + " => " +
  452.                 FormatUtil.FormatDouble (me.getValue(), 2, 2, 100.) + "%  ||"
  453.             );

  454.         System.out.println ("\t---------------------------------------------------");

  455.         System.out.println ("\t---------------------------------------------------------------");

  456.         double[] adblATMPrice = new double[] {
  457.             0.0027, //  "1Y",
  458.             0.0100, //  "2Y",
  459.             0.0185, //  "3Y",
  460.             0.0267, //  "4Y",
  461.             0.0360, //  "5Y",
  462.             0.0511, //  "7Y",
  463.             0.0703  // "10Y"
  464.         };

  465.         for (int i = 0; i < astrMaturityTenor.length; ++i) {
  466.             FRAStandardCapFloor cap = MakeCap (
  467.                 dtSpot,
  468.                 fri,
  469.                 astrMaturityTenor[i],
  470.                 strManifestMeasure,
  471.                 adblATMStrike[i]
  472.             );

  473.             double dblATMVolatility = cap.volatilityFromATMPrice (
  474.                 valParams,
  475.                 null,
  476.                 mktParams,
  477.                 null,
  478.                 adblATMPrice[i]
  479.             );

  480.             Map<String, Double> mapCapStreamOutput = cap.stream().value (
  481.                 valParams,
  482.                 null,
  483.                 mktParams,
  484.                 null
  485.             );

  486.             double dblCapStreamFairPremium = mapCapStreamOutput.get ("FairPremium");

  487.             System.out.println (
  488.                 "\tCap ATM Volatility  " + cap.maturityDate() + " | " +
  489.                 FormatUtil.FormatDouble (adblATMPrice[i], 2, 2, 100.) + "% |" +
  490.                 FormatUtil.FormatDouble (dblCapStreamFairPremium, 2, 2, 100.) + "% |" +
  491.                 FormatUtil.FormatDouble (dblATMVolatility, 2, 2, 100.) + "% ||"
  492.             );
  493.         }

  494.         System.out.println ("\t---------------------------------------------------------------");

  495.         System.out.println ("\t---------------------------------------------------------------");

  496.         EnvManager.TerminateEnv();
  497.     }
  498. }