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

  81. public class PrincipalFactorSequenceGenerator extends org.drip.sequence.random.MultivariateSequenceGenerator
  82. {
  83.     private double[][] _aadblFactor = null;
  84.     private double[] _adblFactorWeight = null;

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

  94.     public PrincipalFactorSequenceGenerator (
  95.         final org.drip.sequence.random.UnivariateSequenceGenerator[] aUSG,
  96.         final double[][] aadblCorrelation,
  97.         final int iNumFactor)
  98.         throws java.lang.Exception
  99.     {
  100.         super (aUSG, aadblCorrelation);

  101.         int iNumVariate = aUSG.length;

  102.         if (0 >= iNumFactor || iNumFactor > iNumVariate)
  103.             throw new java.lang.Exception ("PrincipalFactorSequenceGenerator ctr: Invalid Inputs");

  104.         org.drip.numerical.eigen.QREigenComponentExtractor qrece = new
  105.             org.drip.numerical.eigen.QREigenComponentExtractor (80, 0.00001);

  106.         org.drip.numerical.eigen.EigenComponent[] aEC = qrece.orderedEigenComponentArray (aadblCorrelation);

  107.         if (null == aEC || 0 == aEC.length)
  108.             throw new java.lang.Exception ("PrincipalFactorSequenceGenerator ctr: Invalid Inputs");

  109.         double dblNormalizer = 0.;
  110.         _adblFactorWeight = new double[iNumFactor];
  111.         _aadblFactor = new double[iNumFactor][iNumVariate];

  112.         for (int i = 0; i < iNumFactor; ++i) {
  113.             for (int j = 0; j < iNumVariate; ++j)
  114.                 _aadblFactor[i] = aEC[i].eigenVector();

  115.             _adblFactorWeight[i] = aEC[i].eigenValue();

  116.             dblNormalizer += _adblFactorWeight[i] * _adblFactorWeight[i];
  117.         }

  118.         dblNormalizer = java.lang.Math.sqrt (dblNormalizer);

  119.         for (int i = 0; i < iNumFactor; ++i)
  120.             _adblFactorWeight[i] /= dblNormalizer;
  121.     }

  122.     /**
  123.      * Retrieve the Number of Factors
  124.      *
  125.      * @return The Number of Factors
  126.      */

  127.     public int numFactor()
  128.     {
  129.         return _adblFactorWeight.length;
  130.     }

  131.     /**
  132.      * Retrieve the Principal Component Factor Array
  133.      *
  134.      * @return The Principal Component Factor Array
  135.      */

  136.     public double[][] factors()
  137.     {
  138.         return _aadblFactor;
  139.     }

  140.     /**
  141.      * Retrieve the Array of Factor Weights
  142.      *
  143.      * @return The Array of Factor Weights
  144.      */

  145.     public double[] factorWeight()
  146.     {
  147.         return _adblFactorWeight;
  148.     }

  149.     @Override public double[] random()
  150.     {
  151.         double[] adblBaseRandom = super.random();

  152.         int iNumVariate = _aadblFactor[0].length;
  153.         int iNumFactor = _adblFactorWeight.length;
  154.         double[] adblRandom = new double[iNumFactor];

  155.         if (iNumFactor == iNumVariate) return adblBaseRandom;

  156.         for (int i = 0; i < iNumFactor; ++i) {
  157.             adblRandom[i] = 0.;

  158.             for (int j = 0; j < iNumVariate; ++j)
  159.                 adblRandom[i] += _aadblFactor[i][j] * adblBaseRandom[j];
  160.         }

  161.         return adblRandom;
  162.     }
  163. }