UnivariateMoments.java

  1. package org.drip.measure.statistics;

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

  77. /**
  78.  * <i>UnivariateMoments</i> generates and holds the Specified Univariate Series Mean, Variance, and a few
  79.  * selected Moments.
  80.  *
  81.  *  <br><br>
  82.  *  <ul>
  83.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ComputationalCore.md">Computational Core Module</a></li>
  84.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/NumericalAnalysisLibrary.md">Numerical Analysis Library</a></li>
  85.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/measure/README.md">R<sup>d</sup> Continuous/Discrete Probability Measures</a></li>
  86.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/measure/statistics/README.md">R<sup>1</sup> R<sup>d</sup> Thin Thick Moments</a></li>
  87.  *  </ul>
  88.  *
  89.  * @author Lakshmi Krishnamurthy
  90.  */

  91. public class UnivariateMoments {
  92.     private int _iNumSample = 0;
  93.     private java.lang.String _strName = "";
  94.     private double _dblMean = java.lang.Double.NaN;
  95.     private double _dblVariance = java.lang.Double.NaN;
  96.     private java.util.Map<java.lang.Integer, java.lang.Double> _mapMoment = null;

  97.     /**
  98.      * Construct a UnivariateMoments Instance for the specified Series
  99.      *
  100.      * @param strName Series Name
  101.      * @param adblEntry Series Entry
  102.      * @param aiMoment Array of Moments to be Calculated
  103.      *
  104.      * @return The UnivariateMoments Instance
  105.      */

  106.     public static final UnivariateMoments Standard (
  107.         final java.lang.String strName,
  108.         final double[] adblEntry,
  109.         final int[] aiMoment)
  110.     {
  111.         if (null == adblEntry) return null;

  112.         double dblMean = 0.;
  113.         double dblVariance = 0.;
  114.         int iNumSample = adblEntry.length;
  115.         int iNumMoment = null == aiMoment ? 0 : aiMoment.length;
  116.         double[] adblMoment = 0 == iNumMoment ? null : new double[iNumMoment];

  117.         java.util.Map<java.lang.Integer, java.lang.Double> mapMoment = 0 == iNumMoment ? null : new
  118.             java.util.TreeMap<java.lang.Integer, java.lang.Double>();

  119.         if (0 == iNumSample) return null;

  120.         for (int i = 0; i < iNumSample; ++i) {
  121.             if (!org.drip.numerical.common.NumberUtil.IsValid (adblEntry[i])) return null;

  122.             dblMean += adblEntry[i];
  123.         }

  124.         dblMean /= iNumSample;

  125.         for (int j = 0; j < iNumMoment; ++j)
  126.             adblMoment[j] = 0.;

  127.         for (int i = 0; i < iNumSample; ++i) {
  128.             double dblError = dblMean - adblEntry[i];
  129.             dblVariance += (dblError * dblError);

  130.             for (int j = 0; j < iNumMoment; ++j)
  131.                 adblMoment[j] = adblMoment[j] + java.lang.Math.pow (dblError, aiMoment[j]);
  132.         }

  133.         for (int j = 0; j < iNumMoment; ++j)
  134.             mapMoment.put (aiMoment[j], adblMoment[j]);

  135.         try {
  136.             return new UnivariateMoments (strName, dblMean, dblVariance / iNumSample, iNumSample, mapMoment);
  137.         } catch (java.lang.Exception e) {
  138.             e.printStackTrace();
  139.         }

  140.         return null;
  141.     }

  142.     /**
  143.      * Construct a UnivariateMoments Instance for the specified Series
  144.      *
  145.      * @param strName Series Name
  146.      * @param adblEntry Series Entry
  147.      *
  148.      * @return The UnivariateMoments Instance
  149.      */

  150.     public static final UnivariateMoments Standard (
  151.         final java.lang.String strName,
  152.         final double[] adblEntry)
  153.     {
  154.         return Standard (strName, adblEntry, null);
  155.     }

  156.     protected UnivariateMoments (
  157.         final java.lang.String strName,
  158.         final double dblMean,
  159.         final double dblVariance,
  160.         final int iNumSample,
  161.         final java.util.Map<java.lang.Integer, java.lang.Double> mapMoment)
  162.         throws java.lang.Exception
  163.     {
  164.         if (null == (_strName = strName) || _strName.isEmpty() || !org.drip.numerical.common.NumberUtil.IsValid
  165.             (_dblMean = dblMean) || !org.drip.numerical.common.NumberUtil.IsValid (_dblVariance = dblVariance) ||
  166.                 0 >= (_iNumSample = iNumSample))
  167.             throw new java.lang.Exception ("UnivariateMetrics Constructor => Invalid Inputs!");

  168.         _mapMoment = mapMoment;
  169.     }

  170.     /**
  171.      * Retrieve the Series Name
  172.      *
  173.      * @return The Series Name
  174.      */

  175.     public java.lang.String name()
  176.     {
  177.         return _strName;
  178.     }

  179.     /**
  180.      * Retrieve the Number of Samples
  181.      *
  182.      * @return The Number of Samples
  183.      */

  184.     public int numSample()
  185.     {
  186.         return _iNumSample;
  187.     }

  188.     /**
  189.      * Retrieve the Series Mean
  190.      *
  191.      * @return The Series Mean
  192.      */

  193.     public double mean()
  194.     {
  195.         return _dblMean;
  196.     }

  197.     /**
  198.      * Retrieve the Series Variance
  199.      *
  200.      * @return The Series Variance
  201.      */

  202.     public double variance()
  203.     {
  204.         return _dblVariance;
  205.     }

  206.     /**
  207.      * Retrieve the Series Standard Deviation
  208.      *
  209.      * @return The Series Standard Deviation
  210.      */

  211.     public double stdDev()
  212.     {
  213.         return java.lang.Math.sqrt (_dblVariance);
  214.     }

  215.     /**
  216.      * Retrieve the Series Standard Error
  217.      *
  218.      * @return The Series Standard Error
  219.      */

  220.     public double stdError()
  221.     {
  222.         return java.lang.Math.sqrt (_dblVariance);
  223.     }

  224.     /**
  225.      * Retrieve the Moments Map
  226.      *
  227.      * @return The Map of Moments
  228.      */

  229.     public java.util.Map<java.lang.Integer, java.lang.Double> momentMap()
  230.     {
  231.         return _mapMoment;
  232.     }

  233.     /**
  234.      * Compute the Series t-Statistic around the Series Hypothesis Pivot
  235.      *
  236.      * @param hypothesisPivot The Series Hypothesis Pivot
  237.      *
  238.      * @return The Series t-Statistic around the Series Hypothesis Pivot
  239.      *
  240.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  241.      */

  242.     public double tStatistic (
  243.         final double hypothesisPivot)
  244.         throws java.lang.Exception
  245.     {
  246.         if (!org.drip.numerical.common.NumberUtil.IsValid (hypothesisPivot))
  247.         {
  248.             throw new java.lang.Exception ("UnivariateMetrics::tStatistic => Invalid Inputs");
  249.         }

  250.         return (_dblMean - hypothesisPivot) / java.lang.Math.sqrt (_dblVariance);
  251.     }

  252.     /**
  253.      * Compute the Series t-Statistic for Hypothesis Pivot = 0 (e.g., the False Positive NULL Hypothesis for
  254.      *  for Homoscedastic Univariate Linear Regression)
  255.      *
  256.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  257.      *
  258.      * @return The Series t-Statistic
  259.      */

  260.     public double tStatistic()
  261.         throws java.lang.Exception
  262.     {
  263.         return _dblMean / java.lang.Math.sqrt (_dblVariance);
  264.     }

  265.     /**
  266.      * Estimate the Offset in Terms of the NUmber of Standard Errors
  267.      *
  268.      * @param x The Observation Point
  269.      *
  270.      * @return The Offset in Terms of the NUmber of Standard Errors
  271.      *
  272.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  273.      */

  274.     public double standardErrorOffset (
  275.         final double x)
  276.         throws java.lang.Exception
  277.     {
  278.         if (!org.drip.numerical.common.NumberUtil.IsValid (x))
  279.         {
  280.             throw new java.lang.Exception ("UnivariateMetrics::standardErrorOffset => Invalid Inputs");
  281.         }

  282.         return (_dblMean - x) / java.lang.Math.sqrt (_dblVariance);
  283.     }

  284.     /**
  285.      * Retrieve the Degrees of Freedom
  286.      *
  287.      * @return The Degrees of Freedom
  288.      */

  289.     public int degreesOfFreedom()
  290.     {
  291.         return _iNumSample - 1;
  292.     }

  293.     /**
  294.      * Compute the Predictive Confidence Level
  295.      *
  296.      * @return The Predictive Confidence Level
  297.      */

  298.     public double predictiveConfidenceLevel()
  299.     {
  300.         return java.lang.Math.sqrt (_dblVariance * (1. + 1. / (1. + _iNumSample)));
  301.     }
  302. }