LinearAlgebra.java

  1. package org.drip.sample.matrix;

  2. import org.drip.numerical.common.*;
  3. import org.drip.numerical.linearalgebra.*;
  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>LinearAlgebra</i> implements Samples for Linear Algebra and Matrix Manipulations. It demonstrates the
  74.  * following:
  75.  *  
  76.  * <br><br>
  77.  *  <ul>
  78.  *      <li>
  79.  *          Compute the inverse of a matrix, and multiply with the original to recover the unit matrix
  80.  *      </li>
  81.  *      <li>
  82.  *          Solves system of linear equations using one the exposed techniques
  83.  *      </li>
  84.  *  </ul>
  85.  *  
  86.  * <br><br>
  87.  *  <ul>
  88.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/NumericalCore.md">Numerical Core Module</a></li>
  89.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/NumericalSupportLibrary.md">Numerical Support 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">Sample</a></li>
  91.  *      <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>
  92.  *  </ul>
  93.  * <br><br>
  94.  *
  95.  * @author Lakshmi Krishnamurthy
  96.  */

  97. public class LinearAlgebra {

  98.     /*
  99.      * Sample illustrating the Invocation of Base Matrix Inversion and Product Computation Verification.
  100.      *
  101.      *  WARNING: Insufficient Error Checking, so use caution
  102.      */

  103.     private static final void InverseVerifyDump (
  104.         final String strLabel,
  105.         final double[][] aadblA)
  106.     {
  107.         double[][] aadblAInv = Matrix.InvertUsingGaussianElimination (aadblA);

  108.         System.out.println ("--- TESTS FOR " + strLabel + "---");

  109.         System.out.println ("---------------------------------");

  110.         NumberUtil.Print2DArrayTriplet (
  111.             "\tSOURCE" + strLabel,
  112.             "INVERSE" + strLabel,
  113.             "PRODUCT" + strLabel,
  114.             aadblA,
  115.             aadblAInv,
  116.             Matrix.Product (
  117.                 aadblA,
  118.                 aadblAInv
  119.             ),
  120.             false
  121.         );

  122.         System.out.println ("---------------------------------\n\n");
  123.     }

  124.     /*
  125.      * Sample illustrating the Invocation of Base Matrix Manipulation Functionality
  126.      *
  127.      *  WARNING: Insufficient Error Checking, so use caution
  128.      */

  129.     public static final void MatrixManipulation()
  130.     {
  131.         InverseVerifyDump ("#A", new double[][] {
  132.             {1, 2, 3},
  133.             {4, 5, 6},
  134.             {7, 8, 9.01}
  135.         });

  136.         InverseVerifyDump ("#B", new double[][] {
  137.             { 0.1667,  0.0000,  0.0000,  0.0000},
  138.             { 0.0000,  0.0000,  0.0000,  0.1667},
  139.             {-0.6667,  0.5000,  0.0000,  0.0000},
  140.             { 2.6667, -3.0000,  1.0000,  0.0000}
  141.         });

  142.         InverseVerifyDump ("#C", new double[][] {
  143.             { 1.0000,  0.0000,  0.0000,  0.0000},
  144.             { 1.0000,  1.0000,  1.0000,  1.0000},
  145.             { 0.0000,  1.0000,  0.0000,  0.0000},
  146.             { 0.0000,  0.0000,  2.0000,  0.0000}
  147.         });

  148.         InverseVerifyDump ("#D", new double[][] {
  149.             { 0.0000,  1.0000},
  150.             { 1.0000,  2.0000}
  151.         });

  152.         InverseVerifyDump ("#E", new double[][] {
  153.             { 0.0000,  1.0000},
  154.             { 1.0000,  0.0000}
  155.         });

  156.         InverseVerifyDump ("#F", new double[][] {
  157.             { 1.0000,  0.0000,  0.0000,  0.0000},
  158.             { 1.0000,  1.0000,  1.0000,  1.0000},
  159.             {-1.0000,  1.0000,  0.0000,  0.0000},
  160.             { 1.0000,  2.0000,  3.0000,  4.0000}
  161.         });

  162.         InverseVerifyDump ("#G", new double[][] {
  163.             { 0.0000,  1.0000,  0.0000,  0.0000},
  164.             { 0.0000,  0.0000,  2.0000,  0.0000},
  165.             { 0.0434,  0.0188, 16.0083, 24.0037},
  166.             { 0.0188,  0.0083, 24.0037, 48.0017}
  167.         });
  168.     }

  169.     /*
  170.      * Sample illustrating the Invocation of Linear System Solver Functionality
  171.      *
  172.      *  WARNING: Insufficient Error Checking, so use caution
  173.      */

  174.     public static final void LinearSystemSolver()
  175.     {
  176.         double[][] aadblA = new double[][] {
  177.             {1.000, 0.500, 0.333,  0.000,  0.000, 0.000},
  178.             {0.000, 0.000, 0.000,  1.000,  0.500, 0.333},
  179.             {1.000, 1.000, 1.000, -1.000,  0.000, 0.000},
  180.             {0.000, 0.500, 2.000,  0.000, -0.500, 0.000},
  181.             {0.000, 1.000, 0.000,  0.000,  0.000, 0.000},
  182.             {0.000, 0.000, 0.000,  0.000,  1.000, 0.000},
  183.         };
  184.         double[] adblB = new double[] {0.02, 0.026, 0., 0., 0., 0.};

  185.         org.drip.numerical.common.NumberUtil.Print2DArray (
  186.             "\tCOEFF",
  187.             aadblA,
  188.             false
  189.         );

  190.         /*
  191.          * Solve the Linear System using Gaussian Elimination
  192.          */

  193.         LinearizationOutput lssGaussianElimination = LinearSystemSolver.SolveUsingGaussianElimination (
  194.             aadblA,
  195.             adblB
  196.         );

  197.         for (int i = 0; i < lssGaussianElimination.getTransformedRHS().length; ++i)
  198.             System.out.println ("GaussianElimination[" + i + "] = " + FormatUtil.FormatDouble
  199.                 (lssGaussianElimination.getTransformedRHS()[i], 0, 6, 1.));

  200.         for (int i = 0; i < 6; ++i) {
  201.             double dblRHS = 0.;

  202.             for (int j = 0; j < 6; ++j)
  203.                 dblRHS += aadblA[i][j] * lssGaussianElimination.getTransformedRHS()[j];

  204.             System.out.println ("RHS[" + i + "]: " + dblRHS);
  205.         }

  206.         /*
  207.          * Solve the Linear System using the Gauss-Seidel method
  208.          */

  209.         /* LinearSystemSolution lssGaussSeidel = LinearSystemSolver.SolveUsingGaussSeidel (aadblA, adblB);

  210.         for (int i = 0; i < lssGaussSeidel.getSolution().length; ++i)
  211.             System.out.println ("GaussSeidel[" + i + "] = " + FormatUtil.FormatDouble (lssGaussSeidel.getSolution()[i], 0, 2, 1.)); */
  212.     }

  213.     public static final void main (
  214.         final String[] astrArgs)
  215.     {
  216.         EnvManager.InitEnv ("");

  217.         MatrixManipulation();

  218.         LinearSystemSolver();

  219.         EnvManager.TerminateEnv();
  220.     }
  221. }