ForwardVolatilityStateShifted.java

  1. package org.drip.template.statebump;

  2. import java.util.Map;

  3. import org.drip.analytics.date.*;
  4. import org.drip.numerical.common.FormatUtil;
  5. import org.drip.param.market.CurveSurfaceQuoteContainer;
  6. import org.drip.param.valuation.ValuationParams;
  7. import org.drip.product.definition.Component;
  8. import org.drip.product.fra.FRAStandardCapFloor;
  9. import org.drip.service.env.EnvManager;
  10. import org.drip.service.template.*;
  11. import org.drip.state.discount.MergedDiscountForwardCurve;
  12. import org.drip.state.forward.ForwardCurve;
  13. import org.drip.state.identifier.ForwardLabel;
  14. import org.drip.state.volatility.VolatilityCurve;

  15. /*
  16.  * -*- mode: java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  17.  */

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

  91. /**
  92.  * <i>ForwardVolatilityStateShifted</i> demonstrates the Generation and the Usage of Tenor Bumped Forward
  93.  * Volatility Curves.
  94.  *
  95.  *  <br><br>
  96.  *  <ul>
  97.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ProductCore.md">Product Core Module</a></li>
  98.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/FixedIncomeAnalyticsLibrary.md">Fixed Income Analytics</a></li>
  99.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/template/README.md">Pricing/Risk Templates for Fixed Income Component Products</a></li>
  100.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/template/statebump/README.md">Shifted Latent State Construction Template</a></li>
  101.  *  </ul>
  102.  * <br><br>
  103.  *
  104.  * @author Lakshmi Krishnamurthy
  105.  */

  106. public class ForwardVolatilityStateShifted {

  107.     public static final void main (
  108.         final String[] astrArgs)
  109.         throws Exception
  110.     {
  111.         EnvManager.InitEnv ("");

  112.         String strFRATenor = "3M";
  113.         String strCurrency = "GBP";
  114.         double dblBump = 0.0000001;
  115.         boolean bIsBumpProportional = false;

  116.         JulianDate dtSpot = DateUtil.CreateFromYMD (
  117.             2017,
  118.             DateUtil.DECEMBER,
  119.             21
  120.         );

  121.         ForwardLabel forwardLabel = ForwardLabel.Create (
  122.             strCurrency,
  123.             strFRATenor
  124.         );

  125.         MergedDiscountForwardCurve dcFunding = LatentMarketStateBuilder.SmoothFundingCurve (
  126.             dtSpot,
  127.             strCurrency,
  128.             new String[] {
  129.                  "30D",
  130.                  "60D",
  131.                  "91D",
  132.                 "182D",
  133.                 "273D"
  134.             },
  135.             new double[] {
  136.                 0.0668750,  //  30D
  137.                 0.0675000,  //  60D
  138.                 0.0678125,  //  91D
  139.                 0.0712500,  // 182D
  140.                 0.0750000   // 273D
  141.             },
  142.             "ForwardRate",
  143.             null,
  144.             "ForwardRate",
  145.             new String[] {
  146.                 "2Y",
  147.                 "3Y",
  148.                 "4Y",
  149.                 "5Y",
  150.                 "7Y",
  151.                 "10Y"
  152.             },
  153.             new double[] {
  154.                 0.08265,    //  2Y
  155.                 0.08550,    //  3Y
  156.                 0.08655,    //  4Y
  157.                 0.08770,    //  5Y
  158.                 0.08910,    //  7Y
  159.                 0.08920     // 10Y
  160.             },
  161.             "SwapRate"
  162.         );

  163.         String[] astrMaturityTenor = new String[] {
  164.             "01Y",
  165.             "02Y",
  166.             "03Y",
  167.             "04Y",
  168.             "05Y",
  169.             "07Y",
  170.             "10Y"
  171.         };

  172.         double[] adblStrike = new double[] {
  173.             0.0788, //  "1Y",
  174.             0.0839, //  "2Y",
  175.             0.0864, //  "3Y",
  176.             0.0869, //  "4Y",
  177.             0.0879, //  "5Y",
  178.             0.0890, //  "7Y",
  179.             0.0889  // "10Y"
  180.         };

  181.         double[] adblPrice = new double[] {
  182.             0.0017, //  "1Y",
  183.             0.0132, //  "2Y",
  184.             0.0234, //  "3Y",
  185.             0.0343, //  "4Y",
  186.             0.0491, //  "5Y",
  187.             0.0968, //  "7Y"
  188.             0.1625  // "10Y"
  189.         };

  190.         ForwardCurve fc = dcFunding.nativeForwardCurve (strFRATenor);

  191.         Map<String, VolatilityCurve> bumpedForwardVolatilityCurve = LatentMarketStateBuilder.BumpedForwardVolatilityCurve (
  192.             dtSpot,
  193.             forwardLabel,
  194.             true,
  195.             astrMaturityTenor,
  196.             adblStrike,
  197.             adblPrice,
  198.             "Price",
  199.             dcFunding,
  200.             fc,
  201.             dblBump,
  202.             bIsBumpProportional
  203.         );

  204.         FRAStandardCapFloor[] aFRACapFloor = OTCInstrumentBuilder.CapFloor (
  205.             dtSpot,
  206.             forwardLabel,
  207.             astrMaturityTenor,
  208.             adblStrike,
  209.             true
  210.         );

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

  212.         ValuationParams valParams = ValuationParams.Spot (dtSpot.julian());

  213.         for (Map.Entry<String, VolatilityCurve> meForwardVolatility : bumpedForwardVolatilityCurve.entrySet()) {
  214.             String strKey = meForwardVolatility.getKey();

  215.             if (!strKey.startsWith ("capfloor")) continue;

  216.             CurveSurfaceQuoteContainer csqc = new CurveSurfaceQuoteContainer();

  217.             csqc.setFundingState (dcFunding);

  218.             csqc.setForwardState (fc);

  219.             csqc.setForwardVolatility (meForwardVolatility.getValue());

  220.             System.out.print ("\t|  [" + meForwardVolatility.getKey() + "] => ");

  221.             for (Component comp : aFRACapFloor)
  222.                 System.out.print (FormatUtil.FormatDouble (comp.measureValue (
  223.                     valParams,
  224.                     null,
  225.                     csqc,
  226.                     null,
  227.                     "Price"
  228.                 ), 1, 8, 1.) + " |");

  229.             System.out.print ("|\n");
  230.         }

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

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

  233.         CurveSurfaceQuoteContainer csqcBase = new CurveSurfaceQuoteContainer();

  234.         csqcBase.setFundingState (dcFunding);

  235.         csqcBase.setForwardState (fc);

  236.         csqcBase.setForwardVolatility (bumpedForwardVolatilityCurve.get ("Base"));

  237.         CurveSurfaceQuoteContainer csqcBump = new CurveSurfaceQuoteContainer();

  238.         csqcBump.setFundingState (dcFunding);

  239.         csqcBump.setForwardState (fc);

  240.         csqcBump.setForwardVolatility (bumpedForwardVolatilityCurve.get ("Bump"));

  241.         for (Component comp : aFRACapFloor)
  242.             System.out.println (
  243.                 "\t| PRICE => " +
  244.                 comp.maturityDate() + " | " +
  245.                 FormatUtil.FormatDouble (comp.measureValue (
  246.                     valParams,
  247.                     null,
  248.                     csqcBase,
  249.                     null,
  250.                     "Price"
  251.                 ), 1, 8, 1.) + " | " +
  252.                 FormatUtil.FormatDouble (comp.measureValue (
  253.                     valParams,
  254.                     null,
  255.                     csqcBump,
  256.                     null,
  257.                     "Price"
  258.                 ), 1, 8, 1.) + " ||"
  259.             );

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