CoordinatedMarketStateTrajectory.java

  1. package org.drip.sample.almgren2009;

  2. import org.drip.execution.tradingtime.*;
  3. import org.drip.measure.dynamics.DiffusionEvaluatorOrnsteinUhlenbeck;
  4. import org.drip.measure.process.DiffusionEvolver;
  5. import org.drip.measure.realization.*;
  6. import org.drip.numerical.common.FormatUtil;
  7. import org.drip.service.env.EnvManager;

  8. /*
  9.  * -*- mode: java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  10.  */

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

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

  122. public class CoordinatedMarketStateTrajectory {

  123.     public static final void main (
  124.         final String[] astrArgs)
  125.         throws Exception
  126.     {
  127.         EnvManager.InitEnv (
  128.                 "",
  129.                 true
  130.             );

  131.         double dblBurstiness = 1.;
  132.         double dblRelaxationTime = 1.;
  133.         double dblSimulationTime = 9.75;
  134.         int iNumSimulation = 39;
  135.         double dblReferenceLiquidity = 1.0;
  136.         double dblReferenceVolatility = 1.0;
  137.         double dblInitialMarketState = -0.5;

  138.         double dblTime = 0.;
  139.         double dblMarketState = dblInitialMarketState;
  140.         double dblTimeInterval = dblSimulationTime / iNumSimulation;

  141.         DiffusionEvaluatorOrnsteinUhlenbeck oup1D = DiffusionEvaluatorOrnsteinUhlenbeck.ZeroMean (
  142.             dblBurstiness,
  143.             dblRelaxationTime
  144.         );

  145.         DiffusionEvolver de = new DiffusionEvolver (oup1D);

  146.         CoordinatedMarketState cms = new CoordinatedMarketState (
  147.             new CoordinatedVariation (
  148.                 dblReferenceVolatility,
  149.                 dblReferenceLiquidity
  150.             )
  151.         );

  152.         double dblLiquidity = cms.liquidity (dblMarketState);

  153.         System.out.println();

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

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

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

  157.         System.out.println ("\t||            - Realized Volatility                                 ||");

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

  159.         System.out.println ("\t||            - Liquidity/Volatility Status                         ||");

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

  161.         System.out.println ("\t|| [" +
  162.             FormatUtil.FormatDouble (0., 1, 2, 1.) + "] => " +
  163.             FormatUtil.FormatDouble (dblMarketState, 1, 4, 1.) + " | " +
  164.             FormatUtil.FormatDouble (cms.volatility (dblMarketState), 1, 4, 1.) + " | " +
  165.             FormatUtil.FormatDouble (cms.liquidity (dblMarketState), 1, 4, 1.) + " | " +
  166.             (
  167.                 dblLiquidity < dblReferenceLiquidity ?
  168.                 "  LIQUID,     VOLATILE " :
  169.                 "ILLIQUID, NON-VOLATILE "
  170.             ) +
  171.             " ||"
  172.         );

  173.         for (int i = 0; i < iNumSimulation; ++i) {
  174.             JumpDiffusionEdge gi = de.weinerIncrement (
  175.                 new JumpDiffusionVertex (
  176.                     dblTime,
  177.                     dblMarketState,
  178.                     0.,
  179.                     false
  180.                 ),
  181.                 dblTimeInterval
  182.             );

  183.             dblTime += dblTimeInterval;

  184.             dblMarketState += gi.deterministic() + gi.diffusionStochastic();

  185.             dblLiquidity = cms.liquidity (dblMarketState);

  186.             System.out.println ("\t|| [" +
  187.                 FormatUtil.FormatDouble (dblTimeInterval * (i + 1), 1, 2, 1.) + "] => " +
  188.                 FormatUtil.FormatDouble (dblMarketState, 1, 4, 1.) + " | " +
  189.                 FormatUtil.FormatDouble (cms.volatility (dblMarketState), 1, 4, 1.) + " | " +
  190.                 FormatUtil.FormatDouble (dblLiquidity, 1, 4, 1.) + " | " +
  191.                 (
  192.                     dblLiquidity < dblReferenceLiquidity ?
  193.                     "  LIQUID,     VOLATILE " :
  194.                     "ILLIQUID, NON-VOLATILE "
  195.                 ) +
  196.                 " ||"
  197.             );
  198.         }

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

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