OrnsteinUhlenbeckPair.java

  1. package org.drip.measure.process;

  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>OrnsteinUhlenbeckPair</i> guides the Random Variable Evolution according to 2D Ornstein-Uhlenbeck Mean
  79.  * Reverting Process. The References are:
  80.  *
  81.  * <br><br>
  82.  *  <ul>
  83.  *      <li>
  84.  *          Almgren, R. F. (2009): Optimal Trading in a Dynamic Market
  85.  *              https://www.math.nyu.edu/financial_mathematics/content/02_financial/2009-2.pdf
  86.  *      </li>
  87.  *      <li>
  88.  *          Almgren, R. F. (2012): Optimal Trading with Stochastic Liquidity and Volatility <i>SIAM Journal
  89.  *              of Financial Mathematics</i> <b>3 (1)</b> 163-181
  90.  *      </li>
  91.  *      <li>
  92.  *          Geman, H., D. B. Madan, and M. Yor (2001): Time Changes for Levy Processes <i>Mathematical
  93.  *              Finance</i> <b>11 (1)</b> 79-96
  94.  *      </li>
  95.  *      <li>
  96.  *          Jones, C. M., G. Kaul, and M. L. Lipson (1994): Transactions, Volume, and Volatility <i>Review of
  97.  *              Financial Studies</i> <b>7 (4)</b> 631-651
  98.  *      </li>
  99.  *      <li>
  100.  *          Walia, N. (2006): <i>Optimal Trading - Dynamic Stock Liquidation Strategies</i> <b>Princeton
  101.  *              University</b>
  102.  *      </li>
  103.  *  </ul>
  104.  *
  105.  *  <br><br>
  106.  *  <ul>
  107.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ComputationalCore.md">Computational Core Module</a></li>
  108.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/NumericalAnalysisLibrary.md">Numerical Analysis Library</a></li>
  109.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/measure/README.md">R<sup>d</sup> Continuous/Discrete Probability Measures</a></li>
  110.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/measure/process/README.md">Jump Diffusion Evolver Process Variants</a></li>
  111.  *  </ul>
  112.  *
  113.  * @author Lakshmi Krishnamurthy
  114.  */

  115. public class OrnsteinUhlenbeckPair implements org.drip.measure.process.OrnsteinUhlenbeck {
  116.     private double _dblCorrelation = java.lang.Double.NaN;
  117.     private org.drip.measure.dynamics.DiffusionEvaluatorOrnsteinUhlenbeck _deouDerived = null;
  118.     private org.drip.measure.dynamics.DiffusionEvaluatorOrnsteinUhlenbeck _deouReference = null;

  119.     /**
  120.      * OrnsteinUhlenbeckPair Constructor
  121.      *
  122.      * @param deouReference The Reference R^1 Ornstein-Uhlenbeck Evaluator
  123.      * @param deouDerived The Derived R^1 Ornstein-Uhlenbeck Evaluator
  124.      * @param dblCorrelation The Correlation between the Two Ornstein-Uhlenbeck Processes
  125.      *
  126.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  127.      */

  128.     public OrnsteinUhlenbeckPair (
  129.         final org.drip.measure.dynamics.DiffusionEvaluatorOrnsteinUhlenbeck deouReference,
  130.         final org.drip.measure.dynamics.DiffusionEvaluatorOrnsteinUhlenbeck deouDerived,
  131.         final double dblCorrelation)
  132.         throws java.lang.Exception
  133.     {
  134.         if (null == (_deouReference = deouReference) || null == (_deouDerived = deouDerived) ||
  135.             !org.drip.numerical.common.NumberUtil.IsValid (_dblCorrelation = dblCorrelation) || _dblCorrelation <
  136.                 -1. || _dblCorrelation > 1.)
  137.             throw new java.lang.Exception ("OrnsteinUhlenbeckPair Constructor => Invalid Inputs");
  138.     }

  139.     /**
  140.      * Retrieve the Reference R^1 Ornstein-Uhlenbeck Evaluator
  141.      *
  142.      * @return The Reference R^1 Ornstein-Uhlenbeck Evaluator
  143.      */

  144.     public org.drip.measure.dynamics.DiffusionEvaluatorOrnsteinUhlenbeck reference()
  145.     {
  146.         return _deouReference;
  147.     }

  148.     /**
  149.      * Retrieve the Derived R^1 Ornstein-Uhlenbeck Evaluator
  150.      *
  151.      * @return The Derived R^1 Ornstein-Uhlenbeck Evaluator
  152.      */

  153.     public org.drip.measure.dynamics.DiffusionEvaluatorOrnsteinUhlenbeck derived()
  154.     {
  155.         return _deouDerived;
  156.     }

  157.     /**
  158.      * Retrieve the Correlation between the Ornstein-Uhlenbeck Processes
  159.      *
  160.      * @return The Correlation between the Ornstein-Uhlenbeck Processes
  161.      */

  162.     public double correlation()
  163.     {
  164.         return _dblCorrelation;
  165.     }

  166.     /**
  167.      * Generate the Adjacent JumpDiffusionEdge Increment Array from the specified Ornstein Uhlenbeck Random
  168.      *      Variate Pair
  169.      *
  170.      * @param adblVariatePair The Pair of the Ornstein Uhlenbeck Random Variates
  171.      * @param adblDiffusionPair The Pair of Diffusion Realizations
  172.      * @param dblTimeIncrement The Time Increment Evolution Unit
  173.      *
  174.      * @return The Adjacent JumpDiffusionEdge Increment Array
  175.      */

  176.     public org.drip.measure.realization.JumpDiffusionEdge[] increment (
  177.         final double[] adblVariatePair,
  178.         final double[] adblDiffusionPair,
  179.         final double dblTimeIncrement)
  180.     {
  181.         if (null == adblVariatePair || 2 != adblVariatePair.length ||
  182.             !org.drip.numerical.common.NumberUtil.IsValid (adblVariatePair) || null == adblDiffusionPair || 2 !=
  183.                 adblDiffusionPair.length || !org.drip.numerical.common.NumberUtil.IsValid (adblDiffusionPair) ||
  184.                     !org.drip.numerical.common.NumberUtil.IsValid (dblTimeIncrement) || 0. >= dblTimeIncrement)
  185.             return null;

  186.         double dblRelaxationTime0 = _deouReference.relaxationTime();

  187.         double dblRelaxationTime1 = _deouDerived.relaxationTime();

  188.         try {
  189.             return new org.drip.measure.realization.JumpDiffusionEdge[] {
  190.                 org.drip.measure.realization.JumpDiffusionEdge.Standard (
  191.                     adblVariatePair[0],
  192.                     -1. * adblVariatePair[0] / dblRelaxationTime0 * dblTimeIncrement,
  193.                     _deouReference.burstiness() * adblDiffusionPair[0] * java.lang.Math.sqrt (dblTimeIncrement / dblRelaxationTime0),
  194.                     null,
  195.                     new org.drip.measure.realization.JumpDiffusionEdgeUnit (
  196.                         dblTimeIncrement,
  197.                         adblDiffusionPair[0],
  198.                         0.
  199.                     )
  200.                 ),
  201.                 org.drip.measure.realization.JumpDiffusionEdge.Standard (
  202.                     adblVariatePair[1],
  203.                     -1. * adblVariatePair[1] / dblRelaxationTime1 * dblTimeIncrement,
  204.                     _deouDerived.burstiness() * adblDiffusionPair[1] * java.lang.Math.sqrt (dblTimeIncrement / dblRelaxationTime1),
  205.                     null,
  206.                     new org.drip.measure.realization.JumpDiffusionEdgeUnit (
  207.                         dblTimeIncrement,
  208.                         adblDiffusionPair[1],
  209.                         0.
  210.                     )
  211.                 )
  212.             };
  213.         } catch (java.lang.Exception e) {
  214.             e.printStackTrace();
  215.         }

  216.         return null;
  217.     }

  218.     /**
  219.      * Generate the Weiner Based JumpDiffusionEdge Increment Sequence from the Current Ornstein Uhlenbeck
  220.      *      Random Variate
  221.      *
  222.      * @param adblVariatePair The Ornstein Uhlenbeck Random Variate Pair
  223.      * @param dblTimeIncrement The Time Increment
  224.      *
  225.      * @return The Weiner Based JumpDiffusionEdge Increment Sequence from the Current Ornstein Uhlenbeck
  226.      *      Random Variate
  227.      */

  228.     public org.drip.measure.realization.JumpDiffusionEdge[] weinerIncrement (
  229.         final double[] adblVariatePair,
  230.         final double dblTimeIncrement)
  231.     {
  232.         try {
  233.             double dblFirstWeiner = org.drip.measure.gaussian.NormalQuadrature.Random();

  234.             return increment (adblVariatePair, new double[] {dblFirstWeiner, dblFirstWeiner * _dblCorrelation
  235.                 + org.drip.measure.gaussian.NormalQuadrature.Random() * java.lang.Math.sqrt (1. -
  236.                     _dblCorrelation * _dblCorrelation)}, dblTimeIncrement);
  237.         } catch (java.lang.Exception e) {
  238.             e.printStackTrace();
  239.         }

  240.         return null;
  241.     }

  242.     @Override public double referenceRelaxationTime()
  243.     {
  244.         return _deouReference.relaxationTime();
  245.     }

  246.     @Override public double referenceBurstiness()
  247.     {
  248.         return _deouReference.burstiness();
  249.     }

  250.     @Override public double referenceMeanReversionLevel()
  251.     {
  252.         return _deouReference.meanReversionLevel();
  253.     }
  254. }