PositionChangeComponents.java

  1. package org.drip.historical.attribution;

  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>PositionChangeComponents</i> contains the Decomposition of the Components of the Interval Change for a
  80.  * given Position.
  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/ComputationSupportLibrary.md">Computation Support</a></li>
  86.  *      <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>
  87.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/historical/attribution/README.md">Position Market Change Components Attribution</a></li>
  88.  *  </ul>
  89.  *
  90.  * @author Lakshmi Krishnamurthy
  91.  */

  92. public class PositionChangeComponents {
  93.     private boolean _bChangeTypeReturn = false;
  94.     private double _dblAccrualChange = java.lang.Double.NaN;
  95.     private org.drip.historical.attribution.PositionMarketSnap _pmsFirst = null;
  96.     private org.drip.historical.attribution.PositionMarketSnap _pmsSecond = null;
  97.     private org.drip.analytics.support.CaseInsensitiveHashMap<java.lang.Double> _mapDifferenceMetric = null;

  98.     /**
  99.      * PositionChangeComponents Constructor
  100.      *
  101.      * @param bChangeTypeReturn TRUE - Change Type is Return (Relative)
  102.      * @param pmsFirst The First Position Market Snapshot Instance
  103.      * @param pmsSecond The Second Position Market Snapshot Instance
  104.      * @param dblAccrualChange The Accrual Change Component of Interval Return/Change
  105.      * @param mapDifferenceMetric The Map of Difference Metrics
  106.      *
  107.      * @throws java.lang.Exception Thrown if the Inputs are invalid
  108.      */

  109.     public PositionChangeComponents (
  110.         final boolean bChangeTypeReturn,
  111.         final org.drip.historical.attribution.PositionMarketSnap pmsFirst,
  112.         final org.drip.historical.attribution.PositionMarketSnap pmsSecond,
  113.         final double dblAccrualChange,
  114.         final org.drip.analytics.support.CaseInsensitiveHashMap<java.lang.Double> mapDifferenceMetric)
  115.         throws java.lang.Exception
  116.     {
  117.         _bChangeTypeReturn = bChangeTypeReturn;
  118.         _mapDifferenceMetric = mapDifferenceMetric;

  119.         if (null == (_pmsFirst = pmsFirst) || null == (_pmsSecond = pmsSecond) ||
  120.             _pmsFirst.snapDate().julian() >= _pmsSecond.snapDate().julian() ||
  121.                 !org.drip.numerical.common.NumberUtil.IsValid (_dblAccrualChange = dblAccrualChange))
  122.             throw new java.lang.Exception ("PositionChangeComponents Constructor => Invalid Inputs!");
  123.     }

  124.     /**
  125.      * Return the Position Change Type
  126.      *
  127.      * @return TRUE - Change Type is Return (Relative)
  128.      */

  129.     public boolean changeTypeReturn()
  130.     {
  131.         return _bChangeTypeReturn;
  132.     }

  133.     /**
  134.      * Retrieve the Set of Manifest Measures
  135.      *
  136.      * @return The Set of Manifest Measures
  137.      */

  138.     public java.util.Set<java.lang.String> manifestMeasures()
  139.     {
  140.         return _pmsFirst.manifestMeasures();
  141.     }

  142.     /**
  143.      * Retrieve the First Position Market Snapshot Instance
  144.      *
  145.      * @return The First Position Market Snapshot Instance
  146.      */

  147.     public org.drip.historical.attribution.PositionMarketSnap pmsFirst()
  148.     {
  149.         return _pmsFirst;
  150.     }

  151.     /**
  152.      * Retrieve the Second Position Market Snapshot Instance
  153.      *
  154.      * @return The Second Position Market Snapshot Instance
  155.      */

  156.     public org.drip.historical.attribution.PositionMarketSnap pmsSecond()
  157.     {
  158.         return _pmsSecond;
  159.     }

  160.     /**
  161.      * Retrieve the First Date
  162.      *
  163.      * @return The First Date
  164.      */

  165.     public org.drip.analytics.date.JulianDate firstDate()
  166.     {
  167.         return _pmsFirst.snapDate();
  168.     }

  169.     /**
  170.      * Retrieve the Second Date
  171.      *
  172.      * @return The Second Date
  173.      */

  174.     public org.drip.analytics.date.JulianDate secondDate()
  175.     {
  176.         return _pmsSecond.snapDate();
  177.     }

  178.     /**
  179.      * Retrieve the Gross Interval Clean Change
  180.      *
  181.      * @return The Gross Interval Clean Change
  182.      */

  183.     public double grossCleanChange()
  184.     {
  185.         return _pmsSecond.marketValue() - _pmsFirst.marketValue();
  186.     }

  187.     /**
  188.      * Retrieve the Gross Interval Change
  189.      *
  190.      * @return The Gross Interval Change
  191.      */

  192.     public double grossChange()
  193.     {
  194.         return grossCleanChange() + _dblAccrualChange;
  195.     }

  196.     /**
  197.      * Retrieve the Specific Manifest Measure Market Realization Position Change
  198.      *
  199.      * @param strManifestMeasure The Manifest Measure
  200.      *
  201.      * @return The Specific Manifest Measure Market Realization Position Change
  202.      *
  203.      * @throws java.lang.Exception Thrown if the Inputs are invalid
  204.      */

  205.     public double specificMarketRealizationChange (
  206.         final java.lang.String strManifestMeasure)
  207.         throws java.lang.Exception
  208.     {
  209.         org.drip.historical.attribution.PositionManifestMeasureSnap pmmsFirst = _pmsFirst.manifestMeasureSnap
  210.             (strManifestMeasure);

  211.         org.drip.historical.attribution.PositionManifestMeasureSnap pmmsSecond = _pmsSecond.manifestMeasureSnap
  212.             (strManifestMeasure);

  213.         if (null == pmmsFirst || null == pmmsSecond)
  214.             throw new java.lang.Exception
  215.                 ("PositionChangeComponents::specificMarketRealizationChange => Invalid Inputs");

  216.         return 0.5 * (pmmsFirst.sensitivity() + pmmsSecond.sensitivity()) * (pmmsSecond.realization() -
  217.             pmmsFirst.realization());
  218.     }

  219.     /**
  220.      * Retrieve the Full Manifest Measure Realization Position Change
  221.      *
  222.      * @return The Full Manifest Measure Realization Position Change
  223.      *
  224.      * @throws java.lang.Exception Thrown if the Inputs are invalid
  225.      */

  226.     public double marketRealizationChange()
  227.         throws java.lang.Exception
  228.     {
  229.         java.util.Set<java.lang.String> setstrManiFestMeasure = _pmsFirst.manifestMeasures();

  230.         if (null == setstrManiFestMeasure || 0 == setstrManiFestMeasure.size())
  231.             throw new java.lang.Exception
  232.                 ("PositionChangeComponents::marketRealizationChange => No Manifest Measures");

  233.         double dblMarketRealizationChange = 0.;

  234.         for (java.lang.String strManifestMeasure : setstrManiFestMeasure)
  235.             dblMarketRealizationChange += specificMarketRealizationChange (strManifestMeasure);

  236.         return dblMarketRealizationChange;
  237.     }

  238.     /**
  239.      * Retrieve the Specific Manifest Measure Market Sensitivity Position Change
  240.      *
  241.      * @param strManifestMeasure The Manifest Measure
  242.      *
  243.      * @return The Specific Manifest Measure Market Sensitivity Position Change
  244.      *
  245.      * @throws java.lang.Exception Thrown if the Inputs are invalid
  246.      */

  247.     public double specificMarketSensitivityChange (
  248.         final java.lang.String strManifestMeasure)
  249.         throws java.lang.Exception
  250.     {
  251.         org.drip.historical.attribution.PositionManifestMeasureSnap pmmsFirst = _pmsFirst.manifestMeasureSnap
  252.             (strManifestMeasure);

  253.         org.drip.historical.attribution.PositionManifestMeasureSnap pmmsSecond =
  254.             _pmsSecond.manifestMeasureSnap (strManifestMeasure);

  255.         if (null == pmmsFirst || null == pmmsSecond)
  256.             throw new java.lang.Exception
  257.                 ("PositionChangeComponents::specificMarketSensitivityChange => Invalid Inputs");

  258.         return 0.5 * (pmmsFirst.realization() + pmmsSecond.realization()) * (pmmsSecond.sensitivity() -
  259.             pmmsFirst.sensitivity());
  260.     }

  261.     /**
  262.      * Retrieve the Full Manifest Measure Market Sensitivity Position Change
  263.      *
  264.      * @return The Full Manifest Measure Market Sensitivity Position Change
  265.      *
  266.      * @throws java.lang.Exception Thrown if the Inputs are invalid
  267.      */

  268.     public double marketSensitivityChange()
  269.         throws java.lang.Exception
  270.     {
  271.         java.util.Set<java.lang.String> setstrManiFestMeasure = _pmsFirst.manifestMeasures();

  272.         if (null == setstrManiFestMeasure || 0 == setstrManiFestMeasure.size())
  273.             throw new java.lang.Exception
  274.                 ("PositionChangeComponents::marketSensitivityChange => No Manifest Measures");

  275.         double dblMarketSensitivityChange = 0.;

  276.         for (java.lang.String strManifestMeasure : setstrManiFestMeasure)
  277.             dblMarketSensitivityChange += specificMarketSensitivityChange (strManifestMeasure);

  278.         return dblMarketSensitivityChange;
  279.     }

  280.     /**
  281.      * Retrieve the Specific Manifest Measure Market Roll-down Position Change
  282.      *
  283.      * @param strManifestMeasure The Manifest Measure
  284.      *
  285.      * @return The Specific Manifest Measure Market Roll-down Position Change
  286.      *
  287.      * @throws java.lang.Exception Thrown if the Inputs are invalid
  288.      */

  289.     public double specificMarketRollDownChange (
  290.         final java.lang.String strManifestMeasure)
  291.         throws java.lang.Exception
  292.     {
  293.         org.drip.historical.attribution.PositionManifestMeasureSnap pmmsFirst = _pmsFirst.manifestMeasureSnap
  294.             (strManifestMeasure);

  295.         if (null == pmmsFirst)
  296.             throw new java.lang.Exception
  297.                 ("PositionChangeComponents::specificMarketRollDownChange => Invalid Inputs");

  298.         return pmmsFirst.sensitivity() * (pmmsFirst.rollDown() - pmmsFirst.realization());
  299.     }

  300.     /**
  301.      * Retrieve the Full Manifest Measure Roll-down Position Change
  302.      *
  303.      * @return The Full Manifest Measure Roll-down Position Change
  304.      *
  305.      * @throws java.lang.Exception Thrown if the Inputs are invalid
  306.      */

  307.     public double marketRollDownChange()
  308.         throws java.lang.Exception
  309.     {
  310.         java.util.Set<java.lang.String> setstrManiFestMeasure = _pmsFirst.manifestMeasures();

  311.         if (null == setstrManiFestMeasure || 0 == setstrManiFestMeasure.size())
  312.             throw new java.lang.Exception
  313.                 ("PositionChangeComponents::marketRollDownChange => No Manifest Measures");

  314.         double dblMarketRollDownChange = 0.;

  315.         for (java.lang.String strManifestMeasure : setstrManiFestMeasure)
  316.             dblMarketRollDownChange += specificMarketRollDownChange (strManifestMeasure);

  317.         return dblMarketRollDownChange;
  318.     }

  319.     /**
  320.      * Retrieve the Accrual Interval Change
  321.      *
  322.      * @return The Accrual Interval Change
  323.      */

  324.     public double accrualChange()
  325.     {
  326.         return _dblAccrualChange;
  327.     }

  328.     /**
  329.      * Retrieve the Explained Interval Change
  330.      *
  331.      * @return The Explained Interval Change
  332.      *
  333.      * @throws java.lang.Exception Thrown if the Inputs are invalid
  334.      */

  335.     public double explainedChange()
  336.         throws java.lang.Exception
  337.     {
  338.         return marketRealizationChange() + marketRollDownChange();
  339.     }

  340.     /**
  341.      * Retrieve the Unexplained Interval Change
  342.      *
  343.      * @return The Unexplained Interval Change
  344.      *
  345.      * @throws java.lang.Exception Thrown if the Inputs are invalid
  346.      */

  347.     public double unexplainedChange()
  348.         throws java.lang.Exception
  349.     {
  350.         return grossChange() - explainedChange();
  351.     }

  352.     /**
  353.      * Retrieve the Map of Difference Metrics
  354.      *
  355.      * @return The Map of Difference Metrics
  356.      */

  357.     public org.drip.analytics.support.CaseInsensitiveHashMap<java.lang.Double> differenceMetric()
  358.     {
  359.         return _mapDifferenceMetric;
  360.     }

  361.     /**
  362.      * Retrieve the Row of Header Fields
  363.      *
  364.      * @return The Row of Header Fields
  365.      */

  366.     public java.lang.String header()
  367.     {
  368.         java.lang.String strHeader = "FirstDate,SecondDate,TotalPnL,TotalCleanPnL,MarketShiftPnL," +
  369.             "RollDownPnL,AccrualPnL,ExplainedPnL,UnexplainedPnL," + _pmsFirst.header ("first") +
  370.                 _pmsSecond.header ("second");

  371.         if (null == _mapDifferenceMetric) return strHeader;

  372.         for (java.lang.String strKey : _mapDifferenceMetric.keySet())
  373.             strHeader = strHeader + strKey + "change,";

  374.         return strHeader;
  375.     }

  376.     /**
  377.      * Retrieve the Row of Content Fields
  378.      *
  379.      * @return The Row of Content Fields
  380.      */

  381.     public java.lang.String content()
  382.     {
  383.         java.lang.String strContent = firstDate().toString() + "," + secondDate().toString() + ",";

  384.         strContent = strContent + org.drip.numerical.common.FormatUtil.FormatDouble (grossChange(), 1, 8, 1.) +
  385.             ",";

  386.         strContent = strContent + org.drip.numerical.common.FormatUtil.FormatDouble (grossCleanChange(), 1, 8,
  387.             1.) + ",";

  388.         try {
  389.             strContent = strContent + org.drip.numerical.common.FormatUtil.FormatDouble
  390.                 (marketRealizationChange(), 1, 8, 1.) + ",";

  391.             strContent = strContent + org.drip.numerical.common.FormatUtil.FormatDouble (marketRollDownChange(),
  392.                 1, 8, 1.) + ",";

  393.             strContent = strContent + org.drip.numerical.common.FormatUtil.FormatDouble (_dblAccrualChange, 1, 8,
  394.                 1.) + ",";

  395.             strContent = strContent + org.drip.numerical.common.FormatUtil.FormatDouble (explainedChange(), 1, 8,
  396.                 1.) + ",";

  397.             strContent = strContent + org.drip.numerical.common.FormatUtil.FormatDouble (unexplainedChange(), 1,
  398.                 8, 1.) + ",";
  399.         } catch (java.lang.Exception e) {
  400.             e.printStackTrace();

  401.             return null;
  402.         }

  403.         strContent = strContent + _pmsFirst.content() + _pmsSecond.content();

  404.         if (null == _mapDifferenceMetric) return strContent;

  405.         for (java.lang.String strKey : _mapDifferenceMetric.keySet())
  406.             strContent = strContent + org.drip.numerical.common.FormatUtil.FormatDouble (_mapDifferenceMetric.get
  407.                 (strKey), 1, 8, 1.) + ",";

  408.         return strContent;
  409.     }
  410. }