DiscriminatoryPowerAnalyzerAggregate.java

  1. package org.drip.validation.riskfactorsingle;

  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>DiscriminatoryPowerAnalyzerAggregate</i> implements the Discriminatory Power Analyzer for the given
  76.  * Sample across the One/More Hypothesis and Multiple Events.
  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/riskfactorsingle/README.md">Single Risk Factor Aggregate Tests</a></li>
  109.  *  </ul>
  110.  * <br><br>
  111.  *
  112.  * @author Lakshmi Krishnamurthy
  113.  */

  114. public class DiscriminatoryPowerAnalyzerAggregate
  115. {
  116.     private org.drip.validation.distance.GapTestSetting _gapTestSetting = null;
  117.     private org.drip.validation.riskfactorsingle.EventAggregationWeightFunction _eventAggregationWeightFunction =
  118.         null;

  119.     private java.util.Map<java.lang.String, org.drip.validation.hypothesis.ProbabilityIntegralTransform>
  120.         _eventSamplePITMap = new
  121.             org.drip.analytics.support.CaseInsensitiveHashMap<org.drip.validation.hypothesis.ProbabilityIntegralTransform>();

  122.     /**
  123.      * DiscriminatoryPowerAnalyzerAggregate Constructor
  124.      *
  125.      * @param eventSamplePITMap Event Sample PIT Map
  126.      * @param gapTestSetting The Distance Gap Test Setting
  127.      * @param eventAggregationWeightFunction Event Aggregation Weight Function
  128.      *
  129.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  130.      */

  131.     public DiscriminatoryPowerAnalyzerAggregate (
  132.         final java.util.Map<java.lang.String, org.drip.validation.hypothesis.ProbabilityIntegralTransform>
  133.             eventSamplePITMap,
  134.         final org.drip.validation.distance.GapTestSetting gapTestSetting,
  135.         final org.drip.validation.riskfactorsingle.EventAggregationWeightFunction eventAggregationWeightFunction)
  136.         throws java.lang.Exception
  137.     {
  138.         if (null == (_eventSamplePITMap = eventSamplePITMap) || 0 == _eventSamplePITMap.size() ||
  139.             null == (_gapTestSetting = gapTestSetting) ||
  140.             null == (_eventAggregationWeightFunction = eventAggregationWeightFunction))
  141.         {
  142.             throw new java.lang.Exception
  143.                 ("DiscriminatoryPowerAnalyzerAggregate Constructor => Invalid Inputs");
  144.         }
  145.     }

  146.     /**
  147.      * Retrieve the Gap Test Setting
  148.      *
  149.      * @return The Gap Test Setting
  150.      */

  151.     public org.drip.validation.distance.GapTestSetting gapTestSetting()
  152.     {
  153.         return _gapTestSetting;
  154.     }

  155.     /**
  156.      * Retrieve the Event Aggregation Weight Function
  157.      *
  158.      * @return The Event Aggregation Weight Function
  159.      */

  160.     public org.drip.validation.riskfactorsingle.EventAggregationWeightFunction eventAggregationWeightFunction()
  161.     {
  162.         return _eventAggregationWeightFunction;
  163.     }

  164.     /**
  165.      * Retrieve the Event Sample PIT Map
  166.      *
  167.      * @return The Event Sample PIT Map
  168.      */

  169.     public java.util.Map<java.lang.String, org.drip.validation.hypothesis.ProbabilityIntegralTransform>
  170.         eventSamplePITMap()
  171.     {
  172.         return _eventSamplePITMap;
  173.     }

  174.     private org.drip.validation.riskfactorsingle.GapTestOutcomeAggregate eventOutcomeAggregate (
  175.         final java.lang.String hypothesisID,
  176.         final java.util.Map<java.lang.String, org.drip.validation.evidence.Ensemble> eventEnsembleMap)
  177.     {
  178.         double distanceAggregate = 0.;

  179.         java.util.Map<java.lang.String, org.drip.validation.distance.GapTestOutcome> eventOutcomeMap = new
  180.             org.drip.analytics.support.CaseInsensitiveHashMap<org.drip.validation.distance.GapTestOutcome>();

  181.         try
  182.         {
  183.             for (java.util.Map.Entry<java.lang.String, org.drip.validation.evidence.Ensemble> eventEnsemble :
  184.                 eventEnsembleMap.entrySet())
  185.             {
  186.                 java.lang.String eventID = eventEnsemble.getKey();

  187.                 if (!_eventSamplePITMap.containsKey (eventID))
  188.                 {
  189.                     return null;
  190.                 }

  191.                 DiscriminatoryPowerAnalyzer discriminatoryPowerAnalyzer = new DiscriminatoryPowerAnalyzer
  192.                 (
  193.                     _eventSamplePITMap.get (eventID),
  194.                     _gapTestSetting
  195.                 );

  196.                 org.drip.validation.distance.GapTestOutcome gapTestOutcome =
  197.                     discriminatoryPowerAnalyzer.gapTest (eventEnsemble.getValue());

  198.                 if (null == gapTestOutcome)
  199.                 {
  200.                     return null;
  201.                 }

  202.                 distanceAggregate = distanceAggregate + gapTestOutcome.distance() *
  203.                     _eventAggregationWeightFunction.loading (eventID);

  204.                 eventOutcomeMap.put (
  205.                     eventID,
  206.                     gapTestOutcome
  207.                 );
  208.             }

  209.             return new org.drip.validation.riskfactorsingle.GapTestOutcomeAggregate (
  210.                 eventOutcomeMap,
  211.                 distanceAggregate
  212.             );
  213.         }
  214.         catch (java.lang.Exception e)
  215.         {
  216.             e.printStackTrace();

  217.             return null;
  218.         }
  219.     }

  220.     /**
  221.      * Generate the Hypotheses Outcome Suite Aggregate for the specified Hypothesis Suite Aggregate
  222.      *
  223.      * @param hypothesisSuiteAggregate The Hypothesis Suite Aggregate
  224.      *
  225.      * @return The Suite of Gap Test Outcomes
  226.      */

  227.     public org.drip.validation.riskfactorsingle.HypothesisOutcomeSuiteAggregate hypothesisGapTest (
  228.         final org.drip.validation.riskfactorsingle.HypothesisSuiteAggregate hypothesisSuiteAggregate)
  229.     {
  230.         if (null == hypothesisSuiteAggregate)
  231.         {
  232.             return null;
  233.         }

  234.         java.util.Map<java.lang.String, java.util.Map<java.lang.String,
  235.             org.drip.validation.evidence.Ensemble>> hypothesisEventMap =
  236.                 hypothesisSuiteAggregate.hypothesisEventMap();

  237.         if (0 == hypothesisEventMap.size())
  238.         {
  239.             return null;
  240.         }

  241.         org.drip.validation.riskfactorsingle.HypothesisOutcomeSuiteAggregate hypothesisOutcomeSuiteAggregate = new
  242.             org.drip.validation.riskfactorsingle.HypothesisOutcomeSuiteAggregate();

  243.         for (java.util.Map.Entry<java.lang.String, java.util.Map<java.lang.String,
  244.             org.drip.validation.evidence.Ensemble>> hypothesisEvent : hypothesisEventMap.entrySet())
  245.         {
  246.             java.lang.String hypothesisID = hypothesisEvent.getKey();

  247.             if (!hypothesisOutcomeSuiteAggregate.add (
  248.                 hypothesisID,
  249.                 eventOutcomeAggregate (
  250.                     hypothesisID,
  251.                     hypothesisEvent.getValue()
  252.                 )
  253.             ))
  254.             {
  255.                 return null;
  256.             }
  257.         }

  258.         return hypothesisOutcomeSuiteAggregate;
  259.     }
  260. }