SubMatrixSetExtractor.java

  1. package org.drip.spaces.big;

  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>SubMatrixSetExtractor</i> contains the Functionality to extract the Set of the Sub-matrices contained
  80.  * inside of the given Matrix.
  81.  *
  82.  * <br><br>
  83.  *  <ul>
  84.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ComputationalCore.md">Computational Core Module</a></li>
  85.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/StatisticalLearningLibrary.md">Statistical Learning Library</a></li>
  86.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/spaces/README.md">R<sup>1</sup> and R<sup>d</sup> Vector/Tensor Spaces (Validated and/or Normed), and Function Classes</a></li>
  87.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/spaces/big/README.md">Big-data In-place Manipulator</a></li>
  88.  *  </ul>
  89.  * <br><br>
  90.  *
  91.  * @author Lakshmi Krishnamurthy
  92.  */

  93. public class SubMatrixSetExtractor {

  94.     /**
  95.      * Compute the Aggregate Composite Value of the Supplied Matrix
  96.      *  
  97.      * @param aadbl The Input Matrix
  98.      *
  99.      * @return The Aggregate Composite Value
  100.      *
  101.      * @throws java.lang.Exception Thrown if the Aggregate Composite Value cannot be compouted
  102.      */

  103.     public static final double CompositeValue (
  104.         final double[][] aadbl)
  105.         throws java.lang.Exception
  106.     {
  107.         if (null == aadbl)
  108.             throw new java.lang.Exception ("SubMatrixSetExtractor::CompositeValue => Invalid Inputs");

  109.         int iSize = aadbl.length;
  110.         double dblCompositeValue = 0.;

  111.         if (0 == iSize || 0 == aadbl[0].length)
  112.             throw new java.lang.Exception ("SubMatrixSetExtractor::CompositeValue => Invalid Inputs");

  113.         for (int i = 0; i < iSize; ++i) {
  114.             for (int j = 0; j < iSize; ++j) {
  115.                 if (!org.drip.numerical.common.NumberUtil.IsValid (aadbl[i][j]))
  116.                     throw new java.lang.Exception
  117.                         ("SubMatrixSetExtractor::CompositeValue => Invalid Inputs");

  118.                 dblCompositeValue += aadbl[i][j];
  119.             }
  120.         }

  121.         return dblCompositeValue;
  122.     }

  123.     /**
  124.      * Generate the List of all the sub-matrices contained within a specified Square Matrix starting from the
  125.      *  given Row and Column
  126.      *
  127.      * @param aadblMaster The Master Square Matrix
  128.      * @param iStartRow The Starting Row
  129.      * @param iStartColumn The Starting Column
  130.      *
  131.      * @return The List of all the sub-matrices
  132.      */

  133.     public static final java.util.List<double[][]> SquareSubMatrixList (
  134.         final double[][] aadblMaster,
  135.         final int iStartRow,
  136.         final int iStartColumn)
  137.     {
  138.         if (null == aadblMaster) return null;

  139.         int iMasterSize = aadblMaster.length;
  140.         int iMaxSubMatrixSize = iMasterSize - (iStartColumn > iStartRow ? iStartColumn : iStartRow);

  141.         if (0 == iMasterSize || 0 == aadblMaster[0].length || 0 == iMaxSubMatrixSize) return null;

  142.         java.util.List<double[][]> lsSubMatrix = new java.util.ArrayList<double[][]>();

  143.         for (int iSubMatrixSize = 1; iSubMatrixSize <= iMaxSubMatrixSize; ++iSubMatrixSize) {
  144.             double[][] aadblSubMatrix = new double[iSubMatrixSize][iSubMatrixSize];

  145.             for (int i = iStartRow; i < iStartRow + iSubMatrixSize; ++i) {
  146.                 for (int j = iStartColumn; j < iStartColumn + iSubMatrixSize; ++j)
  147.                     aadblSubMatrix[i - iStartRow][j - iStartColumn] = aadblMaster[i][j];
  148.             }

  149.             lsSubMatrix.add (aadblSubMatrix);
  150.         }

  151.         return lsSubMatrix;
  152.     }

  153.     /**
  154.      * Compute the Maximum Composite Value of all the sub-matrices contained within a specified Square Matrix
  155.      *  starting from the given Row and Column
  156.      *
  157.      * @param aadblMaster The Master Square Matrix
  158.      * @param iStartRow The Starting Row
  159.      * @param iStartColumn The Starting Column
  160.      *
  161.      * @return The List of all the sub-matrices
  162.      *
  163.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  164.      */

  165.     public static final double MaxCompositeSubMatrix (
  166.         final double[][] aadblMaster,
  167.         final int iStartRow,
  168.         final int iStartColumn)
  169.         throws java.lang.Exception
  170.     {
  171.         java.util.List<double[][]> lsSubMatrix = SquareSubMatrixList (aadblMaster, iStartRow, iStartColumn);

  172.         if (null == lsSubMatrix || 0 == lsSubMatrix.size())
  173.             throw new java.lang.Exception ("SubMatrixSetExtractor::MaxCompositeSubMatrix => Invalid Inputs");

  174.         double dblMaxCompositeSubMatrix = java.lang.Double.NEGATIVE_INFINITY;

  175.         for (double[][] aadblSubMatrix : lsSubMatrix) {
  176.             double dblCompositeSubMatrix = CompositeValue (aadblSubMatrix);

  177.             if (dblMaxCompositeSubMatrix < dblCompositeSubMatrix)
  178.                 dblMaxCompositeSubMatrix = dblCompositeSubMatrix;
  179.         }

  180.         return dblMaxCompositeSubMatrix;
  181.     }

  182.     /**
  183.      * Use the "Lean" Method to compute the Maximum Composite Value of all the sub-matrices contained within
  184.      *  a specified Square Matrix starting from the given Row and Column
  185.      *
  186.      * @param aadblMaster The Master Square Matrix
  187.      * @param iStartRow The Starting Row
  188.      * @param iStartColumn The Starting Column
  189.      *
  190.      * @return The List of all the sub-matrices
  191.      *
  192.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  193.      */

  194.     public static final double LeanMaxCompositeSubMatrix (
  195.         final double[][] aadblMaster,
  196.         final int iStartRow,
  197.         final int iStartColumn)
  198.         throws java.lang.Exception
  199.     {
  200.         if (null == aadblMaster)
  201.             throw new java.lang.Exception
  202.                 ("SubMatrixSetExtractor::LeanMaxCompositeSubMatrix => Invalid Inputs");

  203.         double dblCompositeSubMatrix = 0.;
  204.         int iMasterSize = aadblMaster.length;
  205.         double dblMaxCompositeSubMatrix = java.lang.Double.NEGATIVE_INFINITY;
  206.         int iMaxSubMatrixSize = iMasterSize - (iStartColumn > iStartRow ? iStartColumn : iStartRow);

  207.         if (0 == iMasterSize || 0 == aadblMaster[0].length || 0 == iMaxSubMatrixSize)
  208.             throw new java.lang.Exception
  209.                 ("SubMatrixSetExtractor::LeanMaxCompositeSubMatrix => Invalid Inputs");

  210.         for (int iSubMatrixSize = 1; iSubMatrixSize <= iMaxSubMatrixSize; ++iSubMatrixSize) {
  211.             for (int iRow = iStartRow; iRow < iStartRow + iSubMatrixSize - 2; ++iRow)
  212.                 dblCompositeSubMatrix += aadblMaster[iRow][iStartColumn + iSubMatrixSize - 2];

  213.             for (int iColumn = iStartColumn; iColumn < iStartColumn + iSubMatrixSize - 2; ++iColumn)
  214.                 dblCompositeSubMatrix += aadblMaster[iStartRow + iSubMatrixSize - 2][iColumn];

  215.             dblCompositeSubMatrix +=
  216.                 aadblMaster[iStartRow + iSubMatrixSize - 1][iStartColumn + iSubMatrixSize - 1];

  217.             if (dblMaxCompositeSubMatrix < dblCompositeSubMatrix)
  218.                 dblMaxCompositeSubMatrix = dblCompositeSubMatrix;
  219.         }

  220.         return dblMaxCompositeSubMatrix;
  221.     }
  222. }