TrajectoryShortfallAggregate.java

  1. package org.drip.execution.capture;

  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>TrajectoryShortfallAggregate</i> aggregates the  Execution Short-fall Distribution across each Interval
  79.  * in the Trade. The References are:
  80.  *
  81.  * <br><br>
  82.  *  <ul>
  83.  *      <li>
  84.  *          Almgren, R., and N. Chriss (1999): Value under Liquidation <i>Risk</i> <b>12 (12)</b>
  85.  *      </li>
  86.  *      <li>
  87.  *          Almgren, R., and N. Chriss (2000): Optimal Execution of Portfolio Transactions <i>Journal of
  88.  *              Risk</i> <b>3 (2)</b> 5-39
  89.  *      </li>
  90.  *      <li>
  91.  *          Bertsimas, D., and A. W. Lo (1998): Optimal Control of Execution Costs <i>Journal of Financial
  92.  *              Markets</i> <b>1</b> 1-50
  93.  *      </li>
  94.  *      <li>
  95.  *          Chan, L. K. C., and J. Lakonishak (1995): The Behavior of Stock Prices around Institutional
  96.  *              Trades <i>Journal of Finance</i> <b>50</b> 1147-1174
  97.  *      </li>
  98.  *      <li>
  99.  *          Keim, D. B., and A. Madhavan (1997): Transaction Costs and Investment Style: An Inter-exchange
  100.  *              Analysis of Institutional Equity Trades <i>Journal of Financial Economics</i> <b>46</b>
  101.  *              265-292
  102.  *      </li>
  103.  *  </ul>
  104.  *
  105.  *  <br><br>
  106.  *  <ul>
  107.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ProductCore.md">Product Core Module</a></li>
  108.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/TransactionCostAnalyticsLibrary.md">Transaction Cost Analytics</a></li>
  109.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/execution/README.md">Optimal Impact/Capture Based Trading Trajectories - Deterministic, Stochastic, Static, and Dynamic</a></li>
  110.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/execution/capture/README.md">Execution Trajectory Transaction Cost Capture</a></li>
  111.  *  </ul>
  112.  *
  113.  * @author Lakshmi Krishnamurthy
  114.  */

  115. public class TrajectoryShortfallAggregate {
  116.     private java.util.List<org.drip.execution.discrete.ShortfallIncrementDistribution> _lsSID = null;

  117.     /**
  118.      * TrajectoryShortfallAggregate Constructor
  119.      *
  120.      * @param lsSID List of the Incremental Slice Short-fall Distributions
  121.      *
  122.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  123.      */

  124.     public TrajectoryShortfallAggregate (
  125.         final java.util.List<org.drip.execution.discrete.ShortfallIncrementDistribution> lsSID)
  126.         throws java.lang.Exception
  127.     {
  128.         if (null == (_lsSID = lsSID))
  129.             throw new java.lang.Exception ("TrajectoryShortfallAggregate Constructor => Invalid Inputs");

  130.         int iNumInterval = _lsSID.size();

  131.         if (0 == iNumInterval)
  132.             throw new java.lang.Exception ("TrajectoryShortfallAggregate Constructor => Invalid Inputs");

  133.         for (org.drip.execution.discrete.ShortfallIncrementDistribution sid : _lsSID) {
  134.             if (null == sid)
  135.                 throw new java.lang.Exception ("TrajectoryShortfallAggregate Constructor => Invalid Inputs");
  136.         }
  137.     }

  138.     /**
  139.      * Retrieve the List of the Interval Cost Distributions
  140.      *
  141.      * @return The List of the Interval Cost Distributions
  142.      */

  143.     public java.util.List<org.drip.execution.discrete.ShortfallIncrementDistribution> list()
  144.     {
  145.         return _lsSID;
  146.     }

  147.     /**
  148.      * Generate the Total Cost R^1 Normal Distribution
  149.      *
  150.      * @return The Total Cost R^1 Normal Distribution
  151.      */

  152.     public org.drip.measure.gaussian.R1UnivariateNormal totalCostDistribution()
  153.     {
  154.         double dblTotalCostMean = 0.;
  155.         double dblTotalCostVariance = 0.;

  156.         for (org.drip.measure.gaussian.R1UnivariateNormal r1un : _lsSID) {
  157.             dblTotalCostMean = dblTotalCostMean + r1un.mean();

  158.             dblTotalCostVariance = dblTotalCostVariance + r1un.variance();
  159.         }

  160.         try {
  161.             return new org.drip.measure.gaussian.R1UnivariateNormal (dblTotalCostMean, java.lang.Math.sqrt
  162.                 (dblTotalCostVariance));
  163.         } catch (java.lang.Exception e) {
  164.             e.printStackTrace();
  165.         }

  166.         return null;
  167.     }

  168.     /**
  169.      * Generate the Array of Incremental Expectation Sequence
  170.      *
  171.      * @return The Array of Incremental Expectation Sequence
  172.      */

  173.     public double[] incrementalExpectation()
  174.     {
  175.         int iNumInterval = _lsSID.size();

  176.         double[] adblIncrementalExpectationSequence = new double[iNumInterval];

  177.         for (int i = 0; i < iNumInterval; ++i)
  178.             adblIncrementalExpectationSequence[i] = _lsSID.get (i).mean();

  179.         return adblIncrementalExpectationSequence;
  180.     }

  181.     /**
  182.      * Generate the Array of Cumulative Expectation Sequence
  183.      *
  184.      * @return The Array of Cumulative Expectation Sequence
  185.      */

  186.     public double[] cumulativeExpectation()
  187.     {
  188.         int iNumInterval = _lsSID.size();

  189.         double[] adblCumulativeExpectationSequence = new double[iNumInterval];

  190.         for (int i = 0; i < iNumInterval; ++i)
  191.             adblCumulativeExpectationSequence[i] = 0 == i ? _lsSID.get (i).expectation() :
  192.                 adblCumulativeExpectationSequence[i - 1] + _lsSID.get (i).expectation();

  193.         return adblCumulativeExpectationSequence;
  194.     }

  195.     /**
  196.      * Generate the Array of Incremental Variance Sequence
  197.      *
  198.      * @return The Array of Incremental Variance Sequence
  199.      */

  200.     public double[] incrementalVariance()
  201.     {
  202.         int iNumInterval = _lsSID.size();

  203.         double[] adblIncrementalVarianceSequence = new double[iNumInterval];

  204.         for (int i = 0; i < iNumInterval; ++i)
  205.             adblIncrementalVarianceSequence[i] = _lsSID.get (i).variance();

  206.         return adblIncrementalVarianceSequence;
  207.     }

  208.     /**
  209.      * Generate the Array of Cumulative Variance Sequence
  210.      *
  211.      * @return The Array of Cumulative Variance Sequence
  212.      */

  213.     public double[] cumulativeVariance()
  214.     {
  215.         int iNumInterval = _lsSID.size();

  216.         double[] adblCumulativeVarianceSequence = new double[iNumInterval];

  217.         for (int i = 0; i < iNumInterval; ++i)
  218.             adblCumulativeVarianceSequence[i] = 0 == i ? _lsSID.get (i).variance() :
  219.                 adblCumulativeVarianceSequence[i - 1] + _lsSID.get (i).variance();

  220.         return adblCumulativeVarianceSequence;
  221.     }

  222.     /**
  223.      * Generate the Array of Incremental Market Dynamic Expectation Sequence
  224.      *
  225.      * @return The Array of Incremental Market Dynamic Expectation Sequence
  226.      */

  227.     public double[] incrementalMarketDynamicExpectation()
  228.     {
  229.         int iNumInterval = _lsSID.size();

  230.         double[] adblIncrementalMarketDynamicExpectationSequence = new double[iNumInterval];

  231.         for (int i = 0; i < iNumInterval; ++i)
  232.             adblIncrementalMarketDynamicExpectationSequence[i] = _lsSID.get (i).marketDynamicExpectation();

  233.         return adblIncrementalMarketDynamicExpectationSequence;
  234.     }

  235.     /**
  236.      * Generate the Array of Cumulative Market Dynamic Expectation Sequence
  237.      *
  238.      * @return The Array of Cumulative Market Dynamic Expectation Sequence
  239.      */

  240.     public double[] cumulativeMarketDynamicExpectation()
  241.     {
  242.         int iNumInterval = _lsSID.size();

  243.         double[] adblCumulativeMarketDynamicExpectationSequence = new double[iNumInterval];

  244.         for (int i = 0; i < iNumInterval; ++i)
  245.             adblCumulativeMarketDynamicExpectationSequence[i] = 0 == i ? _lsSID.get
  246.                 (i).marketDynamicExpectation() : adblCumulativeMarketDynamicExpectationSequence[i - 1] +
  247.                     _lsSID.get (i).marketDynamicExpectation();

  248.         return adblCumulativeMarketDynamicExpectationSequence;
  249.     }

  250.     /**
  251.      * Generate the Array of Incremental Permanent Impact Expectation Sequence
  252.      *
  253.      * @return The Array of Incremental Permanent Impact Expectation Sequence
  254.      */

  255.     public double[] incrementalPermanentImpactExpectation()
  256.     {
  257.         int iNumInterval = _lsSID.size();

  258.         double[] adblIncrementalPermanentImpactExpectationSequence = new double[iNumInterval];

  259.         for (int i = 0; i < iNumInterval; ++i)
  260.             adblIncrementalPermanentImpactExpectationSequence[i] = _lsSID.get
  261.                 (i).permanentImpactExpectation();

  262.         return adblIncrementalPermanentImpactExpectationSequence;
  263.     }

  264.     /**
  265.      * Generate the Array of Cumulative Permanent Impact Expectation Sequence
  266.      *
  267.      * @return The Array of Cumulative Permanent Impact Expectation Sequence
  268.      */

  269.     public double[] cumulativePermanentImpactExpectation()
  270.     {
  271.         int iNumInterval = _lsSID.size();

  272.         double[] adblCumulativePermanentImpactExpectationSequence = new double[iNumInterval];

  273.         for (int i = 0; i < iNumInterval; ++i)
  274.             adblCumulativePermanentImpactExpectationSequence[i] = 0 == i ? _lsSID.get
  275.                 (i).permanentImpactExpectation() : adblCumulativePermanentImpactExpectationSequence[i - 1] +
  276.                     _lsSID.get (i).permanentImpactExpectation();

  277.         return adblCumulativePermanentImpactExpectationSequence;
  278.     }

  279.     /**
  280.      * Generate the Array of Incremental Temporary Impact Expectation Sequence
  281.      *
  282.      * @return The Array of Incremental Temporary Impact Expectation Sequence
  283.      */

  284.     public double[] incrementalTemporaryImpactExpectation()
  285.     {
  286.         int iNumInterval = _lsSID.size();

  287.         double[] adblIncrementalTemporaryImpactExpectationSequence = new double[iNumInterval];

  288.         for (int i = 0; i < iNumInterval; ++i)
  289.             adblIncrementalTemporaryImpactExpectationSequence[i] = _lsSID.get
  290.                 (i).temporaryImpactExpectation();

  291.         return adblIncrementalTemporaryImpactExpectationSequence;
  292.     }

  293.     /**
  294.      * Generate the Array of Cumulative Temporary Impact Expectation Sequence
  295.      *
  296.      * @return The Array of Cumulative Temporary Impact Expectation Sequence
  297.      */

  298.     public double[] cumulativeTemporaryImpactExpectation()
  299.     {
  300.         int iNumInterval = _lsSID.size();

  301.         double[] adblCumulativeTemporaryImpactExpectationSequence = new double[iNumInterval];

  302.         for (int i = 0; i < iNumInterval; ++i)
  303.             adblCumulativeTemporaryImpactExpectationSequence[i] = 0 == i ? _lsSID.get
  304.                 (i).temporaryImpactExpectation() : adblCumulativeTemporaryImpactExpectationSequence[i - 1] +
  305.                     _lsSID.get (i).temporaryImpactExpectation();

  306.         return adblCumulativeTemporaryImpactExpectationSequence;
  307.     }

  308.     /**
  309.      * Generate the Expected Short-fall
  310.      *
  311.      * @return The Expected Short-fall
  312.      */

  313.     public double shortfallExpectation()
  314.     {
  315.         int iNumInterval = _lsSID.size();

  316.         double dblExpectedShortfall = 0.;

  317.         for (int i = 0; i < iNumInterval; ++i)
  318.             dblExpectedShortfall = dblExpectedShortfall + _lsSID.get (i).expectation();

  319.         return dblExpectedShortfall;
  320.     }

  321.     /**
  322.      * Generate the Short-fall Variance
  323.      *
  324.      * @return The Short-fall Variance
  325.      */

  326.     public double shortfallVariance()
  327.     {
  328.         int iNumInterval = _lsSID.size();

  329.         double dblShortfallVariance = 0.;

  330.         for (int i = 0; i < iNumInterval; ++i)
  331.             dblShortfallVariance = dblShortfallVariance + _lsSID.get (i).variance();

  332.         return dblShortfallVariance;
  333.     }
  334. }