FRAStdCapModels.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.*;
  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>FRAStdCapModels</i> runs a side-by-side comparison of the FRA Cap sequence using different models.
  100.  *
  101.  * <br><br>
  102.  *  <ul>
  103.  *      <li>
  104.  *          Brace, A., D. Gatarek, and M. Musiela (1997): The Market Model of Interest Rate Dynamics
  105.  *              <i>Mathematical Finance</i> <b>7 (2)</b> 127-155
  106.  *      </li>
  107.  *  </ul>
  108.  *  
  109.  * <br><br>
  110.  *  <ul>
  111.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ProductCore.md">Product Core Module</a></li>
  112.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/FixedIncomeAnalyticsLibrary.md">Fixed Income Analytics</a></li>
  113.  *      <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>
  114.  *      <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>
  115.  *  </ul>
  116.  * <br><br>
  117.  *
  118.  * @author Lakshmi Krishnamurthy
  119.  */

  120. public class FRAStdCapModels {

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

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

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

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

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

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

  168.         return aCalibComp;
  169.     }

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

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

  185.         return aIRS;
  186.     }

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

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

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

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

  221.         /*
  222.          * Construct the array of Swap instruments and their quotes.
  223.          */

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

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

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

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

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

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

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

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

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

  327.     private static final Map<JulianDate, Double> ValueCap (
  328.         final ForwardLabel fri,
  329.         final String strManifestMeasure,
  330.         final ValuationParams valParams,
  331.         final CurveSurfaceQuoteContainer mktParams,
  332.         final String[] astrMaturityTenor,
  333.         final double[] adblATMStrike,
  334.         final double[] adblATMVol,
  335.         final FokkerPlanckGenerator fpg)
  336.         throws Exception
  337.     {
  338.         Map<JulianDate, Double> mapDateVol = new TreeMap<JulianDate, Double>();

  339.         for (int i = 0; i < astrMaturityTenor.length; ++i) {
  340.             FRAStandardCapFloor cap = MakeCap (
  341.                 new JulianDate (valParams.valueDate()),
  342.                 fri,
  343.                 astrMaturityTenor[i],
  344.                 strManifestMeasure,
  345.                 adblATMStrike[i],
  346.                 fpg
  347.             );

  348.             Map<String, Double> mapCapStreamOutput = cap.stream().value (
  349.                 valParams,
  350.                 null,
  351.                 mktParams,
  352.                 null
  353.             );

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

  355.             FixFloatComponent swap = OTCFixFloat (
  356.                 new JulianDate (valParams.valueDate()),
  357.                 fri.currency(),
  358.                 astrMaturityTenor[i],
  359.                 0.
  360.             );

  361.             Map<String, Double> mapSwapOutput = swap.value (
  362.                 valParams,
  363.                 null,
  364.                 mktParams,
  365.                 null
  366.             );

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

  368.             double dblCapPrice = cap.priceFromFlatVolatility (
  369.                 valParams,
  370.                 null,
  371.                 mktParams,
  372.                 null,
  373.                 adblATMVol[i]
  374.             );

  375.             cap.stripPiecewiseForwardVolatility (
  376.                 valParams,
  377.                 null,
  378.                 mktParams,
  379.                 null,
  380.                 adblATMVol[i],
  381.                 mapDateVol
  382.             );

  383.             System.out.println (
  384.                 "\tCap  " + cap.maturityDate() + " | " +
  385.                 FormatUtil.FormatDouble (dblCapStreamFairPremium, 1, 2, 100.) + "% |" +
  386.                 FormatUtil.FormatDouble (dblSwapRate, 1, 2, 100.) + "% |" +
  387.                 FormatUtil.FormatDouble (cap.strike(), 1, 2, 100.) + "% |" +
  388.                 FormatUtil.FormatDouble (adblATMVol[i], 2, 2, 100.) + "% |" +
  389.                 FormatUtil.FormatDouble (dblCapPrice, 1, 0, 10000.) + " ||"
  390.             );
  391.         }

  392.         return mapDateVol;
  393.     }

  394.     public static final void main (
  395.         final String[] astrArgs)
  396.         throws Exception
  397.     {
  398.         EnvManager.InitEnv ("");

  399.         JulianDate dtSpot = DateUtil.CreateFromYMD (
  400.             1995,
  401.             DateUtil.FEBRUARY,
  402.             3
  403.         );

  404.         String strFRATenor = "3M";
  405.         String strCurrency = "GBP";
  406.         String strManifestMeasure = "ParForward";

  407.         ForwardLabel fri = ForwardLabel.Create (
  408.             strCurrency,
  409.             strFRATenor
  410.         );

  411.         MergedDiscountForwardCurve dc = MakeDC (
  412.             dtSpot,
  413.             strCurrency
  414.         );

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

  416.         ValuationParams valParams = new ValuationParams (
  417.             dtSpot,
  418.             dtSpot,
  419.             strCurrency
  420.         );

  421.         CurveSurfaceQuoteContainer mktParams = MarketParamsBuilder.Create (
  422.             dc,
  423.             fcNative,
  424.             null,
  425.             null,
  426.             null,
  427.             null,
  428.             null,
  429.             null
  430.         );

  431.         String[] astrMaturityTenor = new String[] {
  432.              "1Y",
  433.              "2Y",
  434.              "3Y",
  435.              "4Y",
  436.              "5Y",
  437.              "7Y",
  438.             "10Y"
  439.         };

  440.         double[] adblATMStrike = new double[] {
  441.             0.0788, //  "1Y",
  442.             0.0839, //  "2Y",
  443.             0.0864, //  "3Y",
  444.             0.0869, //  "4Y",
  445.             0.0879, //  "5Y",
  446.             0.0890, //  "7Y",
  447.             0.0889  // "10Y"
  448.         };

  449.         double[] adblATMVol = new double[] {
  450.             0.1550, //  "1Y",
  451.             0.1775, //  "2Y",
  452.             0.1800, //  "3Y",
  453.             0.1775, //  "4Y",
  454.             0.1775, //  "5Y",
  455.             0.1650, //  "7Y",
  456.             0.1550  // "10Y"
  457.         };

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

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

  460.         Map<JulianDate, Double> mapLognormalDateVol = ValueCap (
  461.             fri,
  462.             strManifestMeasure,
  463.             valParams,
  464.             mktParams,
  465.             astrMaturityTenor,
  466.             adblATMStrike,
  467.             adblATMVol,
  468.             new BlackScholesAlgorithm()
  469.         );

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

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

  472.         Map<JulianDate, Double> mapNormalDateVol = ValueCap (
  473.             fri,
  474.             strManifestMeasure,
  475.             valParams,
  476.             mktParams,
  477.             astrMaturityTenor,
  478.             adblATMStrike,
  479.             adblATMVol,
  480.             new BlackNormalAlgorithm()
  481.         );

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

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

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

  485.         for (Map.Entry<JulianDate, Double> me : mapLognormalDateVol.entrySet())
  486.             System.out.println (
  487.                 "\t" +
  488.                 me.getKey() + " => " +
  489.                 FormatUtil.FormatDouble (me.getValue(), 2, 2, 100.) + "%  |" +
  490.                 FormatUtil.FormatDouble (mapNormalDateVol.get (me.getKey()), 2, 2, 100.) + "%  ||"
  491.             );

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

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

  494.         EnvManager.TerminateEnv();
  495.     }
  496. }