RayleighQuotient.java

  1. package org.drip.sample.matrix;

  2. import org.drip.numerical.common.FormatUtil;
  3. import org.drip.numerical.common.NumberUtil;
  4. import org.drip.numerical.linearalgebra.Matrix;
  5. import org.drip.service.env.EnvManager;

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

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

  68. /**
  69.  * <i>RayleighQuotient</i> demonstrates the Computation of an Approximate to the Eigenvalue using the
  70.  * Rayleigh Quotient. The References are:
  71.  *  
  72.  * <br><br>
  73.  *  <ul>
  74.  *      <li>
  75.  *          Wikipedia - Power Iteration (2018): https://en.wikipedia.org/wiki/Power_iteration
  76.  *      </li>
  77.  *      <li>
  78.  *          Wikipedia - Rayleigh Quotient Iteration (2018):
  79.  *              https://en.wikipedia.org/wiki/Rayleigh_quotient_iteration
  80.  *      </li>
  81.  *  </ul>
  82.  *  
  83.  * <br><br>
  84.  *  <ul>
  85.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/NumericalCore.md">Numerical Core Module</a></li>
  86.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/NumericalSupportLibrary.md">Numerical Support Library</a></li>
  87.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/README.md">Sample</a></li>
  88.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/matrix/README.md">Linear Algebra and Matrix Utilities</a></li>
  89.  *  </ul>
  90.  * <br><br>
  91.  *
  92.  * @author Lakshmi Krishnamurthy
  93.  */

  94. public class RayleighQuotient
  95. {

  96.     private static final void EigenDump (
  97.         final int iteration,
  98.         final double[] eigenvector,
  99.         final double eigenvalue)
  100.         throws Exception
  101.     {
  102.         java.lang.String strDump = "\t|| Iteration => " + FormatUtil.FormatDouble (iteration, 2, 0, 1.) +
  103.             "[" + FormatUtil.FormatDouble (eigenvalue, 3, 4, 1.) + "] => ";

  104.         for (int i = 0; i < eigenvector.length; ++i)
  105.             strDump += FormatUtil.FormatDouble (eigenvector[i], 1, 4, 1.) + " | ";

  106.         System.out.println ("\t" + strDump);
  107.     }

  108.     public static final void main (
  109.         final String[] argumentArray)
  110.         throws Exception
  111.     {
  112.         EnvManager.InitEnv ("");

  113.         int iterationCount = 5;
  114.         double eigenvalue = 200.;
  115.         double[][] a = {
  116.             {1., 2., 3.},
  117.             {1., 2., 1.},
  118.             {3., 2., 1.},
  119.         };
  120.         double[] eigenvector = {
  121.             1. / Math.sqrt (3.),
  122.             1. / Math.sqrt (3.),
  123.             1. / Math.sqrt (3.)
  124.         };

  125.         NumberUtil.PrintMatrix (
  126.             "\t|| A ",
  127.             a
  128.         );

  129.         EigenDump (
  130.             0,
  131.             eigenvector,
  132.             eigenvalue
  133.         );

  134.         int iterationIndex = 0;

  135.         while (++iterationIndex < iterationCount)
  136.         {
  137.             double[][] deDiagonalized = new double[a.length][a.length];

  138.             for (int row = 0; row < a.length; ++row)
  139.             {
  140.                 for (int column = 0; column < a.length; ++column)
  141.                 {
  142.                     deDiagonalized[row][column] = a[row][column];

  143.                     if (row == column)
  144.                     {
  145.                         deDiagonalized[row][column] -= eigenvalue;
  146.                     }
  147.                 }
  148.             }

  149.             eigenvector = Matrix.Normalize (
  150.                 Matrix.Product (
  151.                     Matrix.InvertUsingGaussianElimination (deDiagonalized),
  152.                     eigenvector
  153.                 )
  154.             );

  155.             eigenvalue = Matrix.DotProduct (
  156.                 eigenvector,
  157.                 Matrix.Product (
  158.                     a,
  159.                     eigenvector
  160.                 )
  161.             );

  162.             EigenDump (
  163.                 iterationIndex,
  164.                 eigenvector,
  165.                 eigenvalue
  166.             );
  167.         }

  168.         EnvManager.TerminateEnv();
  169.     }
  170. }