RollingHorizonOptimalHoldings.java

  1. package org.drip.sample.almgren2012;

  2. import org.drip.execution.adaptive.*;
  3. import org.drip.execution.hjb.NonDimensionalCostEvolverSystemic;
  4. import org.drip.execution.latent.*;
  5. import org.drip.execution.risk.MeanVarianceObjectiveUtility;
  6. import org.drip.execution.strategy.OrderSpecification;
  7. import org.drip.execution.tradingtime.CoordinatedVariation;
  8. import org.drip.measure.dynamics.DiffusionEvaluatorOrnsteinUhlenbeck;
  9. import org.drip.numerical.common.FormatUtil;
  10. import org.drip.service.env.EnvManager;

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

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

  86. /**
  87.  * <i>RollingHorizonOptimalHoldings</i> simulates the Holdings from the Sample Realization of the Adaptive
  88.  * Cost Strategy using the Market State Trajectory the follows the Zero Mean Ornstein-Uhlenbeck Evolution
  89.  * Dynamics. Instead of a HJB Based Truly Adaptive Strategy, a Rolling Horizon Approximation is used. The
  90.  * References are:
  91.  *
  92.  * <br><br>
  93.  *  <ul>
  94.  *      <li>
  95.  *          Almgren, R. F., and N. Chriss (2000): Optimal Execution of Portfolio Transactions <i>Journal of
  96.  *              Risk</i> <b>3 (2)</b> 5-39
  97.  *      </li>
  98.  *      <li>
  99.  *          Almgren, R. F. (2009): Optimal Trading in a Dynamic Market
  100.  *              https://www.math.nyu.edu/financial_mathematics/content/02_financial/2009-2.pdf
  101.  *      </li>
  102.  *      <li>
  103.  *          Almgren, R. F. (2012): Optimal Trading with Stochastic Liquidity and Volatility <i>SIAM Journal
  104.  *              of Financial Mathematics</i> <b>3 (1)</b> 163-181
  105.  *      </li>
  106.  *      <li>
  107.  *          Geman, H., D. B. Madan, and M. Yor (2001): Time Changes for Levy Processes <i>Mathematical
  108.  *              Finance</i> <b>11 (1)</b> 79-96
  109.  *      </li>
  110.  *      <li>
  111.  *          Walia, N. (2006): Optimal Trading: Dynamic Stock Liquidation Strategies <b>Princeton
  112.  *              University</b>
  113.  *      </li>
  114.  *  </ul>
  115.  *
  116.  * <br><br>
  117.  *  <ul>
  118.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ProductCore.md">Product Core Module</a></li>
  119.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/TransactionCostAnalyticsLibrary.md">Transaction Cost Analytics</a></li>
  120.  *      <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>
  121.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/almgren2012/README.md">Almgren (2012) Dynamic Optimal Adaptive</a></li>
  122.  *  </ul>
  123.  * <br><br>
  124.  *
  125.  * @author Lakshmi Krishnamurthy
  126.  */

  127. public class RollingHorizonOptimalHoldings {

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

  136.         double dblSize = 1.;
  137.         int iNumTimeNode = 51;
  138.         double dblBurstiness = 1.;
  139.         double dblExecutionTime = 10.;
  140.         double dblRelaxationTime = 1.;
  141.         double dblReferenceLiquidity = 1.;
  142.         double dblReferenceVolatility = 1.;
  143.         double dblInitialMarketState = -0.5;
  144.         double[] adblRiskAversion = new double[] {
  145.             0.01,
  146.             0.04,
  147.             0.09,
  148.             0.16,
  149.             0.36,
  150.             0.64,
  151.             1.00
  152.         };

  153.         double dblNonDimensionalTimeInterval = dblExecutionTime / (iNumTimeNode - 1) / dblRelaxationTime;
  154.         double[][] aadblAdjustedNonDimensionalHoldings = new double[adblRiskAversion.length][];

  155.         OrderSpecification os = new OrderSpecification (
  156.             dblSize,
  157.             dblExecutionTime
  158.         );

  159.         CoordinatedVariation cv = new CoordinatedVariation (
  160.             dblReferenceVolatility,
  161.             dblReferenceLiquidity
  162.         );

  163.         DiffusionEvaluatorOrnsteinUhlenbeck oup1D = DiffusionEvaluatorOrnsteinUhlenbeck.ZeroMean (
  164.             dblBurstiness,
  165.             dblRelaxationTime
  166.         );

  167.         MarketState[] aMS = OrnsteinUhlenbeckSequence.Systemic (
  168.             oup1D,
  169.             dblNonDimensionalTimeInterval * dblRelaxationTime,
  170.             dblInitialMarketState,
  171.             iNumTimeNode
  172.         ).realizedMarketState();

  173.         for (int i = 0; i < adblRiskAversion.length; ++i)
  174.             aadblAdjustedNonDimensionalHoldings[i] = new CoordinatedVariationTrajectoryGenerator (
  175.                 os,
  176.                 cv,
  177.                 new MeanVarianceObjectiveUtility (adblRiskAversion[i]),
  178.                 NonDimensionalCostEvolverSystemic.Standard (oup1D),
  179.                 CoordinatedVariationTrajectoryGenerator.TRADE_RATE_ZERO_INITIALIZATION
  180.             ).rollingHorizon (aMS).nonDimensionalHoldings();

  181.         System.out.println();

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

  183.         System.out.println ("\t||                 ROLLING HORIZON OPTIMAL TRAJECTORY HOLDINGS                 ||");

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

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

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

  187.         for (int j = 0; j < adblRiskAversion.length; ++j)
  188.             System.out.println (
  189.                 "\t||             - Non Dimensional Risk Aversion =>" +
  190.                 FormatUtil.FormatDouble (dblRelaxationTime * dblReferenceVolatility * Math.sqrt (adblRiskAversion[j] / dblReferenceLiquidity), 1, 2, 1.) +
  191.                 "                         ||"
  192.             );

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

  194.         for (int i = 0; i < iNumTimeNode - 1; ++i) {
  195.             String strDump = "\t|| " + FormatUtil.FormatDouble (i * dblNonDimensionalTimeInterval * dblRelaxationTime, 1, 2, 1.);

  196.             for (int j = 0; j < adblRiskAversion.length; ++j)
  197.                 strDump = strDump + " | " + FormatUtil.FormatDouble (aadblAdjustedNonDimensionalHoldings[j][i], 1, 4, 1.);

  198.             System.out.println (strDump + " ||");
  199.         }

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

  201.         EnvManager.TerminateEnv();
  202.     }
  203. }