MultiFactorVolatility.java

  1. package org.drip.dynamics.hjm;

  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>MultiFactorVolatility</i> implements the Volatility of the Multi-factor Stochastic Evolution Process.
  80.  * The Factors may come from the Underlying Stochastic Variables, or from Principal Components.
  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/dynamics/README.md">HJM, Hull White, LMM, and SABR Dynamic Evolution Models</a></li>
  87.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/dynamics/hjm/README.md">HJM Based Latent State Evolution</a></li>
  88.  *  </ul>
  89.  *
  90.  * @author Lakshmi Krishnamurthy
  91.  */

  92. public class MultiFactorVolatility {
  93.     private org.drip.analytics.definition.MarketSurface[] _aMSVolatility = null;
  94.     private org.drip.sequence.random.PrincipalFactorSequenceGenerator _pfsg = null;

  95.     /**
  96.      * MultiFactorVolatility Constructor
  97.      *
  98.      * @param aMSVolatility Array of the Multi-Factor Volatility Surfaces
  99.      * @param pfsg Principal Factor Sequence Generator
  100.      *
  101.      * @throws java.lang.Exception Thrown if Inputs are Invalid
  102.      */

  103.     public MultiFactorVolatility (
  104.         final org.drip.analytics.definition.MarketSurface[] aMSVolatility,
  105.         final org.drip.sequence.random.PrincipalFactorSequenceGenerator pfsg)
  106.         throws java.lang.Exception
  107.     {
  108.         if (null == (_aMSVolatility = aMSVolatility) || null == (_pfsg = pfsg))
  109.             throw new java.lang.Exception ("MultiFactorVolatility ctr: Invalid Inputs");

  110.         int iNumFactor = _pfsg.numFactor();

  111.         if (0 == iNumFactor || _aMSVolatility.length < iNumFactor)
  112.             throw new java.lang.Exception ("MultiFactorVolatility ctr: Invalid Inputs");
  113.     }

  114.     /**
  115.      * Retrieve the Array of Volatility Surfaces
  116.      *
  117.      * @return The Array of Volatility Surfaces
  118.      */

  119.     public org.drip.analytics.definition.MarketSurface[] volatilitySurface()
  120.     {
  121.         return _aMSVolatility;
  122.     }

  123.     /**
  124.      * Retrieve the Principal Factor Sequence Generator
  125.      *
  126.      * @return The Principal Factor Sequence Generator
  127.      */

  128.     public org.drip.sequence.random.PrincipalFactorSequenceGenerator msg()
  129.     {
  130.         return _pfsg;
  131.     }

  132.     /**
  133.      * Retrieve the Factor-Specific Univariate Volatility Function for the Specified Date
  134.      *
  135.      * @param iFactorIndex The Factor Index
  136.      * @param iXDate The X Date
  137.      *
  138.      * @return The Factor-Specific Univariate Volatility Function for the Specified Date
  139.      */

  140.     public org.drip.function.definition.R1ToR1 xDateVolatilityFunction (
  141.         final int iFactorIndex,
  142.         final int iXDate)
  143.     {
  144.         int iNumFactor = _pfsg.numFactor();

  145.         if (iFactorIndex >= iNumFactor) return null;

  146.         final int iNumVariate = _aMSVolatility.length;

  147.         return new org.drip.function.definition.R1ToR1 (null) {
  148.             @Override public double evaluate (
  149.                 final double dblX)
  150.                 throws java.lang.Exception
  151.             {
  152.                 double dblMultiFactorVol = 0.;

  153.                 double[] adblFactor = _pfsg.factors()[iFactorIndex];

  154.                 for (int i = 0; i < iNumVariate; ++i) {
  155.                     org.drip.analytics.definition.NodeStructure tsVolatilityXDate =
  156.                         _aMSVolatility[iFactorIndex].xAnchorTermStructure (iXDate);

  157.                     dblMultiFactorVol += adblFactor[i] * tsVolatilityXDate.node ((int) dblX);
  158.                 }

  159.                 return _pfsg.factorWeight()[iFactorIndex] * dblMultiFactorVol;
  160.             }
  161.         };
  162.     }

  163.     /**
  164.      * Compute the Factor Volatility Integral
  165.      *
  166.      * @param iFactorIndex The Factor Index
  167.      * @param iXDate The X Date
  168.      * @param iYDate The Y Date
  169.      *
  170.      * @return The Factor Volatility Integral
  171.      *
  172.      * @throws java.lang.Exception Thrown if the Factor Volatility Integral cannot be computed
  173.      */

  174.     public double volatilityIntegral (
  175.         final int iFactorIndex,
  176.         final int iXDate,
  177.         final int iYDate)
  178.         throws java.lang.Exception
  179.     {
  180.         org.drip.function.definition.R1ToR1 auVolatilityFunction = xDateVolatilityFunction (iFactorIndex,
  181.             iXDate);

  182.         if (null == auVolatilityFunction)
  183.             throw new java.lang.Exception
  184.                 ("MultiFactorVolatility::volatilityIntegral => Cannot extract X Date Volatility Function");

  185.         return auVolatilityFunction.integrate (iXDate, iYDate) / 365.25;
  186.     }

  187.     /**
  188.      * Compute the Factor Point Volatility
  189.      *
  190.      * @param iFactorIndex The Factor Index
  191.      * @param iXDate The X Date
  192.      * @param iYDate The Y Date
  193.      *
  194.      * @return The Factor Point Volatility
  195.      *
  196.      * @throws java.lang.Exception Thrown if the Factor Point Volatility cannot be computed
  197.      */

  198.     public double factorPointVolatility (
  199.         final int iFactorIndex,
  200.         final int iXDate,
  201.         final int iYDate)
  202.         throws java.lang.Exception
  203.     {
  204.         int iNumFactor = _pfsg.numFactor();

  205.         if (iFactorIndex >= iNumFactor)
  206.             throw new java.lang.Exception
  207.                 ("MultiFactorVolatility::factorPointVolatility => Invalid Factor Index");

  208.         double[] adblFactor = _pfsg.factors()[iFactorIndex];

  209.         int iNumVariate = adblFactor.length;
  210.         double dblFactorPointVolatility = 0.;

  211.         for (int i = 0; i < iNumVariate; ++i)
  212.             dblFactorPointVolatility += adblFactor[i] * _aMSVolatility[i].node (iXDate, iYDate);

  213.         return dblFactorPointVolatility;
  214.     }

  215.     /**
  216.      * Compute the Array of Factor Point Volatilities
  217.      *
  218.      * @param iXDate The X Date
  219.      * @param iYDate The Y Date
  220.      *
  221.      * @return The Array of Factor Point Volatilities
  222.      */

  223.     public double[] factorPointVolatility (
  224.         final int iXDate,
  225.         final int iYDate)
  226.     {
  227.         int iNumFactor = _pfsg.numFactor();

  228.         double[][] aadblFactor = _pfsg.factors();

  229.         int iNumVariate = aadblFactor[0].length;
  230.         double[] adblVariateVolatility = new double[iNumVariate];
  231.         double[] adblFactorPointVolatility = new double[iNumFactor];

  232.         for (int iVariateIndex = 0; iVariateIndex < iNumVariate; ++iVariateIndex) {
  233.             try {
  234.                 adblVariateVolatility[iVariateIndex] = _aMSVolatility[iVariateIndex].node (iXDate, iYDate);
  235.             } catch (java.lang.Exception e) {
  236.                 e.printStackTrace();

  237.                 return null;
  238.             }
  239.         }

  240.         for (int iFactorIndex = 0; iFactorIndex < iNumFactor; ++iFactorIndex) {
  241.             adblFactorPointVolatility[iFactorIndex] = 0.;
  242.             double[] adblFactor = aadblFactor[iFactorIndex];

  243.             for (int iVariateIndex = 0; iVariateIndex < iNumVariate; ++iVariateIndex)
  244.                 adblFactorPointVolatility[iFactorIndex] += adblFactor[iVariateIndex] *
  245.                     adblVariateVolatility[iVariateIndex];
  246.         }

  247.         return adblFactorPointVolatility;
  248.     }

  249.     /**
  250.      * Compute the Weighted Factor Point Volatility
  251.      *
  252.      * @param iFactorIndex The Factor Index
  253.      * @param iXDate The X Date
  254.      * @param iYDate The Y Date
  255.      *
  256.      * @return The Weighted Factor Point Volatility
  257.      *
  258.      * @throws java.lang.Exception Thrown if the Weighted Factor Point Volatility cannot be computed
  259.      */

  260.     public double weightedFactorPointVolatility (
  261.         final int iFactorIndex,
  262.         final int iXDate,
  263.         final int iYDate)
  264.         throws java.lang.Exception
  265.     {
  266.         int iNumFactor = _pfsg.numFactor();

  267.         if (iFactorIndex >= iNumFactor)
  268.             throw new java.lang.Exception
  269.                 ("MultiFactorVolatility::weightedFactorPointVolatility => Invalid Factor Index");

  270.         double[] adblFactor = _pfsg.factors()[iFactorIndex];

  271.         int iNumVariate = adblFactor.length;
  272.         double dblFactorPointVolatility = 0.;

  273.         for (int i = 0; i < iNumVariate; ++i)
  274.             dblFactorPointVolatility += adblFactor[i] * _aMSVolatility[i].node (iXDate, iYDate);

  275.         return _pfsg.factorWeight()[iFactorIndex] * dblFactorPointVolatility;
  276.     }

  277.     /**
  278.      * Compute the Point Volatility Modulus
  279.      *
  280.      * @param iXDate The X Date
  281.      * @param iYDate The Y Date
  282.      *
  283.      * @return The Point Volatility Modulus
  284.      *
  285.      * @throws java.lang.Exception Thrown if the Point Volatility Modulus cannot be computed
  286.      */

  287.     public double pointVolatilityModulus (
  288.         final int iXDate,
  289.         final int iYDate)
  290.         throws java.lang.Exception
  291.     {
  292.         int iNumFactor = _pfsg.numFactor();

  293.         double dblPointVolatilityModulus = 0.;

  294.         for (int i = 0; i < iNumFactor; ++i) {
  295.             double dblWeightedFactorPointVolatility = weightedFactorPointVolatility (i, iXDate, iYDate);

  296.             dblPointVolatilityModulus += dblWeightedFactorPointVolatility * dblWeightedFactorPointVolatility;
  297.         }

  298.         return dblPointVolatilityModulus;
  299.     }

  300.     /**
  301.      * Compute the Point Volatility Modulus Derivative
  302.      *
  303.      * @param iXDate The X Date
  304.      * @param iYDate The Y Date
  305.      * @param iOrder The Derivative Order
  306.      * @param bTerminal TRUE - Derivative off of the Y Date; FALSE - Derivative off of the X Date
  307.      *
  308.      * @return The Point Volatility Modulus Derivative
  309.      *
  310.      * @throws java.lang.Exception Thrown if the Point Volatility Modulus Derivative cannot be computed
  311.      */

  312.     public double pointVolatilityModulusDerivative (
  313.         final int iXDate,
  314.         final int iYDate,
  315.         final int iOrder,
  316.         final boolean bTerminal)
  317.         throws java.lang.Exception
  318.     {
  319.         org.drip.function.definition.R1ToR1 pointVolatilityR1ToR1 = new
  320.             org.drip.function.definition.R1ToR1 (null) {
  321.             @Override public double evaluate (
  322.                 final double dblVariate)
  323.                 throws java.lang.Exception
  324.             {
  325.                 return bTerminal ? pointVolatilityModulus (iXDate, (int) dblVariate) : pointVolatilityModulus
  326.                     ((int) dblVariate, iYDate);
  327.             }
  328.         };

  329.         return bTerminal ? pointVolatilityR1ToR1.derivative (iXDate, iOrder) :
  330.             pointVolatilityR1ToR1.derivative (iXDate, iOrder);
  331.     }
  332. }