ErrorFunction.java

  1. package org.drip.function.e2erf;

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

  74. /**
  75.  * <i>ErrorFunction</i> implements the E<sub>2</sub> Error Function (erf). The References are:
  76.  *
  77.  * <br><br>
  78.  *  <ul>
  79.  *      <li>
  80.  *          Abramowitz, M., and I. A. Stegun (2007): <i>Handbook of Mathematics Functions</i> <b>Dover Book
  81.  *              on Mathematics</b>
  82.  *      </li>
  83.  *      <li>
  84.  *          Chang, S. H., P. C. Cosman, L. B. Milstein (2011): Chernoff-Type Bounds for Gaussian Error
  85.  *              Function <i>IEEE Transactions on Communications</i> <b>59 (11)</b> 2939-2944
  86.  *      </li>
  87.  *      <li>
  88.  *          Cody, W. J. (1991): Algorithm 715: SPECFUN – A Portable FORTRAN Package of Special Function
  89.  *              Routines and Test Drivers <i>ACM Transactions on Mathematical Software</i> <b>19 (1)</b>
  90.  *              22-32
  91.  *      </li>
  92.  *      <li>
  93.  *          Schopf, H. M., and P. H. Supancic (2014): On Burmann’s Theorem and its Application to Problems of
  94.  *              Linear and Non-linear Heat Transfer and Diffusion
  95.  *              https://www.mathematica-journal.com/2014/11/on-burmanns-theorem-and-its-application-to-problems-of-linear-and-nonlinear-heat-transfer-and-diffusion/#more-39602/
  96.  *      </li>
  97.  *      <li>
  98.  *          Wikipedia (2019): Error Function https://en.wikipedia.org/wiki/Error_function
  99.  *      </li>
  100.  *  </ul>
  101.  *
  102.  *  <br><br>
  103.  *  <ul>
  104.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ComputationalCore.md">Computational Core Module</a></li>
  105.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/NumericalAnalysisLibrary.md">Numerical Analysis Library</a></li>
  106.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/function/README.md">R<sup>d</sup> To R<sup>d</sup> Function Analysis</a></li>
  107.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/function/e2erf/README.md">E<sub>2</sub> erf and erf<sup>-1</sup> Implementations</a></li>
  108.  *  </ul>
  109.  *
  110.  * @author Lakshmi Krishnamurthy
  111.  */

  112. public class ErrorFunction extends org.drip.numerical.estimation.R1ToR1IntegrandLimitEstimator
  113. {
  114.     private org.drip.numerical.estimation.R1ToR1Series _r1ToR1SeriesGenerator = null;

  115.     /**
  116.      * Construct the Euler-MacLaurin Instance of the E<sub>2</sub> erf
  117.      *
  118.      * @param termCount The Count of Approximation Terms
  119.      *
  120.      * @return The Euler-MacLaurin Instance of the E<sub>2</sub> erf
  121.      */

  122.     public static final ErrorFunction MacLaurin (
  123.         final int termCount)
  124.     {
  125.         final org.drip.function.e2erf.MacLaurinSeries e2MacLaurinSeriesGenerator =
  126.             org.drip.function.e2erf.MacLaurinSeries.ERF (termCount);

  127.         if (null == e2MacLaurinSeriesGenerator)
  128.         {
  129.             return null;
  130.         }

  131.         try
  132.         {
  133.             return new ErrorFunction (
  134.                 e2MacLaurinSeriesGenerator,
  135.                 null
  136.             )
  137.             {
  138.                 @Override public double evaluate (
  139.                     final double z)
  140.                     throws java.lang.Exception
  141.                 {
  142.                     if (!org.drip.numerical.common.NumberUtil.IsValid (z))
  143.                     {
  144.                         throw new java.lang.Exception
  145.                             ("ErrorFunction::MacLaurin::evaluate => Invalid Inputs");
  146.                     }

  147.                     double erf = 2. / java.lang.Math.sqrt (java.lang.Math.PI) *
  148.                         e2MacLaurinSeriesGenerator.cumulative (
  149.                             0.,
  150.                             z
  151.                         );

  152.                     return erf > 1. ? 1. : erf;
  153.                 }
  154.             };
  155.         }
  156.         catch (java.lang.Exception e)
  157.         {
  158.             e.printStackTrace();
  159.         }

  160.         return null;
  161.     }

  162.     /**
  163.      * Construct the Convergent Hans Heinrich Burmann Version of the E<sub>2</sub> erf
  164.      *
  165.      * @return The Convergent Hans Heinrich Burmann Version of the E<sub>2</sub> erf
  166.      */

  167.     public static final ErrorFunction HansHeinrichBurmannConvergent()
  168.     {
  169.         final org.drip.numerical.estimation.R1ToR1Series
  170.             hansHeinrichBurmannConvergentSeriesGenerator =
  171.                 org.drip.function.e2erf.HansHeinrichBurmannSeries.Convergent();

  172.         if (null == hansHeinrichBurmannConvergentSeriesGenerator)
  173.         {
  174.             return null;
  175.         }

  176.         try
  177.         {
  178.             return new ErrorFunction (
  179.                 hansHeinrichBurmannConvergentSeriesGenerator,
  180.                 null
  181.             )
  182.             {
  183.                 @Override public double evaluate (
  184.                     final double z)
  185.                     throws java.lang.Exception
  186.                 {
  187.                     if (!org.drip.numerical.common.NumberUtil.IsValid (z))
  188.                     {
  189.                         throw new java.lang.Exception
  190.                             ("ErrorFunction::HansHeinrichBurmannConvergent::evaluate => Invalid Inputs");
  191.                     }

  192.                     double erf = 2. / java.lang.Math.sqrt (java.lang.Math.PI) *
  193.                         java.lang.Math.sqrt (1. - java.lang.Math.exp (-1. * z * z)) *
  194.                         hansHeinrichBurmannConvergentSeriesGenerator.cumulative (
  195.                             0.,
  196.                             z
  197.                         );

  198.                     return erf > 1. ? 1. : erf;
  199.                 }
  200.             };
  201.         }
  202.         catch (java.lang.Exception e)
  203.         {
  204.             e.printStackTrace();
  205.         }

  206.         return null;
  207.     }

  208.     /**
  209.      * Construct the Schopf-Supancic (2014) Hans Heinrich Burmann Version of the E<sub>2</sub> erf
  210.      *
  211.      * @return The Schopf-Supancic (2014) Hans Heinrich Burmann Version of the E<sub>2</sub> erf
  212.      */

  213.     public static final ErrorFunction HansHeinrichBurmannSchopfSupancic2014()
  214.     {
  215.         final org.drip.numerical.estimation.R1ToR1Series hansHeinrichBurmannConvergentSeriesGenerator
  216.             = org.drip.function.e2erf.HansHeinrichBurmannSeries.SchopfSupancic2014();

  217.         if (null == hansHeinrichBurmannConvergentSeriesGenerator)
  218.         {
  219.             return null;
  220.         }

  221.         try
  222.         {
  223.             return new ErrorFunction (
  224.                 hansHeinrichBurmannConvergentSeriesGenerator,
  225.                 null
  226.             )
  227.             {
  228.                 @Override public double evaluate (
  229.                     final double z)
  230.                     throws java.lang.Exception
  231.                 {
  232.                     if (!org.drip.numerical.common.NumberUtil.IsValid (z))
  233.                     {
  234.                         throw new java.lang.Exception
  235.                             ("ErrorFunction::HansHeinrichBurmannSchopfSupancic2014::evaluate => Invalid Inputs");
  236.                     }

  237.                     double erf = 2. / java.lang.Math.sqrt (java.lang.Math.PI) *
  238.                         java.lang.Math.sqrt (1. - java.lang.Math.exp (-1. * z * z)) *
  239.                         hansHeinrichBurmannConvergentSeriesGenerator.cumulative (
  240.                             0.,
  241.                             z
  242.                         );

  243.                     return erf > 1. ? 1. : erf;
  244.                 }
  245.             };
  246.         }
  247.         catch (java.lang.Exception e)
  248.         {
  249.             e.printStackTrace();
  250.         }

  251.         return null;
  252.     }

  253.     /**
  254.      * ErrorFunction Constructor
  255.      *
  256.      * @param r1ToR1SeriesGenerator R<sup>1</sup> To R<sup>1</sup> Series Generator
  257.      * @param dc Differential Control
  258.      *
  259.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  260.      */

  261.     public ErrorFunction (
  262.         final org.drip.numerical.estimation.R1ToR1Series r1ToR1SeriesGenerator,
  263.         final org.drip.numerical.differentiation.DerivativeControl dc)
  264.         throws java.lang.Exception
  265.     {
  266.         super (
  267.             dc,
  268.             0.
  269.         );

  270.         _r1ToR1SeriesGenerator = r1ToR1SeriesGenerator;
  271.     }

  272.     @Override public double derivative (
  273.         final double z,
  274.         final int order)
  275.         throws java.lang.Exception
  276.     {
  277.         if (!org.drip.numerical.common.NumberUtil.IsValid (z) ||
  278.             1 > order)
  279.         {
  280.             throw new java.lang.Exception ("ErrorFunction::derivative => Invalid Inputs");
  281.         }

  282.         return 1 == order ? 2. * java.lang.Math.exp (-1. * z * z) / java.lang.Math.sqrt (java.lang.Math.PI) :
  283.             super.derivative (
  284.                 z,
  285.                 order
  286.             );
  287.     }

  288.     @Override public org.drip.function.definition.R1ToR1 antiDerivative()
  289.     {
  290.         return new org.drip.function.definition.R1ToR1 (null)
  291.         {
  292.             @Override public double evaluate (
  293.                 final double x)
  294.                 throws java.lang.Exception
  295.             {
  296.                 if (!org.drip.numerical.common.NumberUtil.IsValid (x))
  297.                 {
  298.                     throw new java.lang.Exception
  299.                         ("ErrorFunction::antiDerivative::evaluate => Invalid Inputs");
  300.                 }

  301.                 return x * this.evaluate (x) + java.lang.Math.exp (-1. * x * x) / java.lang.Math.sqrt
  302.                     (java.lang.Math.PI);
  303.             }
  304.         };
  305.     }

  306.     @Override public org.drip.numerical.estimation.R1Estimate seriesEstimateNative (
  307.         final double x)
  308.     {
  309.         return null == _r1ToR1SeriesGenerator ? seriesEstimate (
  310.             x,
  311.             null,
  312.             null
  313.         ) : seriesEstimate (
  314.             x,
  315.             _r1ToR1SeriesGenerator.termWeightMap(),
  316.             _r1ToR1SeriesGenerator
  317.         );
  318.     }

  319.     @Override public org.drip.function.definition.R1ToR1 integrand()
  320.     {
  321.         return new org.drip.function.definition.R1ToR1 (null)
  322.         {
  323.             @Override public double evaluate (
  324.                 final double z)
  325.             {
  326.                 return 2. * java.lang.Math.exp (-1. * z * z) / java.lang.Math.sqrt (java.lang.Math.PI);
  327.             }
  328.         };
  329.     }

  330.     /**
  331.      * Compute the Q Value for the given X
  332.      *
  333.      * @param x X
  334.      *
  335.      * @return The Q Value
  336.      *
  337.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  338.      */

  339.     public double q (
  340.         final double x)
  341.         throws java.lang.Exception
  342.     {
  343.         return 0.5 * (1. - evaluate (x / java.lang.Math.sqrt (2.)));
  344.     }

  345.     /**
  346.      * Compute the CDF Value for the given X
  347.      *
  348.      * @param x X
  349.      *
  350.      * @return The CDF Value
  351.      *
  352.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  353.      */

  354.     public double cdf (
  355.         final double x)
  356.         throws java.lang.Exception
  357.     {
  358.         return 0.5 * (1. + evaluate (x / java.lang.Math.sqrt (2.)));
  359.     }

  360.     /**
  361.      * Compute the erfc Value for the given X
  362.      *
  363.      * @param x X
  364.      *
  365.      * @return The erfc Value
  366.      *
  367.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  368.      */

  369.     public double erfc (
  370.         final double x)
  371.         throws java.lang.Exception
  372.     {
  373.         return 1. - evaluate (x);
  374.     }

  375.     /**
  376.      * Compute the E<sub>2</sub> erf Gaussian Density Integral over -inf to +inf
  377.      *
  378.      * @param a The Scale Parameter
  379.      * @param b The Displacement Parameter
  380.      * @param r1UnivariateNormal The R<sup>1</sup> Gaussian Distribution Parameters
  381.      *
  382.      * @return The E<sub>2</sub> erf Gaussian Density Integral over -inf to +inf
  383.      *
  384.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  385.      */

  386.     public double gaussianDensityIntegral (
  387.         final double a,
  388.         final double b,
  389.         final org.drip.measure.gaussian.R1UnivariateNormal r1UnivariateNormal)
  390.         throws java.lang.Exception
  391.     {
  392.         if (!org.drip.numerical.common.NumberUtil.IsValid (a) ||
  393.             !org.drip.numerical.common.NumberUtil.IsValid (b) ||
  394.             null == r1UnivariateNormal)
  395.         {
  396.             throw new java.lang.Exception ("ErrorFunction::gaussianDensityIntegral => Invalid Inputs");
  397.         }

  398.         double sigma = r1UnivariateNormal.variance();

  399.         return evaluate (
  400.             (a * r1UnivariateNormal.mean() + b) / java.lang.Math.sqrt (1. + 2 * sigma * sigma * a * a)
  401.         );
  402.     }
  403. }