R2ArrayPathwiseProcessing.java

  1. package org.drip.sample.algo;

  2. import org.drip.numerical.common.FormatUtil;
  3. import org.drip.service.env.EnvManager;
  4. import org.drip.spaces.big.BigR2Array;

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

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

  81. /**
  82.  * <i>R2ArrayPathwiseProcessing</i> demonstrates the Functionality that conducts an in-place Path-wise
  83.  * Processing of an Instance of Big R<sup>2</sup> Array.
  84.  *
  85.  * <br><br>
  86.  *  <ul>
  87.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ComputationalCore.md">Computational Core Module</a></li>
  88.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/StatisticalLearningLibrary.md">Statistical Learning Library</a></li>
  89.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/README.md">DROP API Construction and Usage</a></li>
  90.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/algo/README.md">C<sup>x</sup> R<sup>x</sup> In-Place Manipulation</a></li>
  91.  *  </ul>
  92.  * <br><br>
  93.  *
  94.  * @author Lakshmi Krishnamurthy
  95.  */

  96. public class R2ArrayPathwiseProcessing {

  97.     private static final void MaxPathwiseProduct (
  98.         final int iSize,
  99.         final int iNumSim)
  100.         throws Exception
  101.     {
  102.         double dblExpectedMaxPathResponse = 0.;
  103.         final double[][] aadblA = new double[iSize][iSize];

  104.         for (int iRun = 0; iRun < iNumSim; ++iRun) {
  105.             for (int i = 0; i < iSize; ++i) {
  106.                 for (int j = 0; j < iSize; ++j)
  107.                     aadblA[i][j] = Math.random();
  108.             }

  109.             dblExpectedMaxPathResponse += new BigR2Array (aadblA) {
  110.                 @Override public double pathResponse (
  111.                     final int iX,
  112.                     final int iY,
  113.                     final double dblPriorPathResponse)
  114.                     throws Exception
  115.                 {
  116.                     return dblPriorPathResponse * aadblA[iX][iY];
  117.                 }

  118.                 @Override public double maxPathResponse()
  119.                     throws Exception
  120.                 {
  121.                     return maxPathResponse (
  122.                         0,
  123.                         0,
  124.                         1.
  125.                     );
  126.                 }
  127.             }.maxPathResponse();
  128.         }

  129.         System.out.println (
  130.             "\t|| EXPECTED MAX PATH PRODUCT => " +
  131.             FormatUtil.FormatDouble (dblExpectedMaxPathResponse / iNumSim, 1, 4, 1.) + " ||"
  132.         );
  133.     }

  134.     private static final void MaxPathwiseSum (
  135.         final int iSize,
  136.         final int iNumSim)
  137.         throws Exception
  138.     {
  139.         double dblExpectedMaxPathResponse = 0.;
  140.         final double[][] aadblA = new double[iSize][iSize];

  141.         for (int iRun = 0; iRun < iNumSim; ++iRun) {
  142.             for (int i = 0; i < iSize; ++i) {
  143.                 for (int j = 0; j < iSize; ++j)
  144.                     aadblA[i][j] = Math.random();
  145.             }

  146.             dblExpectedMaxPathResponse += new BigR2Array (aadblA) {
  147.                 @Override public double pathResponse (
  148.                     final int iX,
  149.                     final int iY,
  150.                     final double dblPriorPathResponse)
  151.                     throws Exception
  152.                 {
  153.                     return dblPriorPathResponse + aadblA[iX][iY];
  154.                 }

  155.                 @Override public double maxPathResponse()
  156.                     throws Exception
  157.                 {
  158.                     return maxPathResponse (
  159.                         0,
  160.                         0,
  161.                         0.
  162.                     );
  163.                 }
  164.             }.maxPathResponse();
  165.         }

  166.         System.out.println (
  167.             "\t|| EXPECTED MAX PATH SUM     => " +
  168.             FormatUtil.FormatDouble (dblExpectedMaxPathResponse / iNumSim, 1, 4, 1.) + " ||"
  169.         );
  170.     }

  171.     public static final void main (
  172.         final String[] astrArgs)
  173.         throws Exception
  174.     {
  175.         EnvManager.InitEnv (
  176.             "",
  177.             true
  178.         );

  179.         int iSize = 5;
  180.         int iNumSim = 1000000;
  181.         int iNumRunSet = 5;

  182.         System.out.println ();

  183.         for (int i = 0; i < iNumRunSet; ++i) {
  184.             System.out.println ("\t||--------------------------------------||");

  185.             MaxPathwiseProduct (
  186.                 iSize,
  187.                 iNumSim
  188.             );

  189.             MaxPathwiseSum (
  190.                 iSize,
  191.                 iNumSim
  192.             );

  193.             System.out.println ("\t||--------------------------------------||");
  194.         }

  195.         EnvManager.TerminateEnv();
  196.     }
  197. }