Covariance.java

  1. package org.drip.measure.gaussian;

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

  77. /**
  78.  * <i>Covariance</i> holds the Standard Covariance Matrix, and provides functions to manipulate it.
  79.  *
  80.  *  <br><br>
  81.  *  <ul>
  82.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ComputationalCore.md">Computational Core Module</a></li>
  83.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/NumericalAnalysisLibrary.md">Numerical Analysis Library</a></li>
  84.  *      <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>
  85.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/measure/gaussian/README.md">R<sup>1</sup> R<sup>d</sup> Covariant Gaussian Quadrature</a></li>
  86.  *  </ul>
  87.  *
  88.  * @author Lakshmi Krishnamurthy
  89.  */

  90. public class Covariance {
  91.     private double[][] _aadblPrecision = null;
  92.     private double[][] _aadblCovariance = null;

  93.     /**
  94.      * Covariance Constructor
  95.      *
  96.      * @param aadblCovariance Double Array of the Covariance Matrix
  97.      *
  98.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  99.      */

  100.     public Covariance (
  101.         final double[][] aadblCovariance)
  102.         throws java.lang.Exception
  103.     {
  104.         if (null == (_aadblCovariance = aadblCovariance))
  105.             throw new java.lang.Exception ("Covariance Constructor => Invalid Inputs!");

  106.         int iNumVariate = _aadblCovariance.length;

  107.         if (0 == iNumVariate)
  108.             throw new java.lang.Exception ("R1MultivariateNormal Constructor => Invalid Inputs!");

  109.         for (int i = 0; i < iNumVariate; ++i) {
  110.             if (null == _aadblCovariance[i] || iNumVariate != _aadblCovariance[i].length ||
  111.                 !org.drip.numerical.common.NumberUtil.IsValid (_aadblCovariance[i]))
  112.                 throw new java.lang.Exception ("R1MultivariateNormal Constructor => Invalid Inputs!");
  113.         }

  114.         if (null == (_aadblPrecision = org.drip.numerical.linearalgebra.Matrix.InvertUsingGaussianElimination
  115.             (_aadblCovariance)))
  116.             throw new java.lang.Exception ("R1MultivariateNormal Constructor => Invalid Inputs!");
  117.     }

  118.     /**
  119.      * Retrieve the Number of Variates
  120.      *
  121.      * @return The Number of Variates
  122.      */

  123.     public int numVariate()
  124.     {
  125.         return _aadblCovariance.length;
  126.     }

  127.     /**
  128.      * Retrieve the Covariance Matrix
  129.      *
  130.      * @return The Covariance Matrix
  131.      */

  132.     public double[][] covarianceMatrix()
  133.     {
  134.         return _aadblCovariance;
  135.     }

  136.     /**
  137.      * Retrieve the Precision Matrix
  138.      *
  139.      * @return The Precision Matrix
  140.      */

  141.     public double[][] precisionMatrix()
  142.     {
  143.         return _aadblPrecision;
  144.     }

  145.     /**
  146.      * Retrieve the Variance Array
  147.      *
  148.      * @return The Variance Array
  149.      */

  150.     public double[] variance()
  151.     {
  152.         int iNumVariate = _aadblCovariance.length;
  153.         double[] adblVariance = new double[iNumVariate];

  154.         for (int i = 0; i < iNumVariate; ++i)
  155.             adblVariance[i] = _aadblCovariance[i][i];

  156.         return adblVariance;
  157.     }

  158.     /**
  159.      * Retrieve the Volatility Array
  160.      *
  161.      * @return The Volatility Array
  162.      */

  163.     public double[] volatility()
  164.     {
  165.         int iNumVariate = _aadblCovariance.length;
  166.         double[] adblVolatility = new double[iNumVariate];

  167.         for (int i = 0; i < iNumVariate; ++i)
  168.             adblVolatility[i] = java.lang.Math.sqrt (_aadblCovariance[i][i]);

  169.         return adblVolatility;
  170.     }

  171.     /**
  172.      * Retrieve the Correlation Matrix
  173.      *
  174.      * @return The Correlation Matrix
  175.      */

  176.     public double[][] correlationMatrix()
  177.     {
  178.         int iNumVariate = _aadblCovariance.length;
  179.         double[][] aadblCorrelation = new double[iNumVariate][iNumVariate];

  180.         double[] adblVolatility = volatility();

  181.         for (int i = 0; i < iNumVariate; ++i) {
  182.             for (int j = 0; j < iNumVariate; ++j)
  183.                 aadblCorrelation[i][j] = _aadblCovariance[i][j] / (adblVolatility[i] * adblVolatility[j]);
  184.         }

  185.         return aadblCorrelation;
  186.     }
  187. }