RdSpanningCombinatorialIterator.java

  1. package org.drip.spaces.iterator;

  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>RdSpanningCombinatorialIterator</i> contains the Functionality to conduct a Spanning Iteration through
  80.  * an R<sup>d</sup> Combinatorial Space.
  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/StatisticalLearningLibrary.md">Statistical Learning Library</a></li>
  86.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/spaces/README.md">R<sup>1</sup> and R<sup>d</sup> Vector/Tensor Spaces (Validated and/or Normed), and Function Classes</a></li>
  87.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/spaces/iterator/README.md">Iterative/Exhaustive Vector Space Scanners</a></li>
  88.  *  </ul>
  89.  * <br><br>
  90.  *
  91.  * @author Lakshmi Krishnamurthy
  92.  */

  93. public class RdSpanningCombinatorialIterator extends
  94.     org.drip.spaces.iterator.RdExhaustiveStateSpaceScan {
  95.     private org.drip.spaces.tensor.R1CombinatorialVector[] _aR1CV = null;

  96.     /**
  97.      * Retrieve the RdSpanningCombinatorialIterator Instance associated with the Underlying Vector Space
  98.      *
  99.      * @param aR1CV Array of R^1 Combinatorial Vectors
  100.      *
  101.      * @return The RdSpanningCombinatorialIterator Instance associated with the Underlying Vector Space
  102.      */

  103.     public static final RdSpanningCombinatorialIterator Standard (
  104.         final org.drip.spaces.tensor.R1CombinatorialVector[] aR1CV)
  105.     {
  106.         if (null == aR1CV) return null;

  107.         int iDimension = aR1CV.length;
  108.         int[] aiMax = new int[iDimension];

  109.         if (0 == iDimension) return null;

  110.         for (int i = 0; i < iDimension; ++i)
  111.             aiMax[i] = (int) aR1CV[i].cardinality().number();

  112.         try {
  113.             return new RdSpanningCombinatorialIterator (aR1CV, aiMax);
  114.         } catch (java.lang.Exception e) {
  115.             e.printStackTrace();
  116.         }

  117.         return null;
  118.     }

  119.     /**
  120.      * RdSpanningCombinatorialIterator Constructor
  121.      *
  122.      * @param aR1CV Array of the R^1 Combinatorial Vectors
  123.      * @param aiMax The Array of Dimension Maximum
  124.      *
  125.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  126.      */

  127.     public RdSpanningCombinatorialIterator (
  128.         final org.drip.spaces.tensor.R1CombinatorialVector[] aR1CV,
  129.         final int[] aiMax)
  130.         throws java.lang.Exception
  131.     {
  132.         super (aiMax, false);

  133.         if (null == (_aR1CV = aR1CV) || _aR1CV.length != aiMax.length)
  134.             throw new java.lang.Exception ("RdCombinatorialIterator ctr: Invalid Inputs");
  135.     }

  136.     /**
  137.      * Retrieve the Array of the R^1 Combinatorial Vectors
  138.      *
  139.      * @return The Array of the R^1 Combinatorial Vectors
  140.      */

  141.     public org.drip.spaces.tensor.R1CombinatorialVector[] r1()
  142.     {
  143.         return _aR1CV;
  144.     }

  145.     /**
  146.      * Convert the Vector Space Index Array to the Variate Array
  147.      *
  148.      * @param aiIndex Vector Space Index Array
  149.      *
  150.      * @return Variate Array
  151.      */

  152.     public double[] vectorSpaceIndexToVariate (
  153.         final int[] aiIndex)
  154.     {
  155.         if (null == aiIndex) return null;

  156.         int iDimension = _aR1CV.length;
  157.         double[] adblVariate = new double[iDimension];

  158.         if (iDimension != aiIndex.length) return null;

  159.         for (int i = 0; i < iDimension; ++i)
  160.             adblVariate[i] = _aR1CV[i].elementSpace().get (aiIndex[i]);

  161.         return adblVariate;
  162.     }

  163.     /**
  164.      * Retrieve the Cursor Variate Array
  165.      *
  166.      * @return The Cursor Variate Array
  167.      */

  168.     public double[] cursorVariates()
  169.     {
  170.         return vectorSpaceIndexToVariate (stateIndexCursor());
  171.     }

  172.     /**
  173.      * Retrieve the Subsequent Variate Array
  174.      *
  175.      * @return The Subsequent Variate Array
  176.      */

  177.     public double[] nextVariates()
  178.     {
  179.         return vectorSpaceIndexToVariate (nextStateIndexCursor());
  180.     }
  181. }