DeterministicVolTermStructure.java

  1. package org.drip.sample.option;

  2. import org.drip.analytics.date.*;
  3. import org.drip.numerical.common.FormatUtil;
  4. import org.drip.service.env.EnvManager;
  5. import org.drip.state.creator.ScenarioDeterministicVolatilityBuilder;
  6. import org.drip.state.volatility.VolatilityCurve;

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

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

  54. /**
  55.  * DeterministicVolatilityTermStructure contains an illustration of the Calibration and Extraction of the
  56.  *  Implied and the Deterministic Volatility Term Structures. This does not deal with Local Volatility
  57.  *  Surfaces.
  58.  *
  59.  * @author Lakshmi Krishnamurthy
  60.  */

  61. public class DeterministicVolTermStructure {

  62.     private static final void OnGrid (
  63.         final VolatilityCurve vts,
  64.         final String[] astrMaturityTenor,
  65.         final double[] dblNodeInput)
  66.         throws Exception
  67.     {
  68.         System.out.println ("\n\t|------------------------------------|");

  69.         System.out.println ("\t| TNR =>   CALC  |   IMPL  |  INPUT  |");

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

  71.         for (int i = 0; i < astrMaturityTenor.length; ++i)
  72.             System.out.println ("\t| " + astrMaturityTenor[i] + " => " +
  73.                 FormatUtil.FormatDouble (vts.node (astrMaturityTenor[i]), 2, 2, 100.) + "% | " +
  74.                 FormatUtil.FormatDouble (vts.impliedVol (astrMaturityTenor[i]), 2, 2, 100.) + "% | " +
  75.                 FormatUtil.FormatDouble (dblNodeInput[i], 2, 2, 100.) + "% |");

  76.         System.out.println ("\t|------------------------------------|");
  77.     }

  78.     private static final void OffGrid (
  79.         final String strHeader,
  80.         final String[] astrLabel,
  81.         final VolatilityCurve[] aVTS,
  82.         final String[] astrMaturityTenor)
  83.         throws Exception
  84.     {
  85.         System.out.println ("\n\n\t\t" + strHeader + "\n");

  86.         System.out.print ("\t| TNR =>");

  87.         for (int i = 0; i < aVTS.length; ++i)
  88.             System.out.print (" " + astrLabel[i] + " | ");

  89.         System.out.println ("\n");

  90.         for (int i = 0; i < astrMaturityTenor.length; ++i) {
  91.             System.out.print ("\t| " + astrMaturityTenor[i] + " =>");

  92.             for (int j = 0; j < aVTS.length; ++j)
  93.                 System.out.print ("  " + FormatUtil.FormatDouble (aVTS[j].node (astrMaturityTenor[i]), 2, 2, 100.) + "%   | ");

  94.             System.out.print ("\n");
  95.         }

  96.         System.out.println ("\n");
  97.     }

  98.     public static final void main (
  99.         final String[] astrArgs)
  100.         throws Exception
  101.     {
  102.         /*
  103.          * Initialize the Credit Analytics Library
  104.          */

  105.         EnvManager.InitEnv ("");

  106.         JulianDate dtToday = DateUtil.Today();

  107.         String[] astrMaturityTenor = new String[] {
  108.             "01Y", "02Y", "03Y", "04Y", "05Y", "06Y", "07Y", "08Y", "09Y"
  109.         };
  110.         double[] adblImpliedCallVolatility = new double[] {
  111.             0.700, 0.672, 0.661, 0.596, 0.551, 0.518, 0.492, 0.471, 0.452
  112.         };

  113.         VolatilityCurve tsCallVolatilityCubicPoly =
  114.             ScenarioDeterministicVolatilityBuilder.CubicPolynomialTermStructure (
  115.                 "CUBIC_POLY_CALLVOL_TERMSTRUCTURE",
  116.                 dtToday,
  117.                 "USD",
  118.                 astrMaturityTenor,
  119.                 adblImpliedCallVolatility
  120.             );

  121.         VolatilityCurve tsCallVolatilityQuarticPoly =
  122.             ScenarioDeterministicVolatilityBuilder.QuarticPolynomialTermStructure (
  123.                 "QUARTIC_POLY_CALLVOL_TERMSTRUCTURE",
  124.                 dtToday,
  125.                 "USD",
  126.                 astrMaturityTenor,
  127.                 adblImpliedCallVolatility
  128.             );

  129.         VolatilityCurve tsCallVolatilityKaklisPandelis =
  130.             ScenarioDeterministicVolatilityBuilder.KaklisPandelisTermStructure (
  131.                 "KAKLIS_PANDELIS_CALLVOL_TERMSTRUCTURE",
  132.                 dtToday,
  133.                 "USD",
  134.                 astrMaturityTenor,
  135.                 adblImpliedCallVolatility
  136.             );

  137.         VolatilityCurve tsCallVolatilityKLKHyperbolic =
  138.             ScenarioDeterministicVolatilityBuilder.KLKHyperbolicTermStructure (
  139.                 "KLK_HYPERBOLIC_CALLVOL_TERMSTRUCTURE",
  140.                 dtToday,
  141.                 "USD",
  142.                 astrMaturityTenor,
  143.                 adblImpliedCallVolatility,
  144.                 1.
  145.             );

  146.         VolatilityCurve tsCallVolatilityKLKRationalLinear =
  147.             ScenarioDeterministicVolatilityBuilder.KLKRationalLinearTermStructure (
  148.                 "KLK_RATIONAL_LINEAR_CALLVOL_TERMSTRUCTURE",
  149.                 dtToday,
  150.                 "USD",
  151.                 astrMaturityTenor,
  152.                 adblImpliedCallVolatility,
  153.                 1.
  154.             );

  155.         VolatilityCurve tsCallVolatilityKLKRationalQuadratic =
  156.             ScenarioDeterministicVolatilityBuilder.KLKRationalQuadraticTermStructure (
  157.                 "KLK_RATIONAL_QUADRATIC_CALLVOL_TERMSTRUCTURE",
  158.                 dtToday,
  159.                 "USD",
  160.                 astrMaturityTenor,
  161.                 adblImpliedCallVolatility,
  162.                 0.0001
  163.             );

  164.         OnGrid (
  165.             tsCallVolatilityKLKHyperbolic,
  166.             astrMaturityTenor,
  167.             adblImpliedCallVolatility
  168.         );

  169.         String[] astrOffGridTenor = new String[] {
  170.             "18M", "30M", "42M", "54M", "06Y", "09Y"
  171.         };

  172.         OffGrid (
  173.             "ATM_CALLVOL_TERM_STRUCTURE",
  174.             new String[] {
  175.                 "Cubic Poly", "Quart Poly", "KaklisPand", "KLKHyperbl", "KLKRatlLin", "KLKRatlQua"
  176.             },
  177.             new VolatilityCurve[] {
  178.                 tsCallVolatilityCubicPoly,
  179.                 tsCallVolatilityQuarticPoly,
  180.                 tsCallVolatilityKaklisPandelis,
  181.                 tsCallVolatilityKLKHyperbolic,
  182.                 tsCallVolatilityKLKRationalLinear,
  183.                 tsCallVolatilityKLKRationalQuadratic
  184.             },
  185.             astrOffGridTenor
  186.         );
  187.     }
  188. }