TwoFactorLIBORVolatility.java

  1. package org.drip.sample.lmm;

  2. import org.drip.analytics.date.*;
  3. import org.drip.analytics.definition.MarketSurface;
  4. import org.drip.dynamics.lmm.LognormalLIBORVolatility;
  5. import org.drip.market.otc.*;
  6. import org.drip.numerical.common.FormatUtil;
  7. import org.drip.param.valuation.ValuationParams;
  8. import org.drip.product.creator.SingleStreamComponentBuilder;
  9. import org.drip.product.definition.CalibratableComponent;
  10. import org.drip.product.rates.FixFloatComponent;
  11. import org.drip.sequence.random.*;
  12. import org.drip.service.env.EnvManager;
  13. import org.drip.spline.basis.PolynomialFunctionSetParams;
  14. import org.drip.spline.params.*;
  15. import org.drip.spline.stretch.MultiSegmentSequenceBuilder;
  16. import org.drip.state.creator.*;
  17. import org.drip.state.discount.*;
  18. import org.drip.state.forward.ForwardCurve;
  19. import org.drip.state.identifier.ForwardLabel;

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

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

  85. /**
  86.  * <i>TwoFactorLIBORVolatility</i> demonstrates the Construction and Usage of the 2 Factor LIBOR Forward Rate
  87.  * Volatility. The References are:
  88.  *  
  89.  * <br><br>
  90.  *  <ul>
  91.  *      <li>
  92.  *          Goldys, B., M. Musiela, and D. Sondermann (1994): Log-normality of Rates and Term Structure
  93.  *              Models, The University of New South Wales.
  94.  *      </li>
  95.  *      <li>
  96.  *          Musiela, M. (1994): Nominal Annual Rates and Log-normal Volatility Structure, The University of
  97.  *              New South Wales.
  98.  *      </li>
  99.  *      <li>
  100.  *          Brace, A., D. Gatarek, and M. Musiela (1997): The Market Model of Interest Rate Dynamics,
  101.  *              Mathematical Finance 7 (2), 127-155.
  102.  *      </li>
  103.  *  </ul>
  104.  *  
  105.  * <br><br>
  106.  *  <ul>
  107.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/AnalyticsCore.md">Analytics Core Module</a></li>
  108.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/FixedIncomeAnalyticsLibrary.md">Fixed Income Analytics Library</a></li>
  109.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/README.md">Sample</a></li>
  110.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/lmm/README.md">LIBOR Market (LMM-BGM Variant) Evolution</a></li>
  111.  *  </ul>
  112.  * <br><br>
  113.  *
  114.  * @author Lakshmi Krishnamurthy
  115.  */

  116. public class TwoFactorLIBORVolatility {

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

  129.         return ffConv.createFixFloatComponent (
  130.             dtSpot,
  131.             strMaturityTenor,
  132.             dblCoupon,
  133.             0.,
  134.             1.
  135.         );
  136.     }

  137.     private static final CalibratableComponent[] DepositInstrumentsFromMaturityDays (
  138.         final JulianDate dtEffective,
  139.         final int[] aiDay,
  140.         final int iNumFuture,
  141.         final String strCurrency)
  142.         throws Exception
  143.     {
  144.         CalibratableComponent[] aCalibComp = new CalibratableComponent[aiDay.length + iNumFuture];

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

  157.         CalibratableComponent[] aEDF = SingleStreamComponentBuilder.ForwardRateFuturesPack (
  158.             dtEffective,
  159.             iNumFuture,
  160.             strCurrency
  161.         );

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

  164.         return aCalibComp;
  165.     }

  166.     private static final FixFloatComponent[] SwapInstrumentsFromMaturityTenor (
  167.         final JulianDate dtSpot,
  168.         final String strCurrency,
  169.         final String[] astrMaturityTenor,
  170.         final double[] adblCoupon)
  171.         throws Exception
  172.     {
  173.         FixFloatComponent[] aIRS = new FixFloatComponent[astrMaturityTenor.length];

  174.         for (int i = 0; i < astrMaturityTenor.length; ++i)
  175.             aIRS[i] = OTCFixFloat (
  176.                 dtSpot,
  177.                 strCurrency,
  178.                 astrMaturityTenor[i],
  179.                 adblCoupon[i]
  180.             );

  181.         return aIRS;
  182.     }

  183.     private static final MergedDiscountForwardCurve MakeDC (
  184.         final JulianDate dtSpot,
  185.         final String strCurrency)
  186.         throws Exception
  187.     {
  188.         /*
  189.          * Construct the array of Deposit instruments and their quotes.
  190.          */

  191.         CalibratableComponent[] aDepositComp = DepositInstrumentsFromMaturityDays (
  192.             dtSpot,
  193.             new int[] {
  194.                 30,
  195.                 60,
  196.                 91,
  197.                 182,
  198.                 273
  199.             },
  200.             0,
  201.             strCurrency
  202.         );

  203.         double[] adblDepositQuote = new double[] {
  204.             0.0668750,  //  30D
  205.             0.0675000,  //  60D
  206.             0.0678125,  //  91D
  207.             0.0712500,  // 182D
  208.             0.0750000   // 273D
  209.         };

  210.         String[] astrDepositManifestMeasure = new String[] {
  211.             "ForwardRate", //  30D
  212.             "ForwardRate", //  60D
  213.             "ForwardRate", //  91D
  214.             "ForwardRate", // 182D
  215.             "ForwardRate"  // 273D
  216.         };

  217.         /*
  218.          * Construct the array of Swap instruments and their quotes.
  219.          */

  220.         double[] adblSwapQuote = new double[] {
  221.             0.08265,    //  2Y
  222.             0.08550,    //  3Y
  223.             0.08655,    //  4Y
  224.             0.08770,    //  5Y
  225.             0.08910,    //  7Y
  226.             0.08920     // 10Y
  227.         };

  228.         String[] astrSwapManifestMeasure = new String[] {
  229.             "SwapRate",    //  2Y
  230.             "SwapRate",    //  3Y
  231.             "SwapRate",    //  4Y
  232.             "SwapRate",    //  5Y
  233.             "SwapRate",    //  7Y
  234.             "SwapRate"     // 10Y
  235.         };

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

  249.         /*
  250.          * Construct a shape preserving and smoothing KLK Hyperbolic Spline from the cash/swap instruments.
  251.          */

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

  268.     private static final MarketSurface FactorFlatVolatilitySurface (
  269.         final JulianDate dtStart,
  270.         final String strCurrency,
  271.         final String[] astrMaturityTenor,
  272.         final double[] adblFactorFlatVolatility,
  273.         final double[] adblTermStructureLoading)
  274.         throws Exception
  275.     {
  276.         int iNumNode = astrMaturityTenor.length + 1;
  277.         double[] adblDate = new double[iNumNode];
  278.         double[][] aadblVolatility = new double[iNumNode][iNumNode];

  279.         for (int i = 0; i < iNumNode; ++i)
  280.             adblDate[i] = 0 == i ? adblDate[i] = dtStart.julian() : dtStart.addTenor (astrMaturityTenor[i - 1]).julian();

  281.         for (int i = 0; i < iNumNode; ++i) {
  282.             for (int j = 0; j < iNumNode; ++j)
  283.                 aadblVolatility[i][j] =
  284.                     0 == i || 0 == j ?
  285.                     adblFactorFlatVolatility[0] :
  286.                     adblTermStructureLoading[i - 1] * adblFactorFlatVolatility[j - 1];
  287.         }

  288.         return ScenarioMarketSurfaceBuilder.CustomSplineWireSurface (
  289.             "VIEW_TARGET_VOLATILITY_SURFACE",
  290.             dtStart,
  291.             strCurrency,
  292.             adblDate,
  293.             adblDate,
  294.             aadblVolatility,
  295.             new SegmentCustomBuilderControl (
  296.                 MultiSegmentSequenceBuilder.BASIS_SPLINE_POLYNOMIAL,
  297.                 new PolynomialFunctionSetParams (2),
  298.                 SegmentInelasticDesignControl.Create (
  299.                     0,
  300.                     2
  301.                 ),
  302.                 null,
  303.                 null
  304.             ),
  305.             new SegmentCustomBuilderControl (
  306.                 MultiSegmentSequenceBuilder.BASIS_SPLINE_POLYNOMIAL,
  307.                 new PolynomialFunctionSetParams (4),
  308.                 SegmentInelasticDesignControl.Create (
  309.                     2,
  310.                     2
  311.                 ),
  312.                 null,
  313.                 null
  314.             )
  315.         );
  316.     }

  317.     private static final void DisplayVolArray (
  318.         final String strTenor,
  319.         final double[] adblVol)
  320.     {
  321.         String strDump = "\t | " + strTenor + " =>  ";

  322.         for (int i = 0; i < adblVol.length; ++i)
  323.             strDump += FormatUtil.FormatDouble (adblVol[i], 1, 2, 100.) + "% | ";

  324.         System.out.println (strDump);
  325.     }

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

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

  336.         String strFRATenor = "3M";
  337.         String strCurrency = "GBP";

  338.         ForwardLabel forwardLabel = ForwardLabel.Create (
  339.             strCurrency,
  340.             strFRATenor
  341.         );

  342.         MergedDiscountForwardCurve dc = MakeDC (
  343.             dtSpot,
  344.             strCurrency
  345.         );

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

  347.         String[] astrMaturityTenor = new String[] {
  348.              "3M",
  349.              "6M",
  350.             "12M",
  351.             "18M",
  352.             "24M",
  353.             "30M",
  354.              "3Y",
  355.              "4Y",
  356.              "5Y",
  357.              "7Y",
  358.              "9Y",
  359.             "11Y"
  360.         };

  361.         double[] adblFlatTermStructure = new double[] {
  362.             1.00000000, //  "3M",
  363.             1.00000000, //  "6M",
  364.             0.99168448, // "12M",
  365.             1.00388389, // "18M",
  366.             1.00388389, // "24M",
  367.             1.07602593, // "30M",
  368.             1.07602593, //  "3Y",
  369.             1.04727642, //  "4Y",
  370.             1.02727799, //  "5Y",
  371.             0.96660430, //  "7Y",
  372.             0.93012459, //  "9Y",
  373.             0.81425256  // "11Y"
  374.         };

  375.         double[] adblFlatVolFactor1 = new double[] {
  376.             0.09481393, //  "3M",
  377.             0.08498925, //  "6M",
  378.             0.22939966, // "12M",
  379.             0.19166872, // "18M",
  380.             0.08232925, // "24M",
  381.             0.18548202, // "30M",
  382.             0.13817885, //  "3Y",
  383.             0.08562258, //  "4Y",
  384.             0.14547123, //  "5Y",
  385.             0.08869328, //  "7Y",
  386.             0.04121240, //  "9Y",
  387.             0.15206796  // "11Y"
  388.         };

  389.         double[] adblFlatVolFactor2 = new double[] {
  390.              0.12146092, //  "3M",
  391.              0.05117321, //  "6M",
  392.              0.09100802, // "12M",
  393.              0.02876211, // "18M",
  394.              0.01172983, // "24M",
  395.              0.00047705, // "30M",
  396.             -0.01160086, //  "3Y",
  397.             -0.04673283, //  "4Y",
  398.             -0.04181446, //  "5Y",
  399.             -0.05459175, //  "7Y",
  400.             -0.03631021, //  "9Y",
  401.             -0.16626765  // "11Y"
  402.         };

  403.         MarketSurface mktSurf1 = FactorFlatVolatilitySurface (
  404.             dtSpot,
  405.             strCurrency,
  406.             astrMaturityTenor,
  407.             adblFlatVolFactor1,
  408.             adblFlatTermStructure
  409.         );

  410.         MarketSurface mktSurf2 = FactorFlatVolatilitySurface (
  411.             dtSpot,
  412.             strCurrency,
  413.             astrMaturityTenor,
  414.             adblFlatVolFactor2,
  415.             adblFlatTermStructure
  416.         );

  417.         LognormalLIBORVolatility llv = new LognormalLIBORVolatility (
  418.             dtSpot.julian(),
  419.             forwardLabel,
  420.             new MarketSurface[] {
  421.                 mktSurf1,
  422.                 mktSurf2
  423.             },
  424.             new PrincipalFactorSequenceGenerator (
  425.                 new UnivariateSequenceGenerator[] {
  426.                     new BoxMullerGaussian (
  427.                         0.,
  428.                         1.
  429.                     ),
  430.                     new BoxMullerGaussian (
  431.                         0.,
  432.                         1.
  433.                     )
  434.                 },
  435.                 new double[][] {
  436.                     {1.0, 0.1},
  437.                     {0.1, 1.0},
  438.                 },
  439.                 2
  440.             )
  441.         );

  442.         String[] astrForwardTenor = {
  443.             "1Y", "2Y", "3Y", "4Y", "5Y", "6Y", "7Y", "8Y"
  444.         };

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

  446.         System.out.println ("\t |  CONTINUOUS FORWARD RATE VOL |");

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

  448.         for (String strForwardTenor : astrForwardTenor)
  449.             DisplayVolArray (
  450.                 strForwardTenor,
  451.                 llv.continuousForwardVolatility (
  452.                     dtSpot.addTenor (strForwardTenor).julian(),
  453.                     fcNative
  454.                 )
  455.             );

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

  457.         EnvManager.TerminateEnv();
  458.     }
  459. }