BayesianDriftTransactionDependence.java

  1. package org.drip.sample.trend;

  2. import org.drip.execution.bayesian.*;
  3. import org.drip.execution.cost.LinearTemporaryImpact;
  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.  * BayesianDriftTransactionDependence demonstrates the Gains achieved from using an Optimal Trajectory for a
  54.  *  Price Process as a Function of the Bayesian Drift Parameters. 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 BayesianDriftTransactionDependence {

  73.     static final void RunScenario (
  74.         final double dblAlphaBar,
  75.         final double dblNu,
  76.         final double dblSigma,
  77.         final double dblT,
  78.         final ParticipationRateLinear prlTemporary)
  79.         throws Exception
  80.     {
  81.         PriorDriftDistribution pdd = new PriorDriftDistribution (
  82.             dblAlphaBar,
  83.             dblNu
  84.         );

  85.         double dblTimeWidth = 0.5 * dblT;

  86.         double[] adblAlpha = pdd.realizedDrift (2);

  87.         ConditionalPriceDistribution cpd0 = new ConditionalPriceDistribution (
  88.             adblAlpha[0],
  89.             dblSigma,
  90.             1.0 * dblTimeWidth
  91.         );

  92.         double dblPriceSwing0 = cpd0.priceVolatilitySwing();

  93.         double dblRealizedPriceChange0 = adblAlpha[0] * dblTimeWidth + dblPriceSwing0;

  94.         PriorConditionalCombiner pcc0 = new PriorConditionalCombiner (
  95.             pdd,
  96.             cpd0
  97.         );

  98.         LinearTemporaryImpact lti0 = LinearTemporaryImpact.Unconstrained (
  99.             1.0 * dblTimeWidth,
  100.             dblT,
  101.             1.,
  102.             pcc0,
  103.             dblRealizedPriceChange0,
  104.             prlTemporary
  105.         );

  106.         double dblX0 = 1. - lti0.instantaneousTradeRate() * dblTimeWidth;

  107.         ConditionalPriceDistribution cpd1 = new ConditionalPriceDistribution (
  108.             adblAlpha[1],
  109.             dblSigma,
  110.             2.0 * dblTimeWidth
  111.         );

  112.         double dblPriceSwing1 = cpd1.priceVolatilitySwing();

  113.         double dblRealizedPriceChange1 = adblAlpha[1] * dblTimeWidth + dblPriceSwing1;

  114.         PriorConditionalCombiner pcc1 = new PriorConditionalCombiner (
  115.             pdd,
  116.             cpd1
  117.         );

  118.         LinearTemporaryImpact lti1 = LinearTemporaryImpact.Unconstrained (
  119.             1.0 * dblTimeWidth,
  120.             dblT,
  121.             dblX0,
  122.             pcc1,
  123.             dblRealizedPriceChange1,
  124.             prlTemporary
  125.         );

  126.         System.out.println (
  127.             "\t|[" +
  128.             FormatUtil.FormatDouble (dblAlphaBar, 1, 1, 1.) + "," +
  129.             FormatUtil.FormatDouble (dblNu, 1, 1, 1.) + "," +
  130.             FormatUtil.FormatDouble (dblSigma, 1, 1, 1.) + "] => " +
  131.             FormatUtil.FormatDouble (lti0.trajectory().transactionCostExpectation(), 1, 3, 1.) + " | " +
  132.             FormatUtil.FormatDouble (lti0.trajectory().transactionCostVariance(), 1, 3, 1.) + " | " +
  133.             FormatUtil.FormatDouble (lti0.staticTransactionCost(), 1, 3, 1.) + " | " +
  134.             FormatUtil.FormatDouble (lti0.transactionCostGain(), 1, 3, 1.) + " || " +
  135.             FormatUtil.FormatDouble (lti1.trajectory().transactionCostExpectation(), 1, 3, 1.) + " | " +
  136.             FormatUtil.FormatDouble (lti1.trajectory().transactionCostVariance(), 1, 3, 1.) + " | " +
  137.             FormatUtil.FormatDouble (lti1.staticTransactionCost(), 1, 3, 1.) + " | " +
  138.             FormatUtil.FormatDouble (lti1.transactionCostGain(), 1, 3, 1.) + " ||"
  139.         );
  140.     }

  141.     public static final void main (
  142.         final String[] astrArgs)
  143.         throws Exception
  144.     {
  145.         EnvManager.InitEnv ("");

  146.         double dblT = 1.;
  147.         double dblEta = 0.07;

  148.         double[] adblNu = new double[] {
  149.             0.5,
  150.             1.0,
  151.             2.0
  152.         };

  153.         double[] adblSigma = new double[] {
  154.             0.5,
  155.             1.7,
  156.             2.9
  157.         };

  158.         double[] adblAlphaBar = new double[] {
  159.             0.2,
  160.             0.7,
  161.             1.2
  162.         };

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

  164.         System.out.println();

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

  166.         System.out.println ("\t|                     BAYESIAN GAIN INPUT DRIFT DISTRIBUTION DEPENDENCE                     ||");

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

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

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

  170.         System.out.println ("\t|             - Alpha Bar                                                                   ||");

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

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

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

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

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

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

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

  178.         System.out.println ("\t|             - Phase #1 Transaction Cost Expectation                                       ||");

  179.         System.out.println ("\t|             - Phase #1 Transaction Cost Variance                                          ||");

  180.         System.out.println ("\t|             - Phase #1 Static Transaction Cost                                            ||");

  181.         System.out.println ("\t|             - Phase #1 Transaction Cost Gain                                              ||");

  182.         System.out.println ("\t|             - Phase #2 Transaction Cost Expectation                                       ||");

  183.         System.out.println ("\t|             - Phase #2 Transaction Cost Variance                                          ||");

  184.         System.out.println ("\t|             - Phase #2 Static Transaction Cost                                            ||");

  185.         System.out.println ("\t|             - Phase #2 Transaction Cost Gain                                              ||");

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

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

  188.         for (double dblAlphaBar : adblAlphaBar) {
  189.             for (double dblNu : adblNu) {
  190.                 for (double dblSigma : adblSigma)
  191.                     RunScenario (
  192.                         dblAlphaBar,
  193.                         dblNu,
  194.                         dblSigma,
  195.                         dblT,
  196.                         prlTemporary
  197.                     );
  198.             }
  199.         }

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