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

  78. /**
  79.  * <i>SequenceGenerator</i> generates the specified Univariate Sequence of the Given Distribution Type.
  80.  *
  81.  * <br><br>
  82.  *  <ul>
  83.  *      <li>
  84.  *          Backstrom, T., and J. Fischer (2018): Fast Randomization for Distributed Low Bit-rate Coding of
  85.  *              Speech and Audio <i>IEEE/ACM Transactions on Audio, Speech, and Language Processing</i> <b>26
  86.  *              (1)</b> 19-30
  87.  *      </li>
  88.  *      <li>
  89.  *          Chi-Squared Distribution (2019): Chi-Squared Function
  90.  *              https://en.wikipedia.org/wiki/Chi-squared_distribution
  91.  *      </li>
  92.  *      <li>
  93.  *          Johnson, N. L., S. Klotz, and N. Balakrishnan (1994): <i>Continuous Univariate Distributions
  94.  *              <b>1</b> 2<sup>nd</sup> Edition</i> <b>John Wiley and Sons</b>
  95.  *      </li>
  96.  *      <li>
  97.  *          Lancaster, H, O. (1969): <i>The Chi-Squared Distribution</i> <b>Wiley</b>
  98.  *      </li>
  99.  *      <li>
  100.  *          Pillai, N. S. (1026): An Unexpected Encounter with Cauchy and Levy <i>Annals of Statistics</i>
  101.  *              <b>44 (5)</b> 2089-2097
  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/discrete/README.md">Antithetic, Quadratically Re-sampled, De-biased Distribution</a></li>
  111.  *  </ul>
  112.  *
  113.  * @author Lakshmi Krishnamurthy
  114.  */

  115. public class SequenceGenerator
  116. {

  117.     /**
  118.      * Generate a Sequence of Uniform Random Numbers
  119.      *
  120.      * @param iCount The Count in the Sequence
  121.      *
  122.      * @return The Sequence of Uniform Random Numbers
  123.      */

  124.     public static final double[] Uniform (
  125.         final int iCount)
  126.     {
  127.         if (0 >= iCount) return null;

  128.         double[] adblRandom = new double[iCount];

  129.         for (int i = 0; i < iCount; ++i)
  130.             adblRandom[i] = java.lang.Math.random();

  131.         return adblRandom;
  132.     }

  133.     /**
  134.      * Generate a Sequence of Gaussian Random Numbers
  135.      *
  136.      * @param iCount The Count in the Sequence
  137.      *
  138.      * @return The Sequence of Gaussian Random Numbers
  139.      */

  140.     public static final double[] Gaussian (
  141.         final int iCount)
  142.     {
  143.         if (0 >= iCount) return null;

  144.         double[] adblRandom = new double[iCount];

  145.         for (int i = 0; i < iCount; ++i) {
  146.             try {
  147.                 adblRandom[i] = org.drip.measure.gaussian.NormalQuadrature.Random();
  148.             } catch (java.lang.Exception e) {
  149.                 e.printStackTrace();

  150.                 return null;
  151.             }
  152.         }

  153.         return adblRandom;
  154.     }

  155.     /**
  156.      * Generate a Sequence of Log Normal Random Numbers
  157.      *
  158.      * @param iCount The Count in the Sequence
  159.      *
  160.      * @return The Sequence of Log Normal Random Numbers
  161.      */

  162.     public static final double[] LogNormal (
  163.         final int iCount)
  164.     {
  165.         if (0 >= iCount) return null;

  166.         double[] adblRandom = new double[iCount];

  167.         double dblNormalizer = 1. / java.lang.Math.sqrt (java.lang.Math.E);

  168.         for (int i = 0; i < iCount; ++i) {
  169.             try {
  170.                 adblRandom[i] = java.lang.Math.exp (org.drip.measure.gaussian.NormalQuadrature.Random()) *
  171.                     dblNormalizer;
  172.             } catch (java.lang.Exception e) {
  173.                 e.printStackTrace();

  174.                 return null;
  175.             }
  176.         }

  177.         return adblRandom;
  178.     }

  179.     /**
  180.      * Generate a Sequence of R^d Correlated Gaussian Random Numbers
  181.      *
  182.      * @param iCount The Count in the Sequence
  183.      * @param aadblCorrelation The Correlation Matrix
  184.      *
  185.      * @return The Sequence of R^d Correlated Gaussian Random Numbers
  186.      */

  187.     public static final double[][] GaussianJoint (
  188.         final int iCount,
  189.         final double[][] aadblCorrelation)
  190.     {
  191.         if (0 >= iCount) return null;

  192.         double[][] aadblCholesky = org.drip.numerical.linearalgebra.Matrix.CholeskyBanachiewiczFactorization
  193.             (aadblCorrelation);

  194.         if (null == aadblCholesky) return null;

  195.         int iDimension = aadblCholesky.length;
  196.         double[][] aadblRandom = new double[iCount][];

  197.         for (int k = 0; k < iCount; ++k) {
  198.             double[] adblUncorrelatedRandom = Gaussian (iDimension);

  199.             if (null == adblUncorrelatedRandom || iDimension != adblUncorrelatedRandom.length) return null;

  200.             double[] adblCorrelatedRandom = new double[iDimension];

  201.             for (int i = 0; i < iDimension; ++i) {
  202.                 adblCorrelatedRandom[i] = 0.;

  203.                 for (int j = 0; j < iDimension; ++j)
  204.                     adblCorrelatedRandom[i] += aadblCholesky[i][j] * adblUncorrelatedRandom[j];
  205.             }

  206.             aadblRandom[k] = adblCorrelatedRandom;
  207.         }

  208.         return aadblRandom;
  209.     }

  210.     /**
  211.      * Generate an Array of Chi-Squared Distributed Random Numbers
  212.      *
  213.      * @param count Array Count
  214.      * @param degreesOfFreedom Degrees of Freedom
  215.      *
  216.      * @return Array of Chi-Squared Distributed Random Numbers
  217.      */

  218.     public static final double[] ChiSquared (
  219.         final int count,
  220.         final int degreesOfFreedom)
  221.     {
  222.         if (0 >= degreesOfFreedom)
  223.         {
  224.             return null;
  225.         }

  226.         double[] chiSquaredArray = new double[count];

  227.         for (int index = 0; index < count; ++index)
  228.         {
  229.             double sumOfStandardNormalSquares = 0.;

  230.             for (int drawIndex = 0; drawIndex < degreesOfFreedom; ++drawIndex)
  231.             {
  232.                 try
  233.                 {
  234.                     double randomStandardNormal = org.drip.measure.gaussian.NormalQuadrature.InverseCDF
  235.                         (java.lang.Math.random());

  236.                     sumOfStandardNormalSquares = sumOfStandardNormalSquares +
  237.                         randomStandardNormal * randomStandardNormal;
  238.                 }
  239.                 catch (java.lang.Exception e)
  240.                 {
  241.                     e.printStackTrace();

  242.                     return null;
  243.                 }
  244.             }

  245.             chiSquaredArray[index] = sumOfStandardNormalSquares;
  246.         }

  247.         return chiSquaredArray;
  248.     }

  249.     /**
  250.      * Generate an Array of Scaled Gamma Distributed Random Numbers
  251.      *
  252.      * @param count Array Count
  253.      * @param degreesOfFreedom Degrees of Freedom
  254.      * @param scale Scale Parameter
  255.      *
  256.      * @return Array of Scaled Gamma Distributed Random Numbers
  257.      */

  258.     public static final double[] ScaledGamma (
  259.         final int count,
  260.         final int degreesOfFreedom,
  261.         final double scale)
  262.     {
  263.         if (!org.drip.numerical.common.NumberUtil.IsValid (scale) || 0. >= scale)
  264.         {
  265.             return null;
  266.         }

  267.         double[] chiSquaredArray =  ChiSquared (
  268.             count,
  269.             degreesOfFreedom
  270.         );

  271.         if (null == chiSquaredArray)
  272.         {
  273.             return null;
  274.         }

  275.         double[] scaledGammaArray = new double[count];

  276.         for (int index = 0; index < count; ++index)
  277.         {
  278.             scaledGammaArray[index] = scale * chiSquaredArray[index];
  279.         }

  280.         return scaledGammaArray;
  281.     }

  282.     /**
  283.      * Generate an Array of Chi Distributed Random Numbers
  284.      *
  285.      * @param count Array Count
  286.      * @param degreesOfFreedom Degrees of Freedom
  287.      *
  288.      * @return Array of Chi Distributed Random Numbers
  289.      */

  290.     public static final double[] Chi (
  291.         final int count,
  292.         final int degreesOfFreedom)
  293.     {
  294.         double[] chiSquaredArray =  ChiSquared (
  295.             count,
  296.             degreesOfFreedom
  297.         );

  298.         if (null == chiSquaredArray)
  299.         {
  300.             return null;
  301.         }

  302.         double[] chiArray = new double[count];

  303.         for (int index = 0; index < count; ++index)
  304.         {
  305.             chiArray[index] = java.lang.Math.sqrt (chiSquaredArray[index]);
  306.         }

  307.         return chiArray;
  308.     }

  309.     /**
  310.      * Generate an Array of Unit Scale Rayleigh Distributed Random Numbers
  311.      *
  312.      * @param count Array Count
  313.      *
  314.      * @return Array of Unit Scale Rayleigh Distributed Random Numbers
  315.      */

  316.     public static final double[] UnitScaleRayleigh (
  317.         final int count)
  318.     {
  319.         double[] chiSquaredArray =  ChiSquared (
  320.             count,
  321.             2
  322.         );

  323.         if (null == chiSquaredArray)
  324.         {
  325.             return null;
  326.         }

  327.         double[] unitScaleRayleighArray = new double[count];

  328.         for (int index = 0; index < count; ++index)
  329.         {
  330.             unitScaleRayleighArray[index] = java.lang.Math.sqrt (chiSquaredArray[index]);
  331.         }

  332.         return unitScaleRayleighArray;
  333.     }

  334.     /**
  335.      * Generate an Array of Unit Scale Maxwell Distributed Random Numbers
  336.      *
  337.      * @param count Array Count
  338.      *
  339.      * @return Array of Unit Scale Maxwell Distributed Random Numbers
  340.      */

  341.     public static final double[] UnitScaleMaxwell (
  342.         final int count)
  343.     {
  344.         double[] chiSquaredArray =  ChiSquared (
  345.             count,
  346.             3
  347.         );

  348.         if (null == chiSquaredArray)
  349.         {
  350.             return null;
  351.         }

  352.         double[] unitScaleMaxwellArray = new double[count];

  353.         for (int index = 0; index < count; ++index)
  354.         {
  355.             unitScaleMaxwellArray[index] = java.lang.Math.sqrt (chiSquaredArray[index]);
  356.         }

  357.         return unitScaleMaxwellArray;
  358.     }

  359.     /**
  360.      * Generate an Array of Inverse Chi-Squared Distributed Random Numbers
  361.      *
  362.      * @param count Array Count
  363.      * @param degreesOfFreedom Degrees of Freedom
  364.      *
  365.      * @return Array of Inverse Chi-Squared Distributed Random Numbers
  366.      */

  367.     public static final double[] InverseChiSquared (
  368.         final int count,
  369.         final int degreesOfFreedom)
  370.     {
  371.         double[] chiSquaredArray =  ChiSquared (
  372.             count,
  373.             degreesOfFreedom
  374.         );

  375.         if (null == chiSquaredArray)
  376.         {
  377.             return null;
  378.         }

  379.         double[] inverseChiSquaredArray = new double[count];

  380.         for (int index = 0; index < count; ++index)
  381.         {
  382.             inverseChiSquaredArray[index] = 1. / chiSquaredArray[index];
  383.         }

  384.         return inverseChiSquaredArray;
  385.     }

  386.     /**
  387.      * Generate an Array of Beta Distributed Random Numbers
  388.      *
  389.      * @param count Array Count
  390.      * @param degreesOfFreedom1 Degrees of Freedom #1
  391.      * @param degreesOfFreedom2 Degrees of Freedom #2
  392.      *
  393.      * @return Array of Beta Distributed Random Numbers
  394.      */

  395.     public static final double[] Beta (
  396.         final int count,
  397.         final int degreesOfFreedom1,
  398.         final int degreesOfFreedom2)
  399.     {
  400.         double[] chiSquaredArray1 = ChiSquared (
  401.             count,
  402.             degreesOfFreedom1
  403.         );

  404.         double[] chiSquaredArray2 = ChiSquared (
  405.             count,
  406.             degreesOfFreedom2
  407.         );

  408.         if (null == chiSquaredArray1 || null == chiSquaredArray2)
  409.         {
  410.             return null;
  411.         }

  412.         double[] betaArray = new double[count];

  413.         for (int index = 0; index < count; ++index)
  414.         {
  415.             betaArray[index] = chiSquaredArray1[index] / (chiSquaredArray1[index] + chiSquaredArray2[index]);
  416.         }

  417.         return betaArray;
  418.     }

  419.     /**
  420.      * Generate an Array of F Distributed Random Numbers
  421.      *
  422.      * @param count Array Count
  423.      * @param degreesOfFreedom1 Degrees of Freedom #1
  424.      * @param degreesOfFreedom2 Degrees of Freedom #2
  425.      *
  426.      * @return Array of F Distributed Random Numbers
  427.      */

  428.     public static final double[] F (
  429.         final int count,
  430.         final int degreesOfFreedom1,
  431.         final int degreesOfFreedom2)
  432.     {
  433.         double[] chiSquaredArray1 = ChiSquared (
  434.             count,
  435.             degreesOfFreedom1
  436.         );

  437.         double[] chiSquaredArray2 = ChiSquared (
  438.             count,
  439.             degreesOfFreedom2
  440.         );

  441.         if (null == chiSquaredArray1 || null == chiSquaredArray2)
  442.         {
  443.             return null;
  444.         }

  445.         double[] fArray = new double[count];

  446.         for (int index = 0; index < count; ++index)
  447.         {
  448.             fArray[index] = (chiSquaredArray1[index] * degreesOfFreedom2) /
  449.                 (chiSquaredArray2[index] * degreesOfFreedom1);
  450.         }

  451.         return fArray;
  452.     }

  453.     /**
  454.      * Generate a Rank-reduced Chi-Squared Distributed Array
  455.      *
  456.      * @param count Array Count
  457.      * @param covarianceMatrix The Covariance Matrix
  458.      *
  459.      * @return Rank-reduced Chi-Squared Distributed Array
  460.      */

  461.     public static final double[] RankReducedChiSquare (
  462.         final int count,
  463.         final double[][] covarianceMatrix)
  464.     {
  465.         double[] rankReducedChiSquare = new double[count];
  466.         org.drip.measure.gaussian.Covariance covariance = null;

  467.         try
  468.         {
  469.             covariance = new org.drip.measure.gaussian.Covariance (covarianceMatrix);
  470.         }
  471.         catch (java.lang.Exception e)
  472.         {
  473.             e.printStackTrace();

  474.             return null;
  475.         }

  476.         double[][] gaussianJointArray = GaussianJoint (
  477.             count,
  478.             covariance.correlationMatrix()
  479.         );

  480.         if (null == gaussianJointArray)
  481.         {
  482.             return null;
  483.         }

  484.         double[][] precisionMatrix = covariance.precisionMatrix();

  485.         for (int index = 0; index < count; ++index)
  486.         {
  487.             try
  488.             {
  489.                 rankReducedChiSquare[index] = org.drip.numerical.linearalgebra.Matrix.DotProduct (
  490.                     gaussianJointArray[index],
  491.                     org.drip.numerical.linearalgebra.Matrix.Product (
  492.                         precisionMatrix,
  493.                         gaussianJointArray[index]
  494.                     )
  495.                 );
  496.             }
  497.             catch (java.lang.Exception e)
  498.             {
  499.                 e.printStackTrace();

  500.                 return null;
  501.             }
  502.         }

  503.         return rankReducedChiSquare;
  504.     }


  505.     /**
  506.      * Generate a Pillai (2016) Special Chi-Squared Distributed Array
  507.      *
  508.      * @param count Array Count
  509.      * @param covarianceMatrix The Covariance Matrix
  510.      * @param weightArray Array of Weights
  511.      *
  512.      * @return Pillai (2016) Special Chi-Squared Distributed Array
  513.      */

  514.     public static final double[] PillaiSpecialChiSquare (
  515.         final int count,
  516.         final double[][] covarianceMatrix,
  517.         final double[] weightArray)
  518.     {
  519.         if (!org.drip.numerical.common.NumberUtil.NormalizedPositive (weightArray))
  520.         {
  521.             return null;
  522.         }

  523.         int pillaiVectorSize = weightArray.length;
  524.         double[] pillaiSpecialChiSquare = new double[count];
  525.         org.drip.measure.gaussian.Covariance covariance = null;

  526.         try
  527.         {
  528.             covariance = new org.drip.measure.gaussian.Covariance (covarianceMatrix);
  529.         }
  530.         catch (java.lang.Exception e)
  531.         {
  532.             e.printStackTrace();

  533.             return null;
  534.         }

  535.         if (pillaiVectorSize != covarianceMatrix.length)
  536.         {
  537.             return null;
  538.         }

  539.         double[][] gaussianJointArray = GaussianJoint (
  540.             count,
  541.             covariance.correlationMatrix()
  542.         );

  543.         if (null == gaussianJointArray)
  544.         {
  545.             return null;
  546.         }

  547.         for (int index = 0; index < count; ++index)
  548.         {
  549.             double[] pillaiVector = new double[pillaiVectorSize];

  550.             for (int pillaiVectorIndex = 0; pillaiVectorIndex < pillaiVectorSize; ++pillaiVectorIndex)
  551.             {
  552.                 pillaiVector[pillaiVectorIndex] = weightArray[pillaiVectorIndex] /
  553.                     gaussianJointArray[index][pillaiVectorIndex];
  554.             }

  555.             try
  556.             {
  557.                 pillaiSpecialChiSquare[index] = 1. / org.drip.numerical.linearalgebra.Matrix.DotProduct (
  558.                     pillaiVector,
  559.                     org.drip.numerical.linearalgebra.Matrix.Product (
  560.                         covarianceMatrix,
  561.                         pillaiVector
  562.                     )
  563.                 );
  564.             }
  565.             catch (java.lang.Exception e)
  566.             {
  567.                 e.printStackTrace();

  568.                 return null;
  569.             }
  570.         }

  571.         return pillaiSpecialChiSquare;
  572.     }
  573. }