HorizonChangeExplainProcessor.java

  1. package org.drip.historical.engine;

  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>HorizonChangeExplainProcessor</i> holds the Stubs associated with the Computation of the Horizon
  79.  * Position Change Components for the given Product.
  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/ComputationSupportLibrary.md">Computation Support</a></li>
  85.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/historical/README.md">Historical State Processing Utilities</a></li>
  86.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/historical/engine/README.md">Product Horizon Change Explain Engine</a></li>
  87.  *  </ul>
  88.  *
  89.  * @author Lakshmi Krishnamurthy
  90.  */

  91. public abstract class HorizonChangeExplainProcessor {
  92.     private int _iSettleLag = -1;
  93.     private java.lang.String _strMarketMeasureName = "";
  94.     private org.drip.product.definition.Component _comp = null;
  95.     private org.drip.analytics.date.JulianDate _dtFirst = null;
  96.     private org.drip.analytics.date.JulianDate _dtSecond = null;
  97.     private double _dblMarketMeasureValue = java.lang.Double.NaN;
  98.     private org.drip.param.market.CurveSurfaceQuoteContainer _csqcFirst = null;
  99.     private org.drip.param.market.CurveSurfaceQuoteContainer _csqcSecond = null;
  100.     private
  101.         org.drip.analytics.support.CaseInsensitiveHashMap<org.drip.param.market.CurveSurfaceQuoteContainer>
  102.             _mapCSQCRollDown = null;

  103.     protected HorizonChangeExplainProcessor (
  104.         final org.drip.product.definition.Component comp,
  105.         final int iSettleLag,
  106.         final java.lang.String strMarketMeasureName,
  107.         final double dblMarketMeasureValue,
  108.         final org.drip.analytics.date.JulianDate dtFirst,
  109.         final org.drip.analytics.date.JulianDate dtSecond,
  110.         final org.drip.param.market.CurveSurfaceQuoteContainer csqcFirst,
  111.         final org.drip.param.market.CurveSurfaceQuoteContainer csqcSecond,
  112.         final
  113.             org.drip.analytics.support.CaseInsensitiveHashMap<org.drip.param.market.CurveSurfaceQuoteContainer>
  114.             mapCSQCRollDown)
  115.         throws java.lang.Exception
  116.     {
  117.         if (null == (_comp = comp) || 0 > (_iSettleLag = iSettleLag) || null == (_strMarketMeasureName =
  118.             strMarketMeasureName) || _strMarketMeasureName.isEmpty() ||
  119.                 !org.drip.numerical.common.NumberUtil.IsValid (_dblMarketMeasureValue = dblMarketMeasureValue) ||
  120.                     null == (_dtFirst = dtFirst) || null == (_dtSecond = dtSecond) || _dtSecond.julian() <=
  121.                         _dtFirst.julian() || null == (_csqcFirst = csqcFirst) || null == (_csqcSecond =
  122.                             csqcSecond) || null == (_mapCSQCRollDown = mapCSQCRollDown))
  123.             throw new java.lang.Exception ("HorizonChangeExplainProcessor Constructor => Invalid Inputs");
  124.     }

  125.     /**
  126.      * Retrieve the Component
  127.      *
  128.      * @return The Component
  129.      */

  130.     public org.drip.product.definition.Component component()
  131.     {
  132.         return _comp;
  133.     }

  134.     /**
  135.      * Retrieve the Component Settle Lag
  136.      *
  137.      * @return The Component Settle Lag
  138.      */

  139.     public int settleLag()
  140.     {
  141.         return _iSettleLag;
  142.     }

  143.     /**
  144.      * Retrieve the Component Market Measure Name
  145.      *
  146.      * @return The Component Market Measure Name
  147.      */

  148.     public java.lang.String marketMeasureName()
  149.     {
  150.         return _strMarketMeasureName;
  151.     }

  152.     /**
  153.      * Retrieve the Component Market Measure Value
  154.      *
  155.      * @return The Component Market Measure Value
  156.      */

  157.     public double marketMeasureValue()
  158.     {
  159.         return _dblMarketMeasureValue;
  160.     }

  161.     /**
  162.      * Retrieve the First Date of the Horizon Change
  163.      *
  164.      * @return The First Date of the Horizon Change
  165.      */

  166.     public org.drip.analytics.date.JulianDate firstDate()
  167.     {
  168.         return _dtFirst;
  169.     }

  170.     /**
  171.      * Retrieve the First Date's Market Parameters
  172.      *
  173.      * @return The First Date's Market Parameters
  174.      */

  175.     public org.drip.param.market.CurveSurfaceQuoteContainer firstMarketParameters()
  176.     {
  177.         return _csqcFirst;
  178.     }

  179.     /**
  180.      * Retrieve the Second Date of the Horizon Change
  181.      *
  182.      * @return The Second Date of the Horizon Change
  183.      */

  184.     public org.drip.analytics.date.JulianDate secondDate()
  185.     {
  186.         return _dtSecond;
  187.     }

  188.     /**
  189.      * Retrieve the Second Date's Market Parameters
  190.      *
  191.      * @return The Second Date's Market Parameters
  192.      */

  193.     public org.drip.param.market.CurveSurfaceQuoteContainer secondMarketParameters()
  194.     {
  195.         return _csqcSecond;
  196.     }

  197.     /**
  198.      * Retrieve the Map of the Roll Down Market Parameters
  199.      *
  200.      * @return Map of the Roll Down Market Parameters
  201.      */

  202.     public
  203.         org.drip.analytics.support.CaseInsensitiveHashMap<org.drip.param.market.CurveSurfaceQuoteContainer>
  204.             rollDownMarketParameters()
  205.     {
  206.         return _mapCSQCRollDown;
  207.     }

  208.     /**
  209.      * Generate the Map of the Roll Down Market Quote Metrics
  210.      *
  211.      * @return Map of the Roll Down Market Quote Metrics
  212.      */

  213.     public org.drip.historical.engine.MarketMeasureRollDown rollDownMeasureMap()
  214.     {
  215.         org.drip.param.valuation.ValuationParams valParamsRollDown =
  216.             org.drip.param.valuation.ValuationParams.Spot (_dtSecond.addBusDays (_iSettleLag,
  217.                 _comp.payCurrency()).julian());

  218.         java.util.Set<java.lang.String> setstrRollDownTenor = _mapCSQCRollDown.keySet();

  219.         org.drip.historical.engine.MarketMeasureRollDown mmrd = null;

  220.         for (java.lang.String strRollDownTenor : setstrRollDownTenor) {
  221.             java.util.Map<java.lang.String, java.lang.Double> mapCompMeasures = _comp.value
  222.                 (valParamsRollDown, null, _mapCSQCRollDown.get (strRollDownTenor), null);

  223.             if (null == mapCompMeasures || !mapCompMeasures.containsKey (_strMarketMeasureName)) return null;

  224.             if ("Native".equalsIgnoreCase (strRollDownTenor)) {
  225.                 try {
  226.                     mmrd = new org.drip.historical.engine.MarketMeasureRollDown (mapCompMeasures.get
  227.                         (_strMarketMeasureName));
  228.                 } catch (java.lang.Exception e) {
  229.                     e.printStackTrace();

  230.                     return null;
  231.                 }
  232.             } else
  233.                 mmrd.add (strRollDownTenor, mapCompMeasures.get (_strMarketMeasureName));
  234.         }

  235.         return mmrd;
  236.     }

  237.     /**
  238.      * Generate the Roll Up Version of the Quote Metric
  239.      *
  240.      * @return The Roll Up Version of the Quote Metric
  241.      *
  242.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  243.      */

  244.     public double metricRollUp()
  245.         throws java.lang.Exception
  246.     {
  247.         java.util.Map<java.lang.String, java.lang.Double> mapCompMeasures = _comp.value
  248.             (org.drip.param.valuation.ValuationParams.Spot (_dtSecond.addBusDays (_iSettleLag,
  249.                 _comp.payCurrency()).julian()), null, _csqcFirst, null);

  250.         if (null == mapCompMeasures || !mapCompMeasures.containsKey (_strMarketMeasureName))
  251.             throw new java.lang.Exception ("HorizonChangeExplainProcessor::metricRollUp => Invalid Inputs");

  252.         return mapCompMeasures.get (_strMarketMeasureName);
  253.     }

  254.     /**
  255.      * Generate and Snap Relevant Fields from the First Market Valuation Parameters
  256.      *
  257.      * @return The First Market Parameters Valuation Snapshot
  258.      */

  259.     public abstract org.drip.historical.attribution.PositionMarketSnap snapFirstMarketValue();

  260.     /**
  261.      * Update the Fixings (if any) to the Second Market Parameters
  262.      *
  263.      * @return TRUE - The Fixings were successfully updated to the Second Market Parameters
  264.      */

  265.     public abstract boolean updateFixings();

  266.     /**
  267.      * Generate and Snap Relevant Fields from the Second Market Valuation Parameters
  268.      *
  269.      * @return The Second Market Parameters Valuation Snapshot
  270.      */

  271.     public abstract org.drip.historical.attribution.PositionMarketSnap snapSecondMarketValue();

  272.     /**
  273.      * Generate the Horizon Differential Metrics Map
  274.      *
  275.      * @param pmsFirst The First Position Market Snap
  276.      * @param pmsSecond The Second Position Market Snap
  277.      *
  278.      * @return The Horizon Differential Metrics Map
  279.      */

  280.     public abstract org.drip.analytics.support.CaseInsensitiveHashMap<java.lang.Double>
  281.         crossHorizonDifferentialMetrics (
  282.             final org.drip.historical.attribution.PositionMarketSnap pmsFirst,
  283.             final org.drip.historical.attribution.PositionMarketSnap pmsSecond);
  284. }