CorrelatedPathVertexDimension.java

  1. package org.drip.measure.discrete;

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

  76. /**
  77.  * <i>CorrelatedPathVertexDimension</i> generates Correlated R^d Random Numbers at the specified Vertexes,
  78.  * over the Specified Paths.
  79.  *
  80.  *  <br><br>
  81.  *  <ul>
  82.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ComputationalCore.md">Computational Core Module</a></li>
  83.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/NumericalAnalysisLibrary.md">Numerical Analysis Library</a></li>
  84.  *      <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>
  85.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/measure/discrete/README.md">Antithetic, Quadratically Re-sampled, De-biased Distribution</a></li>
  86.  *  </ul>
  87.  *
  88.  * @author Lakshmi Krishnamurthy
  89.  */

  90. public class CorrelatedPathVertexDimension {
  91.     private int _iNumPath = -1;
  92.     private int _iNumVertex = -1;
  93.     private double[][] _aadblCholesky = null;
  94.     private boolean _bApplyAntithetic = false;
  95.     private double[][] _aadblCorrelation = null;
  96.     private org.drip.measure.crng.RandomNumberGenerator _rng = null;
  97.     private org.drip.measure.discrete.QuadraticResampler _qr = null;

  98.     /**
  99.      * CorrelatedPathVertexDimension Constructor
  100.      *
  101.      * @param rng The Random Number Generator
  102.      * @param aadblCorrelation The Correlation Matrix
  103.      * @param iNumVertex Number of Vertexes
  104.      * @param iNumPath Number of Paths
  105.      * @param bApplyAntithetic TRUE - Apply Antithetic Variables Based Variance Reduction
  106.      * @param qr Quadratic Resampler Instance
  107.      *
  108.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  109.      */

  110.     public CorrelatedPathVertexDimension (
  111.         final org.drip.measure.crng.RandomNumberGenerator rng,
  112.         final double[][] aadblCorrelation,
  113.         final int iNumVertex,
  114.         final int iNumPath,
  115.         final boolean bApplyAntithetic,
  116.         final org.drip.measure.discrete.QuadraticResampler qr)
  117.         throws java.lang.Exception
  118.     {
  119.         if (null == (_rng = rng) || 0 >= (_iNumVertex = iNumVertex) || 0 >= (_iNumPath = iNumPath))
  120.             throw new java.lang.Exception ("CorrelatedPathVertexDimension Constructor => Invalid Inputs");

  121.         _qr = qr;
  122.         _bApplyAntithetic = bApplyAntithetic;

  123.         if (null == (_aadblCholesky = org.drip.numerical.linearalgebra.Matrix.CholeskyBanachiewiczFactorization
  124.             (_aadblCorrelation = aadblCorrelation)))
  125.             throw new java.lang.Exception ("CorrelatedPathVertexDimension Constructor => Invalid Inputs");
  126.     }

  127.     /**
  128.      * Retrieve the Random Number Generator
  129.      *
  130.      * @return The Random Number Generator Instance
  131.      */

  132.     public org.drip.measure.crng.RandomNumberGenerator randomNumberGenerator()
  133.     {
  134.         return _rng;
  135.     }

  136.     /**
  137.      * Retrieve the Correlation Matrix
  138.      *
  139.      * @return The Correlation Matrix
  140.      */

  141.     public double[][] correlation()
  142.     {
  143.         return _aadblCorrelation;
  144.     }

  145.     /**
  146.      * Retrieve the Cholesky Matrix
  147.      *
  148.      * @return The Cholesky Matrix
  149.      */

  150.     public double[][] cholesky()
  151.     {
  152.         return _aadblCholesky;
  153.     }

  154.     /**
  155.      * Retrieve the Number of Vertexes
  156.      *
  157.      * @return The Number of Vertexes
  158.      */

  159.     public int numVertex()
  160.     {
  161.         return _iNumVertex;
  162.     }

  163.     /**
  164.      * Retrieve the Number of Paths
  165.      *
  166.      * @return The Number of Paths
  167.      */

  168.     public int numPath()
  169.     {
  170.         return _iNumPath;
  171.     }

  172.     /**
  173.      * Retrieve the Number of Dimensions
  174.      *
  175.      * @return The Number of Dimensions
  176.      */

  177.     public int numDimension()
  178.     {
  179.         return _aadblCholesky.length;
  180.     }

  181.     /**
  182.      * Indicate if the Antithetic Variable Generation is to be applied
  183.      *
  184.      * @return TRUE - Apply Antithetic Variables Based Variance Reduction
  185.      */

  186.     public boolean applyAntithetic()
  187.     {
  188.         return _bApplyAntithetic;
  189.     }

  190.     /**
  191.      * Retrieve the Quadratic Resampler Instance
  192.      *
  193.      * @return The Quadratic Resampler Instance
  194.      */

  195.     public org.drip.measure.discrete.QuadraticResampler quadraticResampler()
  196.     {
  197.         return _qr;
  198.     }

  199.     /**
  200.      * Generate a Straight Single R^d Vertex Realization
  201.      *
  202.      * @return Straight Single R^d Vertex Realization
  203.      */

  204.     public double[] straightVertexRealization()
  205.     {
  206.         int iDimension = _aadblCholesky.length;
  207.         double[] adblRdCorrelated = new double[iDimension];
  208.         double[] adblRdUncorrelated = new double[iDimension];

  209.         for (int i = 0; i < iDimension; ++i) {
  210.             try {
  211.                 adblRdUncorrelated[i] = org.drip.measure.gaussian.NormalQuadrature.InverseCDF
  212.                     (_rng.nextDouble01());
  213.             } catch (java.lang.Exception e) {
  214.                 e.printStackTrace();

  215.                 return null;
  216.             }
  217.         }

  218.         for (int i = 0; i < iDimension; ++i) {
  219.             adblRdCorrelated[i] = 0.;

  220.             for (int j = 0; j < iDimension; ++j)
  221.                 adblRdCorrelated[i] += _aadblCholesky[i][j] * adblRdUncorrelated[j];
  222.         }

  223.         return adblRdCorrelated;
  224.     }

  225.     /**
  226.      * Generate an Antithetic R^d Vertex Pair Realization
  227.      *
  228.      * @return Antithetic R^d Vertex Pair Realization
  229.      */

  230.     public double[][] antitheticVertexPairRealization()
  231.     {
  232.         double[] adblStraightVertexRealization = straightVertexRealization();

  233.         int iDimension = _aadblCholesky.length;
  234.         double[] adblAntitheticVertexRealization = new double[iDimension];

  235.         for (int i = 0; i < iDimension; ++i)
  236.             adblAntitheticVertexRealization[i] = -1. * adblStraightVertexRealization[i];

  237.         return new double[][] {adblStraightVertexRealization, adblAntitheticVertexRealization};
  238.     }

  239.     /**
  240.      * Generate a Single Straight Path R^d Vertex Realization
  241.      *
  242.      * @return Single Straight Path R^d Vertex Realization
  243.      */

  244.     public org.drip.measure.discrete.VertexRd straightPathVertexRd()
  245.     {
  246.         org.drip.measure.discrete.VertexRd vertexRealization = new
  247.             org.drip.measure.discrete.VertexRd();

  248.         for (int i = 0; i < _iNumVertex; ++i) {
  249.             if (!vertexRealization.add (i, straightVertexRealization())) return null;
  250.         }

  251.         return null != _qr ? org.drip.measure.discrete.VertexRd.FromFlatForm (_qr.transform
  252.             (vertexRealization.flatform())) : vertexRealization;
  253.     }

  254.     /**
  255.      * Generate an Antithetic Pair Path R^d Vertex Realizations
  256.      *
  257.      * @return Antithetic Pair Path R^d Vertex Realizations
  258.      */

  259.     public org.drip.measure.discrete.VertexRd[] antitheticPairPathVertexRd()
  260.     {
  261.         org.drip.measure.discrete.VertexRd straightVertexRealization = new
  262.             org.drip.measure.discrete.VertexRd();

  263.         org.drip.measure.discrete.VertexRd antitheticVertexRealization = new
  264.             org.drip.measure.discrete.VertexRd();

  265.         for (int i = 0; i < _iNumVertex; ++i) {
  266.             double[][] aadblStraightAntitheticRealization = antitheticVertexPairRealization();

  267.             if (!straightVertexRealization.add (i, aadblStraightAntitheticRealization[0]) ||
  268.                 !antitheticVertexRealization.add (i, aadblStraightAntitheticRealization[1]))
  269.                 return null;
  270.         }

  271.         if (null == _qr)
  272.             return new org.drip.measure.discrete.VertexRd[] {straightVertexRealization,
  273.                 antitheticVertexRealization};

  274.         return new org.drip.measure.discrete.VertexRd[] {org.drip.measure.discrete.VertexRd.FromFlatForm
  275.             (_qr.transform (straightVertexRealization.flatform())),
  276.                 org.drip.measure.discrete.VertexRd.FromFlatForm (_qr.transform
  277.                     (antitheticVertexRealization.flatform()))};
  278.     }

  279.     /**
  280.      * Generate Straight Multi-Path R^d Vertex Realizations Array
  281.      *
  282.      * @return Straight Multi-Path R^d Vertex Realizations Array
  283.      */

  284.     public org.drip.measure.discrete.VertexRd[] straightMultiPathVertexRd()
  285.     {
  286.         org.drip.measure.discrete.VertexRd[] aVertexRd = new
  287.             org.drip.measure.discrete.VertexRd[_iNumPath];

  288.         for (int i = 0; i < _iNumPath; ++i) {
  289.             if (null == (aVertexRd[i] = straightPathVertexRd())) return null;
  290.         }

  291.         return aVertexRd;
  292.     }

  293.     /**
  294.      * Generate Antithetic Multi-Path R^d Vertex Realizations Array
  295.      *
  296.      * @return Antithetic Multi-Path R^d Vertex Realizations Array
  297.      */

  298.     public org.drip.measure.discrete.VertexRd[] antitheticMultiPathVertexRd()
  299.     {
  300.         org.drip.measure.discrete.VertexRd[] aVertexRd = new
  301.             org.drip.measure.discrete.VertexRd[_iNumPath];

  302.         int iNumGeneration = _iNumPath / 2;

  303.         for (int i = 0; i < iNumGeneration; ++i) {
  304.             org.drip.measure.discrete.VertexRd[] aAntitheticVertexRd = antitheticPairPathVertexRd();

  305.             if (null == aAntitheticVertexRd || 2 != aAntitheticVertexRd.length) return null;

  306.             if (null == (aVertexRd[i] = aAntitheticVertexRd[0]) || null == (aVertexRd[i + iNumGeneration] =
  307.                 aAntitheticVertexRd[1]))
  308.                 return null;
  309.         }

  310.         if (1 == (_iNumPath % 2)) aVertexRd[_iNumPath - 1] = straightPathVertexRd();

  311.         return aVertexRd;
  312.     }

  313.     /**
  314.      * Generate Multi-Path R^d Vertex Realizations Array
  315.      *
  316.      * @return Multi-Path R^d Vertex Realizations Array
  317.      */

  318.     public org.drip.measure.discrete.VertexRd[] multiPathVertexRd()
  319.     {
  320.         return _bApplyAntithetic ? antitheticMultiPathVertexRd() : straightMultiPathVertexRd();
  321.     }
  322. }