LocalVolatilityTermStructure.java

  1. package org.drip.sample.option;

  2. import org.drip.analytics.date.*;
  3. import org.drip.analytics.definition.*;
  4. import org.drip.numerical.common.FormatUtil;
  5. import org.drip.spline.basis.PolynomialFunctionSetParams;
  6. import org.drip.spline.params.*;
  7. import org.drip.spline.stretch.MultiSegmentSequenceBuilder;
  8. import org.drip.state.creator.ScenarioMarketSurfaceBuilder;

  9. /*
  10.  * -*- mode: java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  11.  */

  12. /*!
  13.  * Copyright (C) 2018 Lakshmi Krishnamurthy
  14.  * Copyright (C) 2017 Lakshmi Krishnamurthy
  15.  * Copyright (C) 2016 Lakshmi Krishnamurthy
  16.  * Copyright (C) 2015 Lakshmi Krishnamurthy
  17.  * Copyright (C) 2014 Lakshmi Krishnamurthy
  18.  *
  19.  *  This file is part of DRIP, a free-software/open-source library for buy/side financial/trading model
  20.  *      libraries targeting analysts and developers
  21.  *      https://lakshmidrip.github.io/DRIP/
  22.  *  
  23.  *  DRIP is composed of four main libraries:
  24.  *  
  25.  *  - DRIP Fixed Income - https://lakshmidrip.github.io/DRIP-Fixed-Income/
  26.  *  - DRIP Asset Allocation - https://lakshmidrip.github.io/DRIP-Asset-Allocation/
  27.  *  - DRIP Numerical Optimizer - https://lakshmidrip.github.io/DRIP-Numerical-Optimizer/
  28.  *  - DRIP Statistical Learning - https://lakshmidrip.github.io/DRIP-Statistical-Learning/
  29.  *
  30.  *  - DRIP Fixed Income: Library for Instrument/Trading Conventions, Treasury Futures/Options,
  31.  *      Funding/Forward/Overnight Curves, Multi-Curve Construction/Valuation, Collateral Valuation and XVA
  32.  *      Metric Generation, Calibration and Hedge Attributions, Statistical Curve Construction, Bond RV
  33.  *      Metrics, Stochastic Evolution and Option Pricing, Interest Rate Dynamics and Option Pricing, LMM
  34.  *      Extensions/Calibrations/Greeks, Algorithmic Differentiation, and Asset Backed Models and Analytics.
  35.  *
  36.  *  - DRIP Asset Allocation: Library for model libraries for MPT framework, Black Litterman Strategy
  37.  *      Incorporator, Holdings Constraint, and Transaction Costs.
  38.  *
  39.  *  - DRIP Numerical Optimizer: Library for Numerical Optimization and Spline Functionality.
  40.  *
  41.  *  - DRIP Statistical Learning: Library for Statistical Evaluation and Machine Learning.
  42.  *
  43.  *  Licensed under the Apache License, Version 2.0 (the "License");
  44.  *      you may not use this file except in compliance with the License.
  45.  *  
  46.  *  You may obtain a copy of the License at
  47.  *      http://www.apache.org/licenses/LICENSE-2.0
  48.  *  
  49.  *  Unless required by applicable law or agreed to in writing, software
  50.  *      distributed under the License is distributed on an "AS IS" BASIS,
  51.  *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  52.  *  
  53.  *  See the License for the specific language governing permissions and
  54.  *      limitations under the License.
  55.  */

  56. /**
  57.  * LocalVolatilityTermStructure contains an illustration of the Calibration and Extraction of the Implied and
  58.  *  the Local Volatility Surfaces and their eventual Strike and Maturity Anchor Term Structures.
  59.  *
  60.  * @author Lakshmi Krishnamurthy
  61.  */

  62. public class LocalVolatilityTermStructure {
  63.     private static final SegmentCustomBuilderControl scbc()
  64.         throws Exception
  65.     {
  66.         return new SegmentCustomBuilderControl (
  67.             MultiSegmentSequenceBuilder.BASIS_SPLINE_POLYNOMIAL,
  68.             new PolynomialFunctionSetParams (4),
  69.             SegmentInelasticDesignControl.Create (
  70.                 2,
  71.                 2
  72.             ),
  73.             null,
  74.             null
  75.         );
  76.     }

  77.     private static final void EvaluateLocalVolSurface (
  78.         final MarketSurface volSurface,
  79.         final double[] adblStrikeATMFactor,
  80.         final String[] astrMaturityTenor)
  81.         throws Exception
  82.     {
  83.         System.out.println ("\n\t  " + volSurface.label());

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

  85.         System.out.print ("\t|------------------------------------------------------------|\n\t|  ATM/TTE  =>");

  86.         NodeStructure[] aTSMaturityAnchor = new NodeStructure[astrMaturityTenor.length];

  87.         for (int j = 0; j < astrMaturityTenor.length; ++j) {
  88.             aTSMaturityAnchor[j] = volSurface.maturityAnchorTermStructure (astrMaturityTenor[j]);

  89.             System.out.print ("    " + astrMaturityTenor[j] + "  ");
  90.         }

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

  92.         for (int i = 0; i < adblStrikeATMFactor.length; ++i) {
  93.             System.out.print ("\t|  " + FormatUtil.FormatDouble (adblStrikeATMFactor[i], 1, 2, 1.) + "    =>");

  94.             NodeStructure tsStrikeAnchor = volSurface.xAnchorTermStructure (adblStrikeATMFactor[i]);

  95.             for (int j = 0; j < astrMaturityTenor.length; ++j) {
  96.                 double dblLocalVol = Math.sqrt (2. * (tsStrikeAnchor.nodeDerivative (astrMaturityTenor[j], 1) +
  97.                     0.0 * adblStrikeATMFactor[i] * aTSMaturityAnchor[j].nodeDerivative ((int) adblStrikeATMFactor[i], 1)) /
  98.                         (adblStrikeATMFactor[i] * adblStrikeATMFactor[i] *
  99.                             aTSMaturityAnchor[j].nodeDerivative ((int) adblStrikeATMFactor[i], 2)));

  100.                 System.out.print ("  " + FormatUtil.FormatDouble (dblLocalVol, 2, 2, 100.) + "%");
  101.             }

  102.             System.out.print ("  |\n");
  103.         }

  104.         System.out.println ("\t|------------------------------------------------------------|");
  105.     }

  106.     public static final void main (
  107.         final String[] astrArgs)
  108.         throws Exception
  109.     {
  110.         JulianDate dtStart = DateUtil.Today();

  111.         double[] adblStrikeATMFactorCalib = new double[] {
  112.             0.8, 0.9, 1.0, 1.1, 1.2
  113.         };
  114.         String[] astrMaturityTenorCalib = new String[] {
  115.             "12M", "24M", "36M", "48M", "60M"
  116.         };
  117.         double[][] aadblVol = new double[][] {
  118.             {0.171, 0.169, 0.168, 0.168, 0.168},
  119.             {0.159, 0.161, 0.161, 0.162, 0.164},
  120.             {0.138, 0.145, 0.149, 0.152, 0.154},
  121.             {0.115, 0.130, 0.137, 0.143, 0.148},
  122.             {0.103, 0.119, 0.128, 0.135, 0.140}
  123.         };

  124.         MarketSurface priceSurfCubicPoly = ScenarioMarketSurfaceBuilder.CustomWireSurface (
  125.             "HESTON1993_CUBICPOLY_CALLPRICE_SURFACE",
  126.             dtStart,
  127.             "USD",
  128.             adblStrikeATMFactorCalib,
  129.             astrMaturityTenorCalib,
  130.             aadblVol,
  131.             scbc(),
  132.             scbc()
  133.         );

  134.         double[] adblStrikeATMFactor = new double[] {
  135.             0.850, 0.925, 1.000, 1.075, 1.150
  136.         };
  137.         String[] astrMaturityTenor = new String[] {
  138.             "18M", "27M", "36M", "45M", "54M"
  139.         };

  140.         NodeStructure[] aTSMaturityAnchor = new NodeStructure[astrMaturityTenor.length];

  141.         for (int j = 0; j < astrMaturityTenor.length; ++j)
  142.             aTSMaturityAnchor[j] = priceSurfCubicPoly.maturityAnchorTermStructure (astrMaturityTenor[j]);

  143.         for (int i = 0; i < adblStrikeATMFactor.length; ++i) {
  144.             NodeStructure tsStrikeAnchor = priceSurfCubicPoly.xAnchorTermStructure (adblStrikeATMFactor[i]);

  145.             for (int j = 0; j < astrMaturityTenor.length; ++j) {
  146.                 System.out.println (Math.sqrt (2. * (tsStrikeAnchor.nodeDerivative (astrMaturityTenor[j], 1) +
  147.                     0.0 * adblStrikeATMFactor[i] * aTSMaturityAnchor[j].nodeDerivative ((int) adblStrikeATMFactor[i], 1)) /
  148.                         (adblStrikeATMFactor[i] * adblStrikeATMFactor[i] *
  149.                             aTSMaturityAnchor[j].nodeDerivative ((int) adblStrikeATMFactor[i], 2))) + " | " +
  150.                                 aTSMaturityAnchor[j].nodeDerivative ((int) adblStrikeATMFactor[i], 2));
  151.             }
  152.         }

  153.         EvaluateLocalVolSurface (
  154.             priceSurfCubicPoly,
  155.             adblStrikeATMFactor,
  156.             astrMaturityTenor
  157.         );
  158.     }
  159. }