ERFCCraig1991G7K15.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.numerical.integration.QuadratureEstimate;
  8. import org.drip.service.env.EnvManager;

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

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

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

  107. public class ERFCCraig1991G7K15
  108. {

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

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

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

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

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

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

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

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

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

  122.         System.out.println ("\t|                - Estimation Error                   ||");

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

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

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

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

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

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

  142.             QuadratureEstimate quadratureEstimate = GaussKronrodQuadratureGenerator.G7K15 (
  143.                 0.,
  144.                 0.5 * Math.PI
  145.             ).estimate (erfcIntegrand);

  146.             System.out.println (
  147.                 "\t| " + FormatUtil.FormatDouble (x, 1, 2, 1.) + " => " +
  148.                 FormatUtil.FormatDouble (erfcTable, 1, 9, 1.) + " | " +
  149.                 FormatUtil.FormatDouble (quadratureEstimate.baseline(), 1, 9, 1.) + " | " +
  150.                 FormatUtil.FormatDouble (quadratureEstimate.error(), 1, 9, 1.) + " ||"
  151.             );
  152.         }

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

  154.         EnvManager.TerminateEnv();
  155.     }
  156. }