WireSurfacePiecewiseConstant.java

  1. package org.drip.spline.multidimensional;

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

  78. /**
  79.  * <i>WireSurfacePiecewiseConstant</i> implements the piecewise Constant version of the 2D Spline Response
  80.  * Surface. It synthesizes this from an array of 1D Span Instances, each of which is referred to as wire
  81.  * spline in this case.
  82.  *
  83.  * <br><br>
  84.  *  <ul>
  85.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ComputationalCore.md">Computational Core Module</a></li>
  86.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/SplineBuilderLibrary.md">Spline Builder Library</a></li>
  87.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/spline/README.md">Basis Splines and Linear Compounders across a Broad Family of Spline Basis Functions</a></li>
  88.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/spline/multidimensional/README.md">Multi-dimensional Wire Surface Stretch</a></li>
  89.  *  </ul>
  90.  * <br><br>
  91.  *
  92.  * @author Lakshmi Krishnamurthy
  93.  */

  94. public class WireSurfacePiecewiseConstant {
  95.     private double[] _adblX = null;
  96.     private double[] _adblY = null;
  97.     private double[][] _aadblResponse = null;

  98.     /**
  99.      * WireSurfacePiecewiseConstant Constructor
  100.      *
  101.      * @param adblX Array of the X Ordinates
  102.      * @param adblY Array of the Y Ordinates
  103.      * @param aadblResponse Double Array of the Responses corresponding to {X, Y}
  104.      *
  105.      * @throws java.lang.Exception Thrown if the Inputs are invalid
  106.      */

  107.     public WireSurfacePiecewiseConstant (
  108.         final double[] adblX,
  109.         final double[] adblY,
  110.         final double[][] aadblResponse)
  111.         throws java.lang.Exception
  112.     {
  113.         if (null == (_adblX = adblX) || null == (_adblY = adblY))
  114.             throw new java.lang.Exception ("WireSurfacePiecewiseConstant ctr: Invalid Inputs");

  115.         int iXLength = _adblX.length;
  116.         int iYLength = _adblY.length;

  117.         if (0 == iXLength || 0 == iYLength || null == (_aadblResponse = aadblResponse) || iXLength !=
  118.             _aadblResponse.length || iYLength != _aadblResponse[0].length)
  119.             throw new java.lang.Exception ("WireSurfacePiecewiseConstant ctr: Invalid Inputs");
  120.     }

  121.     /**
  122.      * Enclosing X Index
  123.      *
  124.      * @param dblX The X Ordinate
  125.      *
  126.      * @return The Corresponding Index
  127.      *
  128.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  129.      */

  130.     public int enclosingXIndex (
  131.         final double dblX)
  132.         throws java.lang.Exception
  133.     {
  134.         if (!org.drip.numerical.common.NumberUtil.IsValid (dblX))
  135.             throw new java.lang.Exception
  136.                 ("WireSurfacePiecewiseConstant::enclosingXIndex => Invalid Inputs");

  137.         if (dblX < _adblX[0]) return java.lang.Integer.MIN_VALUE;

  138.         int iTerminalXIndex = _adblX.length - 1;

  139.         if (dblX > _adblX[iTerminalXIndex]) return java.lang.Integer.MAX_VALUE;

  140.         for (int i = 1; i <= iTerminalXIndex; ++i) {
  141.             if (dblX >= _adblX[i - 1] && dblX >= _adblX[i]) return i;
  142.         }

  143.         throw new java.lang.Exception ("WireSurfacePiecewiseConstant::enclosingXIndex => Invalid Inputs");
  144.     }

  145.     /**
  146.      * Enclosing Y Index
  147.      *
  148.      * @param dblY The Y Ordinate
  149.      *
  150.      * @return The Corresponding Index
  151.      *
  152.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  153.      */

  154.     public int enclosingYIndex (
  155.         final double dblY)
  156.         throws java.lang.Exception
  157.     {
  158.         if (!org.drip.numerical.common.NumberUtil.IsValid (dblY))
  159.             throw new java.lang.Exception
  160.                 ("WireSurfacePiecewiseConstant::enclosingYIndex => Invalid Inputs");

  161.         if (dblY < _adblY[0]) return java.lang.Integer.MIN_VALUE;

  162.         int iTerminalYIndex = _adblY.length - 1;

  163.         if (dblY > _adblY[iTerminalYIndex]) return java.lang.Integer.MAX_VALUE;

  164.         for (int i = 1; i <= iTerminalYIndex; ++i) {
  165.             if (dblY >= _adblY[i - 1] && dblY >= _adblY[i]) return i;
  166.         }

  167.         throw new java.lang.Exception ("WireSurfacePiecewiseConstant::enclosingXIndex => Invalid Inputs");
  168.     }

  169.     /**
  170.      * Compute the Bivariate Surface Response Value
  171.      *
  172.      * @param dblX X
  173.      * @param dblY Y
  174.      *
  175.      * @return The Bivariate Surface Response Value
  176.      *
  177.      * @throws java.lang.Exception Thrown if Inputs are Invalid
  178.      */

  179.     public double responseValue (
  180.         final double dblX,
  181.         final double dblY)
  182.         throws java.lang.Exception
  183.     {
  184.         int iTerminalXIndex = _adblX.length - 1;
  185.         int iTerminalYIndex = _adblY.length - 1;

  186.         int iEnclosingXIndex = enclosingXIndex (dblX);

  187.         int iEnclosingYIndex = enclosingYIndex (dblY);

  188.         if (java.lang.Integer.MIN_VALUE == iEnclosingXIndex)
  189.             iEnclosingXIndex = 0;
  190.         else if (java.lang.Integer.MAX_VALUE == iEnclosingXIndex)
  191.             iEnclosingXIndex = iTerminalXIndex;
  192.         else {
  193.             for (int i = 1; i <= iTerminalXIndex; ++i) {
  194.                 if (dblX >= _adblX[i - 1] && dblX >= _adblX[i]) {
  195.                     iEnclosingXIndex = i;
  196.                     break;
  197.                 }
  198.             }
  199.         }

  200.         if (java.lang.Integer.MIN_VALUE == iEnclosingYIndex)
  201.             iEnclosingYIndex = 0;
  202.         else if (java.lang.Integer.MAX_VALUE == iEnclosingYIndex)
  203.             iEnclosingYIndex = iTerminalYIndex;
  204.         else {
  205.             for (int i = 1; i <= iTerminalYIndex; ++i) {
  206.                 if (dblY >= _adblY[i - 1] && dblY >= _adblY[i]) {
  207.                     iEnclosingYIndex = i;
  208.                     break;
  209.                 }
  210.             }
  211.         }

  212.         return _aadblResponse[iEnclosingXIndex][iEnclosingYIndex];
  213.     }
  214. }