CustomFRAVolatilityCurve.java

  1. package org.drip.sample.forwardvolatility;

  2. import java.util.Map;

  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.MarketParamsBuilder;
  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.*;
  19. import org.drip.state.discount.MergedDiscountForwardCurve;
  20. import org.drip.state.forward.ForwardCurve;
  21. import org.drip.state.identifier.ForwardLabel;
  22. import org.drip.state.volatility.VolatilityCurve;

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

  26. /*!
  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 risk, transaction costs, exposure, margin
  34.  *      calculations, valuation adjustment, and portfolio construction within and across fixed income,
  35.  *      credit, commodity, equity, FX, and structured products.
  36.  *  
  37.  *      https://lakshmidrip.github.io/DROP/
  38.  *  
  39.  *  DROP is composed of three modules:
  40.  *  
  41.  *  - DROP Analytics Core - https://lakshmidrip.github.io/DROP-Analytics-Core/
  42.  *  - DROP Portfolio Core - https://lakshmidrip.github.io/DROP-Portfolio-Core/
  43.  *  - DROP Numerical Core - https://lakshmidrip.github.io/DROP-Numerical-Core/
  44.  *
  45.  *  DROP Analytics Core implements libraries for the following:
  46.  *  - Fixed Income Analytics
  47.  *  - Asset Backed Analytics
  48.  *  - XVA Analytics
  49.  *  - Exposure and Margin Analytics
  50.  *
  51.  *  DROP Portfolio Core implements libraries for the following:
  52.  *  - Asset Allocation Analytics
  53.  *  - Transaction Cost Analytics
  54.  *
  55.  *  DROP Numerical Core implements libraries for the following:
  56.  *  - Statistical Learning
  57.  *  - Numerical Optimizer
  58.  *  - Spline Builder
  59.  *  - Algorithm Support
  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>CustomFRAVolatilityCurve</i> demonstrates the Construction of the FRA Volatility Curve from the FRACap
  90.  * Quotes. The Marks and the Valuation References are sourced from:
  91.  *
  92.  * <br><br>
  93.  *  <ul>
  94.  *      <li>
  95.  *          Brace, A., D. Gatarek, and M. Musiela (1997): The Market Model of Interest Rate Dynamics
  96.  *              <i>Mathematical Finance</i> <b>7 (2)</b> 127-155
  97.  *      </li>
  98.  *  </ul>
  99.  *  
  100.  * <br><br>
  101.  *  <ul>
  102.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/AnalyticsCore.md">Analytics Core Module</a></li>
  103.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/FixedIncomeAnalyticsLibrary.md">Fixed Income Analytics Library</a></li>
  104.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/README.md">Sample</a></li>
  105.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/forwardvolatility/README.md">Forward Volatility Surface Builder</a></li>
  106.  *  </ul>
  107.  * <br><br>
  108.  *
  109.  * @author Lakshmi Krishnamurthy
  110.  */

  111. public class CustomFRAVolatilityCurve {

  112.     private static final FixFloatComponent OTCFixFloat (
  113.         final JulianDate dtSpot,
  114.         final String strCurrency,
  115.         final String strMaturityTenor,
  116.         final double dblCoupon)
  117.     {
  118.         FixedFloatSwapConvention ffConv = IBORFixedFloatContainer.ConventionFromJurisdiction (
  119.             strCurrency,
  120.             "ALL",
  121.             strMaturityTenor,
  122.             "MAIN"
  123.         );

  124.         return ffConv.createFixFloatComponent (
  125.             dtSpot,
  126.             strMaturityTenor,
  127.             dblCoupon,
  128.             0.,
  129.             1.
  130.         );
  131.     }

  132.     private static final CalibratableComponent[] DepositInstrumentsFromMaturityDays (
  133.         final JulianDate dtEffective,
  134.         final int[] aiDay,
  135.         final int iNumFuture,
  136.         final String strCurrency)
  137.         throws Exception
  138.     {
  139.         CalibratableComponent[] aCalibComp = new CalibratableComponent[aiDay.length + iNumFuture];

  140.         for (int i = 0; i < aiDay.length; ++i)
  141.             aCalibComp[i] = SingleStreamComponentBuilder.Deposit (
  142.                 dtEffective,
  143.                 dtEffective.addBusDays (
  144.                     aiDay[i],
  145.                     strCurrency
  146.                 ),
  147.                 ForwardLabel.Create (
  148.                     strCurrency,
  149.                     "3M"
  150.                 )
  151.             );

  152.         CalibratableComponent[] aEDF = SingleStreamComponentBuilder.ForwardRateFuturesPack (
  153.             dtEffective,
  154.             iNumFuture,
  155.             strCurrency
  156.         );

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

  159.         return aCalibComp;
  160.     }

  161.     private static final FixFloatComponent[] SwapInstrumentsFromMaturityTenor (
  162.         final JulianDate dtSpot,
  163.         final String strCurrency,
  164.         final String[] astrMaturityTenor,
  165.         final double[] adblCoupon)
  166.         throws Exception
  167.     {
  168.         FixFloatComponent[] aIRS = new FixFloatComponent[astrMaturityTenor.length];

  169.         for (int i = 0; i < astrMaturityTenor.length; ++i)
  170.             aIRS[i] = OTCFixFloat (
  171.                 dtSpot,
  172.                 strCurrency,
  173.                 astrMaturityTenor[i],
  174.                 adblCoupon[i]
  175.             );

  176.         return aIRS;
  177.     }

  178.     private static final MergedDiscountForwardCurve MakeDC (
  179.         final JulianDate dtSpot,
  180.         final String strCurrency)
  181.         throws Exception
  182.     {
  183.         /*
  184.          * Construct the array of Deposit instruments and their quotes.
  185.          */

  186.         CalibratableComponent[] aDepositComp = DepositInstrumentsFromMaturityDays (
  187.             dtSpot,
  188.             new int[] {
  189.                 30,
  190.                 60,
  191.                 91,
  192.                 182,
  193.                 273
  194.             },
  195.             0,
  196.             strCurrency
  197.         );

  198.         double[] adblDepositQuote = new double[] {
  199.             0.0668750,  //  30D
  200.             0.0675000,  //  60D
  201.             0.0678125,  //  91D
  202.             0.0712500,  // 182D
  203.             0.0750000   // 273D
  204.         };

  205.         String[] astrDepositManifestMeasure = new String[] {
  206.             "ForwardRate", //  30D
  207.             "ForwardRate", //  60D
  208.             "ForwardRate", //  91D
  209.             "ForwardRate", // 182D
  210.             "ForwardRate"  // 273D
  211.         };

  212.         /*
  213.          * Construct the array of Swap instruments and their quotes.
  214.          */

  215.         double[] adblSwapQuote = new double[] {
  216.             0.08265,    //  2Y
  217.             0.08550,    //  3Y
  218.             0.08655,    //  4Y
  219.             0.08770,    //  5Y
  220.             0.08910,    //  7Y
  221.             0.08920     // 10Y
  222.         };

  223.         String[] astrSwapManifestMeasure = new String[] {
  224.             "SwapRate",    //  2Y
  225.             "SwapRate",    //  3Y
  226.             "SwapRate",    //  4Y
  227.             "SwapRate",    //  5Y
  228.             "SwapRate",    //  7Y
  229.             "SwapRate"     // 10Y
  230.         };

  231.         CalibratableComponent[] aSwapComp = SwapInstrumentsFromMaturityTenor (
  232.             dtSpot,
  233.             strCurrency,
  234.             new String[] {
  235.                 "2Y",
  236.                 "3Y",
  237.                 "4Y",
  238.                 "5Y",
  239.                 "7Y",
  240.                 "10Y"
  241.             },
  242.             adblSwapQuote
  243.         );

  244.         /*
  245.          * Construct a shape preserving and smoothing KLK Hyperbolic Spline from the cash/swap instruments.
  246.          */

  247.         return ScenarioDiscountCurveBuilder.CubicKLKHyperbolicDFRateShapePreserver (
  248.             "KLK_HYPERBOLIC_SHAPE_TEMPLATE",
  249.             new ValuationParams (
  250.                 dtSpot,
  251.                 dtSpot,
  252.                 strCurrency
  253.             ),
  254.             aDepositComp,
  255.             adblDepositQuote,
  256.             astrDepositManifestMeasure,
  257.             aSwapComp,
  258.             adblSwapQuote,
  259.             astrSwapManifestMeasure,
  260.             false
  261.         );
  262.     }

  263.     private static final FRAStandardCapFloor MakeCap (
  264.         final JulianDate dtEffective,
  265.         final ForwardLabel fri,
  266.         final String strMaturityTenor,
  267.         final String strManifestMeasure,
  268.         final double dblStrike)
  269.         throws Exception
  270.     {
  271.         ComposableFloatingUnitSetting cfus = new ComposableFloatingUnitSetting (
  272.             fri.tenor(),
  273.             CompositePeriodBuilder.EDGE_DATE_SEQUENCE_SINGLE,
  274.             null,
  275.             fri,
  276.             CompositePeriodBuilder.REFERENCE_PERIOD_IN_ADVANCE,
  277.             0.
  278.         );

  279.         CompositePeriodSetting cps = new CompositePeriodSetting (
  280.             4,
  281.             fri.tenor(),
  282.             fri.currency(),
  283.             null,
  284.             1.,
  285.             null,
  286.             null,
  287.             null,
  288.             null
  289.         );

  290.         Stream floatStream = new Stream (
  291.             CompositePeriodBuilder.FloatingCompositeUnit (
  292.                 CompositePeriodBuilder.RegularEdgeDates (
  293.                     dtEffective.julian(),
  294.                     fri.tenor(),
  295.                     strMaturityTenor,
  296.                     null
  297.                 ),
  298.                 cps,
  299.                 cfus
  300.             )
  301.         );

  302.         return new FRAStandardCapFloor (
  303.             "FRA_CAP",
  304.             floatStream,
  305.             strManifestMeasure,
  306.             true,
  307.             dblStrike,
  308.             new LastTradingDateSetting (
  309.                 LastTradingDateSetting.MID_CURVE_OPTION_QUARTERLY,
  310.                 "",
  311.                 Integer.MIN_VALUE
  312.             ),
  313.             null,
  314.             new BlackScholesAlgorithm()
  315.         );
  316.     }

  317.     public static final void main (
  318.         final String[] astrArgs)
  319.         throws Exception
  320.     {
  321.         EnvManager.InitEnv ("");

  322.         JulianDate dtSpot = DateUtil.CreateFromYMD (
  323.             1995,
  324.             DateUtil.FEBRUARY,
  325.             3
  326.         );

  327.         String strFRATenor = "3M";
  328.         String strCurrency = "GBP";
  329.         String strManifestMeasure = "ParForward";

  330.         ForwardLabel fri = ForwardLabel.Create (
  331.             strCurrency,
  332.             strFRATenor
  333.         );

  334.         MergedDiscountForwardCurve dc = MakeDC (
  335.             dtSpot,
  336.             strCurrency
  337.         );

  338.         String[] astrMaturityTenor = new String[] {
  339.              "1Y",
  340.              "2Y",
  341.              "3Y",
  342.              "4Y",
  343.              "5Y",
  344.              "7Y",
  345.             "10Y"
  346.         };

  347.         double[] adblATMStrike = new double[] {
  348.             0.0788, //  "1Y",
  349.             0.0839, //  "2Y",
  350.             0.0864, //  "3Y",
  351.             0.0869, //  "4Y",
  352.             0.0879, //  "5Y",
  353.             0.0890, //  "7Y",
  354.             0.0889  // "10Y"
  355.         };

  356.         double[] adblATMPrice = new double[] {
  357.             0.0027, //  "1Y",
  358.             0.0152, //  "2Y",
  359.             0.0267, //  "3Y",
  360.             0.0400, //  "4Y",
  361.             0.0546, //  "5Y",
  362.             0.0835, //  "7Y"
  363.             0.1078  // "10Y"
  364.         };

  365.         String[] astrCalibMeasure = new String[astrMaturityTenor.length];
  366.         FRAStandardCapFloor[] aCap = new FRAStandardCapFloor[astrMaturityTenor.length];

  367.         for (int i = 0; i < astrMaturityTenor.length; ++i) {
  368.             astrCalibMeasure[i] = "Price";

  369.             aCap[i] = MakeCap (
  370.                 dtSpot,
  371.                 fri,
  372.                 astrMaturityTenor[i],
  373.                 strManifestMeasure,
  374.                 adblATMStrike[i]
  375.             );
  376.         }

  377.         VolatilityCurve volCurve = ScenarioLocalVolatilityBuilder.NonlinearBuild (
  378.             fri.fullyQualifiedName() + "::VOL",
  379.             dtSpot,
  380.             fri,
  381.             aCap,
  382.             adblATMPrice,
  383.             astrCalibMeasure,
  384.             dc,
  385.             dc.nativeForwardCurve (strFRATenor),
  386.             null
  387.         );

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

  389.         ValuationParams valParams = new ValuationParams (
  390.             dtSpot,
  391.             dtSpot,
  392.             strCurrency
  393.         );

  394.         CurveSurfaceQuoteContainer mktParams = MarketParamsBuilder.Create (
  395.             dc,
  396.             fcNative,
  397.             null,
  398.             null,
  399.             null,
  400.             null,
  401.             null,
  402.             null
  403.         );

  404.         mktParams.setForwardVolatility (volCurve);

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

  406.         System.out.println ("\t|                                                                                 ||");

  407.         System.out.println ("\t|  GBP LIBOR Volatility Calibrations                                              ||");

  408.         System.out.println ("\t|        - Forward Label                                                          ||");

  409.         System.out.println ("\t|        - Cap Stream Start                                                       ||");

  410.         System.out.println ("\t|        - Cap Stream End                                                         ||");

  411.         System.out.println ("\t|        - Market Price                                                           ||");

  412.         System.out.println ("\t|        - Recon Price                                                            ||");

  413.         System.out.println ("\t|        - Flat Cap Volatility                                                    ||");

  414.         System.out.println ("\t|        - Flat Forward Volatility                                                ||");

  415.         System.out.println ("\t|                                                                                 ||");

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

  417.         for (int i = 0; i < astrMaturityTenor.length; ++i) {
  418.             Map<String, Double> mapMeasures = aCap[i].value (
  419.                 valParams,
  420.                 null,
  421.                 mktParams,
  422.                 null
  423.             );

  424.             System.out.println (
  425.                 "\t| " + aCap[i].forwardLabel().get ("DERIVED").fullyQualifiedName() +
  426.                 " [" + aCap[i].stream().effective() + " - " + aCap[i].stream().maturity() + "] => " +
  427.                 FormatUtil.FormatDouble (mapMeasures.get ("Price"), 1, 4, 1.) + " {" +
  428.                 FormatUtil.FormatDouble (adblATMPrice[i], 1, 4, 1.) + "} | " +
  429.                 FormatUtil.FormatDouble (mapMeasures.get ("FlatVolatility"), 2, 2, 100.) + "% | " +
  430.                 FormatUtil.FormatDouble (volCurve.impliedVol (aCap[i].stream().maturity()), 2, 2, 100.) + "% ||"
  431.             );
  432.         }

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

  434.         EnvManager.TerminateEnv();
  435.     }
  436. }