LinearTemporaryImpact.java

  1. package org.drip.execution.cost;

  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>LinearTemporaryImpact</i> computes and holds the Optimal Trajectory using the Linear Temporary Impact
  79.  * Function for the given set of Inputs. It provides a Default Unconstrained Trajectory Implementation. The
  80.  * References are:
  81.  *
  82.  * <br><br>
  83.  *  <ul>
  84.  *      <li>
  85.  *          Bertsimas, D., and A. W. Lo (1998): Optimal Control of Execution Costs <i>Journal of Financial
  86.  *              Markets</i> <b>1</b> 1-50
  87.  *      </li>
  88.  *      <li>
  89.  *          Almgren, R., and N. Chriss (2000): Optimal Execution of Portfolio Transactions <i>Journal of
  90.  *              Risk</i> <b>3 (2)</b> 5-39
  91.  *      </li>
  92.  *      <li>
  93.  *          Brunnermeier, L. K., and L. H. Pedersen (2005): Predatory Trading <i>Journal of Finance</i> <b>60
  94.  *              (4)</b> 1825-1863
  95.  *      </li>
  96.  *      <li>
  97.  *          Almgren, R., and J. Lorenz (2006): Bayesian Adaptive Trading with a Daily Cycle <i>Journal of
  98.  *              Trading</i> <b>1 (4)</b> 38-46
  99.  *      </li>
  100.  *      <li>
  101.  *          Kissell, R., and R. Malamut (2007): Algorithmic Decision Making Framework <i>Journal of
  102.  *              Trading</i> <b>1 (1)</b> 12-21
  103.  *      </li>
  104.  *  </ul>
  105.  *
  106.  *  <br><br>
  107.  *  <ul>
  108.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ProductCore.md">Product Core Module</a></li>
  109.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/TransactionCostAnalyticsLibrary.md">Transaction Cost Analytics</a></li>
  110.  *      <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>
  111.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/execution/cost/README.md">Linear Temporary Market Impact Cost</a></li>
  112.  *  </ul>
  113.  *
  114.  * @author Lakshmi Krishnamurthy
  115.  */

  116. public class LinearTemporaryImpact {
  117.     private double _dblSpotTime = java.lang.Double.NaN;
  118.     private double _dblFinishTime = java.lang.Double.NaN;
  119.     private double _dblSpotHoldings = java.lang.Double.NaN;
  120.     private double _dblGrossPriceChange = java.lang.Double.NaN;
  121.     private double _dblTransactionCostGain = java.lang.Double.NaN;
  122.     private double _dblStaticTransactionCost = java.lang.Double.NaN;
  123.     private double _dblInstantaneousTradeRate = java.lang.Double.NaN;
  124.     private org.drip.execution.bayesian.PriorConditionalCombiner _pcc = null;
  125.     private org.drip.execution.impact.TransactionFunctionLinear _tflTemporary = null;
  126.     private org.drip.execution.optimum.EfficientTradingTrajectoryContinuous _ectt = null;

  127.     /**
  128.      * Generate an Unconstrained LinearTemporaryImpact Instance
  129.      *
  130.      * @param dblSpotTime Spot Time
  131.      * @param dblFinishTime Finish Time
  132.      * @param dblSpotHoldings Spot Holdings
  133.      * @param pcc The Prior/Conditional Combiner
  134.      * @param dblGrossPriceChange The Gross Price Change
  135.      * @param tflTemporary The Temporary Linear Impact Function
  136.      *
  137.      * @return The Unconstrained LinearTemporaryImpact Instance
  138.      */

  139.     public static final LinearTemporaryImpact Unconstrained (
  140.         final double dblSpotTime,
  141.         final double dblFinishTime,
  142.         final double dblSpotHoldings,
  143.         final org.drip.execution.bayesian.PriorConditionalCombiner pcc,
  144.         final double dblGrossPriceChange,
  145.         final org.drip.execution.impact.TransactionFunctionLinear tflTemporary)
  146.     {
  147.         if (!org.drip.numerical.common.NumberUtil.IsValid (dblSpotTime) ||
  148.             !org.drip.numerical.common.NumberUtil.IsValid (dblFinishTime) ||
  149.                 !org.drip.numerical.common.NumberUtil.IsValid (dblSpotHoldings) || null == pcc || null ==
  150.                     tflTemporary)
  151.             return null;

  152.         final double dblHorizon = dblFinishTime - dblSpotTime;

  153.         org.drip.measure.gaussian.R1UnivariateNormal r1unPosterior = pcc.posteriorDriftDistribution
  154.             (dblGrossPriceChange);

  155.         if (null == r1unPosterior) return null;

  156.         final double dblScaledDrift = 0.25 * r1unPosterior.mean() / tflTemporary.slope();

  157.         org.drip.function.definition.R1ToR1 r1ToR1Holdings = new org.drip.function.definition.R1ToR1 (null) {
  158.             @Override public double evaluate (
  159.                 final double dblTau)
  160.                 throws java.lang.Exception
  161.             {
  162.                 if (0. >= dblHorizon) return 0.;

  163.                 if (!org.drip.numerical.common.NumberUtil.IsValid (dblTau))
  164.                     throw new java.lang.Exception
  165.                         ("LinearTemporaryImpact::Holdings::evaluate => Invalid Inputs");

  166.                 if (dblTau <= dblSpotTime) return dblSpotHoldings;

  167.                 if (dblTau >= dblFinishTime) return 0.;

  168.                 return (dblFinishTime - dblTau) * (dblSpotHoldings / (dblFinishTime - dblSpotTime) -
  169.                     dblScaledDrift * (dblTau - dblSpotTime));
  170.             }
  171.         };

  172.         try {
  173.             return new LinearTemporaryImpact (dblSpotTime, dblFinishTime, dblSpotHoldings, pcc,
  174.                 dblGrossPriceChange, tflTemporary, dblFinishTime - dblSpotTime, r1ToR1Holdings, 0 >=
  175.                     dblHorizon ? 0. : dblSpotHoldings / dblHorizon + dblScaledDrift * dblHorizon);
  176.         } catch (java.lang.Exception e) {
  177.             e.printStackTrace();
  178.         }

  179.         return null;
  180.     }

  181.     protected LinearTemporaryImpact (
  182.         final double dblSpotTime,
  183.         final double dblFinishTime,
  184.         final double dblSpotHoldings,
  185.         final org.drip.execution.bayesian.PriorConditionalCombiner pcc,
  186.         final double dblGrossPriceChange,
  187.         final org.drip.execution.impact.TransactionFunctionLinear tflTemporary,
  188.         final double dblCharacteristicTime,
  189.         final org.drip.function.definition.R1ToR1 r1ToR1Holdings,
  190.         final double dblInstantaneousTradeRate)
  191.         throws java.lang.Exception
  192.     {
  193.         if (!org.drip.numerical.common.NumberUtil.IsValid (_dblSpotTime = dblSpotTime) ||
  194.             !org.drip.numerical.common.NumberUtil.IsValid (_dblFinishTime = dblFinishTime) ||
  195.                 !org.drip.numerical.common.NumberUtil.IsValid (_dblSpotHoldings = dblSpotHoldings) || null ==
  196.                     (_pcc = pcc) || !org.drip.numerical.common.NumberUtil.IsValid (_dblGrossPriceChange =
  197.                         dblGrossPriceChange) || null == (_tflTemporary = tflTemporary) || null ==
  198.                             r1ToR1Holdings || !org.drip.numerical.common.NumberUtil.IsValid
  199.                                 (_dblInstantaneousTradeRate = dblInstantaneousTradeRate))
  200.             throw new java.lang.Exception ("LinearTemporaryImpact Constructor => Invalid Inputs");

  201.         final double dblLiquidityCoefficient = _tflTemporary.slope();

  202.         double dblDriftEstimate = _pcc.posteriorDriftDistribution (_dblGrossPriceChange).mean();

  203.         final double dblExecutionTime = _dblFinishTime - _dblSpotTime;
  204.         _dblStaticTransactionCost = _dblSpotHoldings * _dblSpotHoldings * dblLiquidityCoefficient /
  205.             dblExecutionTime + 0.5 * _dblSpotHoldings * dblDriftEstimate * dblExecutionTime -
  206.                 dblExecutionTime * dblExecutionTime * dblExecutionTime * dblDriftEstimate * dblDriftEstimate
  207.                     / (48. * dblLiquidityCoefficient);

  208.         double dblDriftConfidence = _pcc.prior().confidence();

  209.         final double dblPriceVolatility = _pcc.conditional().priceVolatility();

  210.         org.drip.function.definition.R1ToR1 r1ToR1HoldingsSquared = new org.drip.function.definition.R1ToR1
  211.             (null) {
  212.             @Override public double evaluate (
  213.                 final double dblTime)
  214.                 throws java.lang.Exception
  215.             {
  216.                 double dblHoldings = r1ToR1Holdings.evaluate (dblTime);

  217.                 return dblHoldings * dblHoldings;
  218.             }
  219.         };

  220.         final double dblRho = dblPriceVolatility * dblPriceVolatility / (dblDriftConfidence *
  221.             dblDriftConfidence * dblExecutionTime);

  222.         org.drip.function.definition.R1ToR1 r1ToR1Quadrature = new org.drip.function.definition.R1ToR1 (null)
  223.         {
  224.             @Override public double evaluate (
  225.                 final double dblDelta)
  226.                 throws java.lang.Exception
  227.             {
  228.                 if (!org.drip.numerical.common.NumberUtil.IsValid (dblDelta))
  229.                     throw new java.lang.Exception
  230.                         ("LinearTemporaryImpact::r1ToR1Quadrature::evaluate => Invalid Inputs");

  231.                 double dblRemainingTime = 1. - dblDelta;
  232.                 double dblDimensionlessTime = dblDelta + dblRho;
  233.                 return dblRemainingTime * dblRemainingTime * dblRemainingTime / (dblDimensionlessTime *
  234.                     dblDimensionlessTime);
  235.             }
  236.         };

  237.         _dblTransactionCostGain = dblPriceVolatility * dblPriceVolatility * dblExecutionTime *
  238.             dblExecutionTime / (48. * linearTemporaryImpact().slope()) * r1ToR1Quadrature.integrate (0., 1.);

  239.         org.drip.function.definition.R1ToR1 r1ToR1TradeRate = new org.drip.function.definition.R1ToR1 (null)
  240.         {
  241.             @Override public double evaluate (
  242.                 final double dblS)
  243.                 throws java.lang.Exception
  244.             {
  245.                 return r1ToR1Holdings.derivative (dblS, 1);
  246.             }
  247.         };

  248.         final org.drip.function.definition.R1ToR1 r1ToR1TransactionCostExpectationRate = new
  249.             org.drip.function.definition.R1ToR1 (null) {
  250.             @Override public double evaluate (
  251.                 final double dblTime)
  252.                 throws java.lang.Exception
  253.             {
  254.                 double dblTradeRate = r1ToR1Holdings.derivative (dblTime, 1);

  255.                 return dblLiquidityCoefficient * dblLiquidityCoefficient * dblTradeRate * dblTradeRate;
  256.             }
  257.         };

  258.         org.drip.function.definition.R1ToR1 r1ToR1TransactionCostExpectation = new
  259.             org.drip.function.definition.R1ToR1 (null) {
  260.             @Override public double evaluate (
  261.                 final double dblTime)
  262.                 throws java.lang.Exception
  263.             {
  264.                 return r1ToR1TransactionCostExpectationRate.integrate (dblTime, dblExecutionTime);
  265.             }
  266.         };

  267.         final org.drip.function.definition.R1ToR1 r1ToR1TransactionCostVarianceRate = new
  268.             org.drip.function.definition.R1ToR1 (null) {
  269.             @Override public double evaluate (
  270.                 final double dblTime)
  271.                 throws java.lang.Exception
  272.             {
  273.                 double dblHoldings = r1ToR1Holdings.evaluate (dblTime);

  274.                 return dblPriceVolatility * dblPriceVolatility * dblHoldings * dblHoldings;
  275.             }
  276.         };

  277.         org.drip.function.definition.R1ToR1 r1ToR1TransactionCostVariance = new
  278.             org.drip.function.definition.R1ToR1 (null) {
  279.             @Override public double evaluate (
  280.                 final double dblTime)
  281.                 throws java.lang.Exception
  282.             {
  283.                 return r1ToR1TransactionCostVarianceRate.integrate (dblTime, dblExecutionTime);
  284.             }
  285.         };

  286.         _ectt = new org.drip.execution.optimum.EfficientTradingTrajectoryContinuous (dblExecutionTime,
  287.             _dblStaticTransactionCost + _dblTransactionCostGain, dblPriceVolatility * dblPriceVolatility *
  288.                 r1ToR1HoldingsSquared.integrate (_dblSpotTime, _dblFinishTime), dblCharacteristicTime,
  289.                     dblLiquidityCoefficient * _dblSpotHoldings / (dblExecutionTime * dblLiquidityCoefficient
  290.                         * java.lang.Math.sqrt (dblExecutionTime)), r1ToR1Holdings, r1ToR1TradeRate,
  291.                             r1ToR1TransactionCostExpectation, r1ToR1TransactionCostVariance);
  292.     }

  293.     /**
  294.      * Retrieve the Spot Time
  295.      *
  296.      * @return The Spot Time
  297.      */

  298.     public double spotTime()
  299.     {
  300.         return _dblSpotTime;
  301.     }

  302.     /**
  303.      * Retrieve the Finish Time
  304.      *
  305.      * @return The Finish Time
  306.      */

  307.     public double finishTime()
  308.     {
  309.         return _dblFinishTime;
  310.     }

  311.     /**
  312.      * Retrieve the Spot Holdings
  313.      *
  314.      * @return The Spot Holdings
  315.      */

  316.     public double spotHoldings()
  317.     {
  318.         return _dblSpotHoldings;
  319.     }

  320.     /**
  321.      * Retrieve the Prior/Conditional Distributions Combiner
  322.      *
  323.      * @return The Prior/Conditional Distributions Combiner
  324.      */

  325.     public org.drip.execution.bayesian.PriorConditionalCombiner combiner()
  326.     {
  327.         return _pcc;
  328.     }

  329.     /**
  330.      * Retrieve the Gross Price Change
  331.      *
  332.      * @return The Gross Price Change
  333.      */

  334.     public double grossPriceChange()
  335.     {
  336.         return _dblGrossPriceChange;
  337.     }

  338.     /**
  339.      * Retrieve the Drift Expectation Estimate
  340.      *
  341.      * @return The Drift Expectation Estimate
  342.      */

  343.     public double driftExpectationEstimate()
  344.     {
  345.         return _pcc.posteriorDriftDistribution (_dblGrossPriceChange).mean();
  346.     }

  347.     /**
  348.      * Retrieve the Drift Volatility Estimate
  349.      *
  350.      * @return The Drift Volatility Estimate
  351.      */

  352.     public double driftVolatilityEstimate()
  353.     {
  354.         return java.lang.Math.sqrt (_pcc.posteriorDriftDistribution (_dblGrossPriceChange).variance());
  355.     }

  356.     /**
  357.      * Retrieve the Linear Temporary Market Impact Function
  358.      *
  359.      * @return The Linear Temporary Market Impact Function
  360.      */

  361.     public org.drip.execution.impact.TransactionFunctionLinear linearTemporaryImpact()
  362.     {
  363.         return _tflTemporary;
  364.     }

  365.     /**
  366.      * Retrieve the Holdings Trajectory
  367.      *
  368.      * @return The Holdings Trajectory
  369.      */

  370.     public org.drip.execution.optimum.EfficientTradingTrajectoryContinuous trajectory()
  371.     {
  372.         return _ectt;
  373.     }

  374.     /**
  375.      * Retrieve the Instantaneous Trade Rate
  376.      *
  377.      * @return The Instantaneous Trade Rate
  378.      */

  379.     public double instantaneousTradeRate()
  380.     {
  381.         return _dblInstantaneousTradeRate;
  382.     }

  383.     /**
  384.      * Estimate of the Static Transaction Cost
  385.      *
  386.      * @return The Static Transaction Cost Estimate
  387.      */

  388.     public double staticTransactionCost()
  389.     {
  390.         return _dblStaticTransactionCost;
  391.     }

  392.     /**
  393.      * Estimate the Transaction Cost Gain available from the Bayesian Drift
  394.      *
  395.      * @return The Transaction Cost Gain available from the Bayesian Drift
  396.      */

  397.     public double transactionCostGain()
  398.     {
  399.         return _dblTransactionCostGain;
  400.     }
  401. }