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

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

  115. public class LabelCorrelation extends org.drip.measure.stochastic.LabelBase
  116. {
  117.     protected double[][] _matrix = null;

  118.     /**
  119.      * LabelCorrelation Constructor
  120.      *
  121.      * @param labelList The List of Labels
  122.      * @param matrix The Correlation Matrix
  123.      *
  124.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  125.      */

  126.     public LabelCorrelation (
  127.         final java.util.List<java.lang.String> labelList,
  128.         final double[][] matrix)
  129.         throws java.lang.Exception
  130.     {
  131.         super (labelList);

  132.         if (null == (_matrix = matrix))
  133.         {
  134.             throw new java.lang.Exception ("LabelCorrelation Constructor => Invalid Inputs");
  135.         }

  136.         int labelCount = labelList.size();

  137.         if (0 == labelCount || labelCount != _matrix.length)
  138.         {
  139.             throw new java.lang.Exception ("LabelCorrelation Constructor => Invalid Inputs");
  140.         }

  141.         for (int labelIndex = 0; labelIndex < labelCount; ++labelIndex)
  142.         {
  143.             if (null == _matrix[labelIndex] || labelCount != _matrix[labelIndex].length ||
  144.                 !org.drip.numerical.common.NumberUtil.IsValid (_matrix[labelIndex]))
  145.             {
  146.                 throw new java.lang.Exception ("LabelCorrelation Constructor => Invalid Inputs");
  147.             }
  148.         }
  149.     }

  150.     /**
  151.      * Retrieve the Cross-Label Correlation Matrix
  152.      *
  153.      * @return The Cross-Label Correlation Matrix
  154.      */

  155.     public double[][] matrix()
  156.     {
  157.         return _matrix;
  158.     }

  159.     /**
  160.      * Retrieve the Correlation Entry for the Pair of Labels
  161.      *
  162.      * @param label1 Label #1
  163.      * @param label2 Label #2
  164.      *
  165.      * @return The Correlation Entry
  166.      *
  167.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  168.      */

  169.     public double entry (
  170.         final java.lang.String label1,
  171.         final java.lang.String label2)
  172.         throws java.lang.Exception
  173.     {
  174.         if (null == label1 || !_labelList.contains (label1) ||
  175.             null == label2 || !_labelList.contains (label2))
  176.         {
  177.             throw new java.lang.Exception ("LabelCorrelation::entry => Invalid Inputs");
  178.         }

  179.         return _matrix[_labelIndexMap.get (label1)][_labelIndexMap.get (label2)];
  180.     }

  181.     /**
  182.      * Generate the InterestRateTenorCorrelation Instance that corresponds to the Tenor sub-space
  183.      *
  184.      * @param subTenorList The sub-Tenor List
  185.      *
  186.      * @return The InterestRateTenorCorrelation Instance
  187.      */

  188.     public LabelCorrelation subTenor (
  189.         final java.util.List<java.lang.String> subTenorList)
  190.     {
  191.         if (null == subTenorList)
  192.         {
  193.             return null;
  194.         }

  195.         int subTenorSize = subTenorList.size();

  196.         if (0 == subTenorSize)
  197.         {
  198.             return null;
  199.         }

  200.         double[][] subTenorMatrix = new double[subTenorSize][subTenorSize];

  201.         for (int subTenorOuterIndex = 0; subTenorOuterIndex < subTenorSize; ++subTenorOuterIndex)
  202.         {
  203.             for (int subTenorInnerIndex = 0; subTenorInnerIndex < subTenorSize; ++subTenorInnerIndex)
  204.             {
  205.                 try
  206.                 {
  207.                     subTenorMatrix[subTenorOuterIndex][subTenorInnerIndex] = entry (
  208.                         subTenorList.get (subTenorOuterIndex),
  209.                         subTenorList.get (subTenorInnerIndex)
  210.                     );
  211.                 }
  212.                 catch (java.lang.Exception e)
  213.                 {
  214.                     e.printStackTrace();

  215.                     return null;
  216.                 }
  217.             }
  218.         }

  219.         try
  220.         {
  221.             return new LabelCorrelation (
  222.                 subTenorList,
  223.                 subTenorMatrix
  224.             );
  225.         }
  226.         catch (java.lang.Exception e)
  227.         {
  228.             e.printStackTrace();
  229.         }

  230.         return null;
  231.     }
  232. }