OrnsteinUhlenbeckSequence.java

  1. package org.drip.execution.latent;

  2. /*
  3.  * -*- mode: java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  4.  */

  5. /*!
  6.  * Copyright (C) 2020 Lakshmi Krishnamurthy
  7.  * Copyright (C) 2019 Lakshmi Krishnamurthy
  8.  * Copyright (C) 2018 Lakshmi Krishnamurthy
  9.  * Copyright (C) 2017 Lakshmi Krishnamurthy
  10.  * Copyright (C) 2016 Lakshmi Krishnamurthy
  11.  *
  12.  *  This file is part of DROP, an open-source library targeting analytics/risk, transaction cost analytics,
  13.  *      asset liability management analytics, capital, exposure, and margin analytics, valuation adjustment
  14.  *      analytics, and portfolio construction analytics within and across fixed income, credit, commodity,
  15.  *      equity, FX, and structured products. It also includes auxiliary libraries for algorithm support,
  16.  *      numerical analysis, numerical optimization, spline builder, model validation, statistical learning,
  17.  *      and computational support.
  18.  *  
  19.  *      https://lakshmidrip.github.io/DROP/
  20.  *  
  21.  *  DROP is composed of three modules:
  22.  *  
  23.  *  - DROP Product Core - https://lakshmidrip.github.io/DROP-Product-Core/
  24.  *  - DROP Portfolio Core - https://lakshmidrip.github.io/DROP-Portfolio-Core/
  25.  *  - DROP Computational Core - https://lakshmidrip.github.io/DROP-Computational-Core/
  26.  *
  27.  *  DROP Product Core implements libraries for the following:
  28.  *  - Fixed Income Analytics
  29.  *  - Loan Analytics
  30.  *  - Transaction Cost Analytics
  31.  *
  32.  *  DROP Portfolio Core implements libraries for the following:
  33.  *  - Asset Allocation Analytics
  34.  *  - Asset Liability Management Analytics
  35.  *  - Capital Estimation Analytics
  36.  *  - Exposure Analytics
  37.  *  - Margin Analytics
  38.  *  - XVA Analytics
  39.  *
  40.  *  DROP Computational Core implements libraries for the following:
  41.  *  - Algorithm Support
  42.  *  - Computation Support
  43.  *  - Function Analysis
  44.  *  - Model Validation
  45.  *  - Numerical Analysis
  46.  *  - Numerical Optimizer
  47.  *  - Spline Builder
  48.  *  - Statistical Learning
  49.  *
  50.  *  Documentation for DROP is Spread Over:
  51.  *
  52.  *  - Main                     => https://lakshmidrip.github.io/DROP/
  53.  *  - Wiki                     => https://github.com/lakshmiDRIP/DROP/wiki
  54.  *  - GitHub                   => https://github.com/lakshmiDRIP/DROP
  55.  *  - Repo Layout Taxonomy     => https://github.com/lakshmiDRIP/DROP/blob/master/Taxonomy.md
  56.  *  - Javadoc                  => https://lakshmidrip.github.io/DROP/Javadoc/index.html
  57.  *  - Technical Specifications => https://github.com/lakshmiDRIP/DROP/tree/master/Docs/Internal
  58.  *  - Release Versions         => https://lakshmidrip.github.io/DROP/version.html
  59.  *  - Community Credits        => https://lakshmidrip.github.io/DROP/credits.html
  60.  *  - Issues Catalog           => https://github.com/lakshmiDRIP/DROP/issues
  61.  *  - JUnit                    => https://lakshmidrip.github.io/DROP/junit/index.html
  62.  *  - Jacoco                   => https://lakshmidrip.github.io/DROP/jacoco/index.html
  63.  *
  64.  *  Licensed under the Apache License, Version 2.0 (the "License");
  65.  *      you may not use this file except in compliance with the License.
  66.  *  
  67.  *  You may obtain a copy of the License at
  68.  *      http://www.apache.org/licenses/LICENSE-2.0
  69.  *  
  70.  *  Unless required by applicable law or agreed to in writing, software
  71.  *      distributed under the License is distributed on an "AS IS" BASIS,
  72.  *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  73.  *  
  74.  *  See the License for the specific language governing permissions and
  75.  *      limitations under the License.
  76.  */

  77. /**
  78.  * <i>OrnsteinUhlenbeckSequence</i> holds the Sequence of the Market State that drives the Liquidity and the
  79.  * Volatility Market States driven using an Ornstein-Uhlenbeck Process. The References are:
  80.  *
  81.  * <br><br>
  82.  *  <ul>
  83.  *      <li>
  84.  *          Almgren, R. F., and N. Chriss (2000): Optimal Execution of Portfolio Transactions <i>Journal of
  85.  *              Risk</i> <b>3 (2)</b> 5-39
  86.  *      </li>
  87.  *      <li>
  88.  *          Almgren, R. F. (2009): Optimal Trading in a Dynamic Market
  89.  *              https://www.math.nyu.edu/financial_mathematics/content/02_financial/2009-2.pdf
  90.  *      </li>
  91.  *      <li>
  92.  *          Almgren, R. F. (2012): Optimal Trading with Stochastic Liquidity and Volatility <i>SIAM Journal
  93.  *          of Financial Mathematics</i> <b>3 (1)</b> 163-181
  94.  *      </li>
  95.  *      <li>
  96.  *          Geman, H., D. B. Madan, and M. Yor (2001): Time Changes for Levy Processes <i>Mathematical
  97.  *              Finance</i> <b>11 (1)</b> 79-96
  98.  *      </li>
  99.  *      <li>
  100.  *          Jones, C. M., G. Kaul, and M. L. Lipson (1994): Transactions, Volume, and Volatility <i>Review of
  101.  *              Financial Studies</i> <b>7 (4)</b> 631-651
  102.  *      </li>
  103.  *  </ul>
  104.  *
  105.  *  <br><br>
  106.  *  <ul>
  107.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ProductCore.md">Product Core Module</a></li>
  108.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/TransactionCostAnalyticsLibrary.md">Transaction Cost Analytics</a></li>
  109.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/execution/README.md">Optimal Impact/Capture Based Trading Trajectories - Deterministic, Stochastic, Static, and Dynamic</a></li>
  110.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/execution/latent/README.md">Correlated Latent Market State Sequence</a></li>
  111.  *  </ul>
  112.  *
  113.  * @author Lakshmi Krishnamurthy
  114.  */

  115. public class OrnsteinUhlenbeckSequence {
  116.     private int _iCount = 0;
  117.     private org.drip.execution.latent.MarketState[] _aMS = null;
  118.     private double _dblGenerationInterval = java.lang.Double.NaN;
  119.     private org.drip.measure.process.OrnsteinUhlenbeck _ou = null;

  120.     /**
  121.      * Construct a Standard Systemic Instance of OrnsteinUhlenbeckSequence
  122.      *
  123.      * @param deou The 1D Ornstein-Uhlenbeck Generator Scheme
  124.      * @param dblGenerationInterval The Generation Interval
  125.      * @param dblInitialMarketState The Initial Market State
  126.      * @param iCount Count of the Number of States to be generated
  127.      *
  128.      * @return The OrnsteinUhlenbeckSequence Instance
  129.      */

  130.     public static final OrnsteinUhlenbeckSequence Systemic (
  131.         final org.drip.measure.dynamics.DiffusionEvaluatorOrnsteinUhlenbeck deou,
  132.         final double dblGenerationInterval,
  133.         final double dblInitialMarketState,
  134.         final int iCount)
  135.     {
  136.         if (null == deou || !org.drip.numerical.common.NumberUtil.IsValid (dblGenerationInterval) || 0 >=
  137.             dblGenerationInterval || 1 >= iCount)
  138.             return null;

  139.         double dblTime = 0.;
  140.         org.drip.execution.latent.MarketStateSystemic[] aMSS = new
  141.             org.drip.execution.latent.MarketStateSystemic[iCount];

  142.         try {
  143.             aMSS[0] = new org.drip.execution.latent.MarketStateSystemic (dblInitialMarketState);

  144.             org.drip.measure.process.DiffusionEvolver de = new org.drip.measure.process.DiffusionEvolver
  145.                 (deou);

  146.             for (int i = 0; i < iCount - 1; ++i) {
  147.                 org.drip.measure.realization.JumpDiffusionEdge gi = de.weinerIncrement (new
  148.                     org.drip.measure.realization.JumpDiffusionVertex (dblTime, aMSS[i].common(), 0., false),
  149.                         dblGenerationInterval);

  150.                 aMSS[i + 1] = new org.drip.execution.latent.MarketStateSystemic (aMSS[i].common() +
  151.                     gi.deterministic() + gi.diffusionStochastic());

  152.                 dblTime += dblGenerationInterval;
  153.             }
  154.         } catch (java.lang.Exception e) {
  155.             e.printStackTrace();

  156.             return null;
  157.         }

  158.         return new OrnsteinUhlenbeckSequence (deou, aMSS, dblGenerationInterval);
  159.     }

  160.     /**
  161.      * Construct a Standard Correlated Instance of OrnsteinUhlenbeckSequence
  162.      *
  163.      * @param oup2D The 2D Ornstein-Uhlenbeck Generator Scheme
  164.      * @param dblGenerationInterval The Generation Interval
  165.      * @param dblInitialLiquidityMarketState The Initial Liquidity Market State
  166.      * @param dblInitialVolatilityMarketState The Initial Volatility Market State
  167.      * @param iCount Count of the Number of States to be generated
  168.      *
  169.      * @return The OrnsteinUhlenbeckSequence Instance
  170.      */

  171.     public static final OrnsteinUhlenbeckSequence Correlated (
  172.         final org.drip.measure.process.OrnsteinUhlenbeckPair oup2D,
  173.         final double dblGenerationInterval,
  174.         final double dblInitialLiquidityMarketState,
  175.         final double dblInitialVolatilityMarketState,
  176.         final int iCount)
  177.     {
  178.         if (null == oup2D || !org.drip.numerical.common.NumberUtil.IsValid (dblGenerationInterval) || 0 >=
  179.             dblGenerationInterval || 1 >= iCount)
  180.             return null;

  181.         org.drip.execution.latent.MarketStateCorrelated[] aMSC = new
  182.             org.drip.execution.latent.MarketStateCorrelated[iCount];

  183.         try {
  184.             aMSC[0] = new org.drip.execution.latent.MarketStateCorrelated (dblInitialLiquidityMarketState,
  185.                 dblInitialVolatilityMarketState);

  186.             for (int i = 0; i < iCount - 1; ++i) {
  187.                 org.drip.measure.realization.JumpDiffusionEdge[] aGI = oup2D.weinerIncrement
  188.                     (aMSC[i].realization(), dblGenerationInterval);

  189.                 if (null == aGI || 2 != aGI.length) return null;

  190.                 aMSC[i + 1] = new org.drip.execution.latent.MarketStateCorrelated (aMSC[i].liquidity() +
  191.                     aGI[0].deterministic() + aGI[0].diffusionStochastic(), aMSC[i].volatility() +
  192.                         aGI[1].deterministic() + aGI[1].diffusionStochastic());
  193.             }
  194.         } catch (java.lang.Exception e) {
  195.             e.printStackTrace();

  196.             return null;
  197.         }

  198.         return new OrnsteinUhlenbeckSequence (oup2D, aMSC, dblGenerationInterval);
  199.     }

  200.     private OrnsteinUhlenbeckSequence (
  201.         final org.drip.measure.process.OrnsteinUhlenbeck ou,
  202.         final org.drip.execution.latent.MarketState[] aMS,
  203.         final double dblGenerationInterval)
  204.     {
  205.         _ou = ou;
  206.         _aMS = aMS;
  207.         _iCount = aMS.length;
  208.         _dblGenerationInterval = dblGenerationInterval;
  209.     }

  210.     /**
  211.      * Retrieve the Total Count of States realized
  212.      *
  213.      * @return The Total Count of States realized
  214.      */

  215.     public int count()
  216.     {
  217.         return _iCount;
  218.     }

  219.     /**
  220.      * Retrieve the Generation Interval
  221.      *
  222.      * @return The Generation Interval
  223.      */

  224.     public double generationInterval()
  225.     {
  226.         return _dblGenerationInterval;
  227.     }

  228.     /**
  229.      * Retrieve the Sequence of Market State Realization
  230.      *
  231.      * @return The Sequence of Market State Realization
  232.      */

  233.     public org.drip.execution.latent.MarketState[] realizedMarketState()
  234.     {
  235.         return _aMS;
  236.     }

  237.     /**
  238.      * Retrieve the Ornstein-Uhlenbeck Generator Scheme Parameters
  239.      *
  240.      * @return The Ornstein-Uhlenbeck Generator Scheme Parameters
  241.      */

  242.     public org.drip.measure.process.OrnsteinUhlenbeck scheme()
  243.     {
  244.         return _ou;
  245.     }

  246.     /**
  247.      * Retrieve the Initial Market State
  248.      *
  249.      * @return The Initial Market State
  250.      */

  251.     public org.drip.execution.latent.MarketState initialMarketState()
  252.     {
  253.         return _aMS[0];
  254.     }
  255. }