WireSurfaceStretch.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>WireSurfaceStretch</i> implements a 2D spline surface stretch. It synthesizes this from an array of 1D
  80.  * Span instances, each of which is referred to as wire spline in this case.
  81.  *
  82.  * <br><br>
  83.  *  <ul>
  84.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ComputationalCore.md">Computational Core Module</a></li>
  85.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/SplineBuilderLibrary.md">Spline Builder Library</a></li>
  86.  *      <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>
  87.  *      <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>
  88.  *  </ul>
  89.  * <br><br>
  90.  *
  91.  * @author Lakshmi Krishnamurthy
  92.  */

  93. public class WireSurfaceStretch {
  94.     private org.drip.spline.params.SegmentCustomBuilderControl _scbc = null;
  95.     private java.util.TreeMap<java.lang.Double, org.drip.spline.grid.Span> _mapWireSpan = null;

  96.     /**
  97.      * WireSurfaceStretch Constructor
  98.      *
  99.      * @param strName Name
  100.      * @param scbc Segment Custom Builder Control Parameters
  101.      * @param mapWireSpan X-mapped Array of Y Basis Spline Wire Spans
  102.      *
  103.      * @throws java.lang.Exception Thrown if the Inputs are invalid
  104.      */

  105.     public WireSurfaceStretch (
  106.         final java.lang.String strName,
  107.         final org.drip.spline.params.SegmentCustomBuilderControl scbc,
  108.         final java.util.TreeMap<java.lang.Double, org.drip.spline.grid.Span> mapWireSpan)
  109.         throws java.lang.Exception
  110.     {
  111.         if (null == (_mapWireSpan = mapWireSpan) || 0 == _mapWireSpan.size() || null == (_scbc = scbc))
  112.             throw new java.lang.Exception ("WireSurfaceStretch ctr: Invalid Inputs");
  113.     }

  114.     /**
  115.      * Compute the Bivariate Surface Response Value
  116.      *
  117.      * @param dblX X
  118.      * @param dblY Y
  119.      *
  120.      * @return The Bivariate Surface Response Value
  121.      *
  122.      * @throws java.lang.Exception Thrown if Inputs are Invalid
  123.      */

  124.     public double responseValue (
  125.         final double dblX,
  126.         final double dblY)
  127.         throws java.lang.Exception
  128.     {
  129.         int iSize = _mapWireSpan.size();

  130.         int i = 0;
  131.         double[] adblX = new double[iSize];
  132.         double[] adblZ = new double[iSize];
  133.         org.drip.spline.params.SegmentCustomBuilderControl[] aSCBC = new
  134.             org.drip.spline.params.SegmentCustomBuilderControl[iSize - 1];

  135.         for (java.util.Map.Entry<java.lang.Double, org.drip.spline.grid.Span> me : _mapWireSpan.entrySet()) {
  136.             if (null == me)
  137.                 throw new java.lang.Exception ("WireSurfaceStretch::responseValue => Invalid Wire Span Map");

  138.             if (0 != i) aSCBC[i - 1] = _scbc;

  139.             adblX[i] = me.getKey();

  140.             org.drip.spline.grid.Span wireSpan = me.getValue();

  141.             if (null == wireSpan)
  142.                 throw new java.lang.Exception ("WireSurfaceStretch::responseValue => Invalid Wire Span Map");

  143.             double dblLeftY = wireSpan.left();

  144.             double dblRightY = wireSpan.right();

  145.             if (dblY <= dblLeftY)
  146.                 adblZ[i++] = wireSpan.calcResponseValue (dblLeftY);
  147.             else if (dblY >= dblRightY)
  148.                 adblZ[i++] = wireSpan.calcResponseValue (dblRightY);
  149.             else
  150.                 adblZ[i++] = wireSpan.calcResponseValue (dblY);
  151.         }

  152.         org.drip.spline.stretch.MultiSegmentSequence mss =
  153.             org.drip.spline.stretch.MultiSegmentSequenceBuilder.CreateCalibratedStretchEstimator
  154.                 ("org.drip.spline.multidimensional.WireSurfaceStretch@" +
  155.                     org.drip.numerical.common.StringUtil.GUID(), adblX, adblZ, aSCBC, null,
  156.                         org.drip.spline.stretch.BoundarySettings.NaturalStandard(),
  157.                             org.drip.spline.stretch.MultiSegmentSequence.CALIBRATE);

  158.         if (null == mss)
  159.             throw new java.lang.Exception ("WireSurfaceStretch::responseValue => Cannot extract MSS");

  160.         double dblLeftX = mss.getLeftPredictorOrdinateEdge();

  161.         if (dblX <= dblLeftX) return mss.responseValue (dblLeftX);

  162.         double dblRightX = mss.getRightPredictorOrdinateEdge();

  163.         if (dblX >= dblRightX) return mss.responseValue (dblRightX);

  164.         return mss.responseValue (dblX);
  165.     }

  166.     /**
  167.      * Retrieve the Surface Span Stretch that corresponds to the given Y Anchor
  168.      *
  169.      * @param dblYAnchor Y Anchor
  170.      *
  171.      * @return The Surface Span Stretch Instance
  172.      */

  173.     public org.drip.spline.grid.Span wireSpanYAnchor (
  174.         final double dblYAnchor)
  175.     {
  176.         if (!org.drip.numerical.common.NumberUtil.IsValid (dblYAnchor)) return null;

  177.         int iSize = _mapWireSpan.size();

  178.         int i = 0;
  179.         double[] adblX = new double[iSize];
  180.         double[] adblZ = new double[iSize];
  181.         org.drip.spline.params.SegmentCustomBuilderControl[] aSCBC = new
  182.             org.drip.spline.params.SegmentCustomBuilderControl[iSize - 1];

  183.         for (java.util.Map.Entry<java.lang.Double, org.drip.spline.grid.Span> me : _mapWireSpan.entrySet()) {
  184.             if (null == me) return null;

  185.             if (0 != i) aSCBC[i - 1] = _scbc;

  186.             adblX[i] = me.getKey();

  187.             org.drip.spline.grid.Span wireSpan = me.getValue();

  188.             if (null == wireSpan) return null;

  189.             try {
  190.                 double dblLeftY = wireSpan.left();

  191.                 double dblRightY = wireSpan.right();

  192.                 if (dblYAnchor <= dblLeftY)
  193.                     adblZ[i++] = wireSpan.calcResponseValue (dblLeftY);
  194.                 else if (dblYAnchor >= dblRightY)
  195.                     adblZ[i++] = wireSpan.calcResponseValue (dblRightY);
  196.                 else
  197.                     adblZ[i++] = wireSpan.calcResponseValue (dblYAnchor);
  198.             } catch (java.lang.Exception e) {
  199.                 e.printStackTrace();

  200.                 return null;
  201.             }
  202.         }

  203.         try {
  204.             return new org.drip.spline.grid.OverlappingStretchSpan
  205.                 (org.drip.spline.stretch.MultiSegmentSequenceBuilder.CreateCalibratedStretchEstimator
  206.                     ("org.drip.spline.multidimensional.WireSurfaceStretch@" +
  207.                         org.drip.numerical.common.StringUtil.GUID(), adblX, adblZ, aSCBC, null,
  208.                             org.drip.spline.stretch.BoundarySettings.NaturalStandard(),
  209.                                 org.drip.spline.stretch.MultiSegmentSequence.CALIBRATE));
  210.         } catch (java.lang.Exception e) {
  211.             e.printStackTrace();
  212.         }

  213.         return null;
  214.     }

  215.     /**
  216.      * Retrieve the Surface Span Stretch that corresponds to the given X Anchor
  217.      *
  218.      * @param dblXAnchor X Anchor
  219.      *
  220.      * @return The Surface Span Stretch Instance
  221.      */

  222.     public org.drip.spline.grid.Span wireSpanXAnchor (
  223.         final double dblXAnchor)
  224.     {
  225.         if (!org.drip.numerical.common.NumberUtil.IsValid (dblXAnchor)) return null;

  226.         org.drip.spline.grid.Span spanPrev = null;
  227.         org.drip.spline.grid.Span spanCurrent = null;
  228.         double dblXAnchorPrev = java.lang.Double.NaN;
  229.         double dblXAnchorCurrent = java.lang.Double.NaN;

  230.         for (java.util.Map.Entry<java.lang.Double, org.drip.spline.grid.Span> me : _mapWireSpan.entrySet()) {
  231.             if (null == me) return null;

  232.             dblXAnchorCurrent = me.getKey();

  233.             spanCurrent = me.getValue();

  234.             if (!org.drip.numerical.common.NumberUtil.IsValid (dblXAnchorPrev)) {
  235.                 if (dblXAnchor <= (dblXAnchorPrev = dblXAnchorCurrent)) return spanCurrent;

  236.                 spanPrev = spanCurrent;
  237.                 continue;
  238.             }

  239.             if (dblXAnchor > dblXAnchorPrev && dblXAnchor <= dblXAnchorCurrent) {
  240.                 double dblLeftWeight = (dblXAnchorCurrent - dblXAnchor) / (dblXAnchorCurrent -
  241.                     dblXAnchorPrev);

  242.                 java.util.List<java.lang.Double> lsWeight = new java.util.ArrayList<java.lang.Double>();

  243.                 java.util.List<org.drip.spline.grid.Span> lsSpan = new
  244.                     java.util.ArrayList<org.drip.spline.grid.Span>();

  245.                 lsSpan.add (spanPrev);

  246.                 lsSpan.add (spanCurrent);

  247.                 lsWeight.add (dblLeftWeight);

  248.                 lsWeight.add (1. - dblLeftWeight);

  249.                 try {
  250.                     return new org.drip.spline.grid.AggregatedSpan (lsSpan, lsWeight);
  251.                 } catch (java.lang.Exception e) {
  252.                     e.printStackTrace();

  253.                     return null;
  254.                 }
  255.             }

  256.             spanPrev = spanCurrent;
  257.             dblXAnchorPrev = dblXAnchorCurrent;
  258.         }

  259.         return spanCurrent;
  260.     }
  261. }