ERFCCraig1991K15.java

  1. package org.drip.sample.gausskronrod;

  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.GaussKronrodQuadratureGenerator;
  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>ERFCCraig1991K15</i> computes the R<sup>1</sup> Numerical Estimate of the erfc Integrand using the K15
  70.  * Gaussian Integration Quadrature Scheme. The References are:
  71.  *
  72.  * <br><br>
  73.  *  <ul>
  74.  *      <li>
  75.  *          Holoborodko, P. (2011): Gauss-Kronrod Quadrature Nodes and Weights
  76.  *              https://www.advanpix.com/2011/11/07/gauss-kronrod-quadrature-nodes-weights/
  77.  *      </li>
  78.  *      <li>
  79.  *          Kahaner, D., C. Moler, and S. Nash (1989): <i>Numerical Methods and Software</i> <b>Prentice
  80.  *              Hall</b>
  81.  *      </li>
  82.  *      <li>
  83.  *          Laurie, D. (1997): Calculation of Gauss-Kronrod Quadrature Rules <i>Mathematics of
  84.  *              Computation</i> <b>66 (219)</b> 1133-1145
  85.  *      </li>
  86.  *      <li>
  87.  *          Piessens, R., E. de Doncker-Kapenga, C. W. Uberhuber, and D. K. Kahaner (1983): <i>QUADPACK – A
  88.  *              Subroutine Package for Automatic Integration</i> <b>Springer-Verlag</b>
  89.  *      </li>
  90.  *      <li>
  91.  *          Wikipedia (2019): Gauss-Kronrod Quadrature Formula
  92.  *              https://en.wikipedia.org/wiki/Gauss%E2%80%93Kronrod_quadrature_formula
  93.  *      </li>
  94.  *  </ul>
  95.  *
  96.  *  <br><br>
  97.  *  <ul>
  98.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/NumericalCore.md">Numerical Core Module</a></li>
  99.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/NumericalOptimizerLibrary.md">Numerical Optimizer</a></li>
  100.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/README.md">Numerical Analysis</a></li>
  101.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/gausskronrod/README.md">R<sup>1</sup> Gauss-Kronrod Quadrature Schemes</a></li>
  102.  *  </ul>
  103.  *
  104.  * @author Lakshmi Krishnamurthy
  105.  */

  106. public class ERFCCraig1991K15
  107. {

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

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

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

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

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

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

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

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

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

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

  122.         for (Map.Entry<Double, BuiltInEntry> builtInTableEntry : builtInTable.entrySet())
  123.         {
  124.             final double x = builtInTableEntry.getKey();

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

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

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

  137.                     return 2. * java.lang.Math.exp (-1. * x * x / (sinTheta * sinTheta)) / Math.PI;
  138.                 }
  139.             };

  140.             double erfcEstimate = GaussKronrodQuadratureGenerator.K15 (
  141.                 0.,
  142.                 0.5 * Math.PI
  143.             ).integrate (erfcIntegrand);

  144.             System.out.println (
  145.                 "\t| " + FormatUtil.FormatDouble (x, 1, 2, 1.) + " => " +
  146.                 FormatUtil.FormatDouble (erfcTable, 1, 9, 1.) + " | " +
  147.                 FormatUtil.FormatDouble (erfcEstimate, 1, 9, 1.) + " ||"
  148.             );
  149.         }

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

  151.         EnvManager.TerminateEnv();
  152.     }
  153. }