BayesianPriceProcess.java

  1. package org.drip.sample.trend;

  2. import org.drip.execution.bayesian.*;
  3. import org.drip.measure.gaussian.R1UnivariateNormal;
  4. import org.drip.numerical.common.FormatUtil;
  5. import org.drip.service.env.EnvManager;

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

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

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

  71. public class BayesianPriceProcess {

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

  77.         int iN = 25;
  78.         double dblT = 1.0;
  79.         double dblNu = 1.0;
  80.         double dblS0 = 100.;
  81.         double dblSigma = 1.5;
  82.         double dblAlphaBar = 0.7;

  83.         double dblTime = 0.;
  84.         double dblPrice = dblS0;
  85.         double dblTimeWidth = dblT / iN;

  86.         PriorDriftDistribution pdd = new PriorDriftDistribution (
  87.             dblAlphaBar,
  88.             dblNu
  89.         );

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

  91.         System.out.println();

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

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

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

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

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

  97.         System.out.println ("\t|    - Realized Price Volatility Swing             ||");

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

  99.         System.out.println ("\t|    - MAP Drift Estimate                          ||");

  100.         System.out.println ("\t|    - Posterior Drift Volatility                  ||");

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

  102.         for (int i = 0; i < iN; ++i) {
  103.             dblTime = dblTime + dblTimeWidth;

  104.             ConditionalPriceDistribution cpd = new ConditionalPriceDistribution (
  105.                 adblAlpha[i],
  106.                 dblSigma,
  107.                 dblTime
  108.             );

  109.             double dblPriceSwing = cpd.priceVolatilitySwing();

  110.             double dblRealizedPriceChange = adblAlpha[i] * dblTimeWidth + dblPriceSwing;
  111.             dblPrice = dblPrice + dblRealizedPriceChange;

  112.             PriorConditionalCombiner pcc = new PriorConditionalCombiner (
  113.                 pdd,
  114.                 cpd
  115.             );

  116.             R1UnivariateNormal r1unPosterior = pcc.posteriorDriftDistribution (dblRealizedPriceChange);

  117.             System.out.println (
  118.                 "\t| " + FormatUtil.FormatDouble (dblTime, 1, 2, 1.) + " => " +
  119.                 FormatUtil.FormatDouble (adblAlpha[i], 1, 2, 1.) + " | " +
  120.                 FormatUtil.FormatDouble (dblPriceSwing, 1, 2, 1.) + " | " +
  121.                 FormatUtil.FormatDouble (dblPrice, 3, 2, 1.) + " | " +
  122.                 FormatUtil.FormatDouble (r1unPosterior.mean(), 1, 2, 1.) + " | " +
  123.                 FormatUtil.FormatDouble (Math.sqrt (r1unPosterior.variance()), 1, 2, 1.) + " ||"
  124.             );
  125.         }

  126.         System.out.println ("\t|--------------------------------------------------||");
  127.     }
  128. }