MultivariateSequenceGenerator.java

  1. package org.drip.sequence.random;

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

  5. /*!
  6.  * Copyright (C) 2019 Lakshmi Krishnamurthy
  7.  * Copyright (C) 2018 Lakshmi Krishnamurthy
  8.  * Copyright (C) 2017 Lakshmi Krishnamurthy
  9.  * Copyright (C) 2016 Lakshmi Krishnamurthy
  10.  * Copyright (C) 2015 Lakshmi Krishnamurthy
  11.  *
  12.  *  This file is part of DROP, an open-source library targeting risk, transaction costs, exposure, margin
  13.  *      calculations, and portfolio construction within and across fixed income, credit, commodity, equity,
  14.  *      FX, and structured products.
  15.  *  
  16.  *      https://lakshmidrip.github.io/DROP/
  17.  *  
  18.  *  DROP is composed of three main modules:
  19.  *  
  20.  *  - DROP Analytics Core - https://lakshmidrip.github.io/DROP-Analytics-Core/
  21.  *  - DROP Portfolio Core - https://lakshmidrip.github.io/DROP-Portfolio-Core/
  22.  *  - DROP Numerical Core - https://lakshmidrip.github.io/DROP-Numerical-Core/
  23.  *
  24.  *  DROP Analytics Core implements libraries for the following:
  25.  *  - Fixed Income Analytics
  26.  *  - Asset Backed Analytics
  27.  *  - XVA Analytics
  28.  *  - Exposure and Margin Analytics
  29.  *
  30.  *  DROP Portfolio Core implements libraries for the following:
  31.  *  - Asset Allocation Analytics
  32.  *  - Transaction Cost Analytics
  33.  *
  34.  *  DROP Numerical Core implements libraries for the following:
  35.  *  - Statistical Learning Library
  36.  *  - Numerical Optimizer Library
  37.  *  - Machine Learning Library
  38.  *  - Spline Builder Library
  39.  *
  40.  *  Documentation for DROP is Spread Over:
  41.  *
  42.  *  - Main                     => https://lakshmidrip.github.io/DROP/
  43.  *  - Wiki                     => https://github.com/lakshmiDRIP/DROP/wiki
  44.  *  - GitHub                   => https://github.com/lakshmiDRIP/DROP
  45.  *  - Javadoc                  => https://lakshmidrip.github.io/DROP/Javadoc/index.html
  46.  *  - Technical Specifications => https://github.com/lakshmiDRIP/DROP/tree/master/Docs/Internal
  47.  *  - Release Versions         => https://lakshmidrip.github.io/DROP/version.html
  48.  *  - Community Credits        => https://lakshmidrip.github.io/DROP/credits.html
  49.  *  - Issues Catalog           => https://github.com/lakshmiDRIP/DROP/issues
  50.  *  - JUnit                    => https://lakshmidrip.github.io/DROP/junit/index.html
  51.  *  - Jacoco                   => https://lakshmidrip.github.io/DROP/jacoco/index.html
  52.  *
  53.  *  Licensed under the Apache License, Version 2.0 (the "License");
  54.  *      you may not use this file except in compliance with the License.
  55.  *  
  56.  *  You may obtain a copy of the License at
  57.  *      http://www.apache.org/licenses/LICENSE-2.0
  58.  *  
  59.  *  Unless required by applicable law or agreed to in writing, software
  60.  *      distributed under the License is distributed on an "AS IS" BASIS,
  61.  *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  62.  *  
  63.  *  See the License for the specific language governing permissions and
  64.  *      limitations under the License.
  65.  */

  66. /**
  67.  * <i>MultivariateSequenceGenerator</i> implements the Multivariate Random Sequence Generator Functionality.
  68.  *
  69.  * <br><br>
  70.  *  <ul>
  71.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/NumericalCore.md">Numerical Core Module</a></li>
  72.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/StatisticalLearningLibrary.md">Statistical Learning Library</a></li>
  73.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sequence">Sequence</a></li>
  74.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sequence/random">Random</a></li>
  75.  *  </ul>
  76.  * <br><br>
  77.  *
  78.  * @author Lakshmi Krishnamurthy
  79.  */

  80. public class MultivariateSequenceGenerator {
  81.     private double[][] _aadblCholesky = null;
  82.     private double[][] _aadblCorrelation = null;
  83.     private org.drip.sequence.random.UnivariateSequenceGenerator[] _aUSG = null;

  84.     /**
  85.      * MultivariateSequenceGenerator Constructor
  86.      *
  87.      * @param aUSG Array of Univariate Sequence Generators
  88.      * @param aadblCorrelation The Correlation Matrix
  89.      *
  90.      * @throws java.lang.Exception Thrown if the Inputs are invalid
  91.      */

  92.     public MultivariateSequenceGenerator (
  93.         final org.drip.sequence.random.UnivariateSequenceGenerator[] aUSG,
  94.         final double[][] aadblCorrelation)
  95.         throws java.lang.Exception
  96.     {
  97.         if (null == (_aUSG = aUSG) || null == (_aadblCorrelation = aadblCorrelation))
  98.             throw new java.lang.Exception ("MultivariateSequenceGenerator ctr: Invalid Inputs");

  99.         _aadblCholesky = org.drip.numerical.linearalgebra.Matrix.CholeskyBanachiewiczFactorization
  100.             (aadblCorrelation);

  101.         int iNumVariate = aUSG.length;

  102.         if (null == _aadblCholesky || null == _aadblCholesky[0] || iNumVariate != _aadblCholesky.length ||
  103.             iNumVariate != _aadblCholesky[0].length)
  104.             throw new java.lang.Exception ("MultivariateSequenceGenerator ctr: Invalid Inputs");

  105.         for (int i = 0; i < iNumVariate; ++i) {
  106.             if (null == _aUSG[i])
  107.                 throw new java.lang.Exception ("MultivariateSequenceGenerator ctr: Invalid Inputs");

  108.             for (int j = 0; j < iNumVariate; ++j) {
  109.                 if (!org.drip.numerical.common.NumberUtil.IsValid (_aadblCorrelation[i][j]))
  110.                     throw new java.lang.Exception ("MultivariateSequenceGenerator ctr: Invalid Inputs");
  111.             }
  112.         }
  113.     }

  114.     /**
  115.      * Retrieve the Array of Univariate Sequence Generators
  116.      *
  117.      * @return Array of Univariate Sequence Generators
  118.      */

  119.     public org.drip.sequence.random.UnivariateSequenceGenerator[] usg()
  120.     {
  121.         return _aUSG;
  122.     }

  123.     /**
  124.      * Retrieve the Correlation Matrix
  125.      *
  126.      * @return The Correlation Matrix
  127.      */

  128.     public double[][] correlation()
  129.     {
  130.         return _aadblCorrelation;
  131.     }

  132.     /**
  133.      * Retrieve the Cholesky Factorial
  134.      *
  135.      * @return The Cholesky Factorial
  136.      */

  137.     public double[][] cholesky()
  138.     {
  139.         return _aadblCholesky;
  140.     }

  141.     /**
  142.      * Retrieve the Number of Variates
  143.      *
  144.      * @return The Number of Variates
  145.      */

  146.     public int numVariate()
  147.     {
  148.         return _aUSG.length;
  149.     }

  150.     /**
  151.      * Generate the Set of Multivariate Random Numbers according to the specified rule
  152.      *
  153.      * @return The Set of Multivariate Random Numbers
  154.      */

  155.     public double[] random()
  156.     {
  157.         int iNumVariate = _aUSG.length;
  158.         double[] adblRandom = new double[iNumVariate];
  159.         double[] adblUncorrelatedRandom = new double[iNumVariate];

  160.         for (int i = 0; i < iNumVariate; ++i)
  161.             adblUncorrelatedRandom[i] = _aUSG[i].random();

  162.         for (int i = 0; i < iNumVariate; ++i) {
  163.             adblRandom[i] = 0.;

  164.             for (int j = 0; j < iNumVariate; ++j)
  165.                 adblRandom[i] += _aadblCholesky[i][j] * adblUncorrelatedRandom[j];
  166.         }

  167.         return adblRandom;
  168.     }
  169. }