ErrorFunctionComplementAnalytical.java

  1. package org.drip.function.e2erfc;

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

  113. public class ErrorFunctionComplementAnalytical
  114. {

  115.     private static final double ContinuedFractionRecursor (
  116.         final double z,
  117.         final int termIndex,
  118.         final int termCount)
  119.     {
  120.         if (termIndex == termCount)
  121.         {
  122.             return 0.;
  123.         }

  124.         return ((1 == termIndex % 2) ? z * z : 1.) + 0.5 * termIndex / (
  125.             1. + ContinuedFractionRecursor (
  126.                 z,
  127.                 termIndex + 1,
  128.                 termCount
  129.             )
  130.         );
  131.     }

  132.     /**
  133.      * Construct Karagiannidis-Lioumpas (2007) Version of the Analytical Error Function Complement
  134.      *
  135.      * @param A A
  136.      * @param B B
  137.      *
  138.      * @return Karagiannidis-Lioumpas (2007) Version of the Analytical Error Function Complement
  139.      */

  140.     public static final org.drip.function.e2erfc.ErrorFunctionComplement KaragiannidisLioumpas2007 (
  141.         final double A,
  142.         final double B)
  143.     {
  144.         try
  145.         {
  146.             return !org.drip.numerical.common.NumberUtil.IsValid (A) ||
  147.                 !org.drip.numerical.common.NumberUtil.IsValid (B) ? null :
  148.                     new org.drip.function.e2erfc.ErrorFunctionComplement (
  149.                         null,
  150.                         null
  151.                     )
  152.                 {
  153.                     @Override public double evaluate (
  154.                         final double z)
  155.                         throws java.lang.Exception
  156.                     {
  157.                         if (!org.drip.numerical.common.NumberUtil.IsValid (z))
  158.                         {
  159.                             throw new java.lang.Exception
  160.                                 ("ErrorFunctionComplementAnalytical::KaragiannidisLioumpas2007::evaluate => Invalid Inputs");
  161.                         }

  162.                         if (0. == z)
  163.                         {
  164.                             return 1.;
  165.                         }

  166.                         if (z < 0)
  167.                         {
  168.                             return 2. - evaluate (-1. * z);
  169.                         }

  170.                         return (1. - java.lang.Math.exp (-1. * A * z)) * java.lang.Math.exp (-1. * z * z) /
  171.                             (B * z * java.lang.Math.sqrt (java.lang.Math.PI));
  172.                     }
  173.                 };
  174.         }
  175.         catch (java.lang.Exception e)
  176.         {
  177.             e.printStackTrace();
  178.         }

  179.         return null;
  180.     }

  181.     /**
  182.      * Construct Karagiannidis-Lioumpas (2007) Version of the Analytical Error Function Complement
  183.      *
  184.      * @return Karagiannidis-Lioumpas (2007) Version of the Analytical Error Function Complement
  185.      */

  186.     public static final org.drip.function.e2erfc.ErrorFunctionComplement KaragiannidisLioumpas2007()
  187.     {
  188.         return KaragiannidisLioumpas2007 (
  189.             1.980,
  190.             1.135
  191.         );
  192.     }

  193.     /**
  194.      * Construct the Chiani-Dardari-Simon (2012a) Version of the Analytical Error Function Complement
  195.      *
  196.      * @return The Chiani-Dardari-Simon (2012a) Version of the Analytical Error Function Complement
  197.      */

  198.     public static final org.drip.function.e2erfc.ErrorFunctionComplement ChianiDardariSimon2012a()
  199.     {
  200.         try
  201.         {
  202.             return new org.drip.function.e2erfc.ErrorFunctionComplement (
  203.                 null,
  204.                 null
  205.             )
  206.             {
  207.                 @Override public double evaluate (
  208.                     final double z)
  209.                     throws java.lang.Exception
  210.                 {
  211.                     if (!org.drip.numerical.common.NumberUtil.IsValid (z))
  212.                     {
  213.                         throw new java.lang.Exception
  214.                             ("ErrorFunctionComplementAnalytical::ChianiDardariSimon2012a::evaluate => Invalid Inputs");
  215.                     }

  216.                     if (0. == z)
  217.                     {
  218.                         return 1.;
  219.                     }

  220.                     if (z < 0)
  221.                     {
  222.                         return 2. - evaluate (-1. * z);
  223.                     }

  224.                     return 0.5 * java.lang.Math.exp (-2. * z * z) + 0.5 * java.lang.Math.exp (-1. * z * z);
  225.                 }

  226.                 @Override public org.drip.numerical.estimation.R1Estimate boundedEstimate (
  227.                     final double z)
  228.                 {
  229.                     try
  230.                     {
  231.                         double baseline = evaluate (z);

  232.                         return new org.drip.numerical.estimation.R1Estimate (
  233.                             baseline,
  234.                             baseline,
  235.                             java.lang.Math.exp (-1. * z * z)
  236.                         );
  237.                     }
  238.                     catch (java.lang.Exception e)
  239.                     {
  240.                         e.printStackTrace();
  241.                     }

  242.                     return null;
  243.                 }
  244.             };
  245.         }
  246.         catch (java.lang.Exception e)
  247.         {
  248.             e.printStackTrace();
  249.         }

  250.         return null;
  251.     }

  252.     /**
  253.      * Construct the Chiani-Dardari-Simon (2012b) Version of the Analytical Error Function Complement
  254.      *
  255.      * @return The Chiani-Dardari-Simon (2012b) Version of the Analytical Error Function Complement
  256.      */

  257.     public static final org.drip.function.e2erfc.ErrorFunctionComplement ChianiDardariSimon2012b()
  258.     {
  259.         try
  260.         {
  261.             return new org.drip.function.e2erfc.ErrorFunctionComplement (
  262.                 null,
  263.                 null
  264.             )
  265.             {
  266.                 @Override public double evaluate (
  267.                     final double z)
  268.                     throws java.lang.Exception
  269.                 {
  270.                     if (!org.drip.numerical.common.NumberUtil.IsValid (z))
  271.                     {
  272.                         throw new java.lang.Exception
  273.                             ("ErrorFunctionComplementAnalytical::ChianiDardariSimon2012b::evaluate => Invalid Inputs");
  274.                     }

  275.                     if (0. == z)
  276.                     {
  277.                         return 1.;
  278.                     }

  279.                     if (z < 0)
  280.                     {
  281.                         return 2. - evaluate (-1. * z);
  282.                     }

  283.                     return java.lang.Math.exp (-1. * z * z) / 6. + 0.5 * java.lang.Math.exp (-4. * z * z / 3.);
  284.                 }
  285.             };
  286.         }
  287.         catch (java.lang.Exception e)
  288.         {
  289.             e.printStackTrace();
  290.         }

  291.         return null;
  292.     }

  293.     /**
  294.      * Construct the Chang-Cosman-Milstein (2011) Version of the Analytical Error Function Complement
  295.      *
  296.      * @param beta Beta
  297.      *
  298.      * @return The Chang-Cosman-Milstein (2011) Version of the Analytical Error Function Complement
  299.      */

  300.     public static final org.drip.function.e2erfc.ErrorFunctionComplement ChangCosmanMilstein2011 (
  301.         final double beta)
  302.     {
  303.         try
  304.         {
  305.             return !org.drip.numerical.common.NumberUtil.IsValid (beta) || 1. >= beta ? null :
  306.                 new org.drip.function.e2erfc.ErrorFunctionComplement (
  307.                     null,
  308.                     null
  309.                 )
  310.             {
  311.                 @Override public double evaluate (
  312.                     final double z)
  313.                     throws java.lang.Exception
  314.                 {
  315.                     if (!org.drip.numerical.common.NumberUtil.IsValid (z))
  316.                     {
  317.                         throw new java.lang.Exception
  318.                             ("ErrorFunctionComplementAnalytical::ChangCosmanMilstein2011::evaluate => Invalid Inputs");
  319.                     }

  320.                     if (0. == z)
  321.                     {
  322.                         return 1.;
  323.                     }

  324.                     if (z < 0)
  325.                     {
  326.                         return 2. - evaluate (-1. * z);
  327.                     }

  328.                     return java.lang.Math.sqrt (2. * java.lang.Math.E * (beta - 1.) / java.lang.Math.PI) *
  329.                         java.lang.Math.exp (-1. * beta * z * z) / beta;
  330.                 }
  331.             };
  332.         }
  333.         catch (java.lang.Exception e)
  334.         {
  335.             e.printStackTrace();
  336.         }

  337.         return null;
  338.     }

  339.     /**
  340.      * Construct the Continued Fraction Expansion Version of the Analytical Error Function Complement
  341.      *
  342.      * @param termCount Term Count
  343.      *
  344.      * @return The Continued Fraction Expansion Version of the Analytical Error Function Complement
  345.      */

  346.     public static final org.drip.function.e2erfc.ErrorFunctionComplement ContinuedFractionExpansion (
  347.         final int termCount)
  348.     {
  349.         try
  350.         {
  351.             return 0 >= termCount ? null : new org.drip.function.e2erfc.ErrorFunctionComplement (
  352.                 null,
  353.                 null
  354.             )
  355.             {
  356.                 @Override public double evaluate (
  357.                     final double z)
  358.                     throws java.lang.Exception
  359.                 {
  360.                     if (!org.drip.numerical.common.NumberUtil.IsValid (z))
  361.                     {
  362.                         throw new java.lang.Exception
  363.                             ("ErrorFunctionComplementAnalytical::ContinuedFractionExpansion::evaluate => Invalid Inputs");
  364.                     }

  365.                     if (0. == z)
  366.                     {
  367.                         return 1.;
  368.                     }

  369.                     if (z < 0)
  370.                     {
  371.                         return 2. - evaluate (-1. * z);
  372.                     }

  373.                     return z * java.lang.Math.exp (-1. * z * z) / java.lang.Math.sqrt (java.lang.Math.PI) /
  374.                         ContinuedFractionRecursor (
  375.                             z,
  376.                             1,
  377.                             termCount
  378.                         );
  379.                 }
  380.             };
  381.         }
  382.         catch (java.lang.Exception e)
  383.         {
  384.             e.printStackTrace();
  385.         }

  386.         return null;
  387.     }

  388.     /**
  389.      * Construct the Craig 1991 Version of the ErrorFunctionComplement Quadrature
  390.      *
  391.      * @return The Craig 1991 Version of the ErrorFunctionComplement Quadrature
  392.      */

  393.     public static final org.drip.function.e2erfc.ErrorFunctionComplement Craig1991()
  394.     {
  395.         try
  396.         {
  397.             return new org.drip.function.e2erfc.ErrorFunctionComplement (
  398.                 null,
  399.                 null
  400.             )
  401.             {
  402.                 @Override public double evaluate (
  403.                     final double z)
  404.                     throws java.lang.Exception
  405.                 {
  406.                     if (!org.drip.numerical.common.NumberUtil.IsValid (z))
  407.                     {
  408.                         throw new java.lang.Exception
  409.                             ("ErrorFunctionComplementAnalytical::Craig1991::evaluate => Invalid Inputs");
  410.                     }

  411.                     if (0. == z)
  412.                     {
  413.                         return 1.;
  414.                     }

  415.                     if (z < 0)
  416.                     {
  417.                         return 2. - evaluate (-1. * z);
  418.                     }

  419.                     return org.drip.numerical.integration.NewtonCotesQuadratureGenerator.Zero_PlusOne (
  420.                         0.,
  421.                         z,
  422.                         100
  423.                     ).integrate (
  424.                         new org.drip.function.definition.R1ToR1 (null)
  425.                         {
  426.                             @Override public double evaluate (
  427.                                 final double theta)
  428.                                 throws java.lang.Exception
  429.                             {
  430.                                 if (0. == theta)
  431.                                 {
  432.                                     return 0.;
  433.                                 }

  434.                                 double sinTheta = java.lang.Math.sin (theta);

  435.                                 return 2. * java.lang.Math.exp (-1. * z * z / (sinTheta * sinTheta)) /
  436.                                     java.lang.Math.PI;
  437.                             }
  438.                         }
  439.                     );
  440.                 }
  441.             };
  442.         }
  443.         catch (java.lang.Exception e)
  444.         {
  445.             e.printStackTrace();
  446.         }

  447.         return null;
  448.     }
  449. }