SylvesterInterpolantReconciler.java

  1. package org.drip.sample.matrix;

  2. import java.util.Map;

  3. import org.drip.function.definition.R1ToR1;
  4. import org.drip.function.matrix.FrobeniusCovariance;
  5. import org.drip.function.matrix.Square;
  6. import org.drip.numerical.common.NumberUtil;
  7. import org.drip.numerical.eigen.EigenOutput;
  8. import org.drip.numerical.eigen.QREigenComponentExtractor;
  9. import org.drip.numerical.linearalgebra.Matrix;
  10. import org.drip.service.env.EnvManager;

  11. /*
  12.  * -*- mode: java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  13.  */

  14. /*!
  15.  * Copyright (C) 2019 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>SylvesterInterpolantReconciler</i> demonstrates the Construction and Usage of the Sylvester Matrix
  74.  *  Interpolant. The References are:
  75.  *  
  76.  * <br><br>
  77.  *  <ul>
  78.  *      <li>
  79.  *          Claerbout, J. F. (1985): <i>Fundamentals of Geo-physical Data Processing</i> <b>Blackwell
  80.  *              Scientific</b>
  81.  *      </li>
  82.  *      <li>
  83.  *          Horn, R. A., and C. R. Johnson (1991): <i>Topics in Matrix Analysis</i> <b>Cambridge University
  84.  *              Press</b>
  85.  *      </li>
  86.  *      <li>
  87.  *          Schwerdtfeger, A. (1938): <i>Les Fonctions de Matrices: Les Fonctions Univalentes I</i>
  88.  *              <b>Hermann</b> Paris, France
  89.  *      </li>
  90.  *      <li>
  91.  *          Sylvester, J. J. (1883): On the Equation to the Secular Inequalities in the Planetary Theory
  92.  *              <i>The London, Edinburgh, and Dublin Philosophical Magazine and Journal of Science</i> <b>16
  93.  *              (100)</b> 267-269
  94.  *      </li>
  95.  *      <li>
  96.  *          Wikipedia (2019): Sylvester Formula https://en.wikipedia.org/wiki/Sylvester%27s_formula
  97.  *      </li>
  98.  *  </ul>
  99.  *  
  100.  * <br><br>
  101.  *  <ul>
  102.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/NumericalCore.md">Numerical Core Module</a></li>
  103.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/NumericalSupportLibrary.md">Numerical Support Library</a></li>
  104.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/README.md">Sample</a></li>
  105.  *      <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>
  106.  *  </ul>
  107.  * <br><br>
  108.  *
  109.  * @author Lakshmi Krishnamurthy
  110.  */

  111. public class SylvesterInterpolantReconciler
  112. {

  113.     public static final void main (
  114.         final String[] argumentArray)
  115.         throws Exception
  116.     {
  117.         EnvManager.InitEnv (
  118.             ""
  119.         );

  120.         double[][] a =
  121.         {
  122.             {1, 3},
  123.             {4, 2},
  124.         };

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

  129.         EigenOutput eigenOutput = qrece.eigenize (
  130.             a
  131.         );

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

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

  134.         NumberUtil.PrintMatrix (
  135.             "\t| ORIGINAL MATRIX",
  136.             a
  137.         );

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

  139.         System.out.println();

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

  141.         System.out.println ("\t|              EIGEN VALUES               |");

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

  143.         System.out.println ("\t|  " + eigenValueArray[0] + "  |  " + eigenValueArray[1]);

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

  145.         double[][] frobeniusCovariant0 = new double[2][2];
  146.         double[][] frobeniusCovariant1 = new double[2][2];
  147.         frobeniusCovariant0[0][0] = (a[0][0] - eigenValueArray[1]) / (eigenValueArray[0] - eigenValueArray[1]);
  148.         frobeniusCovariant0[1][1] = (a[1][1] - eigenValueArray[1]) / (eigenValueArray[0] - eigenValueArray[1]);
  149.         frobeniusCovariant0[0][1] = a[0][1] / (eigenValueArray[0] - eigenValueArray[1]);
  150.         frobeniusCovariant0[1][0] = a[1][0] / (eigenValueArray[0] - eigenValueArray[1]);
  151.         frobeniusCovariant1[0][0] = (a[0][0] - eigenValueArray[0]) / (eigenValueArray[1] - eigenValueArray[0]);
  152.         frobeniusCovariant1[1][1] = (a[1][1] - eigenValueArray[0]) / (eigenValueArray[1] - eigenValueArray[0]);
  153.         frobeniusCovariant1[0][1] = a[0][1] / (eigenValueArray[1] - eigenValueArray[0]);
  154.         frobeniusCovariant1[1][0] = a[1][0] / (eigenValueArray[1] - eigenValueArray[0]);

  155.         System.out.println();

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

  157.         System.out.println ("\t|          SYLVESTER RECONCILER           |");

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

  159.         NumberUtil.PrintMatrix (
  160.             "\t| FROBENIUS COVARIANT 0",
  161.             frobeniusCovariant0
  162.         );

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

  164.         NumberUtil.PrintMatrix (
  165.             "\t| FROBENIUS COVARIANT 1",
  166.             frobeniusCovariant1
  167.         );

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

  169.         double[][] recoveredA = Matrix.Scale2D (
  170.             frobeniusCovariant0,
  171.             eigenValueArray[0]
  172.         );

  173.         double[][] recoveredA1 = Matrix.Scale2D (
  174.             frobeniusCovariant1,
  175.             eigenValueArray[1]
  176.         );

  177.         recoveredA[0][0] += recoveredA1[0][0];
  178.         recoveredA[0][1] += recoveredA1[0][1];
  179.         recoveredA[1][0] += recoveredA1[1][0];
  180.         recoveredA[1][1] += recoveredA1[1][1];

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

  182.         NumberUtil.PrintMatrix (
  183.             "\t| RECOVERED MATRIX",
  184.             recoveredA
  185.         );

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

  187.         double[][] inverseA = Matrix.Scale2D (
  188.             frobeniusCovariant0,
  189.             1. / eigenValueArray[0]
  190.         );

  191.         double[][] inverseA1 = Matrix.Scale2D (
  192.             frobeniusCovariant1,
  193.             1. / eigenValueArray[1]
  194.         );

  195.         inverseA[0][0] += inverseA1[0][0];
  196.         inverseA[0][1] += inverseA1[0][1];
  197.         inverseA[1][0] += inverseA1[1][0];
  198.         inverseA[1][1] += inverseA1[1][1];

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

  200.         NumberUtil.PrintMatrix (
  201.             "\t| INVERSE MATRIX",
  202.             inverseA
  203.         );

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

  205.         NumberUtil.PrintMatrix (
  206.             "\t| INVERSE MATRIX",
  207.             Matrix.Invert (
  208.                 a,
  209.                 ""
  210.             )
  211.         );

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

  213.         Square aSquare = new Square (
  214.             a
  215.         );

  216.         FrobeniusCovariance frobeniusCovariance = aSquare.frobeniusCovariance();

  217.         Map<Double, Square> componentMap = frobeniusCovariance.componentMap();

  218.         Object[] eigenValueKey = componentMap.keySet().toArray();

  219.         frobeniusCovariant0 = componentMap.get (
  220.             eigenValueKey[0]
  221.         ).grid();

  222.         frobeniusCovariant1 = frobeniusCovariance.componentMap().get (
  223.             eigenValueKey[1]
  224.         ).grid();

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

  226.         System.out.println ("\t|          SYLVESTER RECONCILER           |");

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

  228.         NumberUtil.PrintMatrix (
  229.             "\t| FROBENIUS COVARIANT 0",
  230.             frobeniusCovariant0
  231.         );

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

  233.         NumberUtil.PrintMatrix (
  234.             "\t| FROBENIUS COVARIANT 1",
  235.             frobeniusCovariant1
  236.         );

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

  238.         recoveredA = aSquare.evaluate (
  239.             new R1ToR1 (
  240.                 null
  241.             )
  242.             {
  243.                 @Override public double evaluate (
  244.                     final double x)
  245.                     throws Exception
  246.                 {
  247.                     return x;
  248.                 }
  249.             }
  250.         );

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

  252.         NumberUtil.PrintMatrix (
  253.             "\t| RECOVERED MATRIX",
  254.             recoveredA
  255.         );

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

  257.         inverseA = aSquare.evaluate (
  258.             new R1ToR1 (
  259.                 null
  260.             )
  261.             {
  262.                 @Override public double evaluate (
  263.                     final double x)
  264.                     throws Exception
  265.                 {
  266.                     return 1. / x;
  267.                 }
  268.             }
  269.         );

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

  271.         NumberUtil.PrintMatrix (
  272.             "\t| INVERSE MATRIX",
  273.             inverseA
  274.         );

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

  276.         EnvManager.TerminateEnv();
  277.     }
  278. }