VariableDriftTrajectoryComparator.java

  1. package org.drip.sample.trend;

  2. import org.drip.execution.bayesian.*;
  3. import org.drip.execution.cost.*;
  4. import org.drip.execution.impact.ParticipationRateLinear;
  5. import org.drip.numerical.common.FormatUtil;
  6. import org.drip.service.env.EnvManager;

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

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

  52. /**
  53.  * VariableDriftTrajectoryComparator demonstrates the Optimal Trajectory for a Price Process with Bayesian
  54.  *  Drift, Arithmetic Volatility, and Linear Temporary Market Impact across a Set of Drifts. The References
  55.  *  are:
  56.  *
  57.  *  - Bertsimas, D., and A. W. Lo (1998): Optimal Control of Execution Costs, Journal of Financial Markets 1
  58.  *      1-50.
  59.  *
  60.  *  - Almgren, R., and N. Chriss (2000): Optimal Execution of Portfolio Transactions, Journal of Risk 3 (2)
  61.  *      5-39.
  62.  *
  63.  *  - Brunnermeier, L. K., and L. H. Pedersen (2005): Predatory Trading, Journal of Finance 60 (4) 1825-1863.
  64.  *
  65.  *  - Almgren, R., and J. Lorenz (2006): Bayesian Adaptive Trading with a Daily Cycle, Journal of Trading 1
  66.  *      (4) 38-46.
  67.  *
  68.  *  - Kissell, R., and R. Malamut (2007): Algorithmic Decision Making Framework, Journal of Trading 1 (1)
  69.  *      12-21.
  70.  *
  71.  * @author Lakshmi Krishnamurthy
  72.  */

  73. public class VariableDriftTrajectoryComparator {

  74.     public static final void main (
  75.         final String[] astrArgs)
  76.         throws Exception
  77.     {
  78.         EnvManager.InitEnv ("");

  79.         int iN = 50;
  80.         double dblT = 1.;
  81.         double dblX0 = 1.;
  82.         double dblNu = 1.;
  83.         double dblEta = 0.07;
  84.         double dblSigma = 1.5;
  85.         double dblAlphaBarLow = 0.0;
  86.         double dblAlphaBarMid = 0.7;
  87.         double dblAlphaBarHigh = 1.5;

  88.         double dblTime = 0.;
  89.         double dblTimeWidth = dblT / iN;
  90.         double dblXUnconstrained = dblX0;
  91.         double dblXConstrainedLow = dblX0;
  92.         double dblXConstrainedMid = dblX0;
  93.         double dblXConstrainedHigh = dblX0;

  94.         ParticipationRateLinear prlTemporary = ParticipationRateLinear.SlopeOnly (dblEta);

  95.         PriorDriftDistribution pddLow = new PriorDriftDistribution (
  96.             dblAlphaBarLow,
  97.             dblNu
  98.         );

  99.         PriorDriftDistribution pddMid = new PriorDriftDistribution (
  100.             dblAlphaBarMid,
  101.             dblNu
  102.         );

  103.         PriorDriftDistribution pddHigh = new PriorDriftDistribution (
  104.             dblAlphaBarHigh,
  105.             dblNu
  106.         );

  107.         double[] adblAlphaLow = pddLow.realizedDrift (iN);

  108.         double[] adblAlphaMid = pddMid.realizedDrift (iN);

  109.         double[] adblAlphaHigh = pddHigh.realizedDrift (iN);

  110.         System.out.println();

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

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

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

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

  115.         System.out.println ("\t|    - Constrained Trade Rate Low Drift                                          ||");

  116.         System.out.println ("\t|    - Constrained Trade Rate Mid Drift                                          ||");

  117.         System.out.println ("\t|    - Constrained Trade Rate High Drift                                         ||");

  118.         System.out.println ("\t|    - Unconstrained Trade Rate                                                  ||");

  119.         System.out.println ("\t|    - Constrained Holdings Low Drift                                            ||");

  120.         System.out.println ("\t|    - Constrained Holdings Mid Drift                                            ||");

  121.         System.out.println ("\t|    - Constrained Holdings High Drift                                           ||");

  122.         System.out.println ("\t|    - Unconstrained Holdings                                                    ||");

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

  124.         for (int i = 0; i < iN - 1; ++i) {
  125.             dblTime = dblTime + dblTimeWidth;

  126.             ConditionalPriceDistribution cpdLow = new ConditionalPriceDistribution (
  127.                 adblAlphaLow[i],
  128.                 dblSigma,
  129.                 dblTime
  130.             );

  131.             ConditionalPriceDistribution cpdMid = new ConditionalPriceDistribution (
  132.                 adblAlphaMid[i],
  133.                 dblSigma,
  134.                 dblTime
  135.             );

  136.             ConditionalPriceDistribution cpdHigh = new ConditionalPriceDistribution (
  137.                 adblAlphaHigh[i],
  138.                 dblSigma,
  139.                 dblTime
  140.             );

  141.             double dblPriceSwingLow = cpdLow.priceVolatilitySwing();

  142.             double dblPriceSwingMid = cpdMid.priceVolatilitySwing();

  143.             double dblPriceSwingHigh = cpdHigh.priceVolatilitySwing();

  144.             double dblRealizedPriceChangeLow = adblAlphaLow[i] * dblTimeWidth + dblPriceSwingLow;
  145.             double dblRealizedPriceChangeMid = adblAlphaMid[i] * dblTimeWidth + dblPriceSwingMid;
  146.             double dblRealizedPriceChangeHigh = adblAlphaHigh[i] * dblTimeWidth + dblPriceSwingHigh;

  147.             PriorConditionalCombiner pccLow = new PriorConditionalCombiner (
  148.                 pddLow,
  149.                 cpdLow
  150.             );

  151.             PriorConditionalCombiner pccMid = new PriorConditionalCombiner (
  152.                 pddMid,
  153.                 cpdMid
  154.             );

  155.             PriorConditionalCombiner pccHigh = new PriorConditionalCombiner (
  156.                 pddHigh,
  157.                 cpdHigh
  158.             );

  159.             ConstrainedLinearTemporaryImpact cltiLow = ConstrainedLinearTemporaryImpact.Standard (
  160.                 0.,
  161.                 dblT,
  162.                 dblXConstrainedLow,
  163.                 pccLow,
  164.                 dblRealizedPriceChangeLow,
  165.                 prlTemporary
  166.             );

  167.             ConstrainedLinearTemporaryImpact cltiMid = ConstrainedLinearTemporaryImpact.Standard (
  168.                 0.,
  169.                 dblT,
  170.                 dblXConstrainedMid,
  171.                 pccMid,
  172.                 dblRealizedPriceChangeMid,
  173.                 prlTemporary
  174.             );

  175.             ConstrainedLinearTemporaryImpact cltiHigh = ConstrainedLinearTemporaryImpact.Standard (
  176.                 0.,
  177.                 dblT,
  178.                 dblXConstrainedHigh,
  179.                 pccHigh,
  180.                 dblRealizedPriceChangeHigh,
  181.                 prlTemporary
  182.             );

  183.             double dblConstrainedInstantaneousTradeRateLow = cltiLow.instantaneousTradeRate();

  184.             dblXConstrainedLow = dblXConstrainedLow - dblConstrainedInstantaneousTradeRateLow * dblTimeWidth;

  185.             if (0 > dblXConstrainedLow) dblXConstrainedLow = 0.;

  186.             double dblConstrainedInstantaneousTradeRateMid = cltiMid.instantaneousTradeRate();

  187.             dblXConstrainedMid = dblXConstrainedMid - dblConstrainedInstantaneousTradeRateMid * dblTimeWidth;

  188.             if (0 > dblXConstrainedMid) dblXConstrainedMid = 0.;

  189.             double dblConstrainedInstantaneousTradeRateHigh = cltiHigh.instantaneousTradeRate();

  190.             dblXConstrainedHigh = dblXConstrainedHigh - dblConstrainedInstantaneousTradeRateHigh * dblTimeWidth;

  191.             if (0 > dblXConstrainedHigh) dblXConstrainedHigh = 0.;

  192.             LinearTemporaryImpact lti = LinearTemporaryImpact.Unconstrained (
  193.                 dblTime,
  194.                 dblT,
  195.                 dblXUnconstrained,
  196.                 pccMid,
  197.                 dblRealizedPriceChangeMid,
  198.                 prlTemporary
  199.             );

  200.             double dblUnconstrainedInstantaneousTradeRate = lti.instantaneousTradeRate();

  201.             dblXUnconstrained = dblXUnconstrained - dblUnconstrainedInstantaneousTradeRate * dblTimeWidth;

  202.             System.out.println (
  203.                 "\t| " + FormatUtil.FormatDouble (dblTime, 1, 2, 1.) + " => " +
  204.                 FormatUtil.FormatDouble (dblConstrainedInstantaneousTradeRateLow, 1, 3, 1.) + " | " +
  205.                 FormatUtil.FormatDouble (dblConstrainedInstantaneousTradeRateMid, 1, 3, 1.) + " | " +
  206.                 FormatUtil.FormatDouble (dblConstrainedInstantaneousTradeRateHigh, 1, 3, 1.) + " | " +
  207.                 FormatUtil.FormatDouble (dblUnconstrainedInstantaneousTradeRate, 1, 3, 1.) + " | " +
  208.                 FormatUtil.FormatDouble (dblXConstrainedLow, 1, 3, 1.) + " | " +
  209.                 FormatUtil.FormatDouble (dblXConstrainedMid, 1, 3, 1.) + " | " +
  210.                 FormatUtil.FormatDouble (dblXConstrainedHigh, 1, 3, 1.) + " | " +
  211.                 FormatUtil.FormatDouble (dblXUnconstrained, 1, 3, 1.) + " ||"
  212.             );
  213.         }

  214.         System.out.println ("\t|--------------------------------------------------------------------------------||");
  215.     }
  216. }