NormalQuadrature.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) 2011 Robert Sedgewick
  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>NormalQuadrature</i> implements the Quadrature Metrics behind the Univariate Normal Distribution. It
  82.  * implements the 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 Robert Sedgewick
  93.  * @author Lakshmi Krishnamurthy
  94.  */

  95. public class NormalQuadrature {
  96.     private static final double InverseCDF (
  97.         final double dblY,
  98.         final double dblTolerance,
  99.         final double dblLowCutoff,
  100.         final double dblHighCutoff)
  101.         throws java.lang.Exception
  102.     {
  103.         double dblMid = 0.5 * (dblHighCutoff + dblLowCutoff);

  104.         if (dblHighCutoff - dblLowCutoff < dblTolerance) return dblMid;

  105.         return CDF (dblMid) > dblY ? InverseCDF (dblY, dblTolerance, dblLowCutoff, dblMid) : InverseCDF
  106.             (dblY, dblTolerance, dblMid, dblHighCutoff);
  107.     }

  108.     /**
  109.      * Retrieve the Density at the specified Point using Zero Mean and Unit Variance
  110.      *
  111.      * @param dblX The Ordinate
  112.      *
  113.      * @return The Density at the specified Point Zero Mean and Unit Variance
  114.      *
  115.      * @throws java.lang.Exception Thrown if Inputs are Invalid
  116.      */

  117.     public static final double Density (
  118.         final double dblX)
  119.         throws java.lang.Exception
  120.     {
  121.         if (!org.drip.numerical.common.NumberUtil.IsValid (dblX))
  122.             throw new java.lang.Exception ("NormalQuadrature::Density => Invalid Inputs");

  123.         return java.lang.Math.exp (-0.5 * dblX * dblX) / java.lang.Math.sqrt (2 * java.lang.Math.PI);
  124.     }

  125.     /**
  126.      * Compute the Cumulative Distribution Function up to the specified Variate
  127.      *
  128.      * @param dblX The Variate
  129.      *
  130.      * @return The Cumulative Distribution Function up to the specified Variate
  131.      *
  132.      * @throws java.lang.Exception thrown if the Inputs are Invalid
  133.      */

  134.     public static final double CDF (
  135.         final double dblX)
  136.         throws java.lang.Exception
  137.     {
  138.         if (java.lang.Double.isNaN (dblX))
  139.             throw new java.lang.Exception ("NormalQuadrature::CDF => Invalid Inputs");

  140.         if (dblX < -8.) return 0.;

  141.         if (dblX > 8.) return 1.;

  142.         double dblSum = 0.;
  143.         double dblTerm = dblX;

  144.         for (int i = 3; dblSum + dblTerm != dblSum; i += 2) {
  145.             dblSum  = dblSum + dblTerm;
  146.             dblTerm = dblTerm * dblX * dblX / i;
  147.         }

  148.         return 0.5 + dblSum * Density (dblX);
  149.     }

  150.     /**
  151.      * Compute the Inverse CDF of the Distribution up to the specified Y
  152.      *
  153.      * @param dblY Y
  154.      *
  155.      * @return The Inverse CDF of the Distribution up to the specified Y
  156.      *
  157.      * @throws java.lang.Exception Thrown if Inputs are Invalid
  158.      */

  159.     public static final double InverseCDF (
  160.         final double dblY)
  161.         throws java.lang.Exception
  162.     {
  163.         if (!org.drip.numerical.common.NumberUtil.IsValid (dblY))
  164.             throw new java.lang.Exception ("NormalQuadrature::InverseCDF => Invalid Inputs");

  165.         return InverseCDF (dblY, .00000001, -8., 8.);
  166.     }

  167.     /**
  168.      * Compute the Probit of the Distribution up to the specified p
  169.      *
  170.      * @param p p
  171.      *
  172.      * @return The Probit of the Distribution up to the specified p
  173.      *
  174.      * @throws java.lang.Exception Thrown if Inputs are Invalid
  175.      */

  176.     public static final double Probit (
  177.         final double p)
  178.         throws java.lang.Exception
  179.     {
  180.         return InverseCDF (p);
  181.     }

  182.     /**
  183.      * Generate a Random Univariate Number following a Gaussian Distribution
  184.      *
  185.      * @return The Random Univariate Number
  186.      *
  187.      * @throws java.lang.Exception Thrown the Random Number cannot be generated
  188.      */

  189.     public static final double Random()
  190.         throws java.lang.Exception
  191.     {
  192.         return InverseCDF (java.lang.Math.random());
  193.     }

  194.     /**
  195.      * Compute the Error Function of x
  196.      *
  197.      * @param x x
  198.      *
  199.      * @return The Error Function of x
  200.      *
  201.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  202.      */

  203.     public static final double ERF (
  204.         final double x)
  205.         throws java.lang.Exception
  206.     {
  207.         return 2. * CDF (x * java.lang.Math.sqrt (2.)) - 1.;
  208.     }

  209.     /**
  210.      * Compute the Error Function Complement of x
  211.      *
  212.      * @param x x
  213.      *
  214.      * @return The Error Function Complement of x
  215.      *
  216.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  217.      */

  218.     public static final double ERFC (
  219.         final double x)
  220.         throws java.lang.Exception
  221.     {
  222.         return 2. * CDF (1. - x * java.lang.Math.sqrt (2.));
  223.     }
  224. }