PowerLawOptimalTrajectory.java

  1. package org.drip.sample.almgren2003;

  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.ArithmeticPriceDynamicsSettings;
  7. import org.drip.execution.profiletime.*;
  8. import org.drip.function.definition.R1ToR1;
  9. import org.drip.function.r1tor1.FlatUnivariate;
  10. import org.drip.numerical.common.FormatUtil;
  11. import org.drip.service.env.EnvManager;

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

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

  87. /**
  88.  * <i>PowerLawOptimalTrajectory</i> sketches out the Optimal Trajectories for 3 different values of k -
  89.  * representing Concave, Linear, and Convex Power's respectively. The References are:
  90.  *
  91.  * <br><br>
  92.  *  <ul>
  93.  *      <li>
  94.  *          Almgren, R., and N. Chriss (1999): Value under Liquidation <i>Risk</i> <b>12 (12)</b>
  95.  *      </li>
  96.  *
  97.  *      <li>
  98.  *          Almgren, R., and N. Chriss (2000): Optimal Execution of Portfolio Transactions <i>Journal of
  99.  *              Risk</i> <b>3 (2)</b> 5-39
  100.  *      </li>
  101.  *
  102.  *      <li>
  103.  *          Almgren, R. (2003): Optimal Execution with Nonlinear Impact Functions and Trading-Enhanced Risk
  104.  *              <i>Applied Mathematical Finance</i> <b>10 (1)</b> 1-18.
  105.  *      </li>
  106.  *
  107.  *      <li>
  108.  *          Almgren, R., and N. Chriss (2003): Bidding Principles <i>Risk</i> 97-102
  109.  *      </li>
  110.  *
  111.  *      <li>
  112.  *          Bertsimas, D., and A. W. Lo (1998): Optimal Control of Execution Costs <i>Journal of Financial
  113.  *              Markets</i> <b>1</b> 1-50
  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/almgren2003/README.md">Almgren (2003) Power Law Liquidity</a></li>
  123.  *  </ul>
  124.  * <br><br>
  125.  *
  126.  * @author Lakshmi Krishnamurthy
  127.  */

  128. public class PowerLawOptimalTrajectory {

  129.     private static final void RiskAversionRun (
  130.         final double dblLambda)
  131.         throws Exception
  132.     {
  133.         double dblGamma = 0.;
  134.         double dblHRef = 0.50;
  135.         double dblVRef = 100000.;
  136.         double dblDrift = 0.;
  137.         double dblVolatility = 1.;
  138.         double dblSerialCorrelation = 0.;
  139.         double dblX = 100000.;
  140.         double dblFinishTime = 10.;
  141.         int iNumInterval = 10;

  142.         double[] adblK = new double[] {
  143.             0.25,
  144.             0.50,
  145.             0.75,
  146.             1.00,
  147.             1.25,
  148.             1.50,
  149.             1.75,
  150.             2.00,
  151.             2.25,
  152.             2.50,
  153.             2.75,
  154.             3.00
  155.         };

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

  157.         System.out.println ("\t|\tPOWER LAW OPTIMAL TRAJECTORY; RISK TOLERANCE (thousands) => " + FormatUtil.FormatDouble (1. / dblLambda, 1, 0, 1.e-03));

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

  159.         System.out.println ("\t|\t\tL -> R:");

  160.         System.out.println ("\t|\t\t\tTime Node Trajectory Realization (Percent)");

  161.         System.out.println ("\t|\t\t\tCharacteristic Time (Days)");

  162.         System.out.println ("\t|\t\t\tMaximum Execution Time (Days)");

  163.         System.out.println ("\t|\t\t\tTransaction Cost Expectation (Thousands)");

  164.         System.out.println ("\t|\t\t\tTransaction Cost Variance (Thousands)");

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

  166.         ArithmeticPriceDynamicsSettings apds = new ArithmeticPriceDynamicsSettings (
  167.             dblDrift,
  168.             new FlatUnivariate (dblVolatility),
  169.             dblSerialCorrelation
  170.         );

  171.         ParticipationRateLinear prlPermanent = new ParticipationRateLinear (
  172.             0.,
  173.             dblGamma
  174.         );

  175.         double[] adblExecutionTime = new double[iNumInterval];

  176.         for (int i = 1; i <= iNumInterval; ++i)
  177.             adblExecutionTime[i - 1] = ((double) i) / ((double) iNumInterval);

  178.         for (int i = 0; i < adblK.length; ++i) {
  179.             double dblEta = dblHRef / java.lang.Math.pow (dblVRef, adblK[i]);

  180.             LinearPermanentExpectationParameters lpep = ArithmeticPriceEvolutionParametersBuilder.Almgren2003 (
  181.                 apds,
  182.                 new UniformParticipationRateLinear (prlPermanent),
  183.                 new UniformParticipationRate (
  184.                     new ParticipationRatePower (
  185.                         dblEta,
  186.                         adblK[i]
  187.                     )
  188.                 )
  189.             );

  190.             ContinuousPowerImpact cpi = ContinuousPowerImpact.Standard (
  191.                 dblX,
  192.                 dblFinishTime,
  193.                 lpep,
  194.                 dblLambda
  195.             );

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

  197.             if (0 == i) {
  198.                 String strExecutionTime = "\t|          |  ";

  199.                 for (int j = 0; j < adblExecutionTime.length; ++j)
  200.                     strExecutionTime = strExecutionTime + "   " + FormatUtil.FormatDouble (adblExecutionTime[j], 1, 2, 1.);

  201.                 System.out.println (strExecutionTime);

  202.                 System.out.println ("\t|------------------------------------------------------------------------------------------------------------------------------------||");
  203.             }

  204.             R1ToR1 r1ToR1Holdings = pic.holdings();

  205.             String strHoldings = "\t| k =" + FormatUtil.FormatDouble (adblK[i], 1, 2, 1.) + " | ";

  206.             for (int j = 0; j < iNumInterval; ++j)
  207.                 strHoldings = strHoldings + "  " + FormatUtil.FormatDouble (r1ToR1Holdings.evaluate (adblExecutionTime[j]) / dblX, 2, 2, 100.);

  208.             double dblExecutionTimeUpperBound = pic.executionTimeUpperBound();

  209.             System.out.println (
  210.                 strHoldings + " | " +
  211.                 FormatUtil.FormatDouble (pic.characteristicTime(), 2, 1, 1.) + " | " +
  212.                 FormatUtil.FormatDouble (Double.isNaN (dblExecutionTimeUpperBound) ? 0. : dblExecutionTimeUpperBound, 2, 1, 1.) + " | " +
  213.                 FormatUtil.FormatDouble (pic.transactionCostExpectation(), 3, 0, 1.e-03) + " | " +
  214.                 FormatUtil.FormatDouble (Math.sqrt (pic.transactionCostVariance()), 3, 0, 1.e-03) + " ||"
  215.             );
  216.         }

  217.         System.out.println ("\t|------------------------------------------------------------------------------------------------------------------------------------||");
  218.     }

  219.     public static final void main (
  220.         final String[] astrArgs)
  221.         throws Exception
  222.     {
  223.         EnvManager.InitEnv (
  224.             "",
  225.             true
  226.         );

  227.         double[] adblLambda = new double[] {
  228.             1.e-04,
  229.             5.e-06,
  230.             5.e-07
  231.         };

  232.         for (double dblLambda : adblLambda)
  233.             RiskAversionRun (dblLambda);

  234.         EnvManager.TerminateEnv();
  235.     }
  236. }