ERFIntegrandGaussLegendre.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.function.e2erf.ErrorFunction;
  6. import org.drip.numerical.common.FormatUtil;
  7. import org.drip.numerical.integration.GaussLegendreQuadratureGenerator;
  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>ERFIntegrandGaussLegendre</i> computes the R<sup>1</sup> Numerical Estimate of the erf Integrand using
  71.  * the Gauss-Legendre Integration Quadrature Scheme. The References are:
  72.  *
  73.  * <br><br>
  74.  *  <ul>
  75.  *      <li>
  76.  *          Abramowitz, M., and I. A. Stegun (2007): <i>Handbook of Mathematics Functions</i> <b>Dover Book
  77.  *              on Mathematics</b>
  78.  *      </li>
  79.  *      <li>
  80.  *          Gil, A., J. Segura, and N. M. Temme (2007): <i>Numerical Methods for Special Functions</i>
  81.  *              <b>Society for Industrial and Applied Mathematics</b> Philadelphia
  82.  *      </li>
  83.  *      <li>
  84.  *          Press, W. H., S. A. Teukolsky, W. T. Vetterling, and B. P. Flannery (2007): <i>Numerical Recipes:
  85.  *              The Art of Scientific Computing 3rd Edition</i> <b>Cambridge University Press</b> New York
  86.  *      </li>
  87.  *      <li>
  88.  *          Stoer, J., and R. Bulirsch (2002): <i>Introduction to Numerical Analysis 3rd Edition</i>
  89.  *              <b>Springer</b>
  90.  *      </li>
  91.  *      <li>
  92.  *          Wikipedia (2019): Gaussian Quadrature https://en.wikipedia.org/wiki/Gaussian_quadrature
  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/numerical/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/numerical/gaussquadrature/README.md">R<sup>1</sup> Gauss-Legendre Gauss-Lobatto Quadratures</a></li>
  102.  *  </ul>
  103.  *
  104.  * @author Lakshmi Krishnamurthy
  105.  */

  106. public class ERFIntegrandGaussLegendre
  107. {

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

  113.         R1ToR1 erfIntegrand = new ErrorFunction (
  114.             null,
  115.             null
  116.         ).integrand();

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

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

  119.         System.out.println ("\t|                                   Gauss Legendre erf Estimate                                    ||");

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

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

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

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

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

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

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

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

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

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

  130.         for (Map.Entry<Double, BuiltInEntry> builtInTableEntry : builtInTable.entrySet())
  131.         {
  132.             final double x = builtInTableEntry.getKey();

  133.             double erfTable = builtInTableEntry.getValue().erf();

  134.             double erfEstimate5P = GaussLegendreQuadratureGenerator.FivePoint (
  135.                 0.,
  136.                 x
  137.             ).integrate (erfIntegrand);

  138.             double erfEstimate4P = GaussLegendreQuadratureGenerator.FourPoint (
  139.                 0.,
  140.                 x
  141.             ).integrate (erfIntegrand);

  142.             double erfEstimate3P = GaussLegendreQuadratureGenerator.ThreePoint (
  143.                 0.,
  144.                 x
  145.             ).integrate (erfIntegrand);

  146.             double erfEstimate2P = GaussLegendreQuadratureGenerator.TwoPoint (
  147.                 0.,
  148.                 x
  149.             ).integrate (erfIntegrand);

  150.             double erfEstimate1P = GaussLegendreQuadratureGenerator.OnePoint (
  151.                 0.,
  152.                 x
  153.             ).integrate (erfIntegrand);

  154.             System.out.println (
  155.                 "\t| " + FormatUtil.FormatDouble (x, 1, 2, 1.) + " => " +
  156.                 FormatUtil.FormatDouble (erfTable, 1, 9, 1.) + " | " +
  157.                 FormatUtil.FormatDouble (erfEstimate5P, 1, 9, 1.) + " | " +
  158.                 FormatUtil.FormatDouble (erfEstimate4P, 1, 9, 1.) + " | " +
  159.                 FormatUtil.FormatDouble (erfEstimate3P, 1, 9, 1.) + " | " +
  160.                 FormatUtil.FormatDouble (erfEstimate2P, 1, 9, 1.) + " | " +
  161.                 FormatUtil.FormatDouble (erfEstimate1P, 1, 9, 1.) + " ||"
  162.             );
  163.         }

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

  165.         EnvManager.TerminateEnv();
  166.     }
  167. }