BoundedSequenceAgnosticMetrics.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>BoundedSequenceAgnosticMetrics</i> contains the Sample Distribution Metrics and Agnostic Bounds related
  68.  * to the specified Bounded 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 BoundedSequenceAgnosticMetrics extends
  82.     org.drip.sequence.metrics.SingleSequenceAgnosticMetrics {
  83.     private double _dblSupport = java.lang.Double.NaN;

  84.     /**
  85.      * BoundedSequenceAgnosticMetrics Constructor
  86.      *
  87.      * @param adblSequence The Random Sequence
  88.      * @param distPopulation The Population Distribution
  89.      * @param dblSupport The Support of the Underlying Random Variable
  90.      *
  91.      * @throws java.lang.Exception Thrown if BoundedSequenceAgnosticMetrics cannot be constructed
  92.      */

  93.     public BoundedSequenceAgnosticMetrics (
  94.         final double[] adblSequence,
  95.         final org.drip.measure.continuous.R1Univariate distPopulation,
  96.         final double dblSupport)
  97.         throws java.lang.Exception
  98.     {
  99.         super (adblSequence, distPopulation);

  100.         if (!org.drip.numerical.common.NumberUtil.IsValid (_dblSupport = dblSupport) || 0. >= _dblSupport)
  101.             throw new java.lang.Exception ("BoundedSequenceAgnosticMetrics ctr: Invalid Inputs");
  102.     }

  103.     /**
  104.      * Retrieve the Random Sequence Support
  105.      *
  106.      * @return The Support Underlying the Random Sequence
  107.      */

  108.     public double support()
  109.     {
  110.         return _dblSupport;
  111.     }

  112.     /**
  113.      * Estimate Mean Departure Bounds of the Average using the Chernoff-Hoeffding Bound
  114.      *
  115.      * @param dblLevel The Level at which the Departure is sought
  116.      *
  117.      * @return The Mean Departure Bounds
  118.      */

  119.     public org.drip.sequence.metrics.PivotedDepartureBounds chernoffHoeffdingAverageBounds (
  120.         final double dblLevel)
  121.     {
  122.         if (!org.drip.numerical.common.NumberUtil.IsValid (dblLevel) || dblLevel <= 0) return null;

  123.         double dblBound = java.lang.Math.exp (-2. * sequence().length * dblLevel * dblLevel / (_dblSupport *
  124.             _dblSupport));

  125.         dblBound = dblBound < 1. ? dblBound : 1.;

  126.         try {
  127.             return new org.drip.sequence.metrics.PivotedDepartureBounds
  128.                 (org.drip.sequence.metrics.PivotedDepartureBounds.PIVOT_ANCHOR_TYPE_MEAN,
  129.                     java.lang.Double.NaN, dblBound, dblBound);
  130.         } catch (java.lang.Exception e) {
  131.             e.printStackTrace();
  132.         }

  133.         return null;
  134.     }

  135.     /**
  136.      * Estimate Mean Departure Bounds of the Average using the Bennett Inequality Bounds
  137.      *
  138.      * @param dblLevel The Level at which the Departure is sought
  139.      *
  140.      * @return The Mean Departure Bounds
  141.      */

  142.     public org.drip.sequence.metrics.PivotedDepartureBounds bennettAverageBounds (
  143.         final double dblLevel)
  144.     {
  145.         if (!org.drip.numerical.common.NumberUtil.IsValid (dblLevel) || dblLevel <= 0.) return null;

  146.         try {
  147.             double dblPopulationVariance = populationVariance();

  148.             double dblVariance = (org.drip.numerical.common.NumberUtil.IsValid (dblPopulationVariance) ?
  149.                 dblPopulationVariance : empiricalVariance());

  150.             double dblBennettFactor = _dblSupport * dblLevel / dblVariance;

  151.             dblBennettFactor = (1. + dblBennettFactor) * java.lang.Math.log (1. + dblBennettFactor) -
  152.                 dblBennettFactor;

  153.             double dblBound = java.lang.Math.exp (-1. * sequence().length * dblVariance * dblBennettFactor /
  154.                 (_dblSupport * _dblSupport));

  155.             dblBound = dblBound < 1. ? dblBound : 1.;

  156.             return new org.drip.sequence.metrics.PivotedDepartureBounds
  157.                 (org.drip.sequence.metrics.PivotedDepartureBounds.PIVOT_ANCHOR_TYPE_MEAN,
  158.                     java.lang.Double.NaN, dblBound, dblBound);
  159.         } catch (java.lang.Exception e) {
  160.             e.printStackTrace();
  161.         }

  162.         return null;
  163.     }

  164.     /**
  165.      * Estimate Mean Departure Bounds of the Average using the Bernstein Inequality Bounds
  166.      *
  167.      * @param dblLevel The Level at which the Departure is sought
  168.      *
  169.      * @return The Mean Departure Bounds
  170.      */

  171.     public org.drip.sequence.metrics.PivotedDepartureBounds bernsteinAverageBounds (
  172.         final double dblLevel)
  173.     {
  174.         if (!org.drip.numerical.common.NumberUtil.IsValid (dblLevel) || dblLevel <= 0.) return null;

  175.         try {
  176.             double dblPopulationVariance = populationVariance();

  177.             double dblVariance = (org.drip.numerical.common.NumberUtil.IsValid (dblPopulationVariance) ?
  178.                 dblPopulationVariance : empiricalVariance());

  179.             double dblBound = java.lang.Math.exp (-1. * sequence().length * dblLevel * dblLevel / (2. *
  180.                 dblVariance + (2. * _dblSupport * dblLevel / 3.)));

  181.             dblBound = dblBound < 1. ? dblBound : 1.;

  182.             return new org.drip.sequence.metrics.PivotedDepartureBounds
  183.                 (org.drip.sequence.metrics.PivotedDepartureBounds.PIVOT_ANCHOR_TYPE_MEAN,
  184.                     java.lang.Double.NaN, dblBound, dblBound);
  185.         } catch (java.lang.Exception e) {
  186.             e.printStackTrace();
  187.         }

  188.         return null;
  189.     }
  190. }