UnitSequenceAgnosticMetrics.java

  1. package org.drip.sequence.metrics;

  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>UnitSequenceAgnosticMetrics</i> contains the Sample Distribution Metrics and Agnostic Bounds related to
  68.  * the specified Bounded [0, 1] Sequence.
  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/metrics">Metrics</a></li>
  76.  *  </ul>
  77.  * <br><br>
  78.  *
  79.  * @author Lakshmi Krishnamurthy
  80.  */

  81. public class UnitSequenceAgnosticMetrics extends org.drip.sequence.metrics.BoundedSequenceAgnosticMetrics
  82. {
  83.     private double _dblPopulationMean = java.lang.Double.NaN;

  84.     /**
  85.      * UnitSequenceAgnosticMetrics Constructor
  86.      *
  87.      * @param adblSequence The Random Sequence
  88.      * @param dblPopulationMean The Mean of the Underlying Distribution
  89.      *
  90.      * @throws java.lang.Exception Thrown if UnitSequenceAgnosticMetrics cannot be constructed
  91.      */

  92.     public UnitSequenceAgnosticMetrics (
  93.         final double[] adblSequence,
  94.         final double dblPopulationMean)
  95.         throws java.lang.Exception
  96.     {
  97.         super (adblSequence, null, 1.);

  98.         _dblPopulationMean = dblPopulationMean;
  99.     }

  100.     /**
  101.      * Retrieve the Mean of the Underlying Distribution
  102.      *
  103.      * @return The Mean of the Underlying Distribution
  104.      */

  105.     public double populationMean()
  106.     {
  107.         return _dblPopulationMean;
  108.     }

  109.     /**
  110.      * Compute the Chernoff Binomial Upper Bound
  111.      *
  112.      * @param dblLevel The Level at which the Bound is sought
  113.      *
  114.      * @return The Chernoff Binomial Upper Bound
  115.      *
  116.      * @throws java.lang.Exception Thrown if the Chernoff Binomial Upper Bound cannot be computed
  117.      */

  118.     public double chernoffBinomialUpperBound (
  119.         final double dblLevel)
  120.         throws java.lang.Exception
  121.     {
  122.         if (!org.drip.numerical.common.NumberUtil.IsValid (dblLevel) || 1. < dblLevel)
  123.             throw new java.lang.Exception
  124.                 ("UnitSequenceAgnosticMetrics::chernoffBinomialUpperBound => Invalid Inputs");

  125.         int iNumEntry = sequence().length;

  126.         double dblPopulationMean = org.drip.numerical.common.NumberUtil.IsValid (_dblPopulationMean) ?
  127.             _dblPopulationMean : empiricalExpectation();

  128.         double dblBound = java.lang.Math.pow (dblPopulationMean / dblLevel, iNumEntry * dblLevel) *
  129.             java.lang.Math.pow ((1. - dblPopulationMean) / (1. - dblLevel), iNumEntry * (1. - dblLevel));

  130.         if (!org.drip.numerical.common.NumberUtil.IsValid (dblBound)) return 0.;

  131.         return dblBound > 1. ? 1. : dblBound;
  132.     }

  133.     /**
  134.      * Compute the Chernoff-Poisson Binomial Upper Bound
  135.      *
  136.      * @param dblLevel The Level at which the Bound is sought
  137.      *
  138.      * @return The Chernoff-Poisson Binomial Upper Bound
  139.      *
  140.      * @throws java.lang.Exception Thrown if the Chernoff-Poisson Binomial Upper Bound cannot be computed
  141.      */

  142.     public double chernoffPoissonUpperBound (
  143.         final double dblLevel)
  144.         throws java.lang.Exception
  145.     {
  146.         if (!org.drip.numerical.common.NumberUtil.IsValid (dblLevel) || 1. < dblLevel)
  147.             throw new java.lang.Exception
  148.                 ("UnitSequenceAgnosticMetrics::ChernoffBinomialUpperBound => Invalid Inputs");

  149.         int iNumEntry = sequence().length;

  150.         double dblPopulationMean = org.drip.numerical.common.NumberUtil.IsValid (_dblPopulationMean) ?
  151.             _dblPopulationMean : empiricalExpectation();

  152.         double dblBound = java.lang.Math.pow (dblPopulationMean / dblLevel, iNumEntry * dblLevel) *
  153.             java.lang.Math.exp (iNumEntry * (dblLevel - dblPopulationMean));

  154.         if (!org.drip.numerical.common.NumberUtil.IsValid (dblBound)) return 0.;

  155.         return dblBound > 1. ? 1. : dblBound;
  156.     }

  157.     /**
  158.      * Compute the Karp/Hagerup/Rub Pivot Departure Bounds outlined below:
  159.      *
  160.      *  - Karp, R. M. (1988): Probabilistic Analysis of Algorithms, University of California, Berkeley.
  161.      *  - Hagerup, T., and C. Rub (1990): A Guided Tour of Chernoff Bounds, Information Processing Letters,
  162.      *      33:305-308.
  163.      *
  164.      * @param dblLevel The Level at which the Bound is sought
  165.      *
  166.      * @return The Karp/Hagerup/Rub Pivot Departure Bounds
  167.      */

  168.     public org.drip.sequence.metrics.PivotedDepartureBounds karpHagerupRubBounds (
  169.         final double dblLevel)
  170.     {
  171.         if (!org.drip.numerical.common.NumberUtil.IsValid (dblLevel) || 1. < dblLevel) return null;

  172.         int iNumEntry = sequence().length;

  173.         double dblPopulationMean = org.drip.numerical.common.NumberUtil.IsValid (_dblPopulationMean) ?
  174.             _dblPopulationMean : empiricalExpectation();

  175.         double dblScaledLevel = dblLevel / dblPopulationMean;

  176.         double dblLowerBound = java.lang.Math.exp (-0.5 * dblPopulationMean * iNumEntry * dblScaledLevel *
  177.             dblScaledLevel);

  178.         if (!org.drip.numerical.common.NumberUtil.IsValid (dblLowerBound)) dblLowerBound = 0.;

  179.         double dblUpperBound = java.lang.Math.exp (-1. * dblPopulationMean * iNumEntry * (1. +
  180.             dblScaledLevel) * java.lang.Math.log (1. + dblScaledLevel) - dblScaledLevel);

  181.         if (!org.drip.numerical.common.NumberUtil.IsValid (dblUpperBound)) dblUpperBound = 0.;

  182.         try {
  183.             return new org.drip.sequence.metrics.PivotedDepartureBounds
  184.                 (org.drip.sequence.metrics.PivotedDepartureBounds.PIVOT_ANCHOR_TYPE_MEAN,
  185.                     java.lang.Double.NaN, dblLowerBound > 1. ? 1. : dblLowerBound, dblUpperBound > 1. ? 1. :
  186.                         dblUpperBound);
  187.         } catch (java.lang.Exception e) {
  188.             e.printStackTrace();
  189.         }

  190.         return null;
  191.     }
  192. }