LabelCovariance.java

  1. package org.drip.measure.stochastic;

  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.  *
  9.  *  This file is part of DROP, an open-source library targeting analytics/risk, transaction cost analytics,
  10.  *      asset liability management analytics, capital, exposure, and margin analytics, valuation adjustment
  11.  *      analytics, and portfolio construction analytics within and across fixed income, credit, commodity,
  12.  *      equity, FX, and structured products. It also includes auxiliary libraries for algorithm support,
  13.  *      numerical analysis, numerical optimization, spline builder, model validation, statistical learning,
  14.  *      and computational support.
  15.  *  
  16.  *      https://lakshmidrip.github.io/DROP/
  17.  *  
  18.  *  DROP is composed of three modules:
  19.  *  
  20.  *  - DROP Product Core - https://lakshmidrip.github.io/DROP-Product-Core/
  21.  *  - DROP Portfolio Core - https://lakshmidrip.github.io/DROP-Portfolio-Core/
  22.  *  - DROP Computational Core - https://lakshmidrip.github.io/DROP-Computational-Core/
  23.  *
  24.  *  DROP Product Core implements libraries for the following:
  25.  *  - Fixed Income Analytics
  26.  *  - Loan Analytics
  27.  *  - Transaction Cost Analytics
  28.  *
  29.  *  DROP Portfolio Core implements libraries for the following:
  30.  *  - Asset Allocation Analytics
  31.  *  - Asset Liability Management Analytics
  32.  *  - Capital Estimation Analytics
  33.  *  - Exposure Analytics
  34.  *  - Margin Analytics
  35.  *  - XVA Analytics
  36.  *
  37.  *  DROP Computational Core implements libraries for the following:
  38.  *  - Algorithm Support
  39.  *  - Computation Support
  40.  *  - Function Analysis
  41.  *  - Model Validation
  42.  *  - Numerical Analysis
  43.  *  - Numerical Optimizer
  44.  *  - Spline Builder
  45.  *  - Statistical Learning
  46.  *
  47.  *  Documentation for DROP is Spread Over:
  48.  *
  49.  *  - Main                     => https://lakshmidrip.github.io/DROP/
  50.  *  - Wiki                     => https://github.com/lakshmiDRIP/DROP/wiki
  51.  *  - GitHub                   => https://github.com/lakshmiDRIP/DROP
  52.  *  - Repo Layout Taxonomy     => https://github.com/lakshmiDRIP/DROP/blob/master/Taxonomy.md
  53.  *  - Javadoc                  => https://lakshmidrip.github.io/DROP/Javadoc/index.html
  54.  *  - Technical Specifications => https://github.com/lakshmiDRIP/DROP/tree/master/Docs/Internal
  55.  *  - Release Versions         => https://lakshmidrip.github.io/DROP/version.html
  56.  *  - Community Credits        => https://lakshmidrip.github.io/DROP/credits.html
  57.  *  - Issues Catalog           => https://github.com/lakshmiDRIP/DROP/issues
  58.  *  - JUnit                    => https://lakshmidrip.github.io/DROP/junit/index.html
  59.  *  - Jacoco                   => https://lakshmidrip.github.io/DROP/jacoco/index.html
  60.  *
  61.  *  Licensed under the Apache License, Version 2.0 (the "License");
  62.  *      you may not use this file except in compliance with the License.
  63.  *  
  64.  *  You may obtain a copy of the License at
  65.  *      http://www.apache.org/licenses/LICENSE-2.0
  66.  *  
  67.  *  Unless required by applicable law or agreed to in writing, software
  68.  *      distributed under the License is distributed on an "AS IS" BASIS,
  69.  *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  70.  *  
  71.  *  See the License for the specific language governing permissions and
  72.  *      limitations under the License.
  73.  */

  74. /**
  75.  * <i>LabelCovariance</i> holds the Covariance between any Stochastic Variates identified by their Labels, as
  76.  * well as their Means. The References are:
  77.  *
  78.  * <br><br>
  79.  *  <ul>
  80.  *      <li>
  81.  *          Andersen, L. B. G., M. Pykhtin, and A. Sokol (2017): Credit Exposure in the Presence of Initial
  82.  *              Margin https://papers.ssrn.com/sol3/papers.cfm?abstract_id=2806156 <b>eSSRN</b>
  83.  *      </li>
  84.  *      <li>
  85.  *          Albanese, C., S. Caenazzo, and O. Frankel (2017): Regression Sensitivities for Initial Margin
  86.  *              Calculations https://papers.ssrn.com/sol3/papers.cfm?abstract_id=2763488 <b>eSSRN</b>
  87.  *      </li>
  88.  *      <li>
  89.  *          Anfuso, F., D. Aziz, P. Giltinan, and K. Loukopoulus (2017): A Sound Modeling and Back-testing
  90.  *              Framework for Forecasting Initial Margin Requirements
  91.  *              https://papers.ssrn.com/sol3/papers.cfm?abstract_id=2716279 <b>eSSRN</b>
  92.  *      </li>
  93.  *      <li>
  94.  *          Caspers, P., P. Giltinan, R. Lichters, and N. Nowaczyk (2017): Forecasting Initial Margin
  95.  *              Requirements - A Model Evaluation https://papers.ssrn.com/sol3/papers.cfm?abstract_id=2911167
  96.  *              <b>eSSRN</b>
  97.  *      </li>
  98.  *      <li>
  99.  *          International Swaps and Derivatives Association (2017): SIMM v2.0 Methodology
  100.  *              https://www.isda.org/a/oFiDE/isda-simm-v2.pdf
  101.  *      </li>
  102.  *  </ul>
  103.  *
  104.  *  <br><br>
  105.  *  <ul>
  106.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ComputationalCore.md">Computational Core Module</a></li>
  107.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/NumericalAnalysisLibrary.md">Numerical Analysis Library</a></li>
  108.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/measure/README.md">R<sup>d</sup> Continuous/Discrete Probability Measures</a></li>
  109.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/measure/stochastic/README.md">R<sup>1</sup> R<sup>1</sup> To R<sup>1</sup> Process</a></li>
  110.  *  </ul>
  111.  *
  112.  * @author Lakshmi Krishnamurthy
  113.  */

  114. public class LabelCovariance extends org.drip.measure.stochastic.LabelCorrelation
  115. {
  116.     private double[] _meanArray = null;
  117.     private double[] _volatilityArray = null;
  118.     private org.drip.measure.gaussian.Covariance _covariance = null;

  119.     /**
  120.      * LabelCovariance Constructor
  121.      *
  122.      * @param labelList The List of Labels
  123.      * @param meanArray Array of Variate Means
  124.      * @param volatilityArray Array of Variate Volatilities
  125.      * @param correlationMatrix The Correlation Matrix
  126.      *
  127.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  128.      */

  129.     public LabelCovariance (
  130.         final java.util.List<java.lang.String> labelList,
  131.         final double[] meanArray,
  132.         final double[] volatilityArray,
  133.         final double[][] correlationMatrix)
  134.         throws java.lang.Exception
  135.     {
  136.         super (
  137.             labelList,
  138.             correlationMatrix
  139.         );

  140.         if (null == (_meanArray = meanArray) ||
  141.             null == (_volatilityArray = volatilityArray))
  142.         {
  143.             throw new java.lang.Exception ("LabelCovariance Constructor => Invalid Inputs");
  144.         }

  145.         int variateCount = correlationMatrix.length;
  146.         double[][] covarianceMatrix = new double[variateCount][variateCount];

  147.         if (variateCount != _meanArray.length ||
  148.             variateCount != _volatilityArray.length)
  149.         {
  150.             throw new java.lang.Exception ("LabelCovariance Constructor => Invalid Inputs");
  151.         }

  152.         for (int variateIndexI = 0; variateIndexI < variateCount; ++variateIndexI)
  153.         {
  154.             if (!org.drip.numerical.common.NumberUtil.IsValid (_meanArray[variateIndexI]) ||
  155.                 !org.drip.numerical.common.NumberUtil.IsValid (_volatilityArray[variateIndexI]) ||
  156.                 0. > _volatilityArray[variateIndexI])
  157.             {
  158.                 throw new java.lang.Exception ("LabelCovariance Constructor => Invalid Inputs");
  159.             }

  160.             for (int variateIndexJ = 0; variateIndexJ < variateCount; ++variateIndexJ)
  161.             {
  162.                 covarianceMatrix[variateIndexI][variateIndexJ] =
  163.                     correlationMatrix[variateIndexI][variateIndexJ] * _volatilityArray[variateIndexI] *
  164.                     _volatilityArray[variateIndexJ];
  165.             }
  166.         }

  167.         _covariance = new org.drip.measure.gaussian.Covariance (covarianceMatrix);
  168.     }

  169.     /**
  170.      * Retrieve the Array of Variate Means
  171.      *
  172.      * @return The Array of Variate Means
  173.      */

  174.     public double[] meanArray()
  175.     {
  176.         return _meanArray;
  177.     }

  178.     /**
  179.      * Retrieve the Array of Variate Volatilities
  180.      *
  181.      * @return The Array of Variate Volatilities
  182.      */

  183.     public double[] volatilityArray()
  184.     {
  185.         return _volatilityArray;
  186.     }

  187.     /**
  188.      * Retrieve the Correlation Matrix
  189.      *
  190.      * @return The Correlation Matrix
  191.      */

  192.     public double[][] correlationMatrix()
  193.     {
  194.         return _matrix;
  195.     }

  196.     /**
  197.      * Retrieve the Covariance Matrix
  198.      *
  199.      * @return The Covariance Matrix
  200.      */

  201.     public double[][] covarianceMatrix()
  202.     {
  203.         return _covariance.covarianceMatrix();
  204.     }

  205.     /**
  206.      * Retrieve the Precision Matrix
  207.      *
  208.      * @return The Precision Matrix
  209.      */

  210.     public double[][] precisionMatrix()
  211.     {
  212.         return _covariance.precisionMatrix();
  213.     }

  214.     /**
  215.      * Retrieve the Mean of the Latent State
  216.      *
  217.      * @param label Latent State Label
  218.      *
  219.      * @return Mean of the Latent State
  220.      *
  221.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  222.      */

  223.     public double mean (
  224.         final java.lang.String label)
  225.         throws java.lang.Exception
  226.     {
  227.         if (null == label || !_labelList.contains (label))
  228.         {
  229.             throw new java.lang.Exception ("LabelCovariance::mean => Invalid Inputs");
  230.         }

  231.         return _meanArray[_labelIndexMap.get (label)];
  232.     }

  233.     /**
  234.      * Retrieve the Volatility of the Latent State
  235.      *
  236.      * @param label Latent State Label
  237.      *
  238.      * @return Volatility of the Latent State
  239.      *
  240.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  241.      */

  242.     public double volatility (
  243.         final java.lang.String label)
  244.         throws java.lang.Exception
  245.     {
  246.         if (null == label || !_labelList.contains (label))
  247.         {
  248.             throw new java.lang.Exception ("LabelCovariance::volatility => Invalid Inputs");
  249.         }

  250.         return _volatilityArray[_labelIndexMap.get (label)];
  251.     }
  252. }