Slice.java

  1. package org.drip.execution.discrete;

  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>Slice</i> implements the Arithmetic Dynamics of the Price/Cost Movements exhibited by an Asset owing to
  79.  * the Volatility and the Market Impact Factors on a Trajectory Slice. 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/discrete/README.md">Trajectory Slice Execution Cost Distribution</a></li>
  111.  *  </ul>
  112.  *
  113.  * @author Lakshmi Krishnamurthy
  114.  */

  115. public class Slice implements org.drip.execution.sensitivity.ControlNodesGreekGenerator {
  116.     private double _dblTimeInterval = java.lang.Double.NaN;
  117.     private double _dblLeftHoldings = java.lang.Double.NaN;
  118.     private double _dblRightHoldings = java.lang.Double.NaN;

  119.     /**
  120.      * Slice Constructor
  121.      *
  122.      * @param dblLeftHoldings The Left-of-Slice Holdings
  123.      * @param dblRightHoldings The Right-of-Slice Holdings
  124.      * @param dblTimeInterval The Discrete Time Interval
  125.      *
  126.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  127.      */

  128.     public Slice (
  129.         final double dblLeftHoldings,
  130.         final double dblRightHoldings,
  131.         final double dblTimeInterval)
  132.         throws java.lang.Exception
  133.     {
  134.         if (!org.drip.numerical.common.NumberUtil.IsValid (_dblLeftHoldings = dblLeftHoldings) ||
  135.             !org.drip.numerical.common.NumberUtil.IsValid (_dblRightHoldings = dblRightHoldings) ||
  136.                 !org.drip.numerical.common.NumberUtil.IsValid (_dblTimeInterval = dblTimeInterval) || 0. >=
  137.                     _dblTimeInterval)
  138.             throw new java.lang.Exception ("Slice Constructor => Invalid Inputs!");
  139.     }

  140.     /**
  141.      * Retrieve the Left-of-Slice Holdings
  142.      *
  143.      * @return The Left-of-Slice Holdings
  144.      */

  145.     public double leftHoldings()
  146.     {
  147.         return _dblLeftHoldings;
  148.     }

  149.     /**
  150.      * Retrieve the Right Holdings
  151.      *
  152.      * @return The Right Holdings
  153.      */

  154.     public double rightHoldings()
  155.     {
  156.         return _dblRightHoldings;
  157.     }

  158.     /**
  159.      * Retrieve the Evolution Time Interval of the Arithmetic Dynamics
  160.      *
  161.      * @return The Evolution Time Interval of the Arithmetic Dynamics
  162.      */

  163.     public double timeInterval()
  164.     {
  165.         return _dblTimeInterval;
  166.     }

  167.     /**
  168.      * Indicate if the Slice is a Sell
  169.      *
  170.      * @return TRUE - The Slice is a Sell
  171.      */

  172.     public boolean isSell()
  173.     {
  174.         return _dblLeftHoldings - _dblRightHoldings > 0.;
  175.     }

  176.     /**
  177.      * Generate the Price Evolution Increment Unit Realization given the Walk Realization
  178.      *
  179.      * @param dblPreviousEquilibriumPrice The Previous Equilibrium Price
  180.      * @param ws Realized Walk Suite
  181.      * @param apep The Arithmetic Price Walk Evolution Parameters
  182.      *
  183.      * @return The Realized Price Evolution Increment Unit given the Walk Realization
  184.      */

  185.     public org.drip.execution.discrete.PriceIncrement priceIncrementRealization (
  186.         final double dblPreviousEquilibriumPrice,
  187.         final org.drip.execution.dynamics.WalkSuite ws,
  188.         final org.drip.execution.dynamics.ArithmeticPriceEvolutionParameters apep)
  189.     {
  190.         if (null == ws || null == apep) return null;

  191.         org.drip.execution.parameters.ArithmeticPriceDynamicsSettings apds =
  192.             apep.arithmeticPriceDynamicsSettings();

  193.         double dblSerialCorrelation = apds.serialCorrelation();

  194.         double dblTimeUnitSQRT = java.lang.Math.sqrt (_dblTimeInterval);

  195.         double dblExecutionRate = (_dblRightHoldings - _dblLeftHoldings) / _dblTimeInterval;

  196.         try {
  197.             double dblMarketCoreJumpUnit = apds.epochVolatility() * dblTimeUnitSQRT;

  198.             return new org.drip.execution.discrete.PriceIncrement (
  199.                 dblPreviousEquilibriumPrice,
  200.                 new org.drip.execution.evolution.MarketImpactComponent (
  201.                     _dblTimeInterval * apds.drift(),
  202.                     0.,
  203.                     _dblTimeInterval * apep.permanentExpectation().epochImpactFunction().evaluate (dblExecutionRate),
  204.                     apep.temporaryExpectation().epochImpactFunction().evaluate (dblExecutionRate)
  205.                 ),
  206.                 new org.drip.execution.evolution.MarketImpactComponent (
  207.                     dblMarketCoreJumpUnit * java.lang.Math.sqrt (1. + dblSerialCorrelation * dblSerialCorrelation) *
  208.                         ws.currentWanderer(),
  209.                     dblMarketCoreJumpUnit * dblSerialCorrelation * ws.previousWanderer(),
  210.                     dblTimeUnitSQRT * apep.permanentVolatility().epochImpactFunction().evaluate (dblExecutionRate) *
  211.                         ws.permanentImpactWanderer(),
  212.                     dblTimeUnitSQRT * apep.temporaryVolatility().epochImpactFunction().evaluate (dblExecutionRate) *
  213.                         ws.temporaryImpactWanderer()
  214.                 )
  215.             );
  216.         } catch (java.lang.Exception e) {
  217.             e.printStackTrace();
  218.         }

  219.         return null;
  220.     }

  221.     /**
  222.      * Generate the Cost Evolution Increment Unit Realization given the Walk Realization
  223.      *
  224.      * @param dblPreviousEquilibriumPrice The Previous Equilibrium Price
  225.      * @param ws Realized Walk Suite
  226.      * @param apep The Arithmetic Price Walk Evolution Parameters
  227.      *
  228.      * @return The Cost Evolution Increment Unit Realization given the Walk Realization
  229.      */

  230.     public org.drip.execution.discrete.ShortfallIncrement costIncrementRealization (
  231.         final double dblPreviousEquilibriumPrice,
  232.         final org.drip.execution.dynamics.WalkSuite ws,
  233.         final org.drip.execution.dynamics.ArithmeticPriceEvolutionParameters apep)
  234.     {
  235.         return org.drip.execution.discrete.ShortfallIncrement.Standard (
  236.             priceIncrementRealization (dblPreviousEquilibriumPrice, ws, apep),
  237.             _dblLeftHoldings,
  238.             _dblRightHoldings - _dblLeftHoldings
  239.         );
  240.     }

  241.     /**
  242.      * Generate the R^1 Normal Cost Increment Distribution
  243.      *
  244.      * @param apep The Arithmetic Price Walk Evolution Parameters
  245.      *
  246.      * @return The R^1 Normal Cost Increment Distribution
  247.      */

  248.     public org.drip.execution.discrete.ShortfallIncrementDistribution costIncrementDistribution (
  249.         final org.drip.execution.dynamics.ArithmeticPriceEvolutionParameters apep)
  250.     {
  251.         double dblTradeAmount = _dblRightHoldings > _dblLeftHoldings ? _dblRightHoldings - _dblLeftHoldings :
  252.             _dblLeftHoldings - _dblRightHoldings;
  253.         double dblExecutionRate = dblTradeAmount / _dblTimeInterval;

  254.         org.drip.execution.parameters.ArithmeticPriceDynamicsSettings apds =
  255.             apep.arithmeticPriceDynamicsSettings();

  256.         try {
  257.             double dblMarketCoreVolatility = apds.epochVolatility();

  258.             double dblTemporaryVolatility = apep.temporaryVolatility().epochImpactFunction().evaluate
  259.                 (dblTradeAmount, _dblTimeInterval);

  260.             return new org.drip.execution.discrete.ShortfallIncrementDistribution (
  261.                 _dblTimeInterval * _dblRightHoldings * apep.permanentExpectation().epochImpactFunction().evaluate
  262.                     (dblExecutionRate),
  263.                 dblTradeAmount * apep.temporaryExpectation().epochImpactFunction().evaluate (dblExecutionRate),
  264.                 -1. * _dblRightHoldings * apds.drift() * _dblTimeInterval,
  265.                 0.,
  266.                 dblExecutionRate * dblExecutionRate * dblTemporaryVolatility * dblTemporaryVolatility *
  267.                     _dblTimeInterval,
  268.                 _dblRightHoldings * _dblRightHoldings * dblMarketCoreVolatility * dblMarketCoreVolatility *
  269.                     _dblTimeInterval
  270.             );
  271.         } catch (java.lang.Exception e) {
  272.             e.printStackTrace();
  273.         }

  274.         return null;
  275.     }

  276.     @Override public org.drip.execution.sensitivity.ControlNodesGreek permanentImpactExpectation (
  277.         final org.drip.execution.dynamics.ArithmeticPriceEvolutionParameters apep)
  278.     {
  279.         if (null == apep) return null;

  280.         double dblTradeAmount = _dblRightHoldings - _dblLeftHoldings;
  281.         double dblSign = _dblRightHoldings < _dblLeftHoldings ? -1. : 1.;

  282.         org.drip.execution.impact.TransactionFunction tfPermanentDrift =
  283.             apep.permanentExpectation().epochImpactFunction();

  284.         try {
  285.             double dblPermanentDrift = tfPermanentDrift.evaluate (dblTradeAmount, _dblTimeInterval);

  286.             double dblPermanentDriftLeftJacobian = tfPermanentDrift.leftHoldingsDerivative (dblTradeAmount,
  287.                 _dblTimeInterval, 1);

  288.             double dblPermanentDriftRightJacobian = tfPermanentDrift.rightHoldingsDerivative (dblTradeAmount,
  289.                 _dblTimeInterval, 1);

  290.             double dblPermanentDriftLeftHessian = tfPermanentDrift.leftHoldingsDerivative (dblTradeAmount,
  291.                 _dblTimeInterval, 2);

  292.             double dblPermanentDriftRightHessian = tfPermanentDrift.rightHoldingsDerivative (dblTradeAmount,
  293.                 _dblTimeInterval, 2);

  294.             double dblPermanentDriftCrossHessian = tfPermanentDrift.crossHoldingsDerivative (dblTradeAmount,
  295.                 _dblTimeInterval);

  296.             double dblPermanentDriftImpact = dblSign * _dblTimeInterval * _dblRightHoldings *
  297.                 dblPermanentDrift;
  298.             double dblPermanentDriftImpactLeftJacobian = dblSign * _dblTimeInterval * _dblRightHoldings *
  299.                 dblPermanentDriftLeftJacobian;
  300.             double dblPermanentDriftImpactRightJacobian = dblSign * _dblTimeInterval * _dblRightHoldings *
  301.                 dblPermanentDriftRightJacobian + dblSign * _dblTimeInterval * dblPermanentDrift;
  302.             double dblPermanentDriftImpactLeftHessian = dblSign * _dblTimeInterval * _dblRightHoldings *
  303.                 dblPermanentDriftLeftHessian;
  304.             double dblPermanentDriftImpactRightHessian = dblSign * _dblTimeInterval * _dblRightHoldings *
  305.                 dblPermanentDriftRightHessian + 2. *  dblSign * _dblTimeInterval *
  306.                     dblPermanentDriftRightJacobian;
  307.             double dblPermanentDriftImpactCrossHessian = dblSign * _dblTimeInterval *
  308.                 dblPermanentDriftLeftJacobian + dblSign * _dblTimeInterval * _dblRightHoldings *
  309.                     dblPermanentDriftCrossHessian;

  310.             return new org.drip.execution.sensitivity.ControlNodesGreek (
  311.                 dblPermanentDriftImpact,
  312.                 new double[] {
  313.                     dblPermanentDriftImpactLeftJacobian,
  314.                     dblPermanentDriftImpactRightJacobian
  315.                 },
  316.                 new double[][] {
  317.                     {dblPermanentDriftImpactLeftHessian, dblPermanentDriftImpactCrossHessian},
  318.                     {dblPermanentDriftImpactCrossHessian, dblPermanentDriftImpactRightHessian}
  319.                 }
  320.             );
  321.         } catch (java.lang.Exception e) {
  322.             e.printStackTrace();
  323.         }

  324.         return null;
  325.     }

  326.     @Override public org.drip.execution.sensitivity.ControlNodesGreek permanentImpactVariance (
  327.         final org.drip.execution.dynamics.ArithmeticPriceEvolutionParameters apep)
  328.     {
  329.         try {
  330.             return new org.drip.execution.sensitivity.ControlNodesGreek (0., new double[] {0., 0.}, new
  331.                 double[][] {{0., 0.}, {0., 0.}});
  332.         } catch (java.lang.Exception e) {
  333.             e.printStackTrace();
  334.         }

  335.         return null;
  336.     }

  337.     @Override public org.drip.execution.sensitivity.ControlNodesGreek temporaryImpactExpectation (
  338.         final org.drip.execution.dynamics.ArithmeticPriceEvolutionParameters apep)
  339.     {
  340.         if (null == apep) return null;

  341.         double dblTradeAmount = _dblRightHoldings - _dblLeftHoldings;

  342.         org.drip.execution.impact.TransactionFunction tfTemporaryDrift =
  343.             apep.temporaryExpectation().epochImpactFunction();

  344.         try {
  345.             double dblTemporaryDrift = tfTemporaryDrift.evaluate (dblTradeAmount, _dblTimeInterval);

  346.             double dblTemporaryDriftLeftJacobian = tfTemporaryDrift.leftHoldingsDerivative (dblTradeAmount,
  347.                 _dblTimeInterval, 1);

  348.             double dblTemporaryDriftRightJacobian = tfTemporaryDrift.rightHoldingsDerivative
  349.                 (dblTradeAmount, _dblTimeInterval, 1);

  350.             double dblTemporaryDriftLeftHessian = tfTemporaryDrift.leftHoldingsDerivative (dblTradeAmount,
  351.                 _dblTimeInterval, 2);

  352.             double dblTemporaryDriftRightHessian = tfTemporaryDrift.rightHoldingsDerivative (dblTradeAmount,
  353.                 _dblTimeInterval, 2);

  354.             double dblTemporaryDriftCrossHessian = tfTemporaryDrift.crossHoldingsDerivative (dblTradeAmount,
  355.                 _dblTimeInterval);

  356.             double dblTemporaryDriftImpact = dblTradeAmount * dblTemporaryDrift;

  357.             double dblTemporaryDriftImpactLeftJacobian = -1. * dblTemporaryDrift + dblTradeAmount *
  358.                 dblTemporaryDriftLeftJacobian;
  359.             double dblTemporaryDriftImpactRightJacobian = dblTemporaryDrift + dblTradeAmount *
  360.                 dblTemporaryDriftRightJacobian;
  361.             double dblTemporaryDriftImpactLeftHessian = -2. * dblTemporaryDriftLeftJacobian + dblTradeAmount
  362.                 * dblTemporaryDriftLeftHessian;
  363.             double dblTemporaryDriftImpactRightHessian = 2. * dblTemporaryDriftRightJacobian + dblTradeAmount
  364.                 * dblTemporaryDriftRightHessian;
  365.             double dblTemporaryDriftImpactCrossHessian = -1. * dblTemporaryDriftRightJacobian +
  366.                 dblTemporaryDriftLeftJacobian + dblTradeAmount * dblTemporaryDriftCrossHessian;

  367.             return new org.drip.execution.sensitivity.ControlNodesGreek (
  368.                 dblTemporaryDriftImpact,
  369.                 new double[] {
  370.                     dblTemporaryDriftImpactLeftJacobian,
  371.                     dblTemporaryDriftImpactRightJacobian
  372.                 },
  373.                 new double[][] {
  374.                     {dblTemporaryDriftImpactLeftHessian, dblTemporaryDriftImpactCrossHessian},
  375.                     {dblTemporaryDriftImpactCrossHessian, dblTemporaryDriftImpactRightHessian}
  376.                 }
  377.             );
  378.         } catch (java.lang.Exception e) {
  379.             e.printStackTrace();
  380.         }

  381.         return null;
  382.     }

  383.     @Override public org.drip.execution.sensitivity.ControlNodesGreek temporaryImpactVariance (
  384.         final org.drip.execution.dynamics.ArithmeticPriceEvolutionParameters apep)
  385.     {
  386.         if (null == apep) return null;

  387.         double dblTradeAmount = _dblRightHoldings - _dblLeftHoldings;
  388.         double dblTradeAmountSquared = dblTradeAmount * dblTradeAmount;

  389.         org.drip.execution.impact.TransactionFunction tfTemporaryVolatility =
  390.             apep.temporaryVolatility().epochImpactFunction();

  391.         try {
  392.             if (null == tfTemporaryVolatility)
  393.                 return new org.drip.execution.sensitivity.ControlNodesGreek (0., new double[] {0., 0.}, new
  394.                     double[][] {{0., 0.}, {0., 0.}});

  395.             double dblTemporaryVolatility = tfTemporaryVolatility.evaluate (dblTradeAmount,
  396.                 _dblTimeInterval);

  397.             double dblTemporaryVolatilityLeftJacobian = tfTemporaryVolatility.leftHoldingsDerivative
  398.                 (dblTradeAmount, _dblTimeInterval, 1);

  399.             double dblTemporaryVolatilityRightJacobian = tfTemporaryVolatility.rightHoldingsDerivative
  400.                 (dblTradeAmount, _dblTimeInterval, 1);

  401.             double dblTemporaryVolatilityLeftHessian = tfTemporaryVolatility.leftHoldingsDerivative
  402.                 (dblTradeAmount, _dblTimeInterval, 2);

  403.             double dblTemporaryVolatilityRightHessian = tfTemporaryVolatility.rightHoldingsDerivative
  404.                 (dblTradeAmount, _dblTimeInterval, 2);

  405.             double dblTemporaryVolatilityCrossHessian = tfTemporaryVolatility.crossHoldingsDerivative
  406.                 (dblTradeAmount, _dblTimeInterval);

  407.             double dblTemporaryVolatilitySquared = dblTemporaryVolatility * dblTemporaryVolatility;

  408.             double dblTemporaryVarianceCrossHessian =
  409.                 2. * dblTradeAmountSquared * dblTemporaryVolatilityLeftJacobian * dblTemporaryVolatilityRightJacobian * _dblTimeInterval
  410.                     + 2. * dblTradeAmountSquared * dblTemporaryVolatility * dblTemporaryVolatilityCrossHessian * _dblTimeInterval
  411.                         + 4. * dblTradeAmount * dblTemporaryVolatility * dblTemporaryVolatilityLeftJacobian * _dblTimeInterval
  412.                             - 4. * dblTradeAmount * dblTemporaryVolatility * dblTemporaryVolatilityRightJacobian * _dblTimeInterval
  413.                                 - 2. * dblTemporaryVolatilitySquared * _dblTimeInterval;

  414.             return new org.drip.execution.sensitivity.ControlNodesGreek (
  415.                 dblTradeAmountSquared * dblTemporaryVolatilitySquared * _dblTimeInterval,
  416.                 new double[] {
  417.                     2. * dblTradeAmountSquared * dblTemporaryVolatility * dblTemporaryVolatilityLeftJacobian * _dblTimeInterval
  418.                         - 2. * dblTradeAmount * dblTemporaryVolatilitySquared * _dblTimeInterval,
  419.                     2. * dblTradeAmountSquared * dblTemporaryVolatility * dblTemporaryVolatilityRightJacobian * _dblTimeInterval
  420.                         + 2. * dblTradeAmount * dblTemporaryVolatilitySquared * _dblTimeInterval
  421.                 },
  422.                 new double[][] {
  423.                     {
  424.                         2. * dblTradeAmountSquared * dblTemporaryVolatilityLeftJacobian * dblTemporaryVolatilityLeftJacobian * _dblTimeInterval
  425.                             + 2. * dblTradeAmountSquared * dblTemporaryVolatility * dblTemporaryVolatilityLeftHessian * _dblTimeInterval
  426.                                 - 8. * dblTradeAmount * dblTemporaryVolatility * dblTemporaryVolatilityLeftJacobian * _dblTimeInterval
  427.                                     + 2. * dblTemporaryVolatilitySquared * _dblTimeInterval,
  428.                         dblTemporaryVarianceCrossHessian
  429.                     }, {
  430.                         dblTemporaryVarianceCrossHessian,
  431.                         2. * dblTradeAmountSquared * dblTemporaryVolatilityRightJacobian * dblTemporaryVolatilityRightJacobian * _dblTimeInterval
  432.                             + 2. * dblTradeAmountSquared * dblTemporaryVolatility * dblTemporaryVolatilityRightHessian * _dblTimeInterval
  433.                                 + 8. * dblTradeAmount * dblTemporaryVolatility * dblTemporaryVolatilityRightJacobian * _dblTimeInterval
  434.                                     + 2. * dblTemporaryVolatilitySquared * _dblTimeInterval
  435.                     }
  436.                 }
  437.             );
  438.         } catch (java.lang.Exception e) {
  439.             e.printStackTrace();
  440.         }

  441.         return null;
  442.     }

  443.     @Override public org.drip.execution.sensitivity.ControlNodesGreek marketDynamicsExpectation (
  444.         final org.drip.execution.dynamics.ArithmeticPriceEvolutionParameters apep)
  445.     {
  446.         if (null == apep) return null;

  447.         double dblDrift = apep.arithmeticPriceDynamicsSettings().drift();

  448.         try {
  449.             return new org.drip.execution.sensitivity.ControlNodesGreek (
  450.                 -1. * _dblTimeInterval * dblDrift * _dblRightHoldings,
  451.                 new double[] {
  452.                     0.,
  453.                     -1. * _dblTimeInterval * dblDrift
  454.                 }, new
  455.                 double[][] {
  456.                     {0., 0.},
  457.                     {0., 0.}
  458.                 }
  459.             );
  460.         } catch (java.lang.Exception e) {
  461.             e.printStackTrace();
  462.         }

  463.         return null;
  464.     }

  465.     @Override public org.drip.execution.sensitivity.ControlNodesGreek marketDynamicsVariance (
  466.         final org.drip.execution.dynamics.ArithmeticPriceEvolutionParameters apep)
  467.     {
  468.         if (null == apep) return null;

  469.         try {
  470.             double dblVolatility = apep.arithmeticPriceDynamicsSettings().epochVolatility();

  471.             return new org.drip.execution.sensitivity.ControlNodesGreek (
  472.                 _dblTimeInterval * dblVolatility * dblVolatility * _dblRightHoldings * _dblRightHoldings,
  473.                 new double[] {
  474.                     0.,
  475.                     2. * _dblTimeInterval * dblVolatility * dblVolatility * _dblRightHoldings
  476.                 }, new double[][] {
  477.                     {0., 0.},
  478.                     {0., 2. * _dblTimeInterval * dblVolatility * dblVolatility}
  479.                 }
  480.             );
  481.         } catch (java.lang.Exception e) {
  482.             e.printStackTrace();
  483.         }

  484.         return null;
  485.     }

  486.     @Override public org.drip.execution.sensitivity.ControlNodesGreek expectationContribution (
  487.         final org.drip.execution.dynamics.ArithmeticPriceEvolutionParameters apep)
  488.     {
  489.         org.drip.execution.sensitivity.ControlNodesGreek cngPermanentImpact = permanentImpactExpectation
  490.             (apep);

  491.         if (null == cngPermanentImpact) return null;

  492.         org.drip.execution.sensitivity.ControlNodesGreek cngTemporaryImpact = temporaryImpactExpectation
  493.             (apep);

  494.         if (null == cngTemporaryImpact) return null;

  495.         org.drip.execution.sensitivity.ControlNodesGreek cngMarketCore = marketDynamicsExpectation (apep);

  496.         if (null == cngMarketCore) return null;

  497.         double[][] aadblPermanentImpactExpectationHessian = cngPermanentImpact.hessian();

  498.         double[][] aadblTemporaryImpactExpectationHessian = cngTemporaryImpact.hessian();

  499.         double[] adblPermanentImpactExpectationJacobian = cngPermanentImpact.jacobian();

  500.         double[] adblTemporaryImpactExpectationJacobian = cngTemporaryImpact.jacobian();

  501.         double[][] aadblMarketCoreExpectationHessian = cngMarketCore.hessian();

  502.         double[] adblMarketCoreExpectationJacobian = cngMarketCore.jacobian();

  503.         int iNumSliceNode = adblMarketCoreExpectationJacobian.length;
  504.         double[][] aadblHessian = new double[iNumSliceNode][iNumSliceNode];
  505.         double[] adblJacobian = new double[iNumSliceNode];

  506.         for (int i = 0; i < iNumSliceNode; ++i) {
  507.             adblJacobian[i] = adblPermanentImpactExpectationJacobian[i] +
  508.                 adblTemporaryImpactExpectationJacobian[i] + adblMarketCoreExpectationJacobian[i];

  509.             for (int j = 0; j < iNumSliceNode; ++j)
  510.                 aadblHessian[i][j] = aadblPermanentImpactExpectationHessian[i][j] +
  511.                     aadblTemporaryImpactExpectationHessian[i][j] + aadblMarketCoreExpectationHessian[i][j];
  512.         }

  513.         try {
  514.             return new org.drip.execution.sensitivity.ControlNodesGreek (cngPermanentImpact.value() +
  515.                 cngTemporaryImpact.value() + cngMarketCore.value(), adblJacobian, aadblHessian);
  516.         } catch (java.lang.Exception e) {
  517.             e.printStackTrace();
  518.         }

  519.         return null;
  520.     }

  521.     @Override public org.drip.execution.sensitivity.ControlNodesGreek varianceContribution (
  522.         final org.drip.execution.dynamics.ArithmeticPriceEvolutionParameters apep)
  523.     {
  524.         org.drip.execution.sensitivity.ControlNodesGreek cngPermanentImpact = permanentImpactVariance (apep);

  525.         if (null == cngPermanentImpact) return null;

  526.         org.drip.execution.sensitivity.ControlNodesGreek cngTemporaryImpact = temporaryImpactVariance (apep);

  527.         if (null == cngTemporaryImpact) return null;

  528.         org.drip.execution.sensitivity.ControlNodesGreek cngMarketCore = marketDynamicsVariance (apep);

  529.         if (null == cngMarketCore) return null;

  530.         double[][] aadblPermanentImpactExpectationHessian = cngPermanentImpact.hessian();

  531.         double[][] aadblTemporaryImpactExpectationHessian = cngTemporaryImpact.hessian();

  532.         double[] adblPermanentImpactExpectationJacobian = cngPermanentImpact.jacobian();

  533.         double[] adblTemporaryImpactExpectationJacobian = cngTemporaryImpact.jacobian();

  534.         double[][] aadblMarketCoreExpectationHessian = cngMarketCore.hessian();

  535.         double[] adblMarketCoreExpectationJacobian = cngMarketCore.jacobian();

  536.         int iNumSliceNode = adblMarketCoreExpectationJacobian.length;
  537.         double[][] aadblHessian = new double[iNumSliceNode][iNumSliceNode];
  538.         double[] adblJacobian = new double[iNumSliceNode];

  539.         for (int i = 0; i < iNumSliceNode; ++i) {
  540.             adblJacobian[i] = adblPermanentImpactExpectationJacobian[i] +
  541.                 adblTemporaryImpactExpectationJacobian[i] + adblMarketCoreExpectationJacobian[i];

  542.             for (int j = 0; j < iNumSliceNode; ++j)
  543.                 aadblHessian[i][j] = aadblPermanentImpactExpectationHessian[i][j] +
  544.                     aadblTemporaryImpactExpectationHessian[i][j] + aadblMarketCoreExpectationHessian[i][j];
  545.         }

  546.         try {
  547.             return new org.drip.execution.sensitivity.ControlNodesGreek (cngPermanentImpact.value() +
  548.                 cngTemporaryImpact.value() + cngMarketCore.value(), adblJacobian, aadblHessian);
  549.         } catch (java.lang.Exception e) {
  550.             e.printStackTrace();
  551.         }

  552.         return null;
  553.     }

  554.     /**
  555.      * Estimate the Optimal Adjustment Attributable to the Serial Correlation
  556.      *  
  557.      * @param apep The Arithmetic Price Walk Parameters
  558.      *
  559.      * @return The Optimal Adjustment Attributable to the Serial Correlation
  560.      */

  561.     public org.drip.execution.discrete.OptimalSerialCorrelationAdjustment serialCorrelationAdjustment (
  562.         final org.drip.execution.dynamics.ArithmeticPriceEvolutionParameters apep)
  563.     {
  564.         if (null == apep) return null;

  565.         org.drip.execution.parameters.ArithmeticPriceDynamicsSettings apds =
  566.             apep.arithmeticPriceDynamicsSettings();

  567.         double dblTradeRate = (_dblRightHoldings - _dblLeftHoldings) / _dblTimeInterval;

  568.         org.drip.execution.impact.TransactionFunction miTemporary =
  569.             apep.temporaryExpectation().epochImpactFunction();

  570.         try {
  571.             double dblRhoSigma = apds.serialCorrelation() * apds.epochVolatility();

  572.             double dblDenominator = 1. / (dblTradeRate * miTemporary.derivative (dblTradeRate, 2) + 2. *
  573.                 miTemporary.derivative (dblTradeRate, 1));

  574.             return new org.drip.execution.discrete.OptimalSerialCorrelationAdjustment (dblDenominator *
  575.                 dblRhoSigma * java.lang.Math.pow (_dblTimeInterval, 1.5), 0.5 * dblDenominator * dblRhoSigma
  576.                     * dblRhoSigma * _dblTimeInterval * _dblTimeInterval);
  577.         } catch (java.lang.Exception e) {
  578.             e.printStackTrace();
  579.         }

  580.         return null;
  581.     }
  582. }