PrincipalComponent.java

  1. package org.drip.sample.matrix;

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

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

  85. public class PrincipalComponent {

  86.     private static final void PrincipalComponentRun (
  87.         final PowerIterationComponentExtractor pice)
  88.         throws Exception
  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.         EigenComponent ec = pice.principalComponent (aadblA);

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

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

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

  102.         System.out.println (
  103.             "\t{" +
  104.             FormatUtil.FormatDouble (dblCorr1, 1, 4, 1.) + " ||" +
  105.             FormatUtil.FormatDouble (dblCorr2, 1, 4, 1.) + "} => " + strDump
  106.         );
  107.     }

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

  113.         PowerIterationComponentExtractor pice = new PowerIterationComponentExtractor (
  114.             50,
  115.             0.000001,
  116.             false
  117.         );

  118.         for (int i = 0; i < 50; ++i)
  119.             PrincipalComponentRun (pice);

  120.         EnvManager.TerminateEnv();
  121.     }
  122. }