VolatilityCurveScenario.java

  1. package org.drip.state.boot;

  2. /*
  3.  * -*- mode: java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  4.  */

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

  78. /**
  79.  * <i>VolatilityCurveScenario</i> uses the Volatility calibration instruments along with the component
  80.  * calibrator to produce scenario Volatility curves.
  81.  *
  82.  *  <br><br>
  83.  *  <ul>
  84.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ProductCore.md">Product Core Module</a></li>
  85.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/FixedIncomeAnalyticsLibrary.md">Fixed Income Analytics</a></li>
  86.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/state/README.md">Latent State Inference and Creation Utilities</a></li>
  87.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/state/boot/README.md">Bootable Discount, Credit, Volatility States</a></li>
  88.  *  </ul>
  89.  * <br><br>
  90.  *
  91.  * @author Lakshmi Krishnamurthy
  92.  */

  93. public class VolatilityCurveScenario {

  94.     /**
  95.      * Calibrate a Volatility Curve
  96.      *
  97.      * @param strName Volatility Curve name
  98.      * @param valParams ValuationParams
  99.      * @param lslUnderlying Underlying Latent State Label
  100.      * @param aFRACapFloor Array of the FRA Cap Floor Instruments
  101.      * @param adblCalibQuote Array of component quotes
  102.      * @param astrCalibMeasure Array of the calibration measures
  103.      * @param bFlat Flat Calibration (True), or real bootstrapping (false)
  104.      * @param dc Discount Curve
  105.      * @param fc Forward Curve
  106.      * @param lsfc Latent State Fixings Container
  107.      * @param vcp Valuation Customization Parameters
  108.      *
  109.      * @return VolatilityCurve Instance
  110.      */

  111.     public static final org.drip.state.volatility.VolatilityCurve Standard (
  112.         final java.lang.String strName,
  113.         final org.drip.param.valuation.ValuationParams valParams,
  114.         final org.drip.state.identifier.LatentStateLabel lslUnderlying,
  115.         final org.drip.product.fra.FRAStandardCapFloor[] aFRACapFloor,
  116.         final double[] adblCalibQuote,
  117.         final java.lang.String[] astrCalibMeasure,
  118.         final boolean bFlat,
  119.         final org.drip.state.discount.MergedDiscountForwardCurve dc,
  120.         final org.drip.state.forward.ForwardCurve fc,
  121.         final org.drip.param.market.LatentStateFixingsContainer lsfc,
  122.         final org.drip.param.valuation.ValuationCustomizationParams vcp)
  123.     {
  124.         if (null == valParams || null == aFRACapFloor || null == adblCalibQuote || null == astrCalibMeasure
  125.             || null == dc)
  126.             return null;

  127.         int iNumComp = aFRACapFloor.length;
  128.         int aiPillarDate[] = new int[iNumComp];
  129.         double adblVolatility[] = new double[iNumComp];
  130.         org.drip.state.volatility.ExplicitBootVolatilityCurve ebvc = null;

  131.         if (0 == iNumComp || adblCalibQuote.length != iNumComp || astrCalibMeasure.length != iNumComp)
  132.             return null;

  133.         for (int i = 0; i < iNumComp; ++i) {
  134.             if (null == aFRACapFloor[i]) return null;

  135.             adblVolatility[i] = 0.001;

  136.             aiPillarDate[i] = aFRACapFloor[i].stream().maturity().julian();
  137.         }

  138.         try {
  139.             ebvc = new org.drip.state.nonlinear.FlatForwardVolatilityCurve (dc.epoch().julian(),
  140.                 org.drip.state.identifier.VolatilityLabel.Standard (lslUnderlying), dc.currency(),
  141.                     aiPillarDate, adblVolatility);
  142.         } catch (java.lang.Exception e) {
  143.             e.printStackTrace();

  144.             return null;
  145.         }

  146.         for (int i = 0; i < iNumComp; ++i) {
  147.             try {
  148.                 org.drip.state.nonlinear.NonlinearCurveBuilder.VolatilityCurveNode (valParams,
  149.                     aFRACapFloor[i], adblCalibQuote[i], astrCalibMeasure[i], bFlat, i, ebvc, dc, fc, lsfc,
  150.                         vcp);
  151.             } catch (java.lang.Exception e) {
  152.                 e.printStackTrace();

  153.                 return null;
  154.             }
  155.         }

  156.         return ebvc;
  157.     }

  158.     /**
  159.      * Create an array of tenor bumped Volatility curves
  160.      *
  161.      * @param strName Volatility Curve Name
  162.      * @param valParams ValuationParams
  163.      * @param lslUnderlying Underlying Latent State Label
  164.      * @param aFRACapFloor Array of the FRA Cap Floor Instruments
  165.      * @param adblCalibQuote Array of component quotes
  166.      * @param astrCalibMeasure Array of the calibration measures
  167.      * @param bFlat Flat Calibration (True), or real bootstrapping (false)
  168.      * @param dblBump Amount of bump applied to the tenor
  169.      * @param dc Base Discount Curve
  170.      * @param fc Forward Curve
  171.      * @param lsfc Latent State Fixings Container
  172.      * @param vcp Valuation Customization Parameters
  173.      *
  174.      * @return Array of Volatility Curves
  175.      */

  176.     public static final org.drip.state.volatility.VolatilityCurve[] Tenor (
  177.         final java.lang.String strName,
  178.         final org.drip.param.valuation.ValuationParams valParams,
  179.         final org.drip.state.identifier.LatentStateLabel lslUnderlying,
  180.         final org.drip.product.fra.FRAStandardCapFloor[] aFRACapFloor,
  181.         final double[] adblCalibQuote,
  182.         final java.lang.String[] astrCalibMeasure,
  183.         final boolean bFlat,
  184.         final double dblBump,
  185.         final org.drip.state.discount.MergedDiscountForwardCurve dc,
  186.         final org.drip.state.forward.ForwardCurve fc,
  187.         final org.drip.param.market.LatentStateFixingsContainer lsfc,
  188.         final org.drip.param.valuation.ValuationCustomizationParams vcp)
  189.     {
  190.         if (null == aFRACapFloor || !org.drip.numerical.common.NumberUtil.IsValid (dblBump)) return null;

  191.         int iNumComp = aFRACapFloor.length;
  192.         org.drip.state.volatility.VolatilityCurve[] aVolatilityCurve = new
  193.             org.drip.state.volatility.VolatilityCurve[iNumComp];

  194.         if (0 == iNumComp) return null;

  195.         for (int i = 0; i < iNumComp; ++i) {
  196.             double[] adblTenorQuote = new double [iNumComp];

  197.             for (int j = 0; j < iNumComp; ++j)
  198.                 adblTenorQuote[j] += (j == i ? dblBump : 0.);

  199.             if (null == (aVolatilityCurve[i] = Standard (strName, valParams, lslUnderlying, aFRACapFloor,
  200.                 adblTenorQuote, astrCalibMeasure, bFlat, dc, fc, lsfc, vcp)))
  201.                 return null;
  202.         }

  203.         return aVolatilityCurve;
  204.     }

  205.     /**
  206.      * Create an tenor named map of tenor bumped Volatility curves
  207.      *
  208.      * @param strName Volatility Curve name
  209.      * @param valParams ValuationParams
  210.      * @param lslUnderlying Underlying Latent State Label
  211.      * @param aFRACapFloor Array of the FRA Cap Floor Instruments
  212.      * @param adblCalibQuote Array of component quotes
  213.      * @param astrCalibMeasure Array of the calibration measures
  214.      * @param bFlat Flat Calibration (True), or real bootstrapping (false)
  215.      * @param dblBump Amount of bump applied to the tenor
  216.      * @param dc Base Discount Curve
  217.      * @param fc Forward Curve
  218.      * @param lsfc Latent State Fixings Container
  219.      * @param vcp Valuation Customization Parameters
  220.      *
  221.      * @return Tenor named map of tenor bumped Volatility curves
  222.      */

  223.     public org.drip.analytics.support.CaseInsensitiveTreeMap<org.drip.state.volatility.VolatilityCurve>
  224.         TenorMap (
  225.             final java.lang.String strName,
  226.             final org.drip.param.valuation.ValuationParams valParams,
  227.             final org.drip.state.identifier.LatentStateLabel lslUnderlying,
  228.             final org.drip.product.fra.FRAStandardCapFloor[] aFRACapFloor,
  229.             final double[] adblCalibQuote,
  230.             final java.lang.String[] astrCalibMeasure,
  231.             final boolean bFlat,
  232.             final double dblBump,
  233.             final org.drip.state.discount.MergedDiscountForwardCurve dc,
  234.             final org.drip.state.forward.ForwardCurve fc,
  235.             final org.drip.param.market.LatentStateFixingsContainer lsfc,
  236.             final org.drip.param.valuation.ValuationCustomizationParams vcp)
  237.     {
  238.         if (null == aFRACapFloor || !org.drip.numerical.common.NumberUtil.IsValid (dblBump)) return null;

  239.         int iNumComp = aFRACapFloor.length;

  240.         if (0 == iNumComp) return null;

  241.         org.drip.analytics.support.CaseInsensitiveTreeMap<org.drip.state.volatility.VolatilityCurve>
  242.             mapTenorVolatilityCurve = new
  243.                 org.drip.analytics.support.CaseInsensitiveTreeMap<org.drip.state.volatility.VolatilityCurve>();

  244.         for (int i = 0; i < iNumComp; ++i) {
  245.             double[] adblTenorQuote = new double[iNumComp];
  246.             org.drip.state.volatility.VolatilityCurve volCurve = null;

  247.             for (int j = 0; j < iNumComp; ++j)
  248.                 adblTenorQuote[j] = adblCalibQuote[j] + (j == i ? dblBump : 0.);

  249.             if (null == (volCurve = Standard (strName, valParams, lslUnderlying, aFRACapFloor, adblTenorQuote,
  250.                 astrCalibMeasure, bFlat, dc, fc, lsfc, vcp)))
  251.                 return null;

  252.             mapTenorVolatilityCurve.put (org.drip.analytics.date.DateUtil.YYYYMMDD
  253.                 (aFRACapFloor[i].maturityDate().julian()), volCurve);
  254.         }

  255.         return mapTenorVolatilityCurve;
  256.     }
  257. }