ObjectiveConstraintVariateSet.java

  1. package org.drip.function.rdtor1;

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

  77. /**
  78.  * <i>ObjectiveConstraintVariateSet</i> holds a R<sup>d</sup> To R<sup>1</sup> Variates corresponding to the
  79.  * Objective Function and the Constraint Function respectively.
  80.  *
  81.  *  <br><br>
  82.  *  <ul>
  83.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ComputationalCore.md">Computational Core Module</a></li>
  84.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/NumericalAnalysisLibrary.md">Numerical Analysis Library</a></li>
  85.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/function/README.md">R<sup>d</sup> To R<sup>d</sup> Function Analysis</a></li>
  86.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/function/rdtor1/README.md">Built-in R<sup>d</sup> To R<sup>1</sup> Functions</a></li>
  87.  *  </ul>
  88.  *
  89.  * @author Lakshmi Krishnamurthy
  90.  */

  91. public class ObjectiveConstraintVariateSet {
  92.     private double[] _adblObjectiveVariate = null;
  93.     private double[] _adblConstraintVariate = null;

  94.     /**
  95.      * Make a Unitary Variate Set
  96.      *
  97.      * @param iNumVariate Number of Variates
  98.      *
  99.      * @return Unitary Variate Set
  100.      */

  101.     public static final double[] Unitary (
  102.         final int iNumVariate)
  103.     {
  104.         if (0 >= iNumVariate) return null;

  105.         double[] adblVariate = new double[iNumVariate];

  106.         for (int i = 0; i < iNumVariate; ++i)
  107.             adblVariate[i] = 1.;

  108.         return adblVariate;
  109.     }

  110.     /**
  111.      * Make a Variate Set with/without Constraint
  112.      *
  113.      * @param iNumObjectiveFunctionVariate Number of the Objective Function Variates
  114.      * @param iNumConstraintFunctionVariate Number of the Constraint Function Variates
  115.      *
  116.      * @return Variate Set with/without Constraint
  117.      */

  118.     public static final double[] Uniform (
  119.         final int iNumObjectiveFunctionVariate,
  120.         final int iNumConstraintFunctionVariate)
  121.     {
  122.         if (0 >= iNumObjectiveFunctionVariate) return null;

  123.         double[] adblVariate = new double[iNumObjectiveFunctionVariate + iNumConstraintFunctionVariate];

  124.         for (int i = 0; i < iNumObjectiveFunctionVariate; ++i)
  125.             adblVariate[i] = 1. / iNumObjectiveFunctionVariate;

  126.         for (int i = 0; i < iNumConstraintFunctionVariate; ++i)
  127.             adblVariate[i + iNumObjectiveFunctionVariate] = 0.;

  128.         return adblVariate;
  129.     }

  130.     /**
  131.      * Make a Variate Set using a Pre-set Objective Variate Array with/without Constraint
  132.      *
  133.      * @param adblObjectiveFunctionVariate Array of Pre-set Objective Variates
  134.      * @param iNumConstraintFunctionVariate Number of the Constraint Function Variates
  135.      *
  136.      * @return Variate Set using a Pre-set Objective Variate Array with/without Constraint
  137.      */

  138.     public static final double[] Preset (
  139.         final double[] adblObjectiveFunctionVariate,
  140.         final int iNumConstraintFunctionVariate)
  141.     {
  142.         if (null == adblObjectiveFunctionVariate) return null;

  143.         int iNumObjectiveFunctionVariate = adblObjectiveFunctionVariate.length;

  144.         if (0 >= iNumObjectiveFunctionVariate) return null;

  145.         double[] adblVariate = new double[iNumObjectiveFunctionVariate + iNumConstraintFunctionVariate];

  146.         for (int i = 0; i < iNumObjectiveFunctionVariate; ++i)
  147.             adblVariate[i] = adblObjectiveFunctionVariate[i];

  148.         for (int i = 0; i < iNumConstraintFunctionVariate; ++i)
  149.             adblVariate[i + iNumObjectiveFunctionVariate] = 0.;

  150.         return adblVariate;
  151.     }

  152.     /**
  153.      * Partition the Variate Array into the Objective Function Input Variates and the Constraint Variate
  154.      *
  155.      * @param adblVariate The Input Variate Array
  156.      * @param iNumObjectiveFunctionVariate Number of the Objective Function Variates
  157.      *
  158.      * @return The ObjectiveConstraintVariateSet Instance
  159.      */

  160.     public static final ObjectiveConstraintVariateSet Partition (
  161.         final double[] adblVariate,
  162.         final int iNumObjectiveFunctionVariate)
  163.     {
  164.         if (null == adblVariate || 0 == iNumObjectiveFunctionVariate) return null;

  165.         int iNumVariate = adblVariate.length;
  166.         double[] adblObjectiveVariate = new double[iNumObjectiveFunctionVariate];
  167.         double[] adblConstraintVariate = new double[iNumVariate - iNumObjectiveFunctionVariate];

  168.         if (iNumObjectiveFunctionVariate >= iNumVariate) return null;

  169.         for (int i = 0; i < iNumObjectiveFunctionVariate; ++i)
  170.             adblObjectiveVariate[i] = adblVariate[i];

  171.         for (int i = iNumObjectiveFunctionVariate; i < iNumVariate; ++i)
  172.             adblConstraintVariate[i - iNumObjectiveFunctionVariate] = adblVariate[i];

  173.         try {
  174.             return new ObjectiveConstraintVariateSet (adblObjectiveVariate, adblConstraintVariate);
  175.         } catch (java.lang.Exception e) {
  176.             e.printStackTrace();
  177.         }

  178.         return null;
  179.     }

  180.     /**
  181.      * ObjectiveConstraintVariate Constructor
  182.      *
  183.      * @param adblObjectiveVariate Array of the Objective Function Variates
  184.      * @param adblConstraintVariate Array of the Constraint Function Variates
  185.      *
  186.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  187.      */

  188.     public ObjectiveConstraintVariateSet (
  189.         final double[] adblObjectiveVariate,
  190.         final double[] adblConstraintVariate)
  191.         throws java.lang.Exception
  192.     {
  193.         if (null == (_adblObjectiveVariate = adblObjectiveVariate) ||
  194.             !org.drip.numerical.common.NumberUtil.IsValid (_adblObjectiveVariate) || null ==
  195.                 (_adblConstraintVariate = adblConstraintVariate) || !org.drip.numerical.common.NumberUtil.IsValid
  196.                     (adblConstraintVariate))
  197.             throw new java.lang.Exception ("ObjectiveConstraintVariateSet Constructor => Invalid Inputs!");
  198.     }

  199.     /**
  200.      * Retrieve the Array of the Objective Function Variates
  201.      *
  202.      * @return The Array of the Objective Function Variates
  203.      */

  204.     public double[] objectiveVariates()
  205.     {
  206.         return _adblObjectiveVariate;
  207.     }

  208.     /**
  209.      * Retrieve the Array of the Constraint Function Variates
  210.      *
  211.      * @return The Array of the Constraint Function Variates
  212.      */

  213.     public double[] constraintVariates()
  214.     {
  215.         return _adblConstraintVariate;
  216.     }

  217.     /**
  218.      * Unify the Objective Function and the Constraint Function Input Variate Set
  219.      *
  220.      * @return The Unified Objective Function and the Constraint Function Input Variate Set
  221.      */

  222.     public double[] unify()
  223.     {
  224.         int iNumObjectiveFunctionVariate = _adblObjectiveVariate.length;
  225.         int iNumConstraintFunctionVariate = _adblConstraintVariate.length;
  226.         int iNumVariate = iNumObjectiveFunctionVariate + iNumConstraintFunctionVariate;
  227.         double[] adblVariate = new double[iNumVariate];

  228.         for (int i = 0; i < iNumObjectiveFunctionVariate; ++i)
  229.             adblVariate[i] = _adblObjectiveVariate[i];

  230.         for (int i = 0; i < iNumConstraintFunctionVariate; ++i)
  231.             adblVariate[iNumObjectiveFunctionVariate + i] = _adblConstraintVariate[i];

  232.         return adblVariate;
  233.     }
  234. }