MarketCorrelation.java

  1. package org.drip.exposure.universe;

  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>MarketCorrelation</i> holds the Cross Latent State Correlations needed for computing the Valuation
  77.  * Adjustment. The References are:
  78.  *  
  79.  * <br><br>
  80.  *      <ul>
  81.  *          <li>
  82.  *              Burgard, C., and M. Kjaer (2013): Funding Costs, Funding Strategies <i>Risk</i> <b>23
  83.  *                  (12)</b> 82-87
  84.  *          </li>
  85.  *          <li>
  86.  *              Burgard, C., and M. Kjaer (2014): In the Balance <i>Risk</i> <b>24 (11)</b> 72-75
  87.  *          </li>
  88.  *          <li>
  89.  *              Burgard, C., and M. Kjaer (2014): PDE Representations of Derivatives with Bilateral Counter-
  90.  *                  party Risk and Funding Costs <i>Journal of Credit Risk</i> <b>7 (3)</b> 1-19
  91.  *          </li>
  92.  *          <li>
  93.  *              Gregory, J. (2009): Being Two-faced over Counter-party Credit Risk <i>Risk</i> <b>20 (2)</b>
  94.  *                  86-90
  95.  *          </li>
  96.  *          <li>
  97.  *              Piterbarg, V. (2010): Funding Beyond Discounting: Collateral Agreements and Derivatives
  98.  *                  Pricing <i>Risk</i> <b>21 (2)</b> 97-102
  99.  *          </li>
  100.  *          <li>
  101.  *      </ul>
  102.  *
  103.  *  <br><br>
  104.  *  <ul>
  105.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/PortfolioCore.md">Portfolio Core Module</a></li>
  106.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ExposureAnalyticsLibrary.md">Exposure Analytics</a></li>
  107.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/exposure/README.md">Exposure Group Level Collateralized/Uncollateralized Exposure</a></li>
  108.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/exposure/universe/README.md">Exposure Generation - Market States Simulation</a></li>
  109.  *  </ul>
  110.  *
  111.  * @author Lakshmi Krishnamurthy
  112.  */

  113. public class MarketCorrelation
  114. {
  115.     private double[][] _matrix = null;
  116.     private java.util.List<org.drip.state.identifier.LatentStateLabel> _latentStateLabelList = null;

  117.     private java.util.Map<java.lang.String, java.lang.Integer> _latentStateIndexMap = new
  118.         java.util.HashMap<java.lang.String, java.lang.Integer>();

  119.     /**
  120.      * MarketCorrelation Constructor
  121.      *
  122.      * @param latentStateLabelList The Latent State Label List
  123.      * @param matrix The Square Correlation Matrix
  124.      *
  125.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  126.      */

  127.     public MarketCorrelation (
  128.         final java.util.List<org.drip.state.identifier.LatentStateLabel> latentStateLabelList,
  129.         final double[][] matrix)
  130.         throws java.lang.Exception
  131.     {
  132.         if (null == (_latentStateLabelList = latentStateLabelList) ||
  133.             null == (_matrix = matrix))
  134.         {
  135.             throw new java.lang.Exception ("MarketCorrelation Constructor => Invalid Inputs");
  136.         }

  137.         int latentStateCount = _latentStateLabelList.size();

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

  142.         for (int latentStateIndex = 0; latentStateIndex < latentStateCount; ++latentStateIndex)
  143.         {
  144.             org.drip.state.identifier.LatentStateLabel latentStateLabel = _latentStateLabelList.get
  145.                 (latentStateIndex);

  146.             if (null == latentStateLabel ||
  147.                 null == _matrix[latentStateIndex] ||
  148.                 latentStateCount != _matrix[latentStateIndex].length)
  149.             {
  150.                 throw new java.lang.Exception ("MarketCorrelation Constructor => Invalid Inputs");
  151.             }

  152.             for (int matrixColumnIndex = 0; matrixColumnIndex < latentStateCount; ++matrixColumnIndex)
  153.             {
  154.                 double correlationEntry = _matrix[latentStateIndex][matrixColumnIndex];

  155.                 if (!org.drip.numerical.common.NumberUtil.IsValid (correlationEntry) ||
  156.                     (latentStateIndex == matrixColumnIndex && 1 != correlationEntry) ||
  157.                     (latentStateIndex != matrixColumnIndex && (1 < correlationEntry ||
  158.                         -1 < correlationEntry)))
  159.                 {
  160.                     throw new java.lang.Exception ("MarketCorrelation Constructor => Invalid Inputs");
  161.                 }
  162.             }

  163.             java.lang.String latentStateLabelFQN = latentStateLabel.fullyQualifiedName();

  164.             if (_latentStateIndexMap.containsKey (latentStateLabelFQN))
  165.             {
  166.                 throw new java.lang.Exception ("MarketCorrelation Constructor => Invalid Inputs");
  167.             }

  168.             _latentStateIndexMap.put (
  169.                 latentStateLabelFQN,
  170.                 latentStateIndex
  171.             );
  172.         }
  173.     }

  174.     /**
  175.      * Retrieve the Latent State Label List
  176.      *
  177.      * @return The Latent State Label List
  178.      */

  179.     public java.util.List<org.drip.state.identifier.LatentStateLabel> latentStateLabelList()
  180.     {
  181.         return _latentStateLabelList;
  182.     }

  183.     /**
  184.      * Retrieve the Cross-Latent State Correlation Matrix
  185.      *
  186.      * @return The Cross-Latent State Correlation Matrix
  187.      */

  188.     public double[][] matrix()
  189.     {
  190.         return _matrix;
  191.     }

  192.     /**
  193.      * Check if the Latent State  is available in the Correlation Matrix
  194.      *
  195.      * @param latentStateLabel The Latent State Label
  196.      *
  197.      * @return TRUE - The Latent State  is available in the Correlation Matrix
  198.      */

  199.     public boolean latentStateExists (
  200.         final org.drip.state.identifier.LatentStateLabel latentStateLabel)
  201.     {
  202.         return null != latentStateLabel && _latentStateIndexMap.containsKey
  203.             (latentStateLabel.fullyQualifiedName());
  204.     }

  205.     /**
  206.      * Retrieve the Cross State Correlation
  207.      *
  208.      * @param latentStateLabel1 Latent State Label #1
  209.      * @param latentStateLabel2 Latent State Label #2
  210.      *
  211.      * @return The Cross State Correlation
  212.      */

  213.     public double entry (
  214.         final org.drip.state.identifier.LatentStateLabel latentStateLabel1,
  215.         final org.drip.state.identifier.LatentStateLabel latentStateLabel2)
  216.     {
  217.         return !latentStateExists (latentStateLabel1) || !latentStateExists (latentStateLabel2) ? 0. :
  218.             _matrix[_latentStateIndexMap.get (latentStateLabel1.fullyQualifiedName())]
  219.                 [_latentStateIndexMap.get (latentStateLabel2.fullyQualifiedName())];
  220.     }

  221.     /**
  222.      * Synthesize a MarketCorrelation Instance for the Custom Latent State List
  223.      *
  224.      * @param customLatentStateLabelList The Custom Latent State List
  225.      *
  226.      * @return The MarketCorrelation Instance for the Custom Latent State List
  227.      */

  228.     public MarketCorrelation customMarketCorrelation (
  229.         final java.util.List<org.drip.state.identifier.LatentStateLabel> customLatentStateLabelList)
  230.     {
  231.         if (null == customLatentStateLabelList)
  232.         {
  233.             return null;
  234.         }

  235.         int customLatentStateCount = customLatentStateLabelList.size();

  236.         if (0 == customLatentStateCount)
  237.         {
  238.             return null;
  239.         }

  240.         double[][] customMatix = new double[customLatentStateCount][customLatentStateCount];

  241.         for (int customLatentStateIndexOuter = 0; customLatentStateIndexOuter < customLatentStateCount;
  242.             ++customLatentStateIndexOuter)
  243.         {
  244.             for (int customLatentStateIndexInner = 0; customLatentStateIndexInner < customLatentStateCount;
  245.                 ++customLatentStateIndexInner)
  246.             {
  247.                 customMatix[customLatentStateIndexOuter][customLatentStateIndexInner] =
  248.                     customLatentStateIndexOuter == customLatentStateIndexInner ? 1. :
  249.                     entry (
  250.                         customLatentStateLabelList.get (customLatentStateIndexOuter),
  251.                         customLatentStateLabelList.get (customLatentStateIndexInner)
  252.                     );
  253.             }
  254.         }

  255.         try
  256.         {
  257.             return new MarketCorrelation (
  258.                 customLatentStateLabelList,
  259.                 customMatix
  260.             );
  261.         }
  262.         catch (java.lang.Exception e)
  263.         {
  264.             e.printStackTrace();
  265.         }

  266.         return null;
  267.     }
  268. }