ContinuousForwardRateVolatility.java

  1. package org.drip.sample.lmm;

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

  79. /**
  80.  * <i>ContinuousForwardRateVolatility</i> demonstrates the Implying of the Volatility of the Continuously
  81.  * Compounded Forward Rate from the Corresponding LIBOR Forward Rate Volatility. The References are:
  82.  *  
  83.  * <br><br>
  84.  *  <ul>
  85.  *      <li>
  86.  *          Goldys, B., M. Musiela, and D. Sondermann (1994): Log-normality of Rates and Term Structure
  87.  *              Models, The University of New South Wales.
  88.  *      </li>
  89.  *      <li>
  90.  *          Musiela, M. (1994): Nominal Annual Rates and Log-normal Volatility Structure, The University of
  91.  *              New South Wales.
  92.  *      </li>
  93.  *      <li>
  94.  *          Brace, A., D. Gatarek, and M. Musiela (1997): The Market Model of Interest Rate Dynamics,
  95.  *              Mathematical Finance 7 (2), 127-155.
  96.  *      </li>
  97.  *  </ul>
  98.  *  
  99.  * <br><br>
  100.  *  <ul>
  101.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/AnalyticsCore.md">Analytics Core Module</a></li>
  102.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/FixedIncomeAnalyticsLibrary.md">Fixed Income Analytics Library</a></li>
  103.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/README.md">Sample</a></li>
  104.  *      <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>
  105.  *  </ul>
  106.  * <br><br>
  107.  *
  108.  * @author Lakshmi Krishnamurthy
  109.  */

  110. public class ContinuousForwardRateVolatility {

  111.     private static final MarketSurface FlatVolatilitySurface (
  112.         final JulianDate dtStart,
  113.         final String strCurrency,
  114.         final double dblFlatVol)
  115.         throws Exception
  116.     {
  117.         return ScenarioMarketSurfaceBuilder.CustomSplineWireSurface (
  118.             "VIEW_TARGET_VOLATILITY_SURFACE",
  119.             dtStart,
  120.             strCurrency,
  121.             new double[] {
  122.                 dtStart.julian(),
  123.                 dtStart.addYears (2).julian(),
  124.                 dtStart.addYears (4).julian(),
  125.                 dtStart.addYears (6).julian(),
  126.                 dtStart.addYears (8).julian(),
  127.                 dtStart.addYears (10).julian()
  128.             },
  129.             new double[] {
  130.                 dtStart.julian(),
  131.                 dtStart.addYears (2).julian(),
  132.                 dtStart.addYears (4).julian(),
  133.                 dtStart.addYears (6).julian(),
  134.                 dtStart.addYears (8).julian(),
  135.                 dtStart.addYears (10).julian()
  136.             },
  137.             new double[][] {
  138.                 {dblFlatVol, dblFlatVol, dblFlatVol, dblFlatVol, dblFlatVol, dblFlatVol},
  139.                 {dblFlatVol, dblFlatVol, dblFlatVol, dblFlatVol, dblFlatVol, dblFlatVol},
  140.                 {dblFlatVol, dblFlatVol, dblFlatVol, dblFlatVol, dblFlatVol, dblFlatVol},
  141.                 {dblFlatVol, dblFlatVol, dblFlatVol, dblFlatVol, dblFlatVol, dblFlatVol},
  142.                 {dblFlatVol, dblFlatVol, dblFlatVol, dblFlatVol, dblFlatVol, dblFlatVol},
  143.                 {dblFlatVol, dblFlatVol, dblFlatVol, dblFlatVol, dblFlatVol, dblFlatVol},
  144.             },
  145.             new SegmentCustomBuilderControl (
  146.                 MultiSegmentSequenceBuilder.BASIS_SPLINE_POLYNOMIAL,
  147.                 new PolynomialFunctionSetParams (4),
  148.                 SegmentInelasticDesignControl.Create (
  149.                     2,
  150.                     2
  151.                 ),
  152.                 null,
  153.                 null
  154.             ),
  155.             new SegmentCustomBuilderControl (
  156.                 MultiSegmentSequenceBuilder.BASIS_SPLINE_POLYNOMIAL,
  157.                 new PolynomialFunctionSetParams (4),
  158.                 SegmentInelasticDesignControl.Create (
  159.                     2,
  160.                     2
  161.                 ),
  162.                 null,
  163.                 null
  164.             )
  165.         );
  166.     }

  167.     private static final void DisplayVolArray (
  168.         final String strTenor,
  169.         final double[] adblVol)
  170.     {
  171.         String strDump = "\t | " + strTenor + " => ";

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

  174.         System.out.println (strDump);
  175.     }

  176.     public static void main (
  177.         final String[] astrArgs)
  178.         throws Exception
  179.     {
  180.         EnvManager.InitEnv ("");

  181.         String strTenor = "3M";
  182.         String strCurrency = "USD";
  183.         double dblFlatVol1 = 0.35;
  184.         double dblFlatVol2 = 0.42;
  185.         double dblFlatVol3 = 0.27;
  186.         double dblFlatForwardRate = 0.02;

  187.         int[] aiNumFactor = {
  188.             1, 2, 3
  189.         };

  190.         String[] astrForwardTenor = {
  191.             "1Y", "2Y", "3Y", "4Y", "5Y", "6Y", "7Y", "8Y"
  192.         };

  193.         ForwardLabel forwardLabel = ForwardLabel.Create (
  194.             strCurrency,
  195.             strTenor
  196.         );

  197.         JulianDate dtSpot = org.drip.analytics.date.DateUtil.Today();

  198.         MarketSurface mktSurfFlatVol1 = FlatVolatilitySurface (
  199.             dtSpot,
  200.             strCurrency,
  201.             dblFlatVol1
  202.         );

  203.         MarketSurface mktSurfFlatVol2 = FlatVolatilitySurface (
  204.             dtSpot,
  205.             strCurrency,
  206.             dblFlatVol2
  207.         );

  208.         MarketSurface mktSurfFlatVol3 = FlatVolatilitySurface (
  209.             dtSpot,
  210.             strCurrency,
  211.             dblFlatVol3
  212.         );

  213.         ForwardCurve fc = ScenarioForwardCurveBuilder.FlatForwardForwardCurve (
  214.             dtSpot,
  215.             forwardLabel,
  216.             dblFlatForwardRate
  217.         );

  218.         for (int iNumFactor : aiNumFactor) {
  219.             LognormalLIBORVolatility llv = new LognormalLIBORVolatility (
  220.                 dtSpot.julian(),
  221.                 forwardLabel,
  222.                 new MarketSurface[] {
  223.                     mktSurfFlatVol1,
  224.                     mktSurfFlatVol2,
  225.                     mktSurfFlatVol3
  226.                 },
  227.                 new PrincipalFactorSequenceGenerator (
  228.                     new UnivariateSequenceGenerator[] {
  229.                         new BoxMullerGaussian (
  230.                             0.,
  231.                             1.
  232.                         ),
  233.                         new BoxMullerGaussian (
  234.                             0.,
  235.                             1.
  236.                         ),
  237.                         new BoxMullerGaussian (
  238.                             0.,
  239.                             1.
  240.                         )
  241.                     },
  242.                     new double[][] {
  243.                         {1.0, 0.1, 0.2},
  244.                         {0.1, 1.0, 0.2},
  245.                         {0.2, 0.1, 1.0}
  246.                     },
  247.                     iNumFactor
  248.                 )
  249.             );

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

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

  252.             System.out.println ("\t |    Num Factors: " + iNumFactor + "            |");

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

  254.             for (String strForwardTenor : astrForwardTenor)
  255.                 DisplayVolArray (
  256.                     strForwardTenor,
  257.                     llv.continuousForwardVolatility (
  258.                         dtSpot.addTenor (strForwardTenor).julian(),
  259.                         fc
  260.                     )
  261.                 );

  262.             System.out.println ("\t |------------------------------|");
  263.         }

  264.         EnvManager.TerminateEnv();
  265.     }
  266. }