OptimalTrajectoryMeasures.java

  1. package org.drip.sample.principal;

  2. import org.drip.execution.dynamics.*;
  3. import org.drip.execution.impact.*;
  4. import org.drip.execution.nonadaptive.ContinuousPowerImpact;
  5. import org.drip.execution.optimum.PowerImpactContinuous;
  6. import org.drip.execution.parameters.*;
  7. import org.drip.execution.principal.Almgren2003Estimator;
  8. import org.drip.execution.profiletime.*;
  9. import org.drip.function.definition.R1ToR1;
  10. import org.drip.function.r1tor1.FlatUnivariate;
  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) 2018 Lakshmi Krishnamurthy
  18.  * Copyright (C) 2017 Lakshmi Krishnamurthy
  19.  * Copyright (C) 2016 Lakshmi Krishnamurthy
  20.  *
  21.  *  This file is part of DRIP, a free-software/open-source library for buy/side financial/trading model
  22.  *      libraries targeting analysts and developers
  23.  *      https://lakshmidrip.github.io/DRIP/
  24.  *  
  25.  *  DRIP is composed of four main libraries:
  26.  *  
  27.  *  - DRIP Fixed Income - https://lakshmidrip.github.io/DRIP-Fixed-Income/
  28.  *  - DRIP Asset Allocation - https://lakshmidrip.github.io/DRIP-Asset-Allocation/
  29.  *  - DRIP Numerical Optimizer - https://lakshmidrip.github.io/DRIP-Numerical-Optimizer/
  30.  *  - DRIP Statistical Learning - https://lakshmidrip.github.io/DRIP-Statistical-Learning/
  31.  *
  32.  *  - DRIP Fixed Income: Library for Instrument/Trading Conventions, Treasury Futures/Options,
  33.  *      Funding/Forward/Overnight Curves, Multi-Curve Construction/Valuation, Collateral Valuation and XVA
  34.  *      Metric Generation, Calibration and Hedge Attributions, Statistical Curve Construction, Bond RV
  35.  *      Metrics, Stochastic Evolution and Option Pricing, Interest Rate Dynamics and Option Pricing, LMM
  36.  *      Extensions/Calibrations/Greeks, Algorithmic Differentiation, and Asset Backed Models and Analytics.
  37.  *
  38.  *  - DRIP Asset Allocation: Library for model libraries for MPT framework, Black Litterman Strategy
  39.  *      Incorporator, Holdings Constraint, and Transaction Costs.
  40.  *
  41.  *  - DRIP Numerical Optimizer: Library for Numerical Optimization and Spline Functionality.
  42.  *
  43.  *  - DRIP Statistical Learning: Library for Statistical Evaluation and Machine Learning.
  44.  *
  45.  *  Licensed under the Apache License, Version 2.0 (the "License");
  46.  *      you may not use this file except in compliance with the License.
  47.  *  
  48.  *  You may obtain a copy of the License at
  49.  *      http://www.apache.org/licenses/LICENSE-2.0
  50.  *  
  51.  *  Unless required by applicable law or agreed to in writing, software
  52.  *      distributed under the License is distributed on an "AS IS" BASIS,
  53.  *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  54.  *  
  55.  *  See the License for the specific language governing permissions and
  56.  *      limitations under the License.
  57.  */

  58. /**
  59.  * OptimalTrajectoryMeasures demonstrates the Trade Scheduling using the Equity Market Impact Functions
  60.  *  determined empirically by Almgren, Thum, Hauptmann, and Li (2005), using the Parameterization of Almgren
  61.  *  (2003) for IBM. It generates the Transaction Cost/Principal Discount Measures from the Run The References
  62.  *  are:
  63.  *
  64.  *  - Almgren, R., and N. Chriss (1999): Value under Liquidation, Risk 12 (12).
  65.  *
  66.  *  - Almgren, R., and N. Chriss (2000): Optimal Execution of Portfolio Transactions, Journal of Risk 3 (2)
  67.  *      5-39.
  68.  *
  69.  *  - Almgren, R. (2003): Optimal Execution with Nonlinear Impact Functions and Trading-Enhanced Risk,
  70.  *      Applied Mathematical Finance 10 (1) 1-18.
  71.  *
  72.  *  - Almgren, R., and N. Chriss (2003): Bidding Principles, Risk 97-102.
  73.  *
  74.  *  - Almgren, R., C. Thum, E. Hauptmann, and H. Li (2005): Equity Market Impact, Risk 18 (7) 57-62.
  75.  *
  76.  * @author Lakshmi Krishnamurthy
  77.  */

  78. public class OptimalTrajectoryMeasures {

  79.     public static final void main (
  80.         final String[] astrArgs)
  81.         throws Exception
  82.     {
  83.         EnvManager.InitEnv ("");

  84.         double dblS0 = 50.;
  85.         double dblX = 100000.;
  86.         double dblVolatility = 1.;
  87.         double dblDailyVolume = 1000000.;
  88.         double dblDailyVolumeExecutionFactor = 0.1;
  89.         double dblPermanentImpactFactor = 0.;
  90.         double dblTemporaryImpactFactor = 0.01;
  91.         double dblT = 5.;
  92.         int iNumInterval = 20;
  93.         double dblLambda = 1.e-06;
  94.         double dblK = 1.;
  95.         double dblPrincipalDiscount = 0.15;

  96.         PriceMarketImpactPower pmip = new PriceMarketImpactPower (
  97.             new AssetTransactionSettings (
  98.                 dblS0,
  99.                 dblDailyVolume,
  100.                 0.
  101.             ),
  102.             dblPermanentImpactFactor,
  103.             dblTemporaryImpactFactor,
  104.             dblDailyVolumeExecutionFactor,
  105.             dblK
  106.         );

  107.         LinearPermanentExpectationParameters lpep = ArithmeticPriceEvolutionParametersBuilder.Almgren2003 (
  108.             new ArithmeticPriceDynamicsSettings (
  109.                 0.,
  110.                 new FlatUnivariate (dblVolatility),
  111.                 0.
  112.             ),
  113.             new UniformParticipationRateLinear ((ParticipationRateLinear) pmip.permanentTransactionFunction()),
  114.             new UniformParticipationRate ((ParticipationRatePower) pmip.temporaryTransactionFunction())
  115.         );

  116.         ContinuousPowerImpact cpi = ContinuousPowerImpact.Standard (
  117.             dblX,
  118.             dblT,
  119.             lpep,
  120.             dblLambda
  121.         );

  122.         PowerImpactContinuous pic = (PowerImpactContinuous) cpi.generate();

  123.         R1ToR1 r1ToR1Holdings = pic.holdings();

  124.         double[] adblHoldings = new double[iNumInterval];
  125.         double[] adblExecutionTime = new double[iNumInterval];

  126.         for (int i = 1; i <= iNumInterval; ++i) {
  127.             adblExecutionTime[i - 1] = dblT * i / iNumInterval;

  128.             adblHoldings[i - 1] = r1ToR1Holdings.evaluate (adblExecutionTime[i - 1]);
  129.         }

  130.         Almgren2003Estimator a2003e = new Almgren2003Estimator (
  131.             pic,
  132.             lpep
  133.         );

  134.         System.out.println();

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

  136.         System.out.println ("\t| IBM ATHL 2005 Optimal Trajectory ||");

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

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

  139.         System.out.println ("\t|           - Execution Time Node  ||");

  140.         System.out.println ("\t|           - Holdings Remaining   ||");

  141.         System.out.println ("\t|           - Trade List Amount    ||");

  142.         System.out.println ("\t|           - Holdings (%)         ||");

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

  144.         for (int i = 1; i < adblExecutionTime.length; ++i)
  145.             System.out.println (
  146.                 "\t| " +
  147.                 FormatUtil.FormatDouble (adblExecutionTime[i], 1, 2, 1.) + " | " +
  148.                 FormatUtil.FormatDouble (adblHoldings[i], 5, 0, 1.) + " | " +
  149.                 FormatUtil.FormatDouble (adblHoldings[i] - adblHoldings[i - 1], 5, 0, 1.) + " | " +
  150.                 FormatUtil.FormatDouble (adblHoldings[i] / dblX, 2, 1, 100.) + "% ||"
  151.             );

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

  153.         System.out.println();

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

  155.         System.out.println ("\t|       IBM ATHL 2005 Optimal Trajectory Transaction Cost Measures      ||");

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

  157.         System.out.println (
  158.             "\t| Transaction Cost Expectation ( X 10^-03)                  : " +
  159.             FormatUtil.FormatDouble (pic.transactionCostExpectation(), 5, 2, 1.e-03) + " ||"
  160.         );

  161.         System.out.println (
  162.             "\t| Transaction Cost Variance ( X 10^-06)                     : " +
  163.             FormatUtil.FormatDouble (pic.transactionCostVariance(), 5, 2, 1.e-06) + " ||"
  164.         );

  165.         System.out.println (
  166.             "\t| Characteristic Time                                       : " +
  167.             FormatUtil.FormatDouble (pic.characteristicTime(), 5, 2, 1.) + " ||"
  168.         );

  169.         System.out.println (
  170.             "\t| Efficient Frontier Hyperboloid Boundary Value ( X 10^-12) : " +
  171.             FormatUtil.FormatDouble (pic.hyperboloidBoundaryValue(), 5, 2, 1.e-12) + " ||"
  172.         );

  173.         System.out.println (
  174.             "\t| Break-even Principal Discount (cents per unit)            : " +
  175.             FormatUtil.FormatDouble (a2003e.breakevenPrincipalDiscount(), 5, 2, 100.) + " ||"
  176.         );

  177.         System.out.println (
  178.             "\t| Gross Profit Expectation                                  : " +
  179.             FormatUtil.FormatDouble (a2003e.principalMeasure (dblPrincipalDiscount).mean(), 5, 2, 1.) + " ||"
  180.         );

  181.         System.out.println (
  182.             "\t| Gross Profit Variance ( X 10^-06)                         : " +
  183.             FormatUtil.FormatDouble (a2003e.principalMeasure (dblPrincipalDiscount).variance(), 5, 2, 1.e-06) + " ||"
  184.         );

  185.         System.out.println (
  186.             "\t| Gross Returns Expectation                                 : " +
  187.             FormatUtil.FormatDouble (a2003e.horizonPrincipalMeasure (dblPrincipalDiscount).mean(), 5, 2, 1.) + " ||"
  188.         );

  189.         System.out.println (
  190.             "\t| Gross Returns Variance ( X 10^-06)                        : " +
  191.             FormatUtil.FormatDouble (a2003e.horizonPrincipalMeasure (dblPrincipalDiscount).variance(), 5, 2, 1.e-06) + " ||"
  192.         );

  193.         System.out.println (
  194.             "\t| Information Ratio ( X 10^+03)                             : " +
  195.             FormatUtil.FormatDouble (a2003e.informationRatio (dblPrincipalDiscount), 5, 2, 1.e+03) + " ||"
  196.         );

  197.         System.out.println (
  198.             "\t| Optimal Information Ratio ( X 10^+03)                     : " +
  199.             FormatUtil.FormatDouble (a2003e.optimalInformationRatio (dblPrincipalDiscount), 5, 2, 1.e+03) + " ||"
  200.         );

  201.         System.out.println (
  202.             "\t| Optimal Information Ratio Horizon                         : " +
  203.             FormatUtil.FormatDouble (a2003e.optimalInformationRatioHorizon (dblPrincipalDiscount), 5, 2, 1.) + " ||"
  204.         );

  205.         System.out.println ("\t|-----------------------------------------------------------------------||");
  206.     }
  207. }