BayesianGain.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.  * BayesianGain demonstrates the Gains achieved from using an Optimal Trajectory for a Price Process with
  54.  *  Bayesian Drift, Arithmetic Volatility, and Linear Temporary Market Impact across a Set of Drifts. The
  55.  *  References 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 BayesianGain {

  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 dblAlphaBar = 0.7;

  86.         double dblTime = 0.;
  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|    - Realized Drift                                                   ||");

  101.         System.out.println ("\t|    - Realized Price Change                                            ||");

  102.         System.out.println ("\t|    - Estimated Drift                                                  ||");

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

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

  105.         System.out.println ("\t|    - Transaction Cost                                                 ||");

  106.         System.out.println ("\t|    - Transaction Cost Gain                                            ||");

  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.             LinearTemporaryImpact lti = LinearTemporaryImpact.Unconstrained (
  122.                 dblTime,
  123.                 dblT,
  124.                 dblXUnconstrained,
  125.                 pcc,
  126.                 dblRealizedPriceChange,
  127.                 prlTemporary
  128.             );

  129.             double dblUnconstrainedInstantaneousTradeRate = lti.instantaneousTradeRate();

  130.             dblXUnconstrained = dblXUnconstrained - dblUnconstrainedInstantaneousTradeRate * dblTimeWidth;

  131.             System.out.println (
  132.                 "\t| " + FormatUtil.FormatDouble (dblTime, 1, 2, 1.) + " => " +
  133.                 FormatUtil.FormatDouble (adblAlpha[i], 1, 3, 1.) + " | " +
  134.                 FormatUtil.FormatDouble (dblRealizedPriceChange, 1, 3, 1.) + " | " +
  135.                 FormatUtil.FormatDouble (lti.driftExpectationEstimate(), 1, 3, 1.) + " | " +
  136.                 FormatUtil.FormatDouble (dblUnconstrainedInstantaneousTradeRate, 1, 3, 1.) + " | " +
  137.                 FormatUtil.FormatDouble (dblXUnconstrained, 1, 3, 1.) + " | " +
  138.                 FormatUtil.FormatDouble (lti.staticTransactionCost(), 1, 3, 1.) + " | " +
  139.                 FormatUtil.FormatDouble (lti.transactionCostGain(), 1, 3, 1.) + " ||"
  140.             );
  141.         }

  142.         System.out.println ("\t|-----------------------------------------------------------------------||");
  143.     }
  144. }