SingleSequenceAgnosticMetrics.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>SingleSequenceAgnosticMetrics</i> contains the Sample Distribution Metrics and Agnostic Bounds related
  68.  * to the specified 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 SingleSequenceAgnosticMetrics {
  82.     private boolean _bIsPositive = true;
  83.     private double[] _adblSequence = null;
  84.     private double _dblEmpiricalVariance = java.lang.Double.NaN;
  85.     private double _dblEmpiricalExpectation = java.lang.Double.NaN;
  86.     private org.drip.measure.continuous.R1Univariate _distPopulation = null;

  87.     /**
  88.      * Build out the Sequence and their Metrics
  89.      *
  90.      * @param adblSequence Array of Sequence Entries
  91.      * @param distPopulation The True Underlying Generator Distribution of the Population
  92.      *
  93.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  94.      */

  95.     public SingleSequenceAgnosticMetrics (
  96.         final double[] adblSequence,
  97.         final org.drip.measure.continuous.R1Univariate distPopulation)
  98.         throws java.lang.Exception
  99.     {
  100.         if (null == (_adblSequence = adblSequence))
  101.             throw new java.lang.Exception ("SingleSequenceAgnosticMetrics ctr: Invalid Inputs");

  102.         _dblEmpiricalExpectation = 0.;
  103.         _distPopulation = distPopulation;
  104.         int iNumEntry = _adblSequence.length;

  105.         if (0 == iNumEntry)
  106.             throw new java.lang.Exception ("SingleSequenceAgnosticMetrics ctr: Invalid Inputs");

  107.         for (int i = 0; i < iNumEntry; ++i) {
  108.             if (!org.drip.numerical.common.NumberUtil.IsValid (_adblSequence[i]))
  109.                 throw new java.lang.Exception ("SingleSequenceAgnosticMetrics ctr: Invalid Inputs");

  110.             _dblEmpiricalExpectation += _adblSequence[i];

  111.             if (_adblSequence[i] < 0.) _bIsPositive = false;
  112.         }

  113.         _dblEmpiricalVariance = 0.;
  114.         _dblEmpiricalExpectation /= iNumEntry;

  115.         for (int i = 0; i < iNumEntry; ++i)
  116.             _dblEmpiricalVariance += (_adblSequence[i] - _dblEmpiricalExpectation) * (_adblSequence[i] -
  117.                 _dblEmpiricalExpectation);

  118.         _dblEmpiricalVariance /= iNumEntry;
  119.     }

  120.     /**
  121.      * Compute the Specified Central Moment of the Sample Sequence
  122.      *
  123.      * @param iMoment The Moment
  124.      * @param bAbsolute TRUE - The Moment sought is on the Absolute Value
  125.      *
  126.      * @return The Specified Central Moment of the Sample Sequence
  127.      *
  128.      * @throws java.lang.Exception Thrown if the Inputs are invalid
  129.      */

  130.     public double empiricalCentralMoment (
  131.         final int iMoment,
  132.         final boolean bAbsolute)
  133.         throws java.lang.Exception
  134.     {
  135.         if (0 >= iMoment)
  136.             throw new java.lang.Exception
  137.                 ("SingleSequenceAgnosticMetrics::empiricalCentralMoment => Invalid Moment");

  138.         double dblMoment = 0.;
  139.         int iNumEntry = _adblSequence.length;

  140.         for (int i = 0; i < iNumEntry; ++i) {
  141.             double dblDeparture = _adblSequence[i] - _dblEmpiricalExpectation;

  142.             dblMoment += java.lang.Math.pow (bAbsolute ? java.lang.Math.abs (dblDeparture) : dblDeparture,
  143.                 iMoment);
  144.         }

  145.         return dblMoment / iNumEntry;
  146.     }

  147.     /**
  148.      * Compute the Specified Raw Moment of the Sample Sequence
  149.      *
  150.      * @param iMoment The Moment
  151.      * @param bAbsolute TRUE - The Moment sought is on the Absolute Value
  152.      *
  153.      * @return The Specified Raw Moment of the Sample Sequence
  154.      *
  155.      * @throws java.lang.Exception Thrown if the Inputs are invalid
  156.      */

  157.     public double empiricalRawMoment (
  158.         final int iMoment,
  159.         final boolean bAbsolute)
  160.         throws java.lang.Exception
  161.     {
  162.         if (0 >= iMoment)
  163.             throw new java.lang.Exception
  164.                 ("SingleSequenceAgnosticMetrics::empiricalRawMoment => Invalid Moment");

  165.         double dblMoment = 0.;
  166.         int iNumEntry = _adblSequence.length;

  167.         for (int i = 0; i < iNumEntry; ++i)
  168.             dblMoment += java.lang.Math.pow (bAbsolute ? java.lang.Math.abs (_adblSequence[i]) :
  169.                 _adblSequence[i], iMoment);

  170.         return dblMoment / iNumEntry;
  171.     }

  172.     /**
  173.      * Compute the Specified Anchor Moment of the Sample Sequence
  174.      *
  175.      * @param iMoment The Moment
  176.      * @param dblAnchor The Anchor Pivot off of which the Moment is calculated
  177.      * @param bAbsolute TRUE - The Moment sought is on the Absolute Value
  178.      *
  179.      * @return The Specified Anchor Moment of the Sample Sequence
  180.      *
  181.      * @throws java.lang.Exception Thrown if the Inputs are invalid
  182.      */

  183.     public double empiricalAnchorMoment (
  184.         final int iMoment,
  185.         final double dblAnchor,
  186.         final boolean bAbsolute)
  187.         throws java.lang.Exception
  188.     {
  189.         if (0 >= iMoment || !org.drip.numerical.common.NumberUtil.IsValid (dblAnchor))
  190.             throw new java.lang.Exception
  191.                 ("SingleSequenceAgnosticMetrics::empiricalAnchorMoment => Invalid Inputs");

  192.         double dblMoment = 0.;
  193.         int iNumEntry = _adblSequence.length;

  194.         for (int i = 0; i < iNumEntry; ++i) {
  195.             double dblPivotShift = _adblSequence[i] - dblAnchor;

  196.             dblMoment += java.lang.Math.pow (bAbsolute ? java.lang.Math.abs (dblPivotShift) : dblPivotShift,
  197.                 iMoment);
  198.         }

  199.         return dblMoment / iNumEntry;
  200.     }

  201.     /**
  202.      * Generate the Metrics for the Univariate Function Sequence
  203.      *  
  204.      * @param au The Univariate Function
  205.      *
  206.      * @return Metrics for the Univariate Function Sequence
  207.      */

  208.     public SingleSequenceAgnosticMetrics functionSequenceMetrics (
  209.         final org.drip.function.definition.R1ToR1 au)
  210.     {
  211.         if (null == au) return null;

  212.         int iNumEntry = _adblSequence.length;
  213.         double[] adblFunctionMetrics = new double[iNumEntry];

  214.         try {
  215.             for (int i = 0; i < iNumEntry; ++i)
  216.                 adblFunctionMetrics[i] = au.evaluate (_adblSequence[i]);

  217.             return new SingleSequenceAgnosticMetrics (adblFunctionMetrics, null);
  218.         } catch (java.lang.Exception e) {
  219.             e.printStackTrace();
  220.         }

  221.         return null;
  222.     }

  223.     /**
  224.      * Retrieve the Population Distribution
  225.      *
  226.      * @return The Population Distribution
  227.      */

  228.     public org.drip.measure.continuous.R1Univariate populationDistribution()
  229.     {
  230.         return _distPopulation;
  231.     }

  232.     /**
  233.      * Retrieve the Sample Expectation
  234.      *
  235.      * @return The Sample Expectation
  236.      */

  237.     public double empiricalExpectation()
  238.     {
  239.         return _dblEmpiricalExpectation;
  240.     }

  241.     /**
  242.      * Retrieve the Population Mean
  243.      *
  244.      * @return The Population Mean
  245.      *
  246.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  247.      */

  248.     public double populationMean()
  249.         throws java.lang.Exception
  250.     {
  251.         return null == _distPopulation ? java.lang.Double.NaN : _distPopulation.mean();
  252.     }

  253.     /**
  254.      * Retrieve the Sample Variance
  255.      *
  256.      * @return The Sample Variance
  257.      */

  258.     public double empiricalVariance()
  259.     {
  260.         return _dblEmpiricalVariance;
  261.     }

  262.     /**
  263.      * Retrieve the Population Variance
  264.      *
  265.      * @return The Population Variance
  266.      *
  267.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  268.      */

  269.     public double populationVariance()
  270.         throws java.lang.Exception
  271.     {
  272.         return null == _distPopulation ? java.lang.Double.NaN : _distPopulation.variance();
  273.     }

  274.     /**
  275.      * Retrieve the Sequence Positiveness Flag
  276.      *
  277.      * @return TRUE - The Sequence is Positiveness
  278.      */

  279.     public boolean isPositive()
  280.     {
  281.         return _bIsPositive;
  282.     }

  283.     /**
  284.      * Retrieve the Input Sequence
  285.      *
  286.      * @return The Input Sequence
  287.      */

  288.     public double[] sequence()
  289.     {
  290.         return _adblSequence;
  291.     }

  292.     /**
  293.      * Retrieve the Markov Upper Limiting Probability Bound for the Specified Level:
  294.      *  - P (X gte t) lte E[f(X)] / f(t)
  295.      *
  296.      * @param dblLevel The Specified Level
  297.      * @param auNonDecreasing The Non-decreasing Bounding Transformer Function
  298.      *
  299.      * @return The Markov Upper Limiting Probability Bound for the Specified Level
  300.      *
  301.      * @throws java.lang.Exception Thrown if the Inputs are invalid
  302.      */

  303.     public double markovUpperProbabilityBound (
  304.         final double dblLevel,
  305.         final org.drip.function.definition.R1ToR1 auNonDecreasing)
  306.         throws java.lang.Exception
  307.     {
  308.         if (!isPositive() || !org.drip.numerical.common.NumberUtil.IsValid (dblLevel) || dblLevel <= 0.)
  309.             throw new java.lang.Exception
  310.                 ("SingleSequenceAgnosticMetrics::markovUpperProbabilityBound => Invalid Inputs");

  311.         double dblPopulationMean = populationMean();

  312.         double dblUpperProbabilityBound = (org.drip.numerical.common.NumberUtil.IsValid (dblPopulationMean) ?
  313.             dblPopulationMean : _dblEmpiricalExpectation) / dblLevel;

  314.         if (null != auNonDecreasing) {
  315.             SingleSequenceAgnosticMetrics smFunction = functionSequenceMetrics (auNonDecreasing);

  316.             if (null == smFunction)
  317.                 throw new java.lang.Exception
  318.                     ("SingleSequenceAgnosticMetrics::markovUpperProbabilityBound => Cannot generate Function Sequence Metrics");

  319.             dblUpperProbabilityBound = smFunction.empiricalExpectation() / auNonDecreasing.evaluate
  320.                 (dblLevel);
  321.         }

  322.         return dblUpperProbabilityBound < 1. ? dblUpperProbabilityBound : 1.;
  323.     }

  324.     /**
  325.      * Retrieve the Mean Departure Bounds Using the Chebyshev's Inequality
  326.      *
  327.      * @param dblLevel The Level at which the Departure is sought
  328.      *
  329.      * @return The Mean Departure Bounds Instance
  330.      */

  331.     public org.drip.sequence.metrics.PivotedDepartureBounds chebyshevBound (
  332.         final double dblLevel)
  333.     {
  334.         if (!org.drip.numerical.common.NumberUtil.IsValid (dblLevel) || dblLevel <= 0.) return null;

  335.         try {
  336.             double dblPopulationVariance = populationVariance();

  337.             double dblMeanDepartureBound = (org.drip.numerical.common.NumberUtil.IsValid (dblPopulationVariance) ?
  338.                 dblPopulationVariance : _dblEmpiricalVariance) / (dblLevel * dblLevel);

  339.             dblMeanDepartureBound = dblMeanDepartureBound < 1. ? dblMeanDepartureBound : 1.;

  340.             return new org.drip.sequence.metrics.PivotedDepartureBounds
  341.                 (org.drip.sequence.metrics.PivotedDepartureBounds.PIVOT_ANCHOR_TYPE_MEAN, java.lang.Double.NaN,
  342.                     dblMeanDepartureBound, dblMeanDepartureBound);
  343.         } catch (java.lang.Exception e) {
  344.             e.printStackTrace();
  345.         }

  346.         return null;
  347.     }

  348.     /**
  349.      * Retrieve the Mean Departure Bounds Using the Central Moment Bounding Inequality
  350.      *
  351.      * @param dblLevel The Level at which the Departure is sought
  352.      * @param iMoment The Moment Bound sought
  353.      *
  354.      * @return The Mean Departure Bounds Instance
  355.      */

  356.     public org.drip.sequence.metrics.PivotedDepartureBounds centralMomentBound (
  357.         final double dblLevel,
  358.         final int iMoment)
  359.     {
  360.         if (!org.drip.numerical.common.NumberUtil.IsValid (dblLevel) || dblLevel <= 0.) return null;

  361.         try {
  362.             double dblMeanDepartureBound = empiricalCentralMoment (iMoment, true) / java.lang.Math.pow
  363.                 (dblLevel, iMoment);

  364.             dblMeanDepartureBound = dblMeanDepartureBound < 1. ? dblMeanDepartureBound : 1.;

  365.             return new org.drip.sequence.metrics.PivotedDepartureBounds
  366.                 (org.drip.sequence.metrics.PivotedDepartureBounds.PIVOT_ANCHOR_TYPE_MEAN, java.lang.Double.NaN,
  367.                     dblMeanDepartureBound, dblMeanDepartureBound);
  368.         } catch (java.lang.Exception e) {
  369.             e.printStackTrace();
  370.         }

  371.         return null;
  372.     }

  373.     /**
  374.      * Retrieve the Mean Departure Bounds Using the Chebyshev-Cantelli Inequality
  375.      *
  376.      * @param dblLevel The Level at which the Departure is sought
  377.      *
  378.      * @return The Mean Departure Bounds
  379.      */

  380.     public org.drip.sequence.metrics.PivotedDepartureBounds chebyshevCantelliBound (
  381.         final double dblLevel)
  382.     {
  383.         if (!org.drip.numerical.common.NumberUtil.IsValid (dblLevel) || dblLevel <= 0.) return null;

  384.         try {
  385.             double dblPopulationVariance = populationVariance();

  386.             double dblVariance = (org.drip.numerical.common.NumberUtil.IsValid (dblPopulationVariance) ?
  387.                 dblPopulationVariance : _dblEmpiricalVariance);

  388.             return new org.drip.sequence.metrics.PivotedDepartureBounds
  389.                 (org.drip.sequence.metrics.PivotedDepartureBounds.PIVOT_ANCHOR_TYPE_MEAN, java.lang.Double.NaN,
  390.                     java.lang.Double.NaN, dblVariance / (dblVariance + dblLevel * dblLevel));
  391.         } catch (java.lang.Exception e) {
  392.             e.printStackTrace();
  393.         }

  394.         return null;
  395.     }

  396.     /**
  397.      * Retrieve the Chebyshev's Association Joint Expectation Bound
  398.      *
  399.      * @param au1 Function 1 Operating On Sequence 1
  400.      * @param bNonDecreasing1 TRUE - Function 1 is non-decreasing
  401.      * @param au2 Function 2 Operating On Sequence 2
  402.      * @param bNonDecreasing2 TRUE - Function 2 is non-decreasing
  403.      *
  404.      * @return The Chebyshev's Association Joint Expectation Bound
  405.      */

  406.     public org.drip.sequence.metrics.PivotedDepartureBounds chebyshevAssociationBound (
  407.         final org.drip.function.definition.R1ToR1 au1,
  408.         final boolean bNonDecreasing1,
  409.         final org.drip.function.definition.R1ToR1 au2,
  410.         final boolean bNonDecreasing2)
  411.     {
  412.         if (null == au1 || null == au2) return null;

  413.         double dblBound = functionSequenceMetrics (au1).empiricalExpectation() * functionSequenceMetrics
  414.             (au2).empiricalExpectation();

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

  416.         if (bNonDecreasing1 == bNonDecreasing2) {
  417.             try {
  418.                 return new org.drip.sequence.metrics.PivotedDepartureBounds
  419.                     (org.drip.sequence.metrics.PivotedDepartureBounds.PIVOT_ANCHOR_TYPE_CUSTOM, 0.,
  420.                         dblBound, java.lang.Double.NaN);
  421.             } catch (java.lang.Exception e) {
  422.                 e.printStackTrace();
  423.             }

  424.             return null;
  425.         }

  426.         try {
  427.             return new org.drip.sequence.metrics.PivotedDepartureBounds
  428.                 (org.drip.sequence.metrics.PivotedDepartureBounds.PIVOT_ANCHOR_TYPE_CUSTOM, 0.,
  429.                     java.lang.Double.NaN, dblBound);
  430.         } catch (java.lang.Exception e) {
  431.             e.printStackTrace();
  432.         }

  433.         return null;
  434.     }

  435.     /**
  436.      * Estimate Mean Departure Bounds of the Average using the Weak Law of Large Numbers
  437.      *
  438.      * @param dblLevel The Level at which the Departure is sought
  439.      *
  440.      * @return The Mean Departure Bounds
  441.      */

  442.     public org.drip.sequence.metrics.PivotedDepartureBounds weakLawAverageBounds (
  443.         final double dblLevel)
  444.     {
  445.         if (!org.drip.numerical.common.NumberUtil.IsValid (dblLevel) || dblLevel <= 0.) return null;

  446.         try {
  447.             double dblPopulationVariance = populationVariance();

  448.             double dblVariance = (org.drip.numerical.common.NumberUtil.IsValid (dblPopulationVariance) ?
  449.                 dblPopulationVariance : _dblEmpiricalVariance);

  450.             double dblBound = dblVariance / (_adblSequence.length * dblLevel * dblLevel);
  451.             dblBound = dblBound < 1. ? dblBound : 1.;

  452.             return new org.drip.sequence.metrics.PivotedDepartureBounds
  453.                 (org.drip.sequence.metrics.PivotedDepartureBounds.PIVOT_ANCHOR_TYPE_MEAN,
  454.                     java.lang.Double.NaN, dblBound, dblBound);
  455.         } catch (java.lang.Exception e) {
  456.             e.printStackTrace();
  457.         }

  458.         return null;
  459.     }
  460. }