CorrelatedRdSequence.java

  1. package org.drip.sample.statistics;

  2. import org.drip.measure.crng.RandomNumberGenerator;
  3. import org.drip.measure.discrete.*;
  4. import org.drip.measure.statistics.MultivariateDiscrete;
  5. import org.drip.numerical.common.FormatUtil;
  6. import org.drip.service.env.EnvManager;

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

  10. /*!
  11.  * Copyright (C) 2018 Lakshmi Krishnamurthy
  12.  * Copyright (C) 2017 Lakshmi Krishnamurthy
  13.  *
  14.  *  This file is part of DRIP, a free-software/open-source library for buy/side financial/trading model
  15.  *      libraries targeting analysts and developers
  16.  *      https://lakshmidrip.github.io/DRIP/
  17.  *  
  18.  *  DRIP is composed of four main libraries:
  19.  *  
  20.  *  - DRIP Fixed Income - https://lakshmidrip.github.io/DRIP-Fixed-Income/
  21.  *  - DRIP Asset Allocation - https://lakshmidrip.github.io/DRIP-Asset-Allocation/
  22.  *  - DRIP Numerical Optimizer - https://lakshmidrip.github.io/DRIP-Numerical-Optimizer/
  23.  *  - DRIP Statistical Learning - https://lakshmidrip.github.io/DRIP-Statistical-Learning/
  24.  *
  25.  *  - DRIP Fixed Income: Library for Instrument/Trading Conventions, Treasury Futures/Options,
  26.  *      Funding/Forward/Overnight Curves, Multi-Curve Construction/Valuation, Collateral Valuation and XVA
  27.  *      Metric Generation, Calibration and Hedge Attributions, Statistical Curve Construction, Bond RV
  28.  *      Metrics, Stochastic Evolution and Option Pricing, Interest Rate Dynamics and Option Pricing, LMM
  29.  *      Extensions/Calibrations/Greeks, Algorithmic Differentiation, and Asset Backed Models and Analytics.
  30.  *
  31.  *  - DRIP Asset Allocation: Library for model libraries for MPT framework, Black Litterman Strategy
  32.  *      Incorporator, Holdings Constraint, and Transaction Costs.
  33.  *
  34.  *  - DRIP Numerical Optimizer: Library for Numerical Optimization and Spline Functionality.
  35.  *
  36.  *  - DRIP Statistical Learning: Library for Statistical Evaluation and Machine Learning.
  37.  *
  38.  *  Licensed under the Apache License, Version 2.0 (the "License");
  39.  *      you may not use this file except in compliance with the License.
  40.  *  
  41.  *  You may obtain a copy of the License at
  42.  *      http://www.apache.org/licenses/LICENSE-2.0
  43.  *  
  44.  *  Unless required by applicable law or agreed to in writing, software
  45.  *      distributed under the License is distributed on an "AS IS" BASIS,
  46.  *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  47.  *  
  48.  *  See the License for the specific language governing permissions and
  49.  *      limitations under the License.
  50.  */

  51. /**
  52.  * CorrelatedRdSequence demonstrates the Generation of the Statistical Measures for the Input Correlated
  53.  *  Sequence Set created using the Multi-Path Correlated Random Variable Generator without Quadratic
  54.  *  Re-sampling or Antithetic Variables.
  55.  *
  56.  * @author Lakshmi Krishnamurthy
  57.  */

  58. public class CorrelatedRdSequence {

  59.     public static final void main (
  60.         final String[] astrArgs)
  61.         throws Exception
  62.     {
  63.         EnvManager.InitEnv ("");

  64.         int iNumPath = 1;
  65.         int iNumVertex = 50000;
  66.         boolean bApplyAntithetic = false;

  67.         double[][] aadblCorrelationInput = new double[][] {
  68.             {1.000, 0.161, 0.245, 0.352, 0.259, 0.166, 0.003, 0.038, 0.114},    // USD_LIBOR_3M
  69.             {0.161, 1.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000},    // EUR_LIBOR_3M
  70.             {0.245, 0.000, 1.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000},    // JPY_LIBOR_3M
  71.             {0.352, 0.000, 0.000, 1.000, 0.000, 0.000, 0.000, 0.000, 0.000},    // CHF_LIBOR_3M
  72.             {0.259, 0.000, 0.000, 0.000, 1.000, 0.000, 0.000, 0.000, 0.000},    // GBP_LIBOR_3M
  73.             {0.166, 0.000, 0.000, 0.000, 0.000, 1.000, 0.000, 0.000, 0.000},    // EURUSD
  74.             {0.003, 0.000, 0.000, 0.000, 0.000, 0.000, 1.000, 0.000, 0.000},    // JPYUSD
  75.             {0.038, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 1.000, 0.000},    // CHFUSD
  76.             {0.114, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 1.000},    // GBPUSD
  77.         };

  78.         CorrelatedPathVertexDimension cpvd = new CorrelatedPathVertexDimension (
  79.             new RandomNumberGenerator(),
  80.             aadblCorrelationInput,
  81.             iNumVertex,
  82.             iNumPath,
  83.             bApplyAntithetic,
  84.             null
  85.         );

  86.         VertexRd vertexRd = cpvd.multiPathVertexRd()[0];

  87.         MultivariateDiscrete mds = new MultivariateDiscrete (vertexRd.flatform());

  88.         double[] adblMeanOutput = mds.mean();

  89.         double[] adblErrorOutput = mds.error();

  90.         double[] adblVarianceOutput = mds.variance();

  91.         double[][] aadblCovarianceOutput = mds.covariance();

  92.         double[][] aadblCorrelationOutput = mds.correlation();

  93.         double[] adblStandardDeviationOutput = mds.standardDeviation();

  94.         System.out.println();

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

  96.         System.out.println ("\t||                R^1 METRICS                ||");

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

  98.         System.out.println ("\t||    L -> R:                                ||");

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

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

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

  102.         System.out.println ("\t||            - Standard Deviation           ||");

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

  104.         for (int i = 0; i < adblMeanOutput.length; ++i)
  105.             System.out.println ("\t|| " +
  106.                 FormatUtil.FormatDouble (adblMeanOutput[i], 1, 5, 1.) + " | " +
  107.                 FormatUtil.FormatDouble (adblErrorOutput[i], 1, 5, 1.) + " | " +
  108.                 FormatUtil.FormatDouble (adblVarianceOutput[i], 1, 5, 1.) + " | " +
  109.                 FormatUtil.FormatDouble (adblStandardDeviationOutput[i], 1, 5, 1.) + " ||"
  110.             );

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

  112.         System.out.println();

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

  114.         System.out.println ("\t||                                     INPUT CORRELATION                                    ||");

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

  116.         for (int i = 0; i < adblMeanOutput.length; ++i) {
  117.             String strDump = "\t|| ";

  118.             for (int j = 0; j < adblMeanOutput.length; ++j)
  119.                 strDump = strDump + FormatUtil.FormatDouble (aadblCorrelationInput[i][j], 1, 5, 1.) + " |";

  120.             System.out.println (strDump + "|");
  121.         }

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

  123.         System.out.println();

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

  125.         System.out.println ("\t||                                    OUTPUT CORRELATION                                    ||");

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

  127.         for (int i = 0; i < adblMeanOutput.length; ++i) {
  128.             String strDump = "\t|| ";

  129.             for (int j = 0; j < adblMeanOutput.length; ++j)
  130.                 strDump = strDump + FormatUtil.FormatDouble (aadblCorrelationOutput[i][j], 1, 5, 1.) + " |";

  131.             System.out.println (strDump + "|");
  132.         }

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

  134.         System.out.println();

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

  136.         System.out.println ("\t||                                    OUTPUT COVARIANCE                                     ||");

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

  138.         for (int i = 0; i < adblMeanOutput.length; ++i) {
  139.             String strDump = "\t|| ";

  140.             for (int j = 0; j < adblMeanOutput.length; ++j)
  141.                 strDump = strDump + FormatUtil.FormatDouble (aadblCovarianceOutput[i][j], 1, 5, 1.) + " |";

  142.             System.out.println (strDump + "|");
  143.         }

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

  145.         System.out.println();
  146.     }
  147. }