SubMatrixSetExtraction.java

  1. package org.drip.sample.algo;

  2. import java.util.List;

  3. import org.drip.numerical.common.FormatUtil;
  4. import org.drip.service.env.EnvManager;
  5. import org.drip.spaces.big.SubMatrixSetExtractor;

  6. /*
  7.  * -*- mode: java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  8.  */

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

  82. /**
  83.  * <i>SubMatrixSetStringExtraction</i> demonstrates the Extraction and Usage of the Inner Sub-matrices of a
  84.  * given Master Matrix.
  85.  *
  86.  * <br><br>
  87.  *  <ul>
  88.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ComputationalCore.md">Computational Core Module</a></li>
  89.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/StatisticalLearningLibrary.md">Statistical Learning Library</a></li>
  90.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/README.md">DROP API Construction and Usage</a></li>
  91.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/algo/README.md">C<sup>x</sup> R<sup>x</sup> In-Place Manipulation</a></li>
  92.  *  </ul>
  93.  * <br><br>
  94.  *
  95.  * @author Lakshmi Krishnamurthy
  96.  */

  97. public class SubMatrixSetExtraction {

  98.     public static final void main (
  99.         final String[] astrArgs)
  100.         throws Exception
  101.     {
  102.         EnvManager.InitEnv (
  103.             "",
  104.             true
  105.         );

  106.         int iSize = 4;
  107.         double[][] aadbl = new double[iSize][iSize];

  108.         for (int i = 0; i < iSize; ++i) {
  109.             for (int j = 0; j < iSize; ++j)
  110.                 aadbl[i][j] = Math.random() - 0.5;
  111.         }

  112.         System.out.println ();

  113.         for (int iStartRow = 0; iStartRow < iSize; ++iStartRow) {
  114.             for (int iStartColumn = 0; iStartColumn < iSize; ++iStartColumn) {
  115.                 double dblMaxCompositeSum = -1. * iSize * iSize;

  116.                 List<double[][]> lsSubMatrix = SubMatrixSetExtractor.SquareSubMatrixList (
  117.                     aadbl,
  118.                     iStartRow,
  119.                     iStartColumn
  120.                 );

  121.                 for (double[][] aadblSubMatrix : lsSubMatrix) {
  122.                     double dblCompositeSum = SubMatrixSetExtractor.CompositeValue (aadblSubMatrix);

  123.                     if (dblCompositeSum > dblMaxCompositeSum) dblMaxCompositeSum = dblCompositeSum;
  124.                 }

  125.                 double dblMaxCompositeSumCombined = SubMatrixSetExtractor.MaxCompositeSubMatrix (
  126.                     aadbl,
  127.                     iStartRow,
  128.                     iStartColumn
  129.                 );

  130.                 double dblMaxCompositeSumLean = SubMatrixSetExtractor.LeanMaxCompositeSubMatrix (
  131.                     aadbl,
  132.                     iStartRow,
  133.                     iStartColumn
  134.                 );

  135.                 System.out.println (
  136.                     "\tMax[" + iStartRow + "][" + iStartColumn + "] => " +
  137.                     FormatUtil.FormatDouble (dblMaxCompositeSum, 1, 4, 1.) + " | " +
  138.                     FormatUtil.FormatDouble (dblMaxCompositeSumCombined, 1, 4, 1.) + " | " +
  139.                     FormatUtil.FormatDouble (dblMaxCompositeSumLean, 1, 4, 1.)
  140.                 );
  141.             }
  142.         }

  143.         System.out.println();

  144.         EnvManager.TerminateEnv();
  145.     }
  146. }