PositionMarketSnap.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.  *
  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>PositionMarketSnap</i> contains the Metrics Snapshot associated with the relevant Manifest Measures for
  79.  * a given Position.
  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/attribution/README.md">Position Market Change Components Attribution</a></li>
  87.  *  </ul>
  88.  *
  89.  * @author Lakshmi Krishnamurthy
  90.  */

  91. public class PositionMarketSnap {
  92.     private double _dblMarketValue = java.lang.Double.NaN;
  93.     private org.drip.analytics.date.JulianDate _dtSnap = null;

  94.     private java.util.Map<java.lang.String, org.drip.historical.attribution.PositionManifestMeasureSnap>
  95.         _mapPMMS = new
  96.             org.drip.analytics.support.CaseInsensitiveHashMap<org.drip.historical.attribution.PositionManifestMeasureSnap>();

  97.     private java.util.Map<java.lang.String, java.lang.String> _mapCustomC1 = new
  98.         org.drip.analytics.support.CaseInsensitiveHashMap<java.lang.String>();

  99.     private java.util.Map<java.lang.String, java.lang.Double> _mapCustomR1 = new
  100.         org.drip.analytics.support.CaseInsensitiveHashMap<java.lang.Double>();

  101.     private java.util.Map<java.lang.String, org.drip.analytics.date.JulianDate> _mapCustomDate = new
  102.         org.drip.analytics.support.CaseInsensitiveHashMap<org.drip.analytics.date.JulianDate>();

  103.     /**
  104.      * PositionMarketSnap Constructor
  105.      *
  106.      * @param dtSnap The Snapshot Date
  107.      * @param dblMarketValue The Snapshot Market Value
  108.      *
  109.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  110.      */

  111.     public PositionMarketSnap (
  112.         final org.drip.analytics.date.JulianDate dtSnap,
  113.         final double dblMarketValue)
  114.         throws java.lang.Exception
  115.     {
  116.         if (null == (_dtSnap = dtSnap) || !org.drip.numerical.common.NumberUtil.IsValid (_dblMarketValue =
  117.             dblMarketValue))
  118.             throw new java.lang.Exception ("PositionMarketSnap Constructor: Invalid Inputs");
  119.     }

  120.     /**
  121.      * Retrieve the Date of the Snap
  122.      *
  123.      * @return Date of the Snap
  124.      */

  125.     public org.drip.analytics.date.JulianDate snapDate()
  126.     {
  127.         return _dtSnap;
  128.     }

  129.     /**
  130.      * Retrieve the Position Market Value
  131.      *
  132.      * @return The Position Market Value
  133.      */

  134.     public double marketValue()
  135.     {
  136.         return _dblMarketValue;
  137.     }

  138.     /**
  139.      * Add an Instance of the Position Manifest Measure Snap from the Specified Inputs
  140.      *
  141.      * @param strManifestMeasure The Manifest Measure
  142.      * @param dblManifestMeasureRealization The Manifest Measure Realization
  143.      * @param dblManifestMeasureSensitivity The Manifest Measure Sensitivity
  144.      * @param dblManifestMeasureRollDown The Manifest Measure Roll Down
  145.      *
  146.      * @return TRUE - The Manifest Measure Snap Metrics successfully added
  147.      */

  148.     public boolean addManifestMeasureSnap (
  149.         final java.lang.String strManifestMeasure,
  150.         final double dblManifestMeasureRealization,
  151.         final double dblManifestMeasureSensitivity,
  152.         final double dblManifestMeasureRollDown)
  153.     {
  154.         if (null == strManifestMeasure || strManifestMeasure.isEmpty()) return false;

  155.         try {
  156.             _mapPMMS.put (strManifestMeasure, new org.drip.historical.attribution.PositionManifestMeasureSnap
  157.                 (dblManifestMeasureRealization, dblManifestMeasureSensitivity, dblManifestMeasureRollDown));

  158.             return true;
  159.         } catch (java.lang.Exception e) {
  160.             e.printStackTrace();
  161.         }

  162.         return false;
  163.     }

  164.     /**
  165.      * Retrieve the Snapshot associated with the specified Manifest Measure
  166.      *
  167.      * @param strManifestMeasure The Manifest Measure
  168.      *
  169.      * @return The Snapshot associated with the specified Manifest Measure
  170.      */

  171.     public org.drip.historical.attribution.PositionManifestMeasureSnap manifestMeasureSnap (
  172.         final java.lang.String strManifestMeasure)
  173.     {
  174.         return null == strManifestMeasure || !_mapPMMS.containsKey (strManifestMeasure) ? null : _mapPMMS.get
  175.             (strManifestMeasure);
  176.     }

  177.     /**
  178.      * Retrieve the Set of Manifest Measures
  179.      *
  180.      * @return The Set of Manifest Measures
  181.      */

  182.     public java.util.Set<java.lang.String> manifestMeasures()
  183.     {
  184.         return _mapPMMS.keySet();
  185.     }

  186.     /**
  187.      * Set the Custom Date Entry corresponding to the Specified Key
  188.      *
  189.      * @param strKey The Key
  190.      * @param dtCustom The Custom Date Entry
  191.      *
  192.      * @return TRUE - Custom Date successfully set
  193.      */

  194.     public boolean setDate (
  195.         final java.lang.String strKey,
  196.         final org.drip.analytics.date.JulianDate dtCustom)
  197.     {
  198.         if (null == strKey || strKey.isEmpty() || null == dtCustom) return false;

  199.         _mapCustomDate.put (strKey, dtCustom);

  200.         return true;
  201.     }

  202.     /**
  203.      * Retrieve the Custom Date Entry corresponding to the Specified Key
  204.      *
  205.      * @param strKey The Key
  206.      *
  207.      * @return The Custom Date Entry
  208.      */

  209.     public org.drip.analytics.date.JulianDate date (
  210.         final java.lang.String strKey)
  211.     {
  212.         return null == strKey || !_mapCustomDate.containsKey (strKey) ? null : _mapCustomDate.get (strKey);
  213.     }

  214.     /**
  215.      * Set the Custom C^1 Entry corresponding to the Specified Key
  216.      *
  217.      * @param strKey The Key
  218.      * @param strC1 The Custom C^1 Entry
  219.      *
  220.      * @return TRUE - Custom C^1 Entry successfully set
  221.      */

  222.     public boolean setC1 (
  223.         final java.lang.String strKey,
  224.         final java.lang.String strC1)
  225.     {
  226.         if (null == strKey || strKey.isEmpty() || null == strC1 || strC1.isEmpty()) return false;

  227.         _mapCustomC1.put (strKey, strC1);

  228.         return true;
  229.     }

  230.     /**
  231.      * Retrieve the Custom C^1 Entry corresponding to the Specified Key
  232.      *
  233.      * @param strKey The Key
  234.      *
  235.      * @return The Custom C^1 Entry
  236.      */

  237.     public java.lang.String c1 (
  238.         final java.lang.String strKey)
  239.     {
  240.         return null == strKey || !_mapCustomC1.containsKey (strKey) ? null : _mapCustomC1.get (strKey);
  241.     }

  242.     /**
  243.      * Set the Custom R^1 Entry corresponding to the Specified Key
  244.      *
  245.      * @param strKey The Key
  246.      * @param dblR1 The Custom R^1 Entry
  247.      * @param bIgnoreNaN TRUE - Ignore NaN Entry
  248.      *
  249.      * @return TRUE - Custom Number successfully set
  250.      */

  251.     public boolean setR1 (
  252.         final java.lang.String strKey,
  253.         final double dblR1,
  254.         final boolean bIgnoreNaN)
  255.     {
  256.         if (null == strKey || strKey.isEmpty() || (!bIgnoreNaN && !org.drip.numerical.common.NumberUtil.IsValid
  257.             (dblR1)))
  258.             return false;

  259.         _mapCustomR1.put (strKey, dblR1);

  260.         return true;
  261.     }

  262.     /**
  263.      * Set the Custom R^1 Entry corresponding to the Specified Key
  264.      *
  265.      * @param strKey The Key
  266.      * @param dblR1 The Custom R^1 Entry
  267.      *
  268.      * @return TRUE - Custom Number successfully set
  269.      */

  270.     public boolean setR1 (
  271.         final java.lang.String strKey,
  272.         final double dblR1)
  273.     {
  274.         return setR1 (strKey, dblR1, true);
  275.     }

  276.     /**
  277.      * Retrieve the Custom R^1 Entry corresponding to the Specified Key
  278.      *
  279.      * @param strKey The Key
  280.      *
  281.      * @return The Custom R^1 Entry
  282.      *
  283.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  284.      */

  285.     public double r1 (
  286.         final java.lang.String strKey)
  287.         throws java.lang.Exception
  288.     {
  289.         if (null == strKey || !_mapCustomR1.containsKey (strKey))
  290.             throw new java.lang.Exception ("PositionMarketSnap::r1 => Invalid Inputs");

  291.         return _mapCustomR1.get (strKey);
  292.     }

  293.     /**
  294.      * Set the Market Measure Name
  295.      *
  296.      * @param strMarketMeasureName The Market Measure Name
  297.      *
  298.      * @return The Market Measure Name successfully set
  299.      */

  300.     public boolean setMarketMeasureName (
  301.         final java.lang.String strMarketMeasureName)
  302.     {
  303.         return setC1 ("MarketMeasureName", strMarketMeasureName);
  304.     }

  305.     /**
  306.      * Retrieve the Market Measure Name
  307.      *
  308.      * @return The Market Measure Name
  309.      */

  310.     public java.lang.String marketMeasureName()
  311.     {
  312.         return c1 ("MarketMeasureName");
  313.     }

  314.     /**
  315.      * Set the Market Measure Value
  316.      *
  317.      * @param dblMarketMeasureValue The Market Measure Value
  318.      *
  319.      * @return The Market Measure Value successfully set
  320.      */

  321.     public boolean setMarketMeasureValue (
  322.         final double dblMarketMeasureValue)
  323.     {
  324.         return setR1 ("MarketMeasureValue", dblMarketMeasureValue);
  325.     }

  326.     /**
  327.      * Retrieve the Market Measure Value
  328.      *
  329.      * @return The Market Measure Value
  330.      *
  331.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  332.      */

  333.     public double marketMeasureValue()
  334.         throws java.lang.Exception
  335.     {
  336.         return r1 ("MarketMeasureValue");
  337.     }

  338.     /**
  339.      * Set the Cumulative Coupon Amount
  340.      *
  341.      * @param dblCumulativeCouponAmount The Cumulative Coupon Amount
  342.      *
  343.      * @return TRUE - The Cumulative Coupon Amount successfully set
  344.      */

  345.     public boolean setCumulativeCouponAmount (
  346.         final double dblCumulativeCouponAmount)
  347.     {
  348.         return setR1 ("CumulativeCouponAmount", dblCumulativeCouponAmount);
  349.     }

  350.     /**
  351.      * Retrieve the Cumulative Coupon Amount
  352.      *
  353.      * @return The Cumulative Coupon Amount
  354.      *
  355.      * @throws java.lang.Exception Thrown if the Cumulative Coupon Amount cannot be obtained
  356.      */

  357.     public double cumulativeCouponAmount()
  358.         throws java.lang.Exception
  359.     {
  360.         return r1 ("CumulativeCouponAmount");
  361.     }

  362.     /**
  363.      * Retrieve the Row of Header Fields
  364.      *
  365.      * @param strPrefix The Prefix that precedes each Header Field
  366.      *
  367.      * @return The Row of Header Fields
  368.      */

  369.     public java.lang.String header (
  370.         final java.lang.String strPrefix)
  371.     {
  372.         java.lang.String strHeader = "";

  373.         for (java.lang.String strR1Key : _mapCustomR1.keySet())
  374.             strHeader = strHeader + strPrefix + strR1Key + ",";

  375.         for (java.lang.String strC1Key : _mapCustomC1.keySet())
  376.             strHeader = strHeader + strPrefix + strC1Key + ",";

  377.         for (java.lang.String strDateKey : _mapCustomDate.keySet())
  378.             strHeader = strHeader + strPrefix + strDateKey + ",";

  379.         return strHeader;
  380.     }

  381.     /**
  382.      * Retrieve the Row of Content Fields
  383.      *
  384.      * @return The Row of Content Fields
  385.      */

  386.     public java.lang.String content()
  387.     {
  388.         java.lang.String strContent = "";

  389.         for (java.lang.String strR1Key : _mapCustomR1.keySet())
  390.             strContent = strContent + org.drip.numerical.common.FormatUtil.FormatDouble (_mapCustomR1.get
  391.                 (strR1Key), 1, 8, 1.) + ",";

  392.         for (java.lang.String strC1Key : _mapCustomC1.keySet())
  393.             strContent = strContent + _mapCustomC1.get (strC1Key) + ",";

  394.         for (java.lang.String strDateKey : _mapCustomDate.keySet())
  395.             strContent = strContent + _mapCustomDate.get (strDateKey).toString() + ",";

  396.         return strContent;
  397.     }
  398. }