StochasticVolatilityStateEvolver.java

  1. package org.drip.dynamics.sabr;

  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>StochasticVolatilityStateEvolver</i> provides the SABR Stochastic Volatility Evolution Dynamics.
  80.  *
  81.  *  <br><br>
  82.  *  <ul>
  83.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ProductCore.md">Product Core Module</a></li>
  84.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/FixedIncomeAnalyticsLibrary.md">Fixed Income Analytics</a></li>
  85.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/dynamics/README.md">HJM, Hull White, LMM, and SABR Dynamic Evolution Models</a></li>
  86.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/dynamics/sabr/README.md">SABR Based Latent State Evolution</a></li>
  87.  *  </ul>
  88.  *
  89.  * @author Lakshmi Krishnamurthy
  90.  */

  91. public class StochasticVolatilityStateEvolver implements org.drip.dynamics.evolution.PointStateEvolver {
  92.     private double _dblRho = java.lang.Double.NaN;
  93.     private double _dblBeta = java.lang.Double.NaN;
  94.     private double _dblIdiosyncraticRho = java.lang.Double.NaN;
  95.     private double _dblVolatilityOfVolatility = java.lang.Double.NaN;
  96.     private org.drip.state.identifier.ForwardLabel _lslForward = null;
  97.     private org.drip.sequence.random.UnivariateSequenceGenerator _usgForwardRate = null;
  98.     private org.drip.sequence.random.UnivariateSequenceGenerator _usgForwardRateVolatilityIdiosyncratic =
  99.         null;

  100.     /**
  101.      * Create a Gaussian SABR Instance
  102.      *
  103.      * @param lslForward The Forward Rate Latent State Label
  104.      * @param dblRho SABR Rho
  105.      * @param dblVolatilityOfVolatility SABR Volatility Of Volatility
  106.      * @param usgForwardRate The Forward Rate Univariate Sequence Generator
  107.      * @param usgForwardRateVolatilityIdiosyncratic The Idiosyncratic Component Forward Rate Volatility
  108.      *  Univariate Sequence Generator
  109.      *
  110.      * @return The Gaussian SABR Instance
  111.      */

  112.     public static final StochasticVolatilityStateEvolver Gaussian (
  113.         final org.drip.state.identifier.ForwardLabel lslForward,
  114.         final double dblRho,
  115.         final double dblVolatilityOfVolatility,
  116.         final org.drip.sequence.random.UnivariateSequenceGenerator usgForwardRate,
  117.         final org.drip.sequence.random.UnivariateSequenceGenerator usgForwardRateVolatilityIdiosyncratic)
  118.     {
  119.         try {
  120.             return new StochasticVolatilityStateEvolver (lslForward, 0., dblRho, dblVolatilityOfVolatility,
  121.                 usgForwardRate, usgForwardRateVolatilityIdiosyncratic);
  122.         } catch (java.lang.Exception e) {
  123.             e.printStackTrace();
  124.         }

  125.         return null;
  126.     }

  127.     /**
  128.      * Create a Log-normal SABR Instance
  129.      *
  130.      * @param lslForward The Forward Rate Latent State Label
  131.      * @param dblRho SABR Rho
  132.      * @param dblVolatilityOfVolatility SABR Volatility Of Volatility
  133.      * @param usgForwardRate The Forward Rate Univariate Sequence Generator
  134.      * @param usgForwardRateVolatilityIdiosyncratic The Idiosyncratic Component Forward Rate Volatility
  135.      *  Univariate Sequence Generator
  136.      *
  137.      * @return The Log-normal SABR Instance
  138.      */

  139.     public static final StochasticVolatilityStateEvolver Lognormal (
  140.         final org.drip.state.identifier.ForwardLabel lslForward,
  141.         final double dblRho,
  142.         final double dblVolatilityOfVolatility,
  143.         final org.drip.sequence.random.UnivariateSequenceGenerator usgForwardRate,
  144.         final org.drip.sequence.random.UnivariateSequenceGenerator usgForwardRateVolatilityIdiosyncratic)
  145.     {
  146.         try {
  147.             return new StochasticVolatilityStateEvolver (lslForward, 1., dblRho, dblVolatilityOfVolatility,
  148.                 usgForwardRate, usgForwardRateVolatilityIdiosyncratic);
  149.         } catch (java.lang.Exception e) {
  150.             e.printStackTrace();
  151.         }

  152.         return null;
  153.     }

  154.     /**
  155.      * Create a Constant Elasticity of Variance SABR Instance
  156.      *
  157.      * @param lslForward The Forward Rate Latent State Label
  158.      * @param dblBeta SABR Beta
  159.      * @param dblRho SABR Rho
  160.      * @param usgForwardRate The Forward Rate Univariate Sequence Generator
  161.      * @param usgForwardRateVolatilityIdiosyncratic The Idiosyncratic Component Forward Rate Volatility
  162.      *  Univariate Sequence Generator
  163.      *
  164.      * @return The Constant Elasticity of Variance SABR Instance
  165.      */

  166.     public static final StochasticVolatilityStateEvolver CEV (
  167.         final org.drip.state.identifier.ForwardLabel lslForward,
  168.         final double dblBeta,
  169.         final double dblRho,
  170.         final org.drip.sequence.random.UnivariateSequenceGenerator usgForwardRate,
  171.         final org.drip.sequence.random.UnivariateSequenceGenerator usgForwardRateVolatilityIdiosyncratic)
  172.     {
  173.         try {
  174.             return new StochasticVolatilityStateEvolver (lslForward, dblBeta, dblRho, 0., usgForwardRate,
  175.                 usgForwardRateVolatilityIdiosyncratic);
  176.         } catch (java.lang.Exception e) {
  177.             e.printStackTrace();
  178.         }

  179.         return null;
  180.     }

  181.     /**
  182.      * StochasticVolatilityStateEvolver Constructor
  183.      *
  184.      * @param lslForward The Forward Rate Latent State Label
  185.      * @param dblBeta SABR Beta
  186.      * @param dblRho SABR Rho
  187.      * @param dblVolatilityOfVolatility SABR Volatility Of Volatility
  188.      * @param usgForwardRate The Forward Rate Univariate Sequence Generator
  189.      * @param usgForwardRateVolatilityIdiosyncratic The Idiosyncratic Component Forward Rate Volatility
  190.      *  Univariate Sequence Generator
  191.      *
  192.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  193.      */

  194.     public StochasticVolatilityStateEvolver (
  195.         final org.drip.state.identifier.ForwardLabel lslForward,
  196.         final double dblBeta,
  197.         final double dblRho,
  198.         final double dblVolatilityOfVolatility,
  199.         final org.drip.sequence.random.UnivariateSequenceGenerator usgForwardRate,
  200.         final org.drip.sequence.random.UnivariateSequenceGenerator usgForwardRateVolatilityIdiosyncratic)
  201.         throws java.lang.Exception
  202.     {
  203.         if (null == (_lslForward = lslForward) || !org.drip.numerical.common.NumberUtil.IsValid (_dblBeta =
  204.             dblBeta) || !org.drip.numerical.common.NumberUtil.IsValid (_dblRho = dblRho) || _dblRho < -1. ||
  205.                 _dblRho > 1. || !org.drip.numerical.common.NumberUtil.IsValid (_dblVolatilityOfVolatility =
  206.                     dblVolatilityOfVolatility) || null == (_usgForwardRate = usgForwardRate) || (0. !=
  207.                         _dblVolatilityOfVolatility && null == (_usgForwardRateVolatilityIdiosyncratic =
  208.                             usgForwardRateVolatilityIdiosyncratic)))
  209.             throw new java.lang.Exception ("StochasticVolatilityStateEvolver ctr => Invalid Inputs");

  210.         _dblIdiosyncraticRho = java.lang.Math.sqrt (1. - _dblRho * _dblRho);
  211.     }

  212.     /**
  213.      * Retrieve the Forward Label
  214.      *
  215.      * @return The Forward Label
  216.      */

  217.     public org.drip.state.identifier.ForwardLabel forwardLabel()
  218.     {
  219.         return _lslForward;
  220.     }

  221.     /**
  222.      * Retrieve SABR Volatility of Volatility
  223.      *
  224.      * @return SABR Volatility of Volatility
  225.      */

  226.     public double volatilityOfVolatility()
  227.     {
  228.         return _dblVolatilityOfVolatility;
  229.     }

  230.     /**
  231.      * Retrieve SABR Beta
  232.      *
  233.      * @return SABR Beta
  234.      */

  235.     public double beta()
  236.     {
  237.         return _dblBeta;
  238.     }

  239.     /**
  240.      * Retrieve SABR Rho
  241.      *
  242.      * @return SABR Rho
  243.      */

  244.     public double rho()
  245.     {
  246.         return _dblRho;
  247.     }

  248.     /**
  249.      * The Forward Rate Univariate Random Variable Generator Sequence
  250.      *
  251.      * @return The Forward Rate Univariate Random Variable Generator Sequence
  252.      */

  253.     public org.drip.sequence.random.UnivariateSequenceGenerator usgForwardRate()
  254.     {
  255.         return _usgForwardRate;
  256.     }

  257.     /**
  258.      * The Idiosyncratic Component of Forward Rate Volatility Univariate Random Variable Generator Sequence
  259.      *
  260.      * @return The Idiosyncratic Component of Forward Rate Volatility Univariate Random Variable Generator
  261.      *  Sequence
  262.      */

  263.     public org.drip.sequence.random.UnivariateSequenceGenerator usgForwardRateVolatilityIdiosyncratic()
  264.     {
  265.         return _usgForwardRateVolatilityIdiosyncratic;
  266.     }

  267.     @Override public org.drip.dynamics.evolution.LSQMPointUpdate evolve (
  268.         final int iSpotDate,
  269.         final int iViewDate,
  270.         final int iSpotTimeIncrement,
  271.         final org.drip.dynamics.evolution.LSQMPointUpdate lsqmPrev)
  272.     {
  273.         if (iViewDate < iSpotDate || null == lsqmPrev || !(lsqmPrev instanceof
  274.             org.drip.dynamics.sabr.ForwardRateUpdate))
  275.             return null;
  276.         double dblForwardRateZ = _usgForwardRate.random();


  277.         double dblAnnualizedIncrement = 1. * iSpotTimeIncrement / 365.25;
  278.         org.drip.dynamics.sabr.ForwardRateUpdate fruPrev = (org.drip.dynamics.sabr.ForwardRateUpdate)
  279.             lsqmPrev;

  280.         double dblAnnualizedIncrementSQRT = java.lang.Math.sqrt (dblAnnualizedIncrement);

  281.         try {
  282.             double dblForwardRate = fruPrev.forwardRate();

  283.             double dblForwardRateVolatility = fruPrev.forwardRateVolatility();

  284.             double dblForwardRateIncrement = dblForwardRateVolatility * java.lang.Math.pow (dblForwardRate,
  285.                 _dblBeta) * dblAnnualizedIncrementSQRT * dblForwardRateZ;

  286.             double dblForwardRateVolatilityIncrement = _dblVolatilityOfVolatility * dblForwardRateVolatility
  287.                 * dblAnnualizedIncrementSQRT * (_dblRho * dblForwardRateZ + _dblIdiosyncraticRho *
  288.                     _usgForwardRateVolatilityIdiosyncratic.random());

  289.             return org.drip.dynamics.sabr.ForwardRateUpdate.Create (_lslForward, iSpotDate, iSpotDate +
  290.                 iSpotTimeIncrement, iViewDate, dblForwardRate + dblForwardRateIncrement,
  291.                     dblForwardRateIncrement, dblForwardRateVolatility + dblForwardRateVolatilityIncrement,
  292.                         dblForwardRateVolatilityIncrement);
  293.         } catch (java.lang.Exception e) {
  294.             e.printStackTrace();
  295.         }

  296.         return null;
  297.     }

  298.     /**
  299.      * Compute the Implied ATM Black Volatility for the ATM Forward Rate and the TTE
  300.      *
  301.      * @param dblATMForwardRate ATM Forward Rate
  302.      * @param dblTTE Time to Expiry
  303.      * @param dblSigma0 Initial Sigma
  304.      *
  305.      * @return The Implied Black Volatility Instance
  306.      */

  307.     public org.drip.dynamics.sabr.ImpliedBlackVolatility computeATMBlackVolatility (
  308.         final double dblATMForwardRate,
  309.         final double dblTTE,
  310.         final double dblSigma0)
  311.     {
  312.         if (!org.drip.numerical.common.NumberUtil.IsValid (dblATMForwardRate) ||
  313.             !org.drip.numerical.common.NumberUtil.IsValid (dblTTE) || !org.drip.numerical.common.NumberUtil.IsValid
  314.                 (dblSigma0))
  315.             return null;

  316.         double dblF0KExpSQRT = java.lang.Math.pow (dblATMForwardRate, 1. - _dblBeta);

  317.         double dblA = dblSigma0 / dblF0KExpSQRT;
  318.         double dblB = 1. + dblTTE * (((1. - _dblBeta) * (1. - _dblBeta) * dblSigma0 * dblSigma0 / (24. *
  319.             dblF0KExpSQRT * dblF0KExpSQRT)) + (_dblRho * _dblBeta * _dblVolatilityOfVolatility * dblSigma0 /
  320.                 (4. * dblF0KExpSQRT)) + ((2. - 3. * _dblRho * _dblRho) * _dblVolatilityOfVolatility *
  321.                     _dblVolatilityOfVolatility / 24.));

  322.         try {
  323.             return new org.drip.dynamics.sabr.ImpliedBlackVolatility (dblATMForwardRate, dblATMForwardRate,
  324.                 dblTTE, dblA, 0., 0., dblB, dblA * dblB);
  325.         } catch (java.lang.Exception e) {
  326.             e.printStackTrace();
  327.         }

  328.         return null;
  329.     }

  330.     /**
  331.      * Compute the Implied Black Volatility for the Specified Strike, the ATM Forward Rate, and the TTE
  332.      *
  333.      * @param dblStrike Strike
  334.      * @param dblATMForwardRate ATM Forward Rate
  335.      * @param dblTTE Time to Expiry
  336.      * @param dblSigma0 Initial Sigma
  337.      *
  338.      * @return The Implied Black Volatility Instance
  339.      */

  340.     public org.drip.dynamics.sabr.ImpliedBlackVolatility computeBlackVolatility (
  341.         final double dblStrike,
  342.         final double dblATMForwardRate,
  343.         final double dblTTE,
  344.         final double dblSigma0)
  345.     {
  346.         if (!org.drip.numerical.common.NumberUtil.IsValid (dblStrike) ||
  347.             !org.drip.numerical.common.NumberUtil.IsValid (dblATMForwardRate) ||
  348.                 !org.drip.numerical.common.NumberUtil.IsValid (dblTTE) ||
  349.                     !org.drip.numerical.common.NumberUtil.IsValid (dblSigma0))
  350.             return null;

  351.         if (dblStrike == dblATMForwardRate)
  352.             return computeATMBlackVolatility (dblATMForwardRate, dblTTE, dblSigma0);

  353.         double dblLogF0ByK = java.lang.Math.log (dblATMForwardRate / dblStrike);

  354.         double dblF0KExpSQRT = java.lang.Math.pow (dblATMForwardRate * dblStrike, 0.5 * (1. - _dblBeta));

  355.         double dblZ = _dblVolatilityOfVolatility * dblF0KExpSQRT * dblLogF0ByK / dblSigma0;
  356.         double dblOneMinusBetaLogF0ByK = (1. - _dblBeta) * (1. - _dblBeta) * dblLogF0ByK * dblLogF0ByK;
  357.         double dblA = dblSigma0 / (dblF0KExpSQRT * (1. + (dblOneMinusBetaLogF0ByK / 24.) +
  358.             (dblOneMinusBetaLogF0ByK * dblOneMinusBetaLogF0ByK / 1920.)));
  359.         double dblB = 1. + dblTTE * (((1. - _dblBeta) * (1. - _dblBeta) * dblSigma0 * dblSigma0 / (24. *
  360.             dblF0KExpSQRT * dblF0KExpSQRT)) + (_dblRho * _dblBeta * _dblVolatilityOfVolatility * dblSigma0 /
  361.                 (4. * dblF0KExpSQRT)) + ((2. - 3. * _dblRho * _dblRho) * _dblVolatilityOfVolatility *
  362.                     _dblVolatilityOfVolatility / 24.));

  363.         double dblChiZ = java.lang.Math.log ((java.lang.Math.sqrt (1. - 2. * _dblRho * dblZ + dblZ * dblZ) +
  364.             dblZ - _dblRho) / (1. - _dblRho));

  365.         try {
  366.             return new org.drip.dynamics.sabr.ImpliedBlackVolatility (dblStrike, dblATMForwardRate, dblTTE,
  367.                 dblA, dblZ, dblChiZ, dblB, dblA * dblZ * dblB / dblChiZ);
  368.         } catch (java.lang.Exception e) {
  369.             e.printStackTrace();
  370.         }

  371.         return null;
  372.     }
  373. }