GaussianSequence.java

  1. package org.drip.sample.measure;

  2. import org.drip.measure.discrete.SequenceGenerator;
  3. import org.drip.numerical.common.FormatUtil;
  4. import org.drip.service.env.EnvManager;

  5. /*
  6.  * -*- mode: java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  7.  */

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

  68. /**
  69.  * <i>GaussianSequence</i> demonstrates the Generation of R<sup>1</sup> and Correlated/Uncorrelated
  70.  * R<sup>d</sup> Gaussian Random Number Sequence.
  71.  *  
  72.  * <br><br>
  73.  *  <ul>
  74.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/NumericalCore.md">Numerical Core Module</a></li>
  75.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/NumericalSupportLibrary.md">Numerical Support Library</a></li>
  76.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/README.md">Sample</a></li>
  77.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/measure/README.md">Probability Measure Generators</a></li>
  78.  *  </ul>
  79.  * <br><br>
  80.  *
  81.  * @author Lakshmi Krishnamurthy
  82.  */

  83. public class GaussianSequence {

  84.     private static final void CorrelatedSequence (
  85.         final int iCount,
  86.         final double[][] aadblCorrelation,
  87.         final String strHeader)
  88.         throws Exception
  89.     {
  90.         double[][] aadblGaussianJoint = SequenceGenerator.GaussianJoint (
  91.             iCount,
  92.             aadblCorrelation
  93.         );

  94.         System.out.println();

  95.         System.out.println ("\t||----------------------------------------------------||");

  96.         System.out.println (strHeader);

  97.         System.out.println ("\t||----------------------------------------------------||");

  98.         for (int i = 0; i < iCount; ++i) {
  99.             String strDump = "\t||" + FormatUtil.FormatDouble (i, 2, 0, 1.) + " |";

  100.             for (int j = 0; j < aadblCorrelation.length; ++j)
  101.                 strDump = strDump + " " + FormatUtil.FormatDouble (aadblGaussianJoint[i][j], 1, 6, 1.) + " |";

  102.             System.out.println (strDump + "|");
  103.         }

  104.         System.out.println ("\t||----------------------------------------------------||");

  105.         System.out.println();
  106.     }

  107.     public static final void main (
  108.         final String[] astrArgs)
  109.         throws Exception
  110.     {
  111.         EnvManager.InitEnv ("");

  112.         int iCount = 50;
  113.         double[][] aadblCorrelation1 = new double[][] {
  114.             {1.00, 0.70, 0.25, 0.05},
  115.             {0.70, 1.00, 0.13, 0.01},
  116.             {0.25, 0.13, 1.00, 0.00},
  117.             {0.05, 0.01, 0.00, 1.00}
  118.         };
  119.         double[][] aadblCorrelation2 = new double[][] {
  120.             {1.00, 0.00, 0.00, 0.00},
  121.             {0.00, 1.00, 0.00, 0.00},
  122.             {0.00, 0.00, 1.00, 0.00},
  123.             {0.00, 0.00, 0.00, 1.00}
  124.         };

  125.         CorrelatedSequence (
  126.             iCount,
  127.             aadblCorrelation1,
  128.             "\t||            CORRELATED GAUSSIAN SEQUENCE            ||"
  129.         );

  130.         CorrelatedSequence (
  131.             iCount,
  132.             aadblCorrelation2,
  133.             "\t||           UNCORRELATED GAUSSIAN SEQUENCE           ||"
  134.         );

  135.         EnvManager.TerminateEnv();
  136.     }
  137. }