R1UnivariateNormal.java

  1. package org.drip.measure.gaussian;

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

  80. /**
  81.  * <i>R1UnivariateNormal</i> implements the Univariate R<sup>1</sup> Normal Distribution. It implements the
  82.  * Incremental, the Cumulative, and the Inverse Cumulative Distribution Densities.
  83.  *
  84.  *  <br><br>
  85.  *  <ul>
  86.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ComputationalCore.md">Computational Core Module</a></li>
  87.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/NumericalAnalysisLibrary.md">Numerical Analysis Library</a></li>
  88.  *      <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>
  89.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/measure/gaussian/README.md">R<sup>1</sup> R<sup>d</sup> Covariant Gaussian Quadrature</a></li>
  90.  *  </ul>
  91.  *
  92.  * @author Lakshmi Krishnamurthy
  93.  */

  94. public class R1UnivariateNormal extends org.drip.measure.continuous.R1Univariate {
  95.     private double _dblMean = java.lang.Double.NaN;
  96.     private double _dblSigma = java.lang.Double.NaN;

  97.     /**
  98.      * Generate a N (0, 1) distribution
  99.      *
  100.      * @return The N (0, 1) distribution
  101.      */

  102.     public static final org.drip.measure.gaussian.R1UnivariateNormal Standard()
  103.     {
  104.         try {
  105.             return new R1UnivariateNormal (0., 1.);
  106.         } catch (java.lang.Exception e) {
  107.             e.printStackTrace();
  108.         }

  109.         return null;
  110.     }

  111.     /**
  112.      * Construct a R1 Normal/Gaussian Distribution
  113.      *
  114.      * @param dblMean Mean of the Distribution
  115.      * @param dblSigma Sigma of the Distribution
  116.      *
  117.      * @throws java.lang.Exception Thrown if the inputs are invalid
  118.      */

  119.     public R1UnivariateNormal (
  120.         final double dblMean,
  121.         final double dblSigma)
  122.         throws java.lang.Exception
  123.     {
  124.         if (!org.drip.numerical.common.NumberUtil.IsValid (_dblMean = dblMean) ||
  125.             !org.drip.numerical.common.NumberUtil.IsValid (_dblSigma = dblSigma) || 0. > _dblSigma)
  126.             throw new java.lang.Exception ("R1UnivariateNormal Constructor: Invalid Inputs");
  127.     }

  128.     /**
  129.      * Retrieve the Sigma
  130.      *
  131.      * @return The Sigma
  132.      */

  133.     public double sigma()
  134.     {
  135.         return _dblSigma;
  136.     }

  137.     @Override public double[] support()
  138.     {
  139.         return new double[]
  140.         {
  141.             java.lang.Double.NEGATIVE_INFINITY,
  142.             java.lang.Double.POSITIVE_INFINITY
  143.         };
  144.     }

  145.     @Override public double cumulative (
  146.         final double dblX)
  147.         throws java.lang.Exception
  148.     {
  149.         if (!org.drip.numerical.common.NumberUtil.IsValid (dblX))
  150.             throw new java.lang.Exception ("R1UnivariateNormal::cumulative => Invalid Inputs");

  151.         if (0. == _dblSigma) return dblX >= _dblMean ? 1. : 0.;

  152.         return org.drip.measure.gaussian.NormalQuadrature.CDF ((dblX - _dblMean) / _dblSigma);
  153.     }

  154.     @Override public double incremental (
  155.         final double dblXLeft,
  156.         final double dblXRight)
  157.         throws java.lang.Exception
  158.     {
  159.         return cumulative (dblXRight) - cumulative (dblXLeft);
  160.     }

  161.     @Override public double invCumulative (
  162.         final double dblY)
  163.         throws java.lang.Exception
  164.     {
  165.         if (!org.drip.numerical.common.NumberUtil.IsValid (dblY) || 0. == _dblSigma)
  166.             throw new java.lang.Exception ("R1UnivariateNormal::invCumulative => Cannot calculate");

  167.         return org.drip.measure.gaussian.NormalQuadrature.InverseCDF (dblY) * _dblSigma + _dblMean;
  168.     }

  169.     @Override public double density (
  170.         final double dblX)
  171.         throws java.lang.Exception
  172.     {
  173.         if (!org.drip.numerical.common.NumberUtil.IsValid (dblX))
  174.             throw new java.lang.Exception ("R1UnivariateNormal::density => Invalid Inputs");

  175.         if (0. == _dblSigma) return dblX == _dblMean ? 1. : 0.;

  176.         double dblMeanShift = (dblX - _dblMean) / _dblSigma;

  177.         return java.lang.Math.exp (-0.5 * dblMeanShift * dblMeanShift);
  178.     }

  179.     @Override public double mean()
  180.     {
  181.         return _dblMean;
  182.     }

  183.     @Override public double median()
  184.     {
  185.         return _dblMean;
  186.     }

  187.     @Override public double mode()
  188.     {
  189.         return _dblMean;
  190.     }

  191.     @Override public double variance()
  192.     {
  193.         return _dblSigma * _dblSigma;
  194.     }

  195.     @Override public org.drip.numerical.common.Array2D histogram()
  196.     {
  197.         return null;
  198.     }

  199.     @Override public double random()
  200.     {
  201.         try
  202.         {
  203.             return invCumulative (java.lang.Math.random());
  204.         }
  205.         catch (java.lang.Exception e)
  206.         {
  207.             e.printStackTrace();
  208.         }

  209.         return java.lang.Double.NaN;
  210.     }

  211.     /**
  212.      * Compute the Error Function Around an Absolute Width around the Mean
  213.      *
  214.      * @param dblX The Width
  215.      *
  216.      * @return The Error Function Around an Absolute Width around the Mean
  217.      *
  218.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  219.      */

  220.     public double errorFunction (
  221.         final double dblX)
  222.         throws java.lang.Exception
  223.     {
  224.         if (!org.drip.numerical.common.NumberUtil.IsValid (dblX))
  225.             throw new java.lang.Exception ("R1UnivariateNormal::errorFunction => Invalid Inputs");

  226.         double dblWidth = java.lang.Math.abs (dblX);

  227.         return cumulative (_dblMean + dblWidth) - cumulative (_dblMean - dblWidth);
  228.     }

  229.     /**
  230.      * Compute the Confidence given the Width around the Mean
  231.      *
  232.      * @param dblWidth The Width
  233.      *
  234.      * @return The Error Function Around an Absolute Width around the Mean
  235.      *
  236.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  237.      */

  238.     public double confidence (
  239.         final double dblWidth)
  240.         throws java.lang.Exception
  241.     {
  242.         return errorFunction (dblWidth);
  243.     }

  244.     /**
  245.      * Compute the Width around the Mean given the Confidence Level
  246.      *
  247.      * @param dblConfidence The Confidence Level
  248.      *
  249.      * @return The Width around the Mean given the Confidence Level
  250.      *
  251.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  252.      */

  253.     public double confidenceInterval (
  254.         final double dblConfidence)
  255.         throws java.lang.Exception
  256.     {
  257.         if (!org.drip.numerical.common.NumberUtil.IsValid (dblConfidence) || 0. >= dblConfidence || 1. <=
  258.             dblConfidence)
  259.             throw new java.lang.Exception ("R1UnivariateNormal::confidenceInterval => Invalid Inputs");

  260.         return invCumulative (0.5 * (1. + dblConfidence));
  261.     }
  262. }