SegmentResponseValueConstraint.java

  1. package org.drip.spline.params;

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

  80. /**
  81.  * <i>SegmentResponseValueConstraint</i> holds the following set of fields that characterize a single global
  82.  * linear constraint between the predictor and the response variables within a single segment, expressed
  83.  * linearly across the constituent nodes. Constraints are expressed as
  84.  *
  85.  *          C = Sigma_j [W_j * y_j] = V where
  86.  *
  87.  *  x_j - Predictor j
  88.  *  y_j - Response j
  89.  *  W_j - Weight at ordinate j
  90.  *  V - Value of the Constraint
  91.  *
  92.  * SegmentResponseValueConstraint exports the following functionality:
  93.  *
  94.  * <br><br>
  95.  *  <ul>
  96.  *      <li>
  97.  *          Retrieve the Array of Predictor Ordinates.
  98.  *      </li>
  99.  *      <li>
  100.  *          Retrieve the Array of Response Weights at each Predictor Ordinate.
  101.  *      </li>
  102.  *      <li>
  103.  *          Retrieve the Constraint Value.
  104.  *      </li>
  105.  *      <li>
  106.  *          Convert the Segment Constraint onto Local Predictor Ordinates, the corresponding Response Basis
  107.  *              Function, and the Shape Controller Realizations.
  108.  *      </li>
  109.  *      <li>
  110.  *          Get the Position of the Predictor Knot relative to the Constraints.
  111.  *      </li>
  112.  *      <li>
  113.  *          Generate a SegmentResponseValueConstraint instance from the given predictor/response pair.
  114.  *      </li>
  115.  *  </ul>
  116.  *
  117.  * SegmentResponseValueConstraint can be viewed as the global response point value transform of
  118.  *  SegmentBasisFlexureConstraint.
  119.  *
  120.  * <br><br>
  121.  *  <ul>
  122.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ComputationalCore.md">Computational Core Module</a></li>
  123.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/SplineBuilderLibrary.md">Spline Builder Library</a></li>
  124.  *      <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>
  125.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/spline/params/README.md">Spline Segment Construction Control Parameters</a></li>
  126.  *  </ul>
  127.  * <br><br>
  128.  *
  129.  * @author Lakshmi Krishnamurthy
  130.  */

  131. public class SegmentResponseValueConstraint {

  132.     /**
  133.      * Indicator specifying that the knot is to the left of the constraint ordinates
  134.      */

  135.     public static final int LEFT_OF_CONSTRAINT = 1;

  136.     /**
  137.      * Indicator specifying that the knot is to the right of the constraint ordinates
  138.      */

  139.     public static final int RIGHT_OF_CONSTRAINT = 2;

  140.     /**
  141.      * Indicator specifying that the knot splits the constraint ordinates
  142.      */

  143.     public static final int SPLITS_CONSTRAINT = 4;

  144.     private double[] _adblPredictorOrdinate = null;
  145.     private double[] _adblResponseValueWeight = null;
  146.     private double _dblWeightedResponseValueConstraint = java.lang.Double.NaN;

  147.     /**
  148.      * Generate a SegmentResponseValueConstraint instance from the given predictor/response pair.
  149.      *
  150.      * @param dblPredictorOrdinate The Predictor Ordinate
  151.      * @param dblResponseValue The Response Value
  152.      *
  153.      * @return The SegmentResponseValueConstraint instance
  154.      */

  155.     public static final SegmentResponseValueConstraint FromPredictorResponsePair (
  156.         final double dblPredictorOrdinate,
  157.         final double dblResponseValue)
  158.     {
  159.         if (!org.drip.numerical.common.NumberUtil.IsValid (dblPredictorOrdinate) ||
  160.             !org.drip.numerical.common.NumberUtil.IsValid (dblResponseValue))
  161.             return null;

  162.         try {
  163.             return new SegmentResponseValueConstraint (new double[] {dblPredictorOrdinate}, new double[]
  164.                 {1.}, dblResponseValue);
  165.         } catch (java.lang.Exception e) {
  166.             e.printStackTrace();
  167.         }

  168.         return null;
  169.     }

  170.     /**
  171.      * SegmentResponseValueConstraint constructor
  172.      *
  173.      * @param adblPredictorOrdinate The Array of Global Predictor Ordinates
  174.      * @param adblResponseValueWeight The Array of the Weights to be applied to the Response at each
  175.      *  Predictor Ordinate
  176.      * @param dblWeightedResponseValueConstraint The Value of the Weighted Response Value Constraint
  177.      *
  178.      * @throws java.lang.Exception Throws if the Inputs are Invalid
  179.      */

  180.     public SegmentResponseValueConstraint (
  181.         final double[] adblPredictorOrdinate,
  182.         final double[] adblResponseValueWeight,
  183.         final double dblWeightedResponseValueConstraint)
  184.         throws java.lang.Exception
  185.     {
  186.         if (null == (_adblPredictorOrdinate = adblPredictorOrdinate) || null == (_adblResponseValueWeight =
  187.             adblResponseValueWeight) || !org.drip.numerical.common.NumberUtil.IsValid
  188.                 (_dblWeightedResponseValueConstraint = dblWeightedResponseValueConstraint))
  189.             throw new java.lang.Exception ("SegmentResponseValueConstraint ctr: Invalid Inputs");

  190.         int iNumPredictorOrdinate = adblPredictorOrdinate.length;

  191.         if (0 == iNumPredictorOrdinate || _adblResponseValueWeight.length != iNumPredictorOrdinate)
  192.             throw new java.lang.Exception ("SegmentResponseValueConstraint ctr: Invalid Inputs");
  193.     }

  194.     /**
  195.      * Retrieve the Array of Predictor Ordinates
  196.      *
  197.      * @return The Array of Predictor Ordinates
  198.      */

  199.     public double[] predictorOrdinates()
  200.     {
  201.         return _adblPredictorOrdinate;
  202.     }

  203.     /**
  204.      * Retrieve the Array of Response Weights at each Predictor Ordinate
  205.      *
  206.      * @return The Array of Response Weights at each Predictor Ordinate
  207.      */

  208.     public double[] responseWeights()
  209.     {
  210.         return _adblResponseValueWeight;
  211.     }

  212.     /**
  213.      * Retrieve the Constraint Value
  214.      *
  215.      * @return The Constraint Value
  216.      */

  217.     public double constraintValue()
  218.     {
  219.         return _dblWeightedResponseValueConstraint;
  220.     }

  221.     public void display (
  222.         final java.lang.String strComment)
  223.     {
  224.         for (int i = 0; i < _adblPredictorOrdinate.length; ++i)
  225.             System.out.println ("\t\t" + strComment + " - " + new org.drip.analytics.date.JulianDate ((int)
  226.                 _adblPredictorOrdinate[i]) + " => " + _adblResponseValueWeight[i]);

  227.         System.out.println ("\tConstraint: " + _dblWeightedResponseValueConstraint);
  228.     }

  229.     /**
  230.      * Convert the Segment Constraint onto Local Predictor Ordinates, the corresponding Response Basis
  231.      *  Function, and the Shape Controller Realizations
  232.      *
  233.      * @param lbe The Local Basis Evaluator
  234.      * @param ics Inelastics transformer to convert coordinate space to Local from Global
  235.      *
  236.      * @return The Segment Basis Function Constraint
  237.      */

  238.     public org.drip.spline.params.SegmentBasisFlexureConstraint responseIndexedBasisConstraint (
  239.         final org.drip.spline.segment.BasisEvaluator lbe,
  240.         final org.drip.spline.segment.LatentStateInelastic ics)
  241.     {
  242.         if (null == lbe || null == ics) return null;

  243.         int iNumResponseBasis = lbe.numBasis();

  244.         int iNumPredictorOrdinate = _adblPredictorOrdinate.length;
  245.         double[] adblResponseBasisWeight = new double[iNumResponseBasis];

  246.         if (0 == iNumResponseBasis) return null;

  247.         try {
  248.             for (int i = 0; i < iNumResponseBasis; ++i) {
  249.                 adblResponseBasisWeight[i] = 0.;

  250.                 for (int j = 0; j < iNumPredictorOrdinate; ++j)
  251.                     adblResponseBasisWeight[i] += _adblResponseValueWeight[j] *
  252.                         lbe.shapedBasisFunctionResponse (_adblPredictorOrdinate[j], i);
  253.             }

  254.             return new org.drip.spline.params.SegmentBasisFlexureConstraint (adblResponseBasisWeight,
  255.                 _dblWeightedResponseValueConstraint);
  256.         } catch (java.lang.Exception e) {
  257.             e.printStackTrace();
  258.         }

  259.         return null;
  260.     }

  261.     /**
  262.      * Get the Position of the Predictor Knot relative to the Constraints
  263.      *
  264.      * @param dblPredictorKnot The Predictor Knot Ordinate
  265.      *
  266.      * @return Indicator specifying whether the Knot is Left of the constraints, Right of the Constraints, or
  267.      *  splits the Constraints
  268.      *
  269.      * @throws java.lang.Exception Thrown if Inputs are invalid
  270.      */

  271.     public int knotPosition (
  272.         final double dblPredictorKnot)
  273.         throws java.lang.Exception
  274.     {
  275.         if (!org.drip.numerical.common.NumberUtil.IsValid (dblPredictorKnot))
  276.             throw new java.lang.Exception ("SegmentResponseValueConstraint::knotPosition => Invalid Inputs");

  277.         if (dblPredictorKnot < _adblPredictorOrdinate[0]) return LEFT_OF_CONSTRAINT;

  278.         if (dblPredictorKnot > _adblPredictorOrdinate[_adblPredictorOrdinate.length - 1])
  279.             return RIGHT_OF_CONSTRAINT;

  280.         return SPLITS_CONSTRAINT;
  281.     }
  282. }