OptimalTrajectoryNoDrift.java

  1. package org.drip.sample.almgrenchriss;

  2. import org.drip.execution.capture.*;
  3. import org.drip.execution.dynamics.*;
  4. import org.drip.execution.impact.*;
  5. import org.drip.execution.nonadaptive.DiscreteAlmgrenChriss;
  6. import org.drip.execution.optimum.AlmgrenChrissDiscrete;
  7. import org.drip.execution.parameters.*;
  8. import org.drip.execution.profiletime.UniformParticipationRateLinear;
  9. import org.drip.function.r1tor1.FlatUnivariate;
  10. import org.drip.measure.gaussian.R1UnivariateNormal;
  11. import org.drip.numerical.common.FormatUtil;
  12. import org.drip.service.env.EnvManager;

  13. /*
  14.  * -*- mode: java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  15.  */

  16. /*!
  17.  * Copyright (C) 2020 Lakshmi Krishnamurthy
  18.  * Copyright (C) 2019 Lakshmi Krishnamurthy
  19.  * Copyright (C) 2018 Lakshmi Krishnamurthy
  20.  * Copyright (C) 2017 Lakshmi Krishnamurthy
  21.  * Copyright (C) 2016 Lakshmi Krishnamurthy
  22.  *
  23.  *  This file is part of DROP, an open-source library targeting analytics/risk, transaction cost analytics,
  24.  *      asset liability management analytics, capital, exposure, and margin analytics, valuation adjustment
  25.  *      analytics, and portfolio construction analytics within and across fixed income, credit, commodity,
  26.  *      equity, FX, and structured products. It also includes auxiliary libraries for algorithm support,
  27.  *      numerical analysis, numerical optimization, spline builder, model validation, statistical learning,
  28.  *      and computational support.
  29.  *  
  30.  *      https://lakshmidrip.github.io/DROP/
  31.  *  
  32.  *  DROP is composed of three modules:
  33.  *  
  34.  *  - DROP Product Core - https://lakshmidrip.github.io/DROP-Product-Core/
  35.  *  - DROP Portfolio Core - https://lakshmidrip.github.io/DROP-Portfolio-Core/
  36.  *  - DROP Computational Core - https://lakshmidrip.github.io/DROP-Computational-Core/
  37.  *
  38.  *  DROP Product Core implements libraries for the following:
  39.  *  - Fixed Income Analytics
  40.  *  - Loan Analytics
  41.  *  - Transaction Cost Analytics
  42.  *
  43.  *  DROP Portfolio Core implements libraries for the following:
  44.  *  - Asset Allocation Analytics
  45.  *  - Asset Liability Management Analytics
  46.  *  - Capital Estimation Analytics
  47.  *  - Exposure Analytics
  48.  *  - Margin Analytics
  49.  *  - XVA Analytics
  50.  *
  51.  *  DROP Computational Core implements libraries for the following:
  52.  *  - Algorithm Support
  53.  *  - Computation Support
  54.  *  - Function Analysis
  55.  *  - Model Validation
  56.  *  - Numerical Analysis
  57.  *  - Numerical Optimizer
  58.  *  - Spline Builder
  59.  *  - Statistical Learning
  60.  *
  61.  *  Documentation for DROP is Spread Over:
  62.  *
  63.  *  - Main                     => https://lakshmidrip.github.io/DROP/
  64.  *  - Wiki                     => https://github.com/lakshmiDRIP/DROP/wiki
  65.  *  - GitHub                   => https://github.com/lakshmiDRIP/DROP
  66.  *  - Repo Layout Taxonomy     => https://github.com/lakshmiDRIP/DROP/blob/master/Taxonomy.md
  67.  *  - Javadoc                  => https://lakshmidrip.github.io/DROP/Javadoc/index.html
  68.  *  - Technical Specifications => https://github.com/lakshmiDRIP/DROP/tree/master/Docs/Internal
  69.  *  - Release Versions         => https://lakshmidrip.github.io/DROP/version.html
  70.  *  - Community Credits        => https://lakshmidrip.github.io/DROP/credits.html
  71.  *  - Issues Catalog           => https://github.com/lakshmiDRIP/DROP/issues
  72.  *  - JUnit                    => https://lakshmidrip.github.io/DROP/junit/index.html
  73.  *  - Jacoco                   => https://lakshmidrip.github.io/DROP/jacoco/index.html
  74.  *
  75.  *  Licensed under the Apache License, Version 2.0 (the "License");
  76.  *      you may not use this file except in compliance with the License.
  77.  *  
  78.  *  You may obtain a copy of the License at
  79.  *      http://www.apache.org/licenses/LICENSE-2.0
  80.  *  
  81.  *  Unless required by applicable law or agreed to in writing, software
  82.  *      distributed under the License is distributed on an "AS IS" BASIS,
  83.  *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  84.  *  
  85.  *  See the License for the specific language governing permissions and
  86.  *      limitations under the License.
  87.  */

  88. /**
  89.  * <i>OptimalTrajectoryNoDrift</i> demonstrates the Generation of the Optimal Trading Trajectory in
  90.  * accordance with the Specification of Almgren and Chriss (2000) for the given Risk Aversion Parameter
  91.  * without the Asset Drift. The References are:
  92.  *
  93.  * <br><br>
  94.  *  <ul>
  95.  *      <li>
  96.  *          Almgren, R., and N. Chriss (1999): Value under Liquidation <i>Risk</i> <b>12 (12)</b>
  97.  *      </li>
  98.  *      <li>
  99.  *          Almgren, R., and N. Chriss (2000): Optimal Execution of Portfolio Transactions <i>Journal of
  100.  *              Risk</i> <b>3 (2)</b> 5-39
  101.  *      </li>
  102.  *      <li>
  103.  *          Bertsimas, D., and A. W. Lo (1998): Optimal Control of Execution Costs <i>Journal of Financial
  104.  *              Markets</i> <b>1</b> 1-50
  105.  *      </li>
  106.  *      <li>
  107.  *          Chan, L. K. C., and J. Lakonishak (1995): The Behavior of Stock Prices around Institutional
  108.  *              Trades <i>Journal of Finance</i> <b>50</b> 1147-1174
  109.  *      </li>
  110.  *      <li>
  111.  *          Keim, D. B., and A. Madhavan (1997): Transaction Costs and Investment Style: An Inter-exchange
  112.  *              Analysis of Institutional Equity Trades <i>Journal of Financial Economics</i> <b>46</b>
  113.  *              265-292
  114.  *      </li>
  115.  *  </ul>
  116.  *
  117.  * <br><br>
  118.  *  <ul>
  119.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ProductCore.md">Product Core Module</a></li>
  120.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/TransactionCostAnalyticsLibrary.md">Transaction Cost Analytics</a></li>
  121.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/README.md">DROP API Construction and Usage</a></li>
  122.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/almgrenchriss/README.md">Almgren Chriss Efficient Frontier Trajectories</a></li>
  123.  *  </ul>
  124.  * <br><br>
  125.  *
  126.  * @author Lakshmi Krishnamurthy
  127.  */

  128. public class OptimalTrajectoryNoDrift {

  129.     public static void main (
  130.         final String[] astrArgs)
  131.         throws Exception
  132.     {
  133.         EnvManager.InitEnv (
  134.             "",
  135.             true
  136.         );


  137.         double dblS0 = 50.;
  138.         double dblX = 1000000.;
  139.         double dblT = 5.;
  140.         int iN = 5;
  141.         double dblAnnualVolatility = 0.30;
  142.         double dblAnnualReturns = 0.10;
  143.         double dblBidAsk = 0.125;
  144.         double dblDailyVolume = 5.e06;
  145.         double dblDailyVolumePermanentImpact = 0.1;
  146.         double dblDailyVolumeTemporaryImpact = 0.01;
  147.         double dblLambdaU = 1.e-06;

  148.         ArithmeticPriceDynamicsSettings apds = ArithmeticPriceDynamicsSettings.FromAnnualReturnsSettings (
  149.             dblAnnualReturns,
  150.             dblAnnualVolatility,
  151.             0.,
  152.             dblS0
  153.         );

  154.         double dblAlpha = apds.drift();

  155.         double dblSigma = apds.epochVolatility();

  156.         PriceMarketImpactLinear pmil = new PriceMarketImpactLinear (
  157.             new AssetTransactionSettings (
  158.                 dblS0,
  159.                 dblDailyVolume,
  160.                 dblBidAsk
  161.             ),
  162.             dblDailyVolumePermanentImpact,
  163.             dblDailyVolumeTemporaryImpact
  164.         );

  165.         ParticipationRateLinear prlPermanent = (ParticipationRateLinear) pmil.permanentTransactionFunction();

  166.         ParticipationRateLinear prlTemporary = (ParticipationRateLinear) pmil.temporaryTransactionFunction();

  167.         LinearPermanentExpectationParameters lpep = ArithmeticPriceEvolutionParametersBuilder.LinearExpectation (
  168.             new ArithmeticPriceDynamicsSettings (
  169.                 0.,
  170.                 new FlatUnivariate (dblSigma),
  171.                 0.
  172.             ),
  173.             new UniformParticipationRateLinear (prlPermanent),
  174.             new UniformParticipationRateLinear (prlTemporary)
  175.         );

  176.         DiscreteAlmgrenChriss dac = DiscreteAlmgrenChriss.Standard (
  177.             dblX,
  178.             dblT,
  179.             iN,
  180.             lpep,
  181.             dblLambdaU
  182.         );

  183.         AlmgrenChrissDiscrete acd = (AlmgrenChrissDiscrete) dac.generate();

  184.         double[] adblExecutionTimeNode = acd.executionTimeNode();

  185.         double[] adblTradeList = acd.tradeList();

  186.         double[] adblHoldings = acd.holdings();

  187.         LinearImpactTrajectoryEstimator lite = new LinearImpactTrajectoryEstimator (acd);

  188.         TrajectoryShortfallAggregate tsa = lite.totalCostDistributionDetail (lpep);

  189.         double[] adblIncrementalPermanentImpact = tsa.incrementalPermanentImpactExpectation();

  190.         double[] adblIncrementalTemporaryImpact = tsa.incrementalTemporaryImpactExpectation();

  191.         double[] adblCumulativePermanentImpact = tsa.cumulativePermanentImpactExpectation();

  192.         double[] adblCumulativeTemporaryImpact = tsa.cumulativeTemporaryImpactExpectation();

  193.         double[] adblIncrementalShortfallVariance = tsa.incrementalVariance();

  194.         double[] adblCumulativeShortfallVariance = tsa.cumulativeVariance();

  195.         double[] adblIncrementalShortfallMean = tsa.incrementalExpectation();

  196.         double[] adblCumulativeShortfallMean = tsa.cumulativeExpectation();

  197.         R1UnivariateNormal r1un = lite.totalCostDistributionSynopsis (lpep);

  198.         System.out.println ("\n\t|---------------------------------------------||");

  199.         System.out.println ("\t| ALMGREN-CHRISS TRAJECTORY GENERATOR INPUTS  ||");

  200.         System.out.println ("\t|---------------------------------------------||");

  201.         System.out.println ("\t| Initial Stock Price           : " + dblS0);

  202.         System.out.println ("\t| Initial Holdings              : " + dblX);

  203.         System.out.println ("\t| Liquidation Time              : " + dblT);

  204.         System.out.println ("\t| Number of Time Periods        : " + iN);

  205.         System.out.println ("\t| Annual Volatility             :" + FormatUtil.FormatDouble (dblAnnualVolatility, 1, 0, 100.) + "%");

  206.         System.out.println ("\t| Annual Growth                 :" + FormatUtil.FormatDouble (dblAnnualReturns, 1, 0, 100.) + "%");

  207.         System.out.println ("\t| Bid-Ask Spread                : " + dblBidAsk);

  208.         System.out.println ("\t| Daily Volume                  : " + dblDailyVolume);

  209.         System.out.println ("\t| Daily Volume Temporary Impact : " + dblDailyVolumeTemporaryImpact);

  210.         System.out.println ("\t| Daily Volume Permanent Impact : " + dblDailyVolumePermanentImpact);

  211.         System.out.println ("\t| Daily Volume 5 million Shares : " + prlPermanent.slope());

  212.         System.out.println ("\t| Static Holdings 11,000 Shares : " + dblLambdaU);

  213.         System.out.println ("\t|");

  214.         System.out.println (
  215.             "\t| Daily Volatility              : " +
  216.             FormatUtil.FormatDouble (dblSigma, 1, 4, 1.)
  217.         );

  218.         System.out.println (
  219.             "\t| Daily Returns                 : " +
  220.             FormatUtil.FormatDouble (dblAlpha, 1, 4, 1.)
  221.         );

  222.         System.out.println ("\t| Temporary Impact Fixed Offset :  " + prlTemporary.offset());

  223.         System.out.println ("\t| Eta                           :  " + prlTemporary.slope());

  224.         System.out.println ("\t| Gamma                         :  " + prlPermanent.slope());

  225.         System.out.println ("\t|---------------------------------------------||");

  226.         System.out.println ("\n\t|-----------------------------||");

  227.         System.out.println ("\t| AC2000 Optimal Trajectory   ||");

  228.         System.out.println ("\t| ------ ------- ----------   ||");

  229.         System.out.println ("\t|     L -> R:                 ||");

  230.         System.out.println ("\t|        Time Node            ||");

  231.         System.out.println ("\t|        Holdings             ||");

  232.         System.out.println ("\t|        Trade Amount         ||");

  233.         System.out.println ("\t|-----------------------------||");

  234.         for (int i = 0; i <= iN; ++i) {
  235.             if (i == 0)
  236.                 System.out.println (
  237.                     "\t|" + FormatUtil.FormatDouble (adblExecutionTimeNode[i], 1, 0, 1.) + " => " +
  238.                     FormatUtil.FormatDouble (adblHoldings[i], 7, 1, 1.) + " | " +
  239.                     FormatUtil.FormatDouble (0., 6, 1, 1.) + " ||"
  240.                 );
  241.             else
  242.                 System.out.println (
  243.                     "\t|" + FormatUtil.FormatDouble (adblExecutionTimeNode[i], 1, 0, 1.) + " => " +
  244.                     FormatUtil.FormatDouble (adblHoldings[i], 7, 1, 1.) + " | " +
  245.                     FormatUtil.FormatDouble (adblTradeList[i - 1], 6, 1, 1.) + " ||"
  246.                 );
  247.         }

  248.         System.out.println ("\t|-----------------------------||");

  249.         System.out.println ("\n\t|-----------------------------------------------------------||");

  250.         System.out.println ("\t|               PERIOD LEVEL COST DISTRIBUTION              ||");

  251.         System.out.println ("\t|-----------------------------------------------------------||");

  252.         System.out.println ("\t|  PERIOD   |          MEAN         |        VARIANCE       ||");

  253.         System.out.println ("\t|-----------------------------------------------------------||");

  254.         System.out.println ("\t|  PERIOD   |    INCR   |    CUML   |    INCR   |    CUML   ||");

  255.         System.out.println ("\t|-----------------------------------------------------------||");

  256.         for (int i = 0; i < adblIncrementalShortfallMean.length; ++i)
  257.             System.out.println (
  258.                 "\t| PERIOD #" + (i + 1) + " | " +
  259.                 FormatUtil.FormatDouble (adblIncrementalShortfallMean[i], 6, 1, 1.) + " | " +
  260.                 FormatUtil.FormatDouble (adblCumulativeShortfallMean[i], 6, 1, 1.) + " | " +
  261.                 FormatUtil.FormatDouble (adblIncrementalShortfallVariance[i], 6, 1, 1.e-06) + " | " +
  262.                 FormatUtil.FormatDouble (adblCumulativeShortfallVariance[i], 6, 1, 1.e-06) + " ||"
  263.             );

  264.         System.out.println ("\t|-----------------------------------------------------------||");

  265.         System.out.println ("\n\t|-----------------------------------------------------------||");

  266.         System.out.println ("\t|            PERIOD LEVEL COST IMPACT CONTRIBUTION          ||");

  267.         System.out.println ("\t|-----------------------------------------------------------||");

  268.         System.out.println ("\t|  PERIOD   |       PERMANENT       |       TEMPORARY       ||");

  269.         System.out.println ("\t|-----------------------------------------------------------||");

  270.         System.out.println ("\t|  PERIOD   |    INCR   |    CUML   |    INCR   |    CUML   ||");

  271.         System.out.println ("\t|-----------------------------------------------------------||");

  272.         for (int i = 0; i < adblIncrementalPermanentImpact.length; ++i)
  273.             System.out.println (
  274.                 "\t| PERIOD #" + (i + 1) + " | " +
  275.                 FormatUtil.FormatDouble (adblIncrementalPermanentImpact[i], 6, 1, 1.) + " | " +
  276.                 FormatUtil.FormatDouble (adblCumulativePermanentImpact[i], 6, 1, 1.) + " | " +
  277.                 FormatUtil.FormatDouble (adblIncrementalTemporaryImpact[i], 6, 1, 1.) + " | " +
  278.                 FormatUtil.FormatDouble (adblCumulativeTemporaryImpact[i], 6, 1, 1.) + " ||"
  279.             );

  280.         System.out.println ("\t|-----------------------------------------------------------||");

  281.         System.out.println ("\n\t|--------------------------------------------------------------||");

  282.         System.out.println ("\t| TRANSACTION COST RECONCILIATION: AC2000 vs. EXPLICIT LINEAR  ||");

  283.         System.out.println ("\t|--------------------------------------------------------------||");

  284.         System.out.println (
  285.             "\t| Transaction Cost Expectation         : " +
  286.             FormatUtil.FormatDouble (r1un.mean(), 6, 1, 1.) + " | " +
  287.             FormatUtil.FormatDouble (acd.transactionCostExpectation(), 6, 1, 1.) + " ||"
  288.         );

  289.         System.out.println (
  290.             "\t| Transaction Cost Variance (X 10^-06) : " +
  291.             FormatUtil.FormatDouble (r1un.variance(), 6, 1, 1.e-06) + " | " +
  292.             FormatUtil.FormatDouble (acd.transactionCostVariance(), 6, 1, 1.e-06) + " ||"
  293.         );

  294.         System.out.println ("\t|--------------------------------------------------------------||");

  295.         System.out.println ("\n\t|-----------------------||");

  296.         System.out.println ("\t|  AC2000 METRICS DUMP  ||");

  297.         System.out.println ("\t|-----------------------||");

  298.         System.out.println ("\t| Kappa       : " + FormatUtil.FormatDouble (acd.kappa(), 1, 4, 1.) + " ||");

  299.         System.out.println ("\t| Kappa Tilda : " + FormatUtil.FormatDouble (acd.kappaTilda(), 1, 4, 1.) + " ||");

  300.         System.out.println ("\t| Half Life   : " + FormatUtil.FormatDouble (acd.halfLife(), 1, 4, 1.) + " ||");

  301.         System.out.println ("\t| Market Power: " + FormatUtil.FormatDouble (acd.marketPower(), 1, 4, 1.) + " ||");

  302.         System.out.println ("\t|-----------------------||");

  303.         EnvManager.TerminateEnv();
  304.     }
  305. }