ScenarioLocalVolatilityBuilder.java

  1. package org.drip.state.creator;

  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>ScenarioLocalVolatilityBuilder</i> implements the construction of the Local Volatility surface using
  80.  * the input option instruments, their Call Prices, and a wide variety of custom build schemes.
  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/creator/README.md">Scenario State Curve/Surface Builders</a></li>
  88.  *  </ul>
  89.  * <br><br>
  90.  *
  91.  * @author Lakshmi Krishnamurthy
  92.  */

  93. public class ScenarioLocalVolatilityBuilder {

  94.     /**
  95.      * Create a Volatility Curve from the Calibration Instruments
  96.      *
  97.      * @param strName Volatility Curve name
  98.      * @param dtSpot Spot Date
  99.      * @param lslUnderlying Underlying Latent State Label
  100.      * @param aFRACapFloor Array of the FRA Cap Floor Instruments
  101.      * @param adblCalibQuote Input Calibration Quotes
  102.      * @param astrCalibMeasure Input Calibration Measures
  103.      * @param dc Base Discount Curve
  104.      * @param fc Forward Curve
  105.      * @param lsfc Latent State Fixings Container
  106.      *
  107.      * @return The Calibrated Volatility Curve
  108.      */

  109.     public static final org.drip.state.volatility.VolatilityCurve NonlinearBuild (
  110.         final java.lang.String strName,
  111.         final org.drip.analytics.date.JulianDate dtSpot,
  112.         final org.drip.state.identifier.LatentStateLabel lslUnderlying,
  113.         final org.drip.product.fra.FRAStandardCapFloor[] aFRACapFloor,
  114.         final double[] adblCalibQuote,
  115.         final java.lang.String[] astrCalibMeasure,
  116.         final org.drip.state.discount.MergedDiscountForwardCurve dc,
  117.         final org.drip.state.forward.ForwardCurve fc,
  118.         final org.drip.param.market.LatentStateFixingsContainer lsfc)
  119.     {
  120.         return null == dtSpot ? null : org.drip.state.boot.VolatilityCurveScenario.Standard (strName,
  121.             org.drip.param.valuation.ValuationParams.Spot (dtSpot.julian()), lslUnderlying, aFRACapFloor,
  122.                 adblCalibQuote, astrCalibMeasure, false, dc, fc, lsfc, null);
  123.     }

  124.     /**
  125.      * Build an Instance of the Volatility Surface using custom wire span and surface splines
  126.      *
  127.      * @param strName Name of the Volatility Surface
  128.      * @param dtStart Start/Epoch Julian Date
  129.      * @param strCurrency Currency
  130.      * @param dblRiskFreeRate Risk Free Discounting Rate
  131.      * @param adblStrike Array of Strikes
  132.      * @param adblMaturity Array of Maturities
  133.      * @param aadblCallPrice Double Array of the Call Prices
  134.      * @param scbcWireSpan The Wire Span Segment Customizer
  135.      * @param scbcSurface The Surface Segment Customizer
  136.      *
  137.      * @return Instance of the Market Node Surface
  138.      */

  139.     public static final org.drip.analytics.definition.MarketSurface CustomSplineWireSurface (
  140.         final java.lang.String strName,
  141.         final org.drip.analytics.date.JulianDate dtStart,
  142.         final java.lang.String strCurrency,
  143.         final double dblRiskFreeRate,
  144.         final double[] adblStrike,
  145.         final double[] adblMaturity,
  146.         final double[][] aadblCallPrice,
  147.         final org.drip.spline.params.SegmentCustomBuilderControl scbcWireSpan,
  148.         final org.drip.spline.params.SegmentCustomBuilderControl scbcSurface)
  149.     {
  150.         if (!org.drip.numerical.common.NumberUtil.IsValid (dblRiskFreeRate)) return null;

  151.         org.drip.analytics.definition.MarketSurface msCallPrice =
  152.             org.drip.state.creator.ScenarioMarketSurfaceBuilder.CustomSplineWireSurface (strName +
  153.                 "_CALL_PRICE_SURFACE", dtStart, strCurrency, adblStrike, adblMaturity, aadblCallPrice,
  154.                     scbcWireSpan, scbcSurface);

  155.         if (null == msCallPrice) return null;

  156.         int iNumStrike = adblStrike.length;
  157.         int iNumMaturity = adblMaturity.length;
  158.         double[][] aadblLocalVolatility = new double[iNumStrike][iNumMaturity];
  159.         org.drip.analytics.definition.NodeStructure[] aTSMaturityAnchor = new
  160.             org.drip.analytics.definition.NodeStructure[iNumMaturity];

  161.         for (int j = 0; j < iNumMaturity; ++j) {
  162.             if (null == (aTSMaturityAnchor[j] = msCallPrice.yAnchorTermStructure (adblMaturity[j])))
  163.                 return null;
  164.         }

  165.         for (int i = 0; i < iNumStrike; ++i) {
  166.             org.drip.analytics.definition.NodeStructure tsStrikeAnchor = msCallPrice.xAnchorTermStructure
  167.                 (adblStrike[i]);

  168.             if (null == tsStrikeAnchor) return null;

  169.             for (int j = 0; j < iNumMaturity; ++j) {
  170.                 try {
  171.                     aadblLocalVolatility[i][j] = java.lang.Math.sqrt ((tsStrikeAnchor.nodeDerivative ((int)
  172.                         adblMaturity[j], 1) + dblRiskFreeRate * adblStrike[i] *
  173.                             aTSMaturityAnchor[j].nodeDerivative ((int) adblStrike[i], 1)) / (adblStrike[i] *
  174.                                 adblStrike[i] * aTSMaturityAnchor[j].nodeDerivative ((int) adblStrike[i],
  175.                                     2)));
  176.                 } catch (java.lang.Exception e) {
  177.                     e.printStackTrace();

  178.                     return null;
  179.                 }
  180.             }
  181.         }

  182.         return org.drip.state.creator.ScenarioMarketSurfaceBuilder.CustomSplineWireSurface (strName, dtStart,
  183.             strCurrency, adblStrike, adblMaturity, aadblLocalVolatility, scbcWireSpan, scbcSurface);
  184.     }

  185.     /**
  186.      * Construct a Scenario Market Surface off of cubic polynomial wire spline and cubic polynomial surface
  187.      *  Spline.
  188.      *
  189.      * @param strName Name of the Volatility Surface
  190.      * @param dtStart Start/Epoch Julian Date
  191.      * @param strCurrency Currency
  192.      * @param dblRiskFreeRate Risk Free Discounting Rate
  193.      * @param adblStrike Array of Strikes
  194.      * @param astrTenor Array of Maturity Tenors
  195.      * @param aadblNode Double Array of the Surface Nodes
  196.      *
  197.      * @return Instance of the Market Node Surface
  198.      */

  199.     public static final org.drip.analytics.definition.MarketSurface CubicPolynomialWireSurface (
  200.         final java.lang.String strName,
  201.         final org.drip.analytics.date.JulianDate dtStart,
  202.         final java.lang.String strCurrency,
  203.         final double dblRiskFreeRate,
  204.         final double[] adblStrike,
  205.         final java.lang.String[] astrTenor,
  206.         final double[][] aadblNode)
  207.     {
  208.         if (null == astrTenor) return null;

  209.         int iNumTenor = astrTenor.length;
  210.         double[] adblMaturity = new double[iNumTenor];
  211.         org.drip.spline.params.SegmentCustomBuilderControl scbcSurface = null;
  212.         org.drip.spline.params.SegmentCustomBuilderControl scbcWireSpan = null;

  213.         if (0 == iNumTenor) return null;

  214.         for (int i = 0; i < iNumTenor; ++i)
  215.             adblMaturity[i] = dtStart.addTenor (astrTenor[i]).julian();

  216.         try {
  217.             scbcWireSpan = new org.drip.spline.params.SegmentCustomBuilderControl
  218.                 (org.drip.spline.stretch.MultiSegmentSequenceBuilder.BASIS_SPLINE_POLYNOMIAL, new
  219.                     org.drip.spline.basis.PolynomialFunctionSetParams (4),
  220.                         org.drip.spline.params.SegmentInelasticDesignControl.Create (2, 2), null, null);

  221.             scbcSurface = new org.drip.spline.params.SegmentCustomBuilderControl
  222.                 (org.drip.spline.stretch.MultiSegmentSequenceBuilder.BASIS_SPLINE_POLYNOMIAL, new
  223.                     org.drip.spline.basis.PolynomialFunctionSetParams (4),
  224.                         org.drip.spline.params.SegmentInelasticDesignControl.Create (2, 2), null, null);
  225.         } catch (java.lang.Exception e) {
  226.             e.printStackTrace();

  227.             return null;
  228.         }

  229.         return CustomSplineWireSurface (strName, dtStart, strCurrency, dblRiskFreeRate, adblStrike,
  230.             adblMaturity, aadblNode, scbcWireSpan, scbcSurface);
  231.     }
  232. }