PredictorResponseWeightConstraint.java

  1. package org.drip.state.estimator;

  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>PredictorResponseWeightConstraint</i> holds the Linearized Constraints (and, optionally, their quote
  82.  * sensitivities) necessary needed for the Linear Calibration. Linearized Constraints are expressed as
  83.  *
  84.  *          Sum_i[Predictor Weight_i * Function (Response_i)] = Constraint Value
  85.  *
  86.  * where Function can either be univariate function, or weighted spline basis set. To this end, it
  87.  * implements the following functionality:
  88.  *
  89.  *  <br><br>
  90.  *  <ul>
  91.  *      <li>
  92.  *          Update/Retrieve Predictor/Response Weights and their Quote Sensitivities
  93.  *      </li>
  94.  *      <li>
  95.  *          Update/Retrieve Predictor/Response Constraint Values and their Quote Sensitivities
  96.  *      </li>
  97.  *      <li>
  98.  *          Display the contents of PredictorResponseWeightConstraint
  99.  *      </li>
  100.  *  </ul>
  101.  *
  102.  *  <br><br>
  103.  *  <ul>
  104.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ProductCore.md">Product Core Module</a></li>
  105.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/FixedIncomeAnalyticsLibrary.md">Fixed Income Analytics</a></li>
  106.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/state/README.md">Latent State Inference and Creation Utilities</a></li>
  107.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/state/estimator/README.md">Multi-Pass Customized Stretch Curve</a></li>
  108.  *  </ul>
  109.  * <br><br>
  110.  *
  111.  * @author Lakshmi Krishnamurthy
  112.  */

  113. public class PredictorResponseWeightConstraint {
  114.     private java.util.HashSet<org.drip.state.identifier.LatentStateLabel> _setLSL = null;

  115.     private org.drip.state.estimator.PredictorResponseRelationSetup _prrsCalib = new
  116.         org.drip.state.estimator.PredictorResponseRelationSetup();

  117.     private org.drip.analytics.support.CaseInsensitiveHashMap<org.drip.state.estimator.PredictorResponseRelationSetup>
  118.         _mapPRRSSens = new
  119.             org.drip.analytics.support.CaseInsensitiveHashMap<org.drip.state.estimator.PredictorResponseRelationSetup>();

  120.     private org.drip.state.estimator.PredictorResponseRelationSetup getPRRS (
  121.         final java.lang.String strManifestMeasure)
  122.     {
  123.         if (null == strManifestMeasure || strManifestMeasure.isEmpty()) return null;

  124.         if (!_mapPRRSSens.containsKey (strManifestMeasure))
  125.             _mapPRRSSens.put (strManifestMeasure, new
  126.                 org.drip.state.estimator.PredictorResponseRelationSetup());

  127.         return _mapPRRSSens.get (strManifestMeasure);
  128.     }

  129.     /**
  130.      * Empty PredictorResponseWeightConstraint constructor
  131.      */

  132.     public PredictorResponseWeightConstraint()
  133.     {
  134.     }

  135.     /**
  136.      * Add a Predictor/Response Weight entry to the Linearized Constraint
  137.      *
  138.      * @param dblPredictor The Predictor Node
  139.      * @param dblResponseWeight The Response Weight at the Node
  140.      *
  141.      * @return TRUE - Successfully added
  142.      */

  143.     public boolean addPredictorResponseWeight (
  144.         final double dblPredictor,
  145.         final double dblResponseWeight)
  146.     {
  147.         return _prrsCalib.addPredictorResponseWeight (dblPredictor, dblResponseWeight);
  148.     }

  149.     /**
  150.      * Add a Predictor/Response Weight entry to the Linearized Constraint
  151.      *
  152.      * @param strManifestMeasure The Manifest Measure
  153.      * @param dblPredictor The Predictor Node
  154.      * @param dblDResponseWeightDManifestMeasure The Response Weight-to-Manifest Measure Sensitivity at the
  155.      *  Node
  156.      *
  157.      * @return TRUE - Successfully added
  158.      */

  159.     public boolean addDResponseWeightDManifestMeasure (
  160.         final java.lang.String strManifestMeasure,
  161.         final double dblPredictor,
  162.         final double dblDResponseWeightDManifestMeasure)
  163.     {
  164.         return getPRRS (strManifestMeasure).addPredictorResponseWeight (dblPredictor,
  165.             dblDResponseWeightDManifestMeasure);
  166.     }

  167.     /**
  168.      * Update the Constraint Value
  169.      *
  170.      * @param dblValue The Constraint Value Update Increment
  171.      *
  172.      * @return TRUE - This Update Succeeded
  173.      */

  174.     public boolean updateValue (
  175.         final double dblValue)
  176.     {
  177.         return _prrsCalib.updateValue (dblValue);
  178.     }

  179.     /**
  180.      * Update the Constraint Value Sensitivity
  181.      *
  182.      * @param strManifestMeasure The Manifest Measure
  183.      * @param dblDValueDManifestMeasure The Constraint Value Sensitivity Update Increment
  184.      *
  185.      * @return TRUE - This Sensitivity Update Succeeded
  186.      */

  187.     public boolean updateDValueDManifestMeasure (
  188.         final java.lang.String strManifestMeasure,
  189.         final double dblDValueDManifestMeasure)
  190.     {
  191.         return getPRRS (strManifestMeasure).updateValue (dblDValueDManifestMeasure);
  192.     }

  193.     /**
  194.      * Retrieve the Constraint Value
  195.      *
  196.      * @return The Constraint Value
  197.      */

  198.     public double getValue()
  199.     {
  200.         return _prrsCalib.getValue();
  201.     }

  202.     /**
  203.      * Retrieve the Constraint Value Sensitivity
  204.      *
  205.      * @param strManifestMeasure The Manifest Measure
  206.      *
  207.      * @return The Constraint Value Sensitivity
  208.      *
  209.      * @throws java.lang.Exception Thrown if the Inputs are invalid
  210.      */

  211.     public double getDValueDManifestMeasure (
  212.         final java.lang.String strManifestMeasure)
  213.         throws java.lang.Exception
  214.     {
  215.         if (!_mapPRRSSens.containsKey (strManifestMeasure))
  216.             throw new java.lang.Exception
  217.                 ("PredictorResponseWeightConstraint::getDValueDManifestMeasure => Cannot locate manifest measure "
  218.                     + strManifestMeasure);

  219.         return _mapPRRSSens.get (strManifestMeasure).getValue();
  220.     }

  221.     /**
  222.      * Add a Merging Latent State Label
  223.      *
  224.      * @param lslMerge The Merging Latent State Label
  225.      *
  226.      * @return TRUE - The Latent State Label Successfully Added
  227.      */

  228.     public boolean addMergeLabel (
  229.         final org.drip.state.identifier.LatentStateLabel lslMerge)
  230.     {
  231.         if (null == lslMerge) return false;

  232.         if (null == _setLSL) _setLSL = new java.util.HashSet<org.drip.state.identifier.LatentStateLabel>();

  233.         _setLSL.add (lslMerge);

  234.         return true;
  235.     }

  236.     /**
  237.      * Return the Set of Merged Latent State Labels
  238.      *
  239.      * @return The Set of Merged Latent State Labels
  240.      */

  241.     public java.util.Set<org.drip.state.identifier.LatentStateLabel> mergeLabelSet()
  242.     {
  243.         return _setLSL;
  244.     }

  245.     /**
  246.      * Retrieve the Predictor To-From Response Weight Map
  247.      *
  248.      * @return The Predictor To-From Response Weight Map
  249.      */

  250.     public java.util.TreeMap<java.lang.Double, java.lang.Double> getPredictorResponseWeight()
  251.     {
  252.         return _prrsCalib.getPredictorResponseWeight();
  253.     }

  254.     /**
  255.      * Retrieve the Predictor To-From Response Weight Sensitivity Map
  256.      *
  257.      * @param strManifestMeasure The Manifest Measure
  258.      *
  259.      * @return The Predictor To-From Response Weight Sensitivity Map
  260.      */

  261.     public java.util.TreeMap<java.lang.Double, java.lang.Double> getDResponseWeightDManifestMeasure (
  262.         final java.lang.String strManifestMeasure)
  263.     {
  264.         return !_mapPRRSSens.containsKey (strManifestMeasure) ? null : _mapPRRSSens.get
  265.             (strManifestMeasure).getPredictorResponseWeight();
  266.     }

  267.     /**
  268.      * "Absorb" the other PRWC Instance into the Current One
  269.      *
  270.      * @param prwcOther The "Other" PRWC Instance
  271.      *
  272.      * @return TRUE - At least one entry of the "Other" was absorbed
  273.      */

  274.     public boolean absorb (
  275.         final PredictorResponseWeightConstraint prwcOther)
  276.     {
  277.         if (null == prwcOther || !_prrsCalib.absorb (prwcOther._prrsCalib)) return false;

  278.         if (0 == prwcOther._mapPRRSSens.size()) return true;

  279.         if (0 != _mapPRRSSens.size()) {
  280.             for (java.util.Map.Entry<java.lang.String, org.drip.state.estimator.PredictorResponseRelationSetup>
  281.                 me : _mapPRRSSens.entrySet()) {
  282.                 java.lang.String strKey = me.getKey();

  283.                 if (prwcOther._mapPRRSSens.containsKey (strKey))
  284.                     me.getValue().absorb (prwcOther._mapPRRSSens.get (strKey));
  285.             }
  286.         }

  287.         for (java.util.Map.Entry<java.lang.String, org.drip.state.estimator.PredictorResponseRelationSetup>
  288.             me : prwcOther._mapPRRSSens.entrySet()) {
  289.             java.lang.String strKey = me.getKey();

  290.             if (!_mapPRRSSens.containsKey (strKey)) _mapPRRSSens.put (strKey, me.getValue());
  291.         }

  292.         java.util.Set<org.drip.state.identifier.LatentStateLabel> lsLSL = prwcOther.mergeLabelSet();

  293.         if (null == lsLSL || 0 == lsLSL.size()) return true;

  294.         for (org.drip.state.identifier.LatentStateLabel lsl : lsLSL) {
  295.             if (!addMergeLabel (lsl)) return false;
  296.         }

  297.         return true;
  298.     }

  299.     /**
  300.      * Return the Set of Available Sensitivities (if any)
  301.      *
  302.      * @return The Set of Available Sensitivities
  303.      */

  304.     public java.util.Set<java.lang.String> sensitivityKeys()
  305.     {
  306.         return _mapPRRSSens.keySet();
  307.     }

  308.     /**
  309.      * Display the Constraints and the corresponding Weights
  310.      *
  311.      * @param strComment The Prefix Comment
  312.      */

  313.     public void displayString (
  314.         final java.lang.String strComment)
  315.     {
  316.         java.util.Map<java.lang.Double, java.lang.Double> mapPRW = _prrsCalib.getPredictorResponseWeight();

  317.         if (null != mapPRW && 0 != mapPRW.size()) {
  318.             for (java.util.Map.Entry<java.lang.Double, java.lang.Double> me : mapPRW.entrySet()) {
  319.                 double dblDate = me.getKey();

  320.                 System.out.println ("\t\t" + strComment + " - " + new org.drip.analytics.date.JulianDate
  321.                     ((int) dblDate) + " => " + me.getValue());
  322.             }
  323.         }

  324.         System.out.println ("\t" + strComment + " Constraint: " + _prrsCalib.getValue());

  325.         if (null != _setLSL) {
  326.             java.lang.String strLabels = "\t" + strComment + " Labels:";

  327.             for (org.drip.state.identifier.LatentStateLabel lsl : _setLSL)
  328.                 strLabels += " " + lsl.fullyQualifiedName();

  329.             System.out.println (strLabels);
  330.         }

  331.         System.out.flush();
  332.     }
  333. }