ERFCCraig1991GaussLegendre.java

  1. package org.drip.sample.gaussquadrature;

  2. import java.util.Map;

  3. import org.drip.function.definition.R1ToR1;
  4. import org.drip.function.e2erf.BuiltInEntry;
  5. import org.drip.numerical.common.FormatUtil;
  6. import org.drip.numerical.integration.GaussLegendreQuadratureGenerator;
  7. import org.drip.service.env.EnvManager;

  8. /*
  9.  * -*- mode: java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  10.  */

  11. /*!
  12.  * Copyright (C) 2019 Lakshmi Krishnamurthy
  13.  *
  14.  *  This file is part of DROP, an open-source library targeting risk, transaction costs, exposure, margin
  15.  *      calculations, and portfolio construction within and across fixed income, credit, commodity, equity,
  16.  *      FX, and structured products.
  17.  *  
  18.  *      https://lakshmidrip.github.io/DROP/
  19.  *  
  20.  *  DROP is composed of three main modules:
  21.  *  
  22.  *  - DROP Analytics Core - https://lakshmidrip.github.io/DROP-Analytics-Core/
  23.  *  - DROP Portfolio Core - https://lakshmidrip.github.io/DROP-Portfolio-Core/
  24.  *  - DROP Numerical Core - https://lakshmidrip.github.io/DROP-Numerical-Core/
  25.  *
  26.  *  DROP Analytics Core implements libraries for the following:
  27.  *  - Fixed Income Analytics
  28.  *  - Asset Backed Analytics
  29.  *  - XVA Analytics
  30.  *  - Exposure and Margin Analytics
  31.  *
  32.  *  DROP Portfolio Core implements libraries for the following:
  33.  *  - Asset Allocation Analytics
  34.  *  - Transaction Cost Analytics
  35.  *
  36.  *  DROP Numerical Core implements libraries for the following:
  37.  *  - Statistical Learning Library
  38.  *  - Numerical Optimizer Library
  39.  *  - Machine Learning Library
  40.  *  - Spline Builder Library
  41.  *
  42.  *  Documentation for DROP is Spread Over:
  43.  *
  44.  *  - Main                     => https://lakshmidrip.github.io/DROP/
  45.  *  - Wiki                     => https://github.com/lakshmiDRIP/DROP/wiki
  46.  *  - GitHub                   => https://github.com/lakshmiDRIP/DROP
  47.  *  - Javadoc                  => https://lakshmidrip.github.io/DROP/Javadoc/index.html
  48.  *  - Technical Specifications => https://github.com/lakshmiDRIP/DROP/tree/master/Docs/Internal
  49.  *  - Release Versions         => https://lakshmidrip.github.io/DROP/version.html
  50.  *  - Community Credits        => https://lakshmidrip.github.io/DROP/credits.html
  51.  *  - Issues Catalog           => https://github.com/lakshmiDRIP/DROP/issues
  52.  *  - JUnit                    => https://lakshmidrip.github.io/DROP/junit/index.html
  53.  *  - Jacoco                   => https://lakshmidrip.github.io/DROP/jacoco/index.html
  54.  *
  55.  *  Licensed under the Apache License, Version 2.0 (the "License");
  56.  *      you may not use this file except in compliance with the License.
  57.  *  
  58.  *  You may obtain a copy of the License at
  59.  *      http://www.apache.org/licenses/LICENSE-2.0
  60.  *  
  61.  *  Unless required by applicable law or agreed to in writing, software
  62.  *      distributed under the License is distributed on an "AS IS" BASIS,
  63.  *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  64.  *  
  65.  *  See the License for the specific language governing permissions and
  66.  *      limitations under the License.
  67.  */

  68. /**
  69.  * <i>ERFCCraig1991GaussLegendre</i> computes the R<sup>1</sup> Numerical Estimate of the erfc Integrand
  70.  * using the Gauss-Legendre Integration Quadrature Scheme. The References are:
  71.  *
  72.  * <br><br>
  73.  *  <ul>
  74.  *      <li>
  75.  *          Abramowitz, M., and I. A. Stegun (2007): <i>Handbook of Mathematics Functions</i> <b>Dover Book
  76.  *              on Mathematics</b>
  77.  *      </li>
  78.  *      <li>
  79.  *          Gil, A., J. Segura, and N. M. Temme (2007): <i>Numerical Methods for Special Functions</i>
  80.  *              <b>Society for Industrial and Applied Mathematics</b> Philadelphia
  81.  *      </li>
  82.  *      <li>
  83.  *          Press, W. H., S. A. Teukolsky, W. T. Vetterling, and B. P. Flannery (2007): <i>Numerical Recipes:
  84.  *              The Art of Scientific Computing 3rd Edition</i> <b>Cambridge University Press</b> New York
  85.  *      </li>
  86.  *      <li>
  87.  *          Stoer, J., and R. Bulirsch (2002): <i>Introduction to Numerical Analysis 3rd Edition</i>
  88.  *              <b>Springer</b>
  89.  *      </li>
  90.  *      <li>
  91.  *          Wikipedia (2019): Gaussian Quadrature https://en.wikipedia.org/wiki/Gaussian_quadrature
  92.  *      </li>
  93.  *  </ul>
  94.  *
  95.  *  <br><br>
  96.  *  <ul>
  97.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/NumericalCore.md">Numerical Core Module</a></li>
  98.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/NumericalOptimizerLibrary.md">Numerical Optimizer</a></li>
  99.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/numerical/README.md">Numerical Analysis</a></li>
  100.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/numerical/gaussquadrature/README.md">R<sup>1</sup> Gauss-Legendre Gauss-Lobatto Quadratures</a></li>
  101.  *  </ul>
  102.  *
  103.  * @author Lakshmi Krishnamurthy
  104.  */

  105. public class ERFCCraig1991GaussLegendre
  106. {

  107.     public static final void main (
  108.         final String[] argumentArray)
  109.         throws Exception
  110.     {
  111.         EnvManager.InitEnv ("");

  112.         Map<Double, BuiltInEntry> builtInTable = BuiltInEntry.Table();

  113.         System.out.println ("\t|--------------------------------------------------------------------------------------------------||");

  114.         System.out.println ("\t|                                     Craig 1991 erfc Estimate                                     ||");

  115.         System.out.println ("\t|--------------------------------------------------------------------------------------------------||");

  116.         System.out.println ("\t|        L -> R:                                                                                   ||");

  117.         System.out.println ("\t|                - x                                                                               ||");

  118.         System.out.println ("\t|                - Built-in Estimate                                                               ||");

  119.         System.out.println ("\t|                - 5P Estimate                                                                     ||");

  120.         System.out.println ("\t|                - 4P Estimate                                                                     ||");

  121.         System.out.println ("\t|                - 3P Estimate                                                                     ||");

  122.         System.out.println ("\t|                - 2P Estimate                                                                     ||");

  123.         System.out.println ("\t|                - 1P Estimate                                                                     ||");

  124.         System.out.println ("\t|--------------------------------------------------------------------------------------------------||");

  125.         for (Map.Entry<Double, BuiltInEntry> builtInTableEntry : builtInTable.entrySet())
  126.         {
  127.             final double x = builtInTableEntry.getKey();

  128.             double erfcTable = builtInTableEntry.getValue().erfc();

  129.             R1ToR1 erfcIntegrand = new R1ToR1 (null)
  130.             {
  131.                 @Override public double evaluate (
  132.                     final double theta)
  133.                     throws java.lang.Exception
  134.                 {
  135.                     if (0. == theta)
  136.                     {
  137.                         return 0.;
  138.                     }

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

  140.                     return 2. * java.lang.Math.exp (-1. * x * x / (sinTheta * sinTheta)) / Math.PI;
  141.                 }
  142.             };

  143.             double erfcEstimate5P = GaussLegendreQuadratureGenerator.FivePoint (
  144.                 0.,
  145.                 0.5 * Math.PI
  146.             ).integrate (erfcIntegrand);

  147.             double erfcEstimate4P = GaussLegendreQuadratureGenerator.FourPoint (
  148.                 0.,
  149.                 0.5 * Math.PI
  150.             ).integrate (erfcIntegrand);

  151.             double erfcEstimate3P = GaussLegendreQuadratureGenerator.ThreePoint (
  152.                 0.,
  153.                 0.5 * Math.PI
  154.             ).integrate (erfcIntegrand);

  155.             double erfcEstimate2P = GaussLegendreQuadratureGenerator.TwoPoint (
  156.                 0.,
  157.                 0.5 * Math.PI
  158.             ).integrate (erfcIntegrand);

  159.             double erfcEstimate1P = GaussLegendreQuadratureGenerator.OnePoint (
  160.                 0.,
  161.                 0.5 * Math.PI
  162.             ).integrate (erfcIntegrand);

  163.             System.out.println (
  164.                 "\t| " + FormatUtil.FormatDouble (x, 1, 2, 1.) + " => " +
  165.                 FormatUtil.FormatDouble (erfcTable, 1, 9, 1.) + " | " +
  166.                 FormatUtil.FormatDouble (erfcEstimate5P, 1, 9, 1.) + " | " +
  167.                 FormatUtil.FormatDouble (erfcEstimate4P, 1, 9, 1.) + " | " +
  168.                 FormatUtil.FormatDouble (erfcEstimate3P, 1, 9, 1.) + " | " +
  169.                 FormatUtil.FormatDouble (erfcEstimate2P, 1, 9, 1.) + " | " +
  170.                 FormatUtil.FormatDouble (erfcEstimate1P, 1, 9, 1.) + " ||"
  171.             );
  172.         }

  173.         System.out.println ("\t|--------------------------------------------------------------------------------------------------||");

  174.         EnvManager.TerminateEnv();
  175.     }
  176. }