NormalSampleCohort.java

  1. package org.drip.validation.riskfactorjoint;

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

  74. /**
  75.  * <i>NormalSampleCohort</i> holds the Joint Realizations from a Multivariate Normal Distribution and its
  76.  * Reduction to a Synthetic Single Risk Factor.
  77.  *
  78.  *  <br><br>
  79.  *  <ul>
  80.  *      <li>
  81.  *          Anfuso, F., D. Karyampas, and A. Nawroth (2017): A Sound Basel III Compliant Framework for
  82.  *              Back-testing Credit Exposure Models
  83.  *              https://papers.ssrn.com/sol3/papers.cfm?abstract_id=2264620 <b>eSSRN</b>
  84.  *      </li>
  85.  *      <li>
  86.  *          Diebold, F. X., T. A. Gunther, and A. S. Tay (1998): Evaluating Density Forecasts with
  87.  *              Applications to Financial Risk Management <i>International Economic Review</i> <b>39 (4)</b>
  88.  *              863-883
  89.  *      </li>
  90.  *      <li>
  91.  *          Kenyon, C., and R. Stamm (2012): <i>Discounting, LIBOR, CVA, and Funding: Interest Rate and
  92.  *              Credit Pricing</i> <b>Palgrave Macmillan</b>
  93.  *      </li>
  94.  *      <li>
  95.  *          Wikipedia (2018): Probability Integral Transform
  96.  *              https://en.wikipedia.org/wiki/Probability_integral_transform
  97.  *      </li>
  98.  *      <li>
  99.  *          Wikipedia (2019): p-value https://en.wikipedia.org/wiki/P-value
  100.  *      </li>
  101.  *  </ul>
  102.  *
  103.  *  <br><br>
  104.  *  <ul>
  105.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ComputationalCore.md">Computational Core Module</a></li>
  106.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ModelValidationAnalyticsLibrary.md">Model Validation Analytics Library</a></li>
  107.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/validation/README.md">Risk Factor and Hypothesis Validation, Evidence Processing, and Model Testing</a></li>
  108.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/validation/riskfactorjoint/README.md">Joint Risk Factor Aggregate Tests</a></li>
  109.  *  </ul>
  110.  * <br><br>
  111.  *
  112.  * @author Lakshmi Krishnamurthy
  113.  */

  114. public class NormalSampleCohort implements org.drip.validation.riskfactorjoint.SampleCohort
  115. {
  116.     private double _horizon = java.lang.Double.NaN;
  117.     private org.drip.measure.stochastic.LabelRdVertex _labelRdVertex = null;
  118.     private org.drip.measure.stochastic.LabelCovariance _latentStateLabelCovariance = null;

  119.     /**
  120.      * Generate a Correlated NormalSampleCohort
  121.      *
  122.      * @param labelList Label List
  123.      * @param annualMeanArray Array of Annual Means
  124.      * @param annualVolatilityArray Array of Annual Volatilities
  125.      * @param correlationMatrix Correlation Matrix
  126.      * @param vertexCount Vertex Count
  127.      * @param horizon Horizon
  128.      *
  129.      * @return NormalSampleCohort Instance
  130.      */

  131.     public static final NormalSampleCohort Correlated (
  132.         final java.util.List<java.lang.String> labelList,
  133.         final double[] annualMeanArray,
  134.         final double[] annualVolatilityArray,
  135.         final double[][] correlationMatrix,
  136.         final int vertexCount,
  137.         final double horizon)
  138.     {
  139.         if (!org.drip.numerical.common.NumberUtil.IsValid (horizon))
  140.         {
  141.             return null;
  142.         }

  143.         org.drip.measure.discrete.CorrelatedPathVertexDimension correlatedPathVertexDimension = null;

  144.         try
  145.         {
  146.             correlatedPathVertexDimension = new org.drip.measure.discrete.CorrelatedPathVertexDimension (
  147.                 new org.drip.measure.crng.RandomNumberGenerator(),
  148.                 correlationMatrix,
  149.                 vertexCount,
  150.                 1,
  151.                 false,
  152.                 null
  153.             );
  154.         }
  155.         catch (java.lang.Exception e)
  156.         {
  157.             e.printStackTrace();

  158.             return null;
  159.         }

  160.         org.drip.measure.discrete.VertexRd[] vertexRdArray =
  161.             correlatedPathVertexDimension.straightMultiPathVertexRd();

  162.         if (null == vertexRdArray || null == vertexRdArray[0])
  163.         {
  164.             return null;
  165.         }

  166.         double[][] realization = vertexRdArray[0].flatform();

  167.         if (null == realization)
  168.         {
  169.             return null;
  170.         }

  171.         double horizonSQRT = Math.sqrt (horizon);

  172.         for (int vertexIndex = 0; vertexIndex < vertexCount; ++vertexIndex)
  173.         {
  174.             for (int entityIndex = 0; entityIndex < correlationMatrix.length; ++entityIndex)
  175.             {
  176.                 realization[vertexIndex][entityIndex] =
  177.                     realization[vertexIndex][entityIndex] * annualVolatilityArray[entityIndex] * horizonSQRT +
  178.                     annualMeanArray[entityIndex] * horizon;
  179.             }
  180.         }

  181.         try
  182.         {
  183.             return new NormalSampleCohort (
  184.                 new org.drip.measure.stochastic.LabelRdVertex (
  185.                     labelList,
  186.                     realization
  187.                 ),
  188.                 new org.drip.measure.stochastic.LabelCovariance (
  189.                     labelList,
  190.                     annualMeanArray,
  191.                     annualVolatilityArray,
  192.                     correlationMatrix
  193.                 ),
  194.                 horizon
  195.             );
  196.         }
  197.         catch (java.lang.Exception e)
  198.         {
  199.             e.printStackTrace();
  200.         }

  201.         return null;
  202.     }

  203.     /**
  204.      * NormalSampleCohort Constructor
  205.      *
  206.      * @param labelRdVertex R<sup>d</sup> Labeled Vertex
  207.      * @param latentStateLabelCovariance R<sup>d</sup> Labeled Covariance
  208.      * @param horizon Horizon
  209.      *
  210.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  211.      */

  212.     public NormalSampleCohort (
  213.         final org.drip.measure.stochastic.LabelRdVertex labelRdVertex,
  214.         final org.drip.measure.stochastic.LabelCovariance latentStateLabelCovariance,
  215.         final double horizon)
  216.         throws java.lang.Exception
  217.     {
  218.         if (null == (_labelRdVertex = labelRdVertex) ||
  219.             null == (_latentStateLabelCovariance = latentStateLabelCovariance) ||
  220.             !org.drip.numerical.common.NumberUtil.IsValid (_horizon = horizon) || _horizon <= 0.)
  221.         {
  222.             throw new java.lang.Exception ("NormalSampleCohort Constructor => Invalid Inputs");
  223.         }
  224.     }

  225.     /**
  226.      * Retrieve the Latent State Label Covariance
  227.      *
  228.      * @return The Latent State Label Covariance
  229.      */

  230.     public org.drip.measure.stochastic.LabelCorrelation latentStateLabelCovariance()
  231.     {
  232.         return _latentStateLabelCovariance;
  233.     }

  234.     /**
  235.      * Retrieve the Sample Horizon
  236.      *
  237.      * @return The Sample Horizon
  238.      */

  239.     public double horizon()
  240.     {
  241.         return _horizon;
  242.     }

  243.     @Override public java.util.List<java.lang.String> latentStateLabelList()
  244.     {
  245.         return _latentStateLabelCovariance.labelList();
  246.     }

  247.     @Override public org.drip.measure.stochastic.LabelRdVertex vertexRd()
  248.     {
  249.         return _labelRdVertex;
  250.     }

  251.     @Override public org.drip.validation.evidence.Sample reduce (
  252.         final java.lang.String label1,
  253.         final java.lang.String label2)
  254.     {
  255.         double annualMean1 = java.lang.Double.NaN;
  256.         double annualMean2 = java.lang.Double.NaN;
  257.         double correlation = java.lang.Double.NaN;
  258.         double annualPrecision1 = java.lang.Double.NaN;
  259.         double annualPrecision2 = java.lang.Double.NaN;
  260.         double annualVolatility1 = java.lang.Double.NaN;
  261.         double annualVolatility2 = java.lang.Double.NaN;

  262.         try
  263.         {
  264.             correlation = _latentStateLabelCovariance.entry (
  265.                 label1,
  266.                 label2
  267.             );

  268.             annualMean1 = _latentStateLabelCovariance.mean (label1);

  269.             annualMean2 = _latentStateLabelCovariance.mean (label2);

  270.             annualPrecision1 = (1. / (annualVolatility1 = _latentStateLabelCovariance.volatility (label1)));

  271.             annualPrecision2 = (1. / (annualVolatility2 = _latentStateLabelCovariance.volatility (label2)));
  272.         }
  273.         catch (java.lang.Exception e)
  274.         {
  275.             e.printStackTrace();

  276.             return null;
  277.         }

  278.         double[] vertexR1_1 = _labelRdVertex.vertexR1 (label1);

  279.         double[] vertexR1_2 = _labelRdVertex.vertexR1 (label2);

  280.         if (null == vertexR1_1 || null == vertexR1_2)
  281.         {
  282.             return null;
  283.         }

  284.         int cohortCount = vertexR1_1.length;
  285.         double[] cohortRealization = new double[cohortCount];
  286.         double cohortScale = java.lang.Math.exp (_horizon * (0.5 * (annualVolatility1 + annualVolatility2) -
  287.             (1. + correlation) - (annualMean1 * annualPrecision1 + annualMean2 * annualPrecision2)));

  288.         for (int cohortIndex = 0; cohortIndex < cohortCount; ++cohortIndex)
  289.         {
  290.             cohortRealization[cohortIndex] = cohortScale * java.lang.Math.pow (
  291.                 vertexR1_1[cohortIndex],
  292.                 annualPrecision1
  293.             ) * java.lang.Math.pow (
  294.                 vertexR1_2[cohortIndex],
  295.                 annualPrecision2
  296.             );
  297.         }

  298.         try
  299.         {
  300.             return new org.drip.validation.evidence.Sample (cohortRealization);
  301.         }
  302.         catch (java.lang.Exception e)
  303.         {
  304.             e.printStackTrace();
  305.         }

  306.         return null;
  307.     }
  308. }