Eigenization.java

  1. package org.drip.sample.matrix;

  2. import org.drip.numerical.common.*;
  3. import org.drip.numerical.eigen.*;
  4. import org.drip.service.env.EnvManager;

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

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

  72. /**
  73.  * <i>Eigenization</i> demonstrates how to generate the eigenvalue and eigenvector for the Input Matrix.
  74.  *  
  75.  * <br><br>
  76.  *  <ul>
  77.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/NumericalCore.md">Numerical Core Module</a></li>
  78.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/NumericalSupportLibrary.md">Numerical Support Library</a></li>
  79.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/README.md">Sample</a></li>
  80.  *      <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>
  81.  *  </ul>
  82.  * <br><br>
  83.  *
  84.  * @author Lakshmi Krishnamurthy
  85.  */

  86. public class Eigenization {

  87.     private static final void EigenRun (
  88.         final QREigenComponentExtractor qrece)
  89.     {
  90.         double dblCorr1 = 0.5 * Math.random();

  91.         double dblCorr2 = 0.5 * Math.random();

  92.         double[][] aadblA = {
  93.             {     1.0, dblCorr1,      0.0},
  94.             {dblCorr1,      1.0, dblCorr2},
  95.             {     0.0, dblCorr2,      1.0}
  96.         };

  97.         EigenOutput eo = qrece.eigenize (aadblA);

  98.         if (null == eo) return;

  99.         System.out.println ("\n\t|----------------------------------------|");

  100.         System.out.println (
  101.             "\t|-----------" +
  102.             FormatUtil.FormatDouble (dblCorr1, 1, 4, 1.) + " ||| " +
  103.             FormatUtil.FormatDouble (dblCorr2, 1, 4, 1.) + " ---------|"
  104.         );

  105.         System.out.println ("\t|----------------------------------------|");

  106.         for (int i = 0; i < aadblA.length; ++i) {
  107.             java.lang.String strDump = "\t[" + FormatUtil.FormatDouble (eo.eigenValueArray()[i], 1, 4, 1.) + "] => ";

  108.             for (int j = 0; j < aadblA.length; ++j)
  109.                 strDump += FormatUtil.FormatDouble (eo.eigenVectorArray()[i][j], 1, 4, 1.) + " | ";

  110.             System.out.println (strDump);
  111.         }

  112.         EigenComponent ec = qrece.principalComponent (aadblA);

  113.         double[] adblEigenvector = ec.eigenVector();

  114.         java.lang.String strDump = "[" + FormatUtil.FormatDouble (ec.eigenValue(), 1, 4, 1.) + "] => ";

  115.         for (int i = 0; i < adblEigenvector.length; ++i)
  116.             strDump += FormatUtil.FormatDouble (adblEigenvector[i], 1, 4, 1.) + " | ";

  117.         System.out.println ("\t" + strDump);

  118.         System.out.println ("\t|----------------------------------------|");
  119.     }

  120.     public static final void main (
  121.         final String[] astrArgs)
  122.         throws Exception
  123.     {
  124.         EnvManager.InitEnv ("");

  125.         QREigenComponentExtractor qrece = new QREigenComponentExtractor (
  126.             50,
  127.             0.00001
  128.         );

  129.         int iNumRun = 10;

  130.         for (int iRun = 0; iRun < iNumRun; ++iRun)
  131.             EigenRun (qrece);

  132.         EnvManager.TerminateEnv();
  133.     }
  134. }