BlackVolatility.java

  1. package org.drip.sample.sabr;

  2. import org.drip.dynamics.sabr.*;
  3. import org.drip.numerical.common.FormatUtil;
  4. import org.drip.sequence.random.BoxMullerGaussian;
  5. import org.drip.service.env.EnvManager;
  6. import org.drip.state.identifier.ForwardLabel;

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

  53. /**
  54.  * BlackVolatility demonstrates the Construction and Usage of the SABR Model to Imply the Black Volatility of
  55.  *  a given Contract.
  56.  *
  57.  * @author Lakshmi Krishnamurthy
  58.  */

  59. public class BlackVolatility {

  60.     private static StochasticVolatilityStateEvolver SABREvolver (
  61.         final double dblBeta,
  62.         final double dblRho,
  63.         final double dblVolatilityOfVolatility)
  64.         throws Exception
  65.     {
  66.         return new StochasticVolatilityStateEvolver (
  67.             ForwardLabel.Create (
  68.                 "USD",
  69.                 "6M"
  70.             ),
  71.             dblBeta,
  72.             dblRho,
  73.             dblVolatilityOfVolatility,
  74.             new BoxMullerGaussian (
  75.                 0.,
  76.                 1.
  77.             ),
  78.             new BoxMullerGaussian (
  79.                 0.,
  80.                 1.
  81.             )
  82.         );
  83.     }

  84.     private static void VolatilitySurface (
  85.         final StochasticVolatilityStateEvolver seSABR,
  86.         final double[] adblStrike,
  87.         final double dblATMForwardRate,
  88.         final double dblTTE,
  89.         final double dblSigma0)
  90.     {
  91.         String strDump = "\t| " + FormatUtil.FormatDouble (dblTTE, 1, 2, 1.) + " => ";

  92.         for (int i = 0; i < adblStrike.length; ++i) {
  93.             ImpliedBlackVolatility ibv = seSABR.computeBlackVolatility (
  94.                 adblStrike[i],
  95.                 dblATMForwardRate,
  96.                 dblTTE,
  97.                 dblSigma0
  98.             );

  99.             strDump += FormatUtil.FormatDouble (ibv.impliedVolatility(), 2, 1, 100.) + " | ";
  100.         }

  101.         System.out.println (strDump);
  102.     }

  103.     public static void main (
  104.         final String[] astrArgs)
  105.         throws Exception
  106.     {
  107.         EnvManager.InitEnv ("");

  108.         double dblRho = 0.;
  109.         double dblBeta = 0.7;
  110.         double dblATMForwardRate = 0.04;
  111.         double dblVolatilityOfVolatility = 0.5;
  112.         double adblForwardRateVolatility = 0.10;
  113.         double[] adblStrike = {
  114.             0.30, 0.35, 0.40, 0.45, 0.50
  115.         };
  116.         double[] adblTTE = {
  117.             0.25, 0.50, 0.75, 1.00, 2.00, 3.00, 4.00, 5.00, 7.00, 9.99
  118.         };

  119.         StochasticVolatilityStateEvolver seSABR = SABREvolver (
  120.             dblBeta,
  121.             dblRho,
  122.             dblVolatilityOfVolatility
  123.         );

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

  125.         System.out.println ("\t|         SABR IMPLIED BLACK VOLATILITY          |");

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

  127.         for (double dblTTE : adblTTE)
  128.             VolatilitySurface (
  129.                 seSABR,
  130.                 adblStrike,
  131.                 dblATMForwardRate,
  132.                 dblTTE,
  133.                 adblForwardRateVolatility
  134.             );

  135.         System.out.println ("\t|------------------------------------------------|");
  136.     }
  137. }