AggregatedSpan.java

  1. package org.drip.spline.grid;

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

  79. /**
  80.  * <i>AggregatedSpan</i> implements the Span interface. Here response from an array of spans whose responses
  81.  * are aggregated by their weights.
  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/grid/README.md">Aggregated/Overlapping Stretch/Span Grids</a></li>
  89.  *  </ul>
  90.  * <br><br>
  91.  *
  92.  * @author Lakshmi Krishnamurthy
  93.  */

  94. public class AggregatedSpan implements org.drip.spline.grid.Span {
  95.     private java.util.List<java.lang.Double> _lsWeight = null;
  96.     private java.util.List<org.drip.spline.grid.Span> _lsSpan = null;

  97.     /**
  98.      * AggregatedSpan Constructor
  99.      *
  100.      * @param lsSpan List of Spans
  101.      * @param lsWeight List of Weights
  102.      *
  103.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  104.      */

  105.     public AggregatedSpan (
  106.         final java.util.List<org.drip.spline.grid.Span> lsSpan,
  107.         final java.util.List<java.lang.Double> lsWeight)
  108.         throws java.lang.Exception
  109.     {
  110.         if (null == (_lsSpan = lsSpan) || null == (_lsWeight = lsWeight))
  111.             throw new java.lang.Exception ("AggregatedSpan ctr: Invalid Inputs");

  112.         int iNumSpan = _lsSpan.size();

  113.         if (0 == iNumSpan || iNumSpan != _lsWeight.size())
  114.             throw new java.lang.Exception ("AggregatedSpan ctr: Invalid Inputs");

  115.         for (org.drip.spline.grid.Span span : _lsSpan) {
  116.             if (null == span) throw new java.lang.Exception ("AggregatedSpan ctr: Invalid Inputs");
  117.         }
  118.     }

  119.     @Override public boolean addStretch (
  120.         final org.drip.spline.stretch.MultiSegmentSequence mss)
  121.     {
  122.         return false;
  123.     }

  124.     @Override public org.drip.spline.stretch.MultiSegmentSequence getContainingStretch (
  125.         final double dblPredictorOrdinate)
  126.     {
  127.         for (org.drip.spline.grid.Span span : _lsSpan) {
  128.             org.drip.spline.stretch.MultiSegmentSequence mss = span.getContainingStretch
  129.                 (dblPredictorOrdinate);

  130.             if (null != mss) return mss;
  131.         }

  132.         return null;
  133.     }

  134.     @Override public org.drip.spline.stretch.MultiSegmentSequence getStretch (
  135.         final java.lang.String strName)
  136.     {
  137.         if (null == strName || strName.isEmpty()) return null;

  138.         for (org.drip.spline.grid.Span span : _lsSpan) {
  139.             org.drip.spline.stretch.MultiSegmentSequence mss = span.getStretch (strName);

  140.             if (null != mss) return mss;
  141.         }

  142.         return null;
  143.     }

  144.     @Override public double left()
  145.         throws java.lang.Exception
  146.     {
  147.         return _lsSpan.get (0).left();
  148.     }

  149.     @Override public double right()
  150.         throws java.lang.Exception
  151.     {
  152.         return _lsSpan.get (_lsSpan.size() - 1).right();
  153.     }

  154.     @Override public double calcResponseValue (
  155.         final double dblPredictorOrdinate)
  156.         throws java.lang.Exception
  157.     {
  158.         int i = 0;
  159.         double dblResponseValue = 0.;

  160.         for (org.drip.spline.grid.Span span : _lsSpan)
  161.             dblResponseValue += span.calcResponseValue (dblPredictorOrdinate) * _lsWeight.get (i++);

  162.         return dblResponseValue;
  163.     }

  164.     @Override public double calcResponseValueDerivative (
  165.         final double dblPredictorOrdinate,
  166.         final int iOrder)
  167.         throws java.lang.Exception
  168.     {
  169.         int i = 0;
  170.         double dblResponseValueDerivative = 0.;

  171.         for (org.drip.spline.grid.Span span : _lsSpan)
  172.             dblResponseValueDerivative += span.calcResponseValueDerivative (dblPredictorOrdinate,iOrder) *
  173.                 _lsWeight.get (i++);

  174.         return dblResponseValueDerivative;
  175.     }

  176.     @Override public boolean isMergeState (
  177.         final double dblPredictorOrdinate,
  178.         final org.drip.state.identifier.LatentStateLabel lsl)
  179.     {
  180.         for (org.drip.spline.grid.Span span : _lsSpan) {
  181.             if (span.isMergeState (dblPredictorOrdinate, lsl)) return true;
  182.         }

  183.         return false;
  184.     }

  185.     @Override public org.drip.numerical.differentiation.WengertJacobian jackDResponseDManifestMeasure (
  186.         final java.lang.String strManifestMeasure,
  187.         final double dblPredictorOrdinate,
  188.         final int iOrder)
  189.     {
  190.         int i = 0;
  191.         org.drip.numerical.differentiation.WengertJacobian wjAggregate = null;

  192.         for (org.drip.spline.grid.Span span : _lsSpan) {
  193.             org.drip.numerical.differentiation.WengertJacobian wj = span.jackDResponseDManifestMeasure
  194.                 (strManifestMeasure, dblPredictorOrdinate, iOrder);

  195.             if (null == wj) return null;

  196.             if (null == wjAggregate) {
  197.                 if (!(wjAggregate = wj).scale (_lsWeight.get (i++))) return null;
  198.             } else {
  199.                 if (!wjAggregate.cumulativeMerge (wj, _lsWeight.get (i++))) return null;
  200.             }
  201.         }

  202.         return wjAggregate;
  203.     }

  204.     @Override public boolean in (
  205.         final double dblPredictorOrdinate)
  206.         throws java.lang.Exception
  207.     {
  208.         for (org.drip.spline.grid.Span span : _lsSpan) {
  209.             if (span.in (dblPredictorOrdinate)) return true;
  210.         }

  211.         return false;
  212.     }

  213.     @Override public java.lang.String displayString()
  214.     {
  215.         int i = 0;

  216.         java.lang.StringBuffer sb = new java.lang.StringBuffer();

  217.         for (org.drip.spline.grid.Span span : _lsSpan)
  218.             sb.append (span.displayString() + " | " + _lsWeight.get (i++));

  219.         return sb.toString();
  220.     }
  221. }