Array2D.java

  1. package org.drip.numerical.common;

  2. /*
  3.  * -*- mode: java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  4.  */

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

  82. /**
  83.  * <i>Array2D</i> the contains array of x and y. It provides methods to create/access different varieties of
  84.  * x/y schedule creation.
  85.  *
  86.  * <br><br>
  87.  *  <ul>
  88.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ComputationalCore.md">Computational Core Module</a></li>
  89.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/NumericalAnalysisLibrary.md">Numerical Analysis Library</a></li>
  90.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/numerical">Numerical Quadrature, Differentiation, Eigenization, Linear Algebra, and Utilities</a></li>
  91.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/numerical/common">Primitives/Array Manipulate Format Display Utilities</a></li>
  92.  *  </ul>
  93.  * <br><br>
  94.  *
  95.  * @author Lakshmi Krishnamurthy
  96.  */

  97. public class Array2D {
  98.     private double _adblX[] = null;
  99.     private double _adblY[] = null;

  100.     /**
  101.      * Generate the Array2D Schedule from the String Representation of the Vertex Dates and Edge Payments
  102.      *  Combination.
  103.      *
  104.      * @param strDateAmountVertex The String Array consisting of the Date and the Amount Vertexes
  105.      * @param iMaturityDate The Maturity Date
  106.      * @param dblInitialAmount The Initial Amount
  107.      *
  108.      * @return The Array2D Instance
  109.      */

  110.     public static final Array2D FromDateAmountVertex (
  111.         final java.lang.String strDateAmountVertex,
  112.         final int iMaturityDate,
  113.         final double dblInitialAmount)
  114.     {
  115.         if (null == strDateAmountVertex || strDateAmountVertex.isEmpty() ||
  116.             !org.drip.numerical.common.NumberUtil.IsValid (dblInitialAmount) || 0. >= dblInitialAmount)
  117.             return null;

  118.         java.lang.String[] astrDateAmountEdge = org.drip.numerical.common.StringUtil.Split (strDateAmountVertex,
  119.             ";");

  120.         if (null == astrDateAmountEdge) return null;

  121.         int iNumDateFactorPair = astrDateAmountEdge.length;
  122.         int iNumVertex = iNumDateFactorPair / 2 + 1;
  123.         double[] adblVertexDate = new double[iNumVertex];
  124.         double[] adblVertexFactor = new double[iNumVertex];
  125.         double[] adblVertexOutstanding = new double[iNumVertex];
  126.         adblVertexOutstanding[0] = dblInitialAmount;

  127.         if (0 == iNumDateFactorPair || 1 == iNumDateFactorPair % 2) return null;

  128.         for (int i = 0; i < iNumVertex - 1; ++i) {
  129.             org.drip.analytics.date.JulianDate dt = org.drip.analytics.date.DateUtil.FromMDY
  130.                 (astrDateAmountEdge[2 * i], "/");

  131.             java.lang.String strEdgeAmount = astrDateAmountEdge[2 * i + 1];

  132.             if (null == dt || null == strEdgeAmount || strEdgeAmount.isEmpty()) return null;

  133.             adblVertexDate[i] = dt.julian();

  134.             double dblPrincipalPayAmount = java.lang.Double.parseDouble (strEdgeAmount.trim());

  135.             if (!org.drip.numerical.common.NumberUtil.IsValid (dblPrincipalPayAmount)) return null;

  136.             if (0. >= dblPrincipalPayAmount || 0. >= (adblVertexOutstanding[i + 1] = adblVertexOutstanding[i]
  137.                 - dblPrincipalPayAmount))
  138.                 return null;
  139.         }

  140.         for (int i = 0; i < iNumVertex; ++i)
  141.             adblVertexFactor[i] = adblVertexOutstanding[i] / dblInitialAmount;

  142.         adblVertexDate[iNumVertex - 1] = iMaturityDate;

  143.         return FromArray (adblVertexDate, adblVertexFactor);
  144.     }

  145.     /**
  146.      * Generate an Array2D Instance from the String Array containing semi-colon delimited Date/Factor Vertex
  147.      *  Pair
  148.      *
  149.      * @param strDateFactorVertex The String Array containing semi-colon delimited Date/Edge Pair
  150.      * @param iMaturityDate The Maturity Date
  151.      * @param dblInitialAmount The Initial Amount
  152.      *
  153.      * @return The Array2D Instance
  154.      */

  155.     public static final Array2D FromDateFactorVertex (
  156.         final java.lang.String strDateFactorVertex,
  157.         final int iMaturityDate,
  158.         final double dblInitialAmount)
  159.     {
  160.         if (null == strDateFactorVertex || strDateFactorVertex.isEmpty() ||
  161.             !org.drip.numerical.common.NumberUtil.IsValid (dblInitialAmount))
  162.             return null;

  163.         java.lang.String[] astrDateFactorVertex = org.drip.numerical.common.StringUtil.Split
  164.             (strDateFactorVertex, ";");

  165.         if (null == astrDateFactorVertex) return null;

  166.         int iNumDateFactorPair = astrDateFactorVertex.length;
  167.         int iNumVertex = iNumDateFactorPair / 2 + 1;
  168.         double[] adblVertexDate = new double[iNumVertex];
  169.         double[] adblVertexFactor = new double[iNumVertex];
  170.         adblVertexFactor[0] = 1.;

  171.         if (0 == iNumDateFactorPair || 1 == iNumDateFactorPair % 2) return null;

  172.         for (int i = 0; i < iNumVertex - 1; ++i) {
  173.             org.drip.analytics.date.JulianDate dt = org.drip.analytics.date.DateUtil.FromMDY
  174.                 (astrDateFactorVertex[2 * i], "/");

  175.             java.lang.String strFactor = astrDateFactorVertex[2 * i + 1];

  176.             if (null == dt || null == strFactor || strFactor.isEmpty()) return null;

  177.             adblVertexDate[i] = dt.julian();

  178.             if (!org.drip.numerical.common.NumberUtil.IsValid (adblVertexFactor[i + 1] =
  179.                 java.lang.Double.parseDouble (strFactor.trim()) / dblInitialAmount))
  180.                 return null;
  181.         }

  182.         adblVertexDate[iNumVertex - 1] = iMaturityDate;

  183.         return FromArray (adblVertexDate, adblVertexFactor);
  184.     }

  185.     /**
  186.      * Generate an Array2D Instance from the String Array containing semi-colon delimited Date/Factor Vertex
  187.      *  Pair
  188.      *
  189.      * @param strDateFactorVertex The String Array containing semi-colon delimited Date/Edge Pair
  190.      * @param iMaturityDate The Maturity Date
  191.      *
  192.      * @return The Array2D Instance
  193.      */

  194.     public static final Array2D FromDateFactorVertex (
  195.         final java.lang.String strDateFactorVertex,
  196.         final int iMaturityDate)
  197.     {
  198.         if (null == strDateFactorVertex || strDateFactorVertex.isEmpty()) return null;

  199.         java.lang.String[] astrDateFactorVertex = org.drip.numerical.common.StringUtil.Split
  200.             (strDateFactorVertex, ";");

  201.         if (null == astrDateFactorVertex) return null;

  202.         int iNumDateFactorPair = astrDateFactorVertex.length;
  203.         int iNumVertex = iNumDateFactorPair / 2 + 1;
  204.         double[] adblVertexDate = new double[iNumVertex];
  205.         double[] adblVertexFactor = new double[iNumVertex];
  206.         adblVertexFactor[0] = 1.;

  207.         if (0 == iNumDateFactorPair || 1 == iNumDateFactorPair % 2) return null;

  208.         for (int i = 0; i < iNumVertex - 1; ++i) {
  209.             org.drip.analytics.date.JulianDate dt = org.drip.analytics.date.DateUtil.FromMDY
  210.                 (astrDateFactorVertex[2 * i], "/");

  211.             java.lang.String strFactor = astrDateFactorVertex[2 * i + 1];

  212.             if (null == dt || null == strFactor || strFactor.isEmpty()) return null;

  213.             adblVertexDate[i] = dt.julian();

  214.             if (!org.drip.numerical.common.NumberUtil.IsValid (adblVertexFactor[i + 1] =
  215.                 java.lang.Double.parseDouble (strFactor.trim())))
  216.                 return null;
  217.         }

  218.         adblVertexDate[iNumVertex - 1] = iMaturityDate;

  219.         for (int i = 0; i < iNumVertex - 1; ++i) {
  220.             System.out.println (new org.drip.analytics.date.JulianDate ((int) adblVertexDate[i]) + " | " + adblVertexFactor[i]);
  221.         }

  222.         return FromArray (adblVertexDate, adblVertexFactor);
  223.     }

  224.     /**
  225.      * Create the Array2D Instance from a Matched String Array of X and Y
  226.      *
  227.      * @param strX String Array of X
  228.      * @param strY String Array of Y
  229.      *
  230.      * @return The Array2D Instance
  231.      */

  232.     public static final Array2D FromStringSet (
  233.         final java.lang.String strX,
  234.         final java.lang.String strY)
  235.     {
  236.         if (null == strX || strX.isEmpty() || null == strY || strY.isEmpty()) return null;

  237.         try {
  238.             return new Array2D (org.drip.numerical.common.StringUtil.MakeDoubleArrayFromStringTokenizer (new
  239.                 java.util.StringTokenizer (strX, ";")),
  240.                     org.drip.numerical.common.StringUtil.MakeDoubleArrayFromStringTokenizer (new
  241.                         java.util.StringTokenizer (strY, ";")));
  242.         } catch (java.lang.Exception e) {
  243.             e.printStackTrace();
  244.         }

  245.         return null;
  246.     }

  247.     /**
  248.      * Create the Array2D Instance from a Matched Array of X and Y
  249.      *
  250.      * @param adblX Array of X
  251.      * @param adblY Array of Y
  252.      *
  253.      * @return The Array2D Instance
  254.      */

  255.     public static final Array2D FromArray (
  256.         final double[] adblX,
  257.         final double[] adblY)
  258.     {
  259.         try {
  260.             return new Array2D (adblX, adblY);
  261.         } catch (java.lang.Exception e) {
  262.             e.printStackTrace();
  263.         }

  264.         return null;
  265.     }

  266.     /**
  267.      * Create the Array2D Instance from a Matched Array of X and Y
  268.      *
  269.      * @param aiX Array of X
  270.      * @param adblY Array of Y
  271.      *
  272.      * @return The Array2D Instance
  273.      */

  274.     public static final Array2D FromArray (
  275.         final int[] aiX,
  276.         final double[] adblY)
  277.     {
  278.         if (null == aiX) return null;

  279.         int iSize = aiX.length;
  280.         double[] adblX = new double[iSize];

  281.         if (0 == iSize) return null;

  282.         for (int i = 0; i < iSize; ++i)
  283.             adblX[i] = aiX[i];

  284.         return FromArray (adblX, adblY);
  285.     }

  286.     /**
  287.      * Create the Array2D Instance from a Matched Array of X and Y Deltas
  288.      *
  289.      * @param adblX Array of X
  290.      * @param adblYDelta Array of Y Deltas
  291.      * @param dblYInitial The Initial Value of Y
  292.      *
  293.      * @return The Array2D Instance
  294.      */

  295.     public static final Array2D FromXYDeltaArray (
  296.         final double[] adblX,
  297.         final double[] adblYDelta,
  298.         final double dblYInitial)
  299.     {
  300.         if (null == adblX || null == adblYDelta || !org.drip.numerical.common.NumberUtil.IsValid (dblYInitial))
  301.             return null;

  302.         int i = 0;
  303.         int iLength = adblX.length;
  304.         double[] adblY = new double[iLength];
  305.         adblY[0] = dblYInitial;

  306.         if (0 == iLength || iLength != adblYDelta.length) return null;

  307.         for (double dblYDelta : adblYDelta) {
  308.             if (i < iLength - 1) adblY[i + 1] = adblY[i] - dblYDelta;

  309.             ++i;
  310.         }

  311.         try {
  312.             return new Array2D (adblX, adblY);
  313.         } catch (java.lang.Exception e) {
  314.             e.printStackTrace();
  315.         }

  316.         return null;
  317.     }

  318.     /**
  319.      * Create an Array2D Instance from the Flat Unit Y
  320.      *
  321.      * @return The Array2D Instance
  322.      */

  323.     public static final Array2D BulletSchedule()
  324.     {
  325.         double[] adblFactor = new double[1];
  326.         double[] adblDate = new double[1];
  327.         adblFactor[0] = 1.;

  328.         adblDate[0] = org.drip.analytics.date.DateUtil.CreateFromYMD (1900, 1, 1).julian();

  329.         try {
  330.             return new Array2D (adblDate, adblFactor);
  331.         } catch (java.lang.Exception e) {
  332.             e.printStackTrace();
  333.         }

  334.         return null;
  335.     }

  336.     private Array2D (
  337.         final double[] adblX,
  338.         final double[] adblY)
  339.         throws java.lang.Exception
  340.     {
  341.         if (null == adblX || null == adblY) throw new java.lang.Exception ("Array2D ctr => Invalid params");

  342.         int iLength = adblX.length;
  343.         _adblX = new double[iLength];
  344.         _adblY = new double[iLength];

  345.         if (0 == iLength || iLength != adblY.length)
  346.             throw new java.lang.Exception ("Array2D ctr => Invalid params");

  347.         for (int i = 0; i < iLength; ++i) {
  348.             // System.out.println (new org.drip.analytics.date.JulianDate ((int) _adblX[i]) + " | " + _adblY[i]);

  349.             if (!org.drip.numerical.common.NumberUtil.IsValid (_adblX[i] = adblX[i]) ||
  350.                 !org.drip.numerical.common.NumberUtil.IsValid (_adblY[i] = adblY[i]))
  351.                 throw new java.lang.Exception ("Array2D ctr => Invalid params");
  352.         }
  353.     }

  354.     /**
  355.      * Retrieve the Y given X
  356.      *
  357.      * @param dblX X
  358.      *
  359.      * @return Y
  360.      *
  361.      * @throws java.lang.Exception Thrown if the Y cannot be computed
  362.      */

  363.     public double y (
  364.         final double dblX)
  365.         throws java.lang.Exception
  366.     {
  367.         if (!org.drip.numerical.common.NumberUtil.IsValid (dblX))
  368.             throw new java.lang.Exception ("Array2D::y => Invalid Input");

  369.         if (dblX <= _adblX[0]) return _adblY[0];

  370.         int iLength = _adblX.length;

  371.         for (int i = 1; i < iLength; ++i) {
  372.             if (dblX > _adblX[i - 1] && dblX <= _adblX[i]) return _adblY[i];
  373.         }

  374.         return _adblY[iLength - 1];
  375.     }

  376.     /**
  377.      * Retrieve the Index that corresponds to the given X
  378.      *
  379.      * @param dblX X
  380.      *
  381.      * @return Index
  382.      *
  383.      * @throws java.lang.Exception Thrown if the Index cannot be computed
  384.      */

  385.     public int index (
  386.         final double dblX)
  387.         throws java.lang.Exception
  388.     {
  389.         if (!org.drip.numerical.common.NumberUtil.IsValid (dblX))
  390.             throw new java.lang.Exception ("Array2D::index => Invalid Input/State");

  391.         if (dblX <= _adblX[0]) return 0;

  392.         int iLength = _adblX.length;

  393.         for (int i = 1; i < iLength; ++i) {
  394.             if (dblX <= _adblX[i]) return i;
  395.         }

  396.         return iLength - 1;
  397.     }

  398.     /**
  399.      * Retrieve the X-Weighted Y
  400.      *
  401.      * @param dblStartX Start X
  402.      * @param dblEndX End X
  403.      *
  404.      * @return The X-Weighted Y
  405.      *
  406.      * @throws java.lang.Exception Thrown if the Y cannot be computed
  407.      */

  408.     public double y (
  409.         final double dblStartX,
  410.         final double dblEndX)
  411.         throws java.lang.Exception
  412.     {
  413.         if (!org.drip.numerical.common.NumberUtil.IsValid (dblStartX) ||
  414.             !org.drip.numerical.common.NumberUtil.IsValid (dblEndX))
  415.             throw new java.lang.Exception ("Array2D::y => Invalid Inputs");

  416.         int iEndIndex = index (dblEndX);

  417.         int iStartIndex = index (dblStartX);

  418.         if (iStartIndex == iEndIndex) return _adblY[iStartIndex];

  419.         double dblWeightedY = _adblY[iStartIndex] * (_adblX[iStartIndex] - dblStartX);

  420.         for (int i = iStartIndex + 1; i <= iEndIndex; ++i)
  421.             dblWeightedY += _adblY[i] * (_adblX[i] - _adblX[i - 1]);

  422.         return (dblWeightedY + _adblY[iEndIndex] * (dblEndX - _adblX[iEndIndex])) / (dblEndX - dblStartX);
  423.     }

  424.     /**
  425.      * Retrieve the Array of X
  426.      *
  427.      * @return Array of X
  428.      */

  429.     public double[] x()
  430.     {
  431.         return _adblX;
  432.     }

  433.     /**
  434.      * Retrieve the Array of Y
  435.      *
  436.      * @return Array of Y
  437.      */

  438.     public double[] y()
  439.     {
  440.         return _adblY;
  441.     }

  442.     /**
  443.      * Indicate if this Array2D Instance matches the "other" Entry-by-Entry
  444.      *
  445.      * @param a2DOther The "Other" Array2D Instance
  446.      *
  447.      * @return TRUE - The Array2D Instances match Entry-by-Entry
  448.      */

  449.     public boolean match (
  450.         final Array2D a2DOther)
  451.     {
  452.         if (null == a2DOther) return false;

  453.         double[] adblOtherX = a2DOther._adblX;
  454.         double[] adblOtherY = a2DOther._adblY;
  455.         int iNumOtherX = adblOtherX.length;
  456.         int iNumOtherY = adblOtherY.length;

  457.         if (iNumOtherX != _adblX.length || iNumOtherY != _adblY.length) return false;

  458.         for (int i = 0; i < iNumOtherX; ++i) {
  459.             if (adblOtherX[i] != _adblX[i]) return false;
  460.         }

  461.         for (int i = 0; i < iNumOtherY; ++i) {
  462.             if (adblOtherY[i] != _adblY[i]) return false;
  463.         }

  464.         return true;
  465.     }
  466. }