QREigenComponentExtractor.java

  1. package org.drip.numerical.eigen;

  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>QREigenComponentExtractor</i> extracts the Eigenvalues and Eigenvectors using QR Decomposition.
  80.  *
  81.  * <br><br>
  82.  *  <ul>
  83.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ComputationalCore.md">Computational Core Module</a></li>
  84.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/NumericalAnalysisLibrary.md">Numerical Analysis Library</a></li>
  85.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/numerical">Numerical Quadrature, Differentiation, Eigenization, Linear Algebra, and Utilities</a></li>
  86.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/numerical/eigen">QR PICE Eigen-Component Extraction Methodologies</a></li>
  87.  *  </ul>
  88.  * <br><br>
  89.  *
  90.  * @author Lakshmi Krishnamurthy
  91.  */

  92. public class QREigenComponentExtractor
  93.     implements org.drip.numerical.eigen.ComponentExtractor
  94. {
  95.     private int _maxIterations = -1;
  96.     private double _tolerance = java.lang.Double.NaN;

  97.     /**
  98.      * QREigenComponentExtractor Constructor
  99.      *
  100.      * @param maxIterations Maximum Number of Iterations
  101.      * @param tolerance Tolerance
  102.      *
  103.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  104.      */

  105.     public QREigenComponentExtractor (
  106.         final int maxIterations,
  107.         final double tolerance)
  108.         throws java.lang.Exception
  109.     {
  110.         if (0 >= (_maxIterations = maxIterations) ||
  111.             !org.drip.numerical.common.NumberUtil.IsValid (
  112.                 _tolerance = tolerance
  113.             ) || 0. == _tolerance
  114.         )
  115.         {
  116.             throw new java.lang.Exception ("QREigenComponentExtractor ctr: Invalid Inputs!");
  117.         }
  118.     }

  119.     /**
  120.      * Retrieve the Maximum Number of Iterations
  121.      *
  122.      * @return The Maximum Number of Iterations
  123.      */

  124.     public int maxIterations()
  125.     {
  126.         return _maxIterations;
  127.     }

  128.     /**
  129.      * Retrieve the Tolerance Level
  130.      *
  131.      * @return The Tolerance Level
  132.      */

  133.     public double tolerance()
  134.     {
  135.         return _tolerance;
  136.     }

  137.     @Override public org.drip.numerical.eigen.EigenOutput eigenize (
  138.         final double[][] a)
  139.     {
  140.         org.drip.numerical.linearalgebra.QR qr = org.drip.numerical.linearalgebra.Matrix.QRDecomposition (
  141.             a
  142.         );

  143.         if (null == qr)
  144.         {
  145.             return null;
  146.         }

  147.         double[][] q = qr.q();

  148.         double[][] qTranspose = org.drip.numerical.linearalgebra.Matrix.Transpose (
  149.             q
  150.         );

  151.         if (null == qTranspose)
  152.         {
  153.             return null;
  154.         }

  155.         int iterationIndex = 0;
  156.         int eigenComponentCount = a.length;
  157.         double[] eigenvalueArray = new double[eigenComponentCount];
  158.         double[][] b = new double[eigenComponentCount][eigenComponentCount];
  159.         double[][] v = new double[eigenComponentCount][eigenComponentCount];

  160.         if (0 == eigenComponentCount || null == qTranspose[0] || eigenComponentCount != qTranspose[0].length)
  161.         {
  162.             return null;
  163.         }

  164.         for (int rowIndex = 0;
  165.             rowIndex < eigenComponentCount;
  166.             ++rowIndex)
  167.         {
  168.             for (int columnIndex = 0;
  169.                 columnIndex < eigenComponentCount;
  170.                 ++columnIndex)
  171.             {
  172.                 b[rowIndex][columnIndex] = q[rowIndex][columnIndex];
  173.                 v[rowIndex][columnIndex] = a[rowIndex][columnIndex];
  174.             }
  175.         }

  176.         while (iterationIndex++ < _maxIterations &&
  177.             org.drip.numerical.linearalgebra.Matrix.NON_TRIANGULAR ==
  178.             org.drip.numerical.linearalgebra.Matrix.TriangularType (
  179.                 v,
  180.                 _tolerance
  181.             )
  182.         )
  183.         {
  184.             if (null == (qr = org.drip.numerical.linearalgebra.Matrix.QRDecomposition (
  185.                 v = org.drip.numerical.linearalgebra.Matrix.Product (
  186.                     qTranspose,
  187.                     org.drip.numerical.linearalgebra.Matrix.Product (
  188.                         v,
  189.                         q
  190.                     )
  191.                 )
  192.             )))
  193.             {
  194.                 return null;
  195.             }

  196.             qTranspose = org.drip.numerical.linearalgebra.Matrix.Transpose (
  197.                 q = qr.q()
  198.             );

  199.             b = org.drip.numerical.linearalgebra.Matrix.Product (
  200.                 b,
  201.                 q
  202.             );
  203.         }

  204.         if (iterationIndex >= _maxIterations)
  205.         {
  206.             return null;
  207.         }

  208.         for (int rowIndex = 0;
  209.             rowIndex < eigenComponentCount;
  210.             ++rowIndex)
  211.         {
  212.             eigenvalueArray[rowIndex] = v[rowIndex][rowIndex];
  213.         }

  214.         try
  215.         {
  216.             return new org.drip.numerical.eigen.EigenOutput (
  217.                 org.drip.numerical.linearalgebra.Matrix.Transpose (
  218.                     b
  219.                 ),
  220.                 eigenvalueArray
  221.             );
  222.         } catch (java.lang.Exception e)
  223.         {
  224.             e.printStackTrace();
  225.         }

  226.         return null;
  227.     }

  228.     /**
  229.      * Generate the Ordered List of Eigenvalues for the specified Eigen-output
  230.      *
  231.      * @param eigenOutput The Eigen Output
  232.      *
  233.      * @return The Order List
  234.      */

  235.     public java.util.List<java.lang.Integer> eigenComponentOrderList (
  236.         final org.drip.numerical.eigen.EigenOutput eigenOutput)
  237.     {
  238.         if (null == eigenOutput)
  239.         {
  240.             return null;
  241.         }

  242.         double[] eigenvalueArray = eigenOutput.eigenValueArray();

  243.         int eigenComponentCount = eigenvalueArray.length;

  244.         java.util.List<java.lang.Double> eigenValueList = new java.util.ArrayList<java.lang.Double>();

  245.         java.util.List<java.lang.Integer> eigenValueOrder = new java.util.ArrayList<java.lang.Integer>();

  246.         for (int eigenComponentIndex = 0;
  247.             eigenComponentIndex < eigenComponentCount;
  248.             ++eigenComponentIndex)
  249.         {
  250.             int eigenValueOrderSize = eigenValueOrder.size();

  251.             if (0 == eigenValueOrderSize)
  252.             {
  253.                 eigenValueOrder.add (
  254.                     eigenComponentIndex
  255.                 );

  256.                 eigenValueList.add (
  257.                     eigenvalueArray[eigenComponentIndex]
  258.                 );
  259.             }
  260.             else
  261.             {
  262.                 int insertionIndex = 0;

  263.                 for (int eigenValueOrderIndex = 0;
  264.                     eigenValueOrderIndex < eigenValueOrderSize;
  265.                     ++eigenValueOrderIndex)
  266.                 {
  267.                     if (eigenvalueArray[eigenComponentIndex] <= eigenValueList.get (
  268.                         eigenValueOrderIndex
  269.                     ))
  270.                     {
  271.                         insertionIndex = eigenValueOrderIndex;
  272.                         break;
  273.                     }
  274.                 }

  275.                 eigenValueOrder.add (
  276.                     insertionIndex,
  277.                     eigenComponentIndex
  278.                 );

  279.                 eigenValueList.add (
  280.                     insertionIndex,
  281.                     eigenvalueArray[eigenComponentIndex]
  282.                 );
  283.             }
  284.         }

  285.         return eigenValueOrder;
  286.     }

  287.     /**
  288.      * Generate the Ordered List of Eigen Components arranged by Ascending Eigenvalue
  289.      *
  290.      * @param a Input Matrix
  291.      *
  292.      * @return The Ordered List of Eigen Components arranged by Ascending Eigenvalue
  293.      */

  294.     public org.drip.numerical.eigen.EigenComponent[] orderedEigenComponentArray (
  295.         final double[][] a)
  296.     {
  297.         org.drip.numerical.eigen.EigenOutput eigenOutput = eigenize (
  298.             a
  299.         );

  300.         java.util.List<java.lang.Integer> eigenComponentOrderList = eigenComponentOrderList (
  301.             eigenOutput
  302.         );

  303.         if (null == eigenComponentOrderList)
  304.         {
  305.             return null;
  306.         }

  307.         double[] eigenValueArray = eigenOutput.eigenValueArray();

  308.         double[][] eigenVectorArray = eigenOutput.eigenVectorArray();

  309.         int eigenComponentCount = eigenComponentOrderList.size();

  310.         org.drip.numerical.eigen.EigenComponent[] eigenComponentArray =
  311.             new org.drip.numerical.eigen.EigenComponent[eigenComponentCount];

  312.         for (int eigenComponentIndex = 0;
  313.             eigenComponentIndex < eigenComponentCount;
  314.             ++eigenComponentIndex)
  315.         {
  316.             int eigenComponentOrder = eigenComponentOrderList.get (
  317.                 eigenComponentIndex
  318.             );

  319.             try
  320.             {
  321.                 eigenComponentArray[eigenComponentIndex] = new org.drip.numerical.eigen.EigenComponent (
  322.                     eigenVectorArray[eigenComponentOrder],
  323.                     eigenValueArray[eigenComponentOrder]
  324.                 );
  325.             }
  326.             catch (java.lang.Exception e)
  327.             {
  328.                 e.printStackTrace();

  329.                 return null;
  330.             }
  331.         }

  332.         return eigenComponentArray;
  333.     }

  334.     @Override public org.drip.numerical.eigen.EigenComponent principalComponent (
  335.         final double[][] a)
  336.     {
  337.         org.drip.numerical.eigen.EigenComponent[] eigenComponentArray = orderedEigenComponentArray (
  338.             a
  339.         );

  340.         return null == eigenComponentArray ? null : eigenComponentArray[0];
  341.     }

  342.     /**
  343.      * Generate the UD Form of the Input Matrix
  344.      *
  345.      * @param a The Input Matrix
  346.      *
  347.      * @return The UD Form
  348.      */

  349.     public org.drip.numerical.linearalgebra.UD udForm (
  350.         final double[][] a)
  351.     {
  352.         org.drip.numerical.eigen.EigenComponent[] eigenComponentArray = orderedEigenComponentArray (
  353.             a
  354.         );

  355.         if (null == eigenComponentArray)
  356.         {
  357.             return null;
  358.         }

  359.         int eigenComponentCount = eigenComponentArray.length;
  360.         double[][] d = new double[eigenComponentCount][eigenComponentCount];
  361.         double[][] u = new double[eigenComponentCount][];

  362.         for (int eigenComponentIndexI = 0;
  363.             eigenComponentIndexI < eigenComponentCount;
  364.             ++eigenComponentIndexI)
  365.         {
  366.             u[eigenComponentIndexI] = eigenComponentArray[eigenComponentIndexI].eigenVector();

  367.             for (int eigenComponentIndexJ = 0;
  368.                 eigenComponentIndexJ < eigenComponentCount;
  369.                 ++eigenComponentIndexJ)
  370.             {
  371.                 d[eigenComponentIndexI][eigenComponentIndexJ] = eigenComponentIndexI != eigenComponentIndexJ
  372.                     ? 0. : eigenComponentArray[eigenComponentIndexI].eigenValue();
  373.             }
  374.         }

  375.         try
  376.         {
  377.             return new org.drip.numerical.linearalgebra.UD (
  378.                 u,
  379.                 d
  380.             );
  381.         }
  382.         catch (java.lang.Exception e)
  383.         {
  384.             e.printStackTrace();
  385.         }

  386.         return null;
  387.     }
  388. }