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

  72. public class FixedDriftTrajectoryComparator {

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

  78.         int iN = 50;
  79.         double dblT = 1.;
  80.         double dblX0 = 1.;
  81.         double dblNu = 1.;
  82.         double dblEta = 0.07;
  83.         double dblSigma = 1.5;
  84.         double dblAlphaBar = 0.7;

  85.         double dblTime = 0.;
  86.         double dblXConstrained = dblX0;
  87.         double dblTimeWidth = dblT / iN;
  88.         double dblXUnconstrained = dblX0;

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

  90.         PriorDriftDistribution pdd = new PriorDriftDistribution (
  91.             dblAlphaBar,
  92.             dblNu
  93.         );

  94.         double[] adblAlpha = pdd.realizedDrift (iN);

  95.         System.out.println();

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

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

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

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

  100.         System.out.println ("\t|    - Trade Start Time                                                 ||");

  101.         System.out.println ("\t|    - Trade Finish Time                                                ||");

  102.         System.out.println ("\t|    - Critical Trade Rate                                              ||");

  103.         System.out.println ("\t|    - Constrained Trade Rate                                           ||");

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

  105.         System.out.println ("\t|    - Constrained Holdings                                             ||");

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

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

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

  110.             ConditionalPriceDistribution cpd = new ConditionalPriceDistribution (
  111.                 adblAlpha[i],
  112.                 dblSigma,
  113.                 dblTime
  114.             );

  115.             double dblPriceSwing = cpd.priceVolatilitySwing();

  116.             double dblRealizedPriceChange = adblAlpha[i] * dblTimeWidth + dblPriceSwing;

  117.             PriorConditionalCombiner pcc = new PriorConditionalCombiner (
  118.                 pdd,
  119.                 cpd
  120.             );

  121.             ConstrainedLinearTemporaryImpact clti = ConstrainedLinearTemporaryImpact.Standard (
  122.                 0.,
  123.                 dblT,
  124.                 dblXConstrained,
  125.                 pcc,
  126.                 dblRealizedPriceChange,
  127.                 prlTemporary
  128.             );

  129.             double dblConstrainedInstantaneousTradeRate = clti.instantaneousTradeRate();

  130.             dblXConstrained = dblXConstrained - dblConstrainedInstantaneousTradeRate * dblTimeWidth;

  131.             if (0 > dblXConstrained) dblXConstrained = 0.;

  132.             LinearTemporaryImpact lti = LinearTemporaryImpact.Unconstrained (
  133.                 dblTime,
  134.                 dblT,
  135.                 dblXUnconstrained,
  136.                 pcc,
  137.                 dblRealizedPriceChange,
  138.                 prlTemporary
  139.             );

  140.             double dblUnconstrainedInstantaneousTradeRate = lti.instantaneousTradeRate();

  141.             dblXUnconstrained = dblXUnconstrained - dblUnconstrainedInstantaneousTradeRate * dblTimeWidth;

  142.             System.out.println (
  143.                 "\t| " + FormatUtil.FormatDouble (dblTime, 1, 2, 1.) + " => " +
  144.                 FormatUtil.FormatDouble (clti.tradeStartTime(), 1, 3, 1.) + " | " +
  145.                 FormatUtil.FormatDouble (clti.tradeFinishTime(), 1, 3, 1.) + " | " +
  146.                 FormatUtil.FormatDouble (clti.criticalDrift(), 1, 3, 1.) + " | " +
  147.                 FormatUtil.FormatDouble (dblConstrainedInstantaneousTradeRate, 1, 3, 1.) + " | " +
  148.                 FormatUtil.FormatDouble (dblUnconstrainedInstantaneousTradeRate, 1, 3, 1.) + " | " +
  149.                 FormatUtil.FormatDouble (dblXConstrained, 1, 3, 1.) + " | " +
  150.                 FormatUtil.FormatDouble (dblXUnconstrained, 1, 3, 1.) + " ||"
  151.             );
  152.         }

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