ERFIntegrand.java

  1. package org.drip.sample.newtoncotes;

  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.NewtonCotesQuadratureGenerator;
  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>ERFIntegrand</i> computes the R<sup>1</sup> Numerical Estimate of the erf Integrand using Newton-Cotes
  71.  * Grids. The References are:
  72.  *
  73.  * <br><br>
  74.  *  <ul>
  75.  *      <li>
  76.  *          Briol, F. X., C. J. Oates, M. Girolami, and M. A. Osborne (2015): <i>Frank-Wolfe Bayesian
  77.  *              Quadrature: Probabilistic Integration with Theoretical Guarantees</i> <b>arXiv</b>
  78.  *      </li>
  79.  *      <li>
  80.  *          Forsythe, G. E., M. A. Malcolm, and C. B. Moler (1977): <i>Computer Methods for Mathematical
  81.  *              Computation</i> <b>Prentice Hall</b> Englewood Cliffs NJ
  82.  *      </li>
  83.  *      <li>
  84.  *          Leader, J. J. (2004): <i>Numerical Analysis and Scientific Computation</i> <b>Addison Wesley</b>
  85.  *      </li>
  86.  *      <li>
  87.  *          Stoer, J., and R. Bulirsch (1980): <i>Introduction to Numerical Analysis</i>
  88.  *              <b>Springer-Verlag</b> New York
  89.  *      </li>
  90.  *      <li>
  91.  *          Wikipedia (2019): Numerical Integration https://en.wikipedia.org/wiki/Numerical_integration
  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/sample/README.md">Sample</a></li>
  100.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/newtoncotes/README.md">R<sup>1</sup> Newton-Cotes Quadrature Schemes</a></li>
  101.  *  </ul>
  102.  *
  103.  * @author Lakshmi Krishnamurthy
  104.  */

  105. public class ERFIntegrand
  106. {

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

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

  116.         int nodeCount10 = 10;
  117.         int nodeCount50 = 50;
  118.         int nodeCount100 = 100;

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

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

  121.         System.out.println ("\t|                     Newton Cotes erf Estimate                      ||");

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

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

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

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

  126.         System.out.println ("\t|                - Newton Cotes Estimate (10 Nodes)                  ||");

  127.         System.out.println ("\t|                - Newton Cotes Estimate (50 Nodes)                  ||");

  128.         System.out.println ("\t|                - Newton Cotes Estimate (100 Nodes)                 ||");

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

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

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

  134.             double erfEstimate10 = NewtonCotesQuadratureGenerator.MinusOne_PlusOne (
  135.                 0.,
  136.                 x,
  137.                 nodeCount10
  138.             ).integrate (erfIntegrand);

  139.             double erfEstimate50 = NewtonCotesQuadratureGenerator.MinusOne_PlusOne (
  140.                 0.,
  141.                 x,
  142.                 nodeCount50
  143.             ).integrate (erfIntegrand);

  144.             double erfEstimate100 = NewtonCotesQuadratureGenerator.MinusOne_PlusOne (
  145.                 0.,
  146.                 x,
  147.                 nodeCount100
  148.             ).integrate (erfIntegrand);

  149.             System.out.println (
  150.                 "\t| " + FormatUtil.FormatDouble (x, 1, 2, 1.) + " => " +
  151.                 FormatUtil.FormatDouble (erfTable, 1, 9, 1.) + " | " +
  152.                 FormatUtil.FormatDouble (erfEstimate10, 1, 9, 1.) + " | " +
  153.                 FormatUtil.FormatDouble (erfEstimate50, 1, 9, 1.) + " | " +
  154.                 FormatUtil.FormatDouble (erfEstimate100, 1, 9, 1.) + " ||"
  155.             );
  156.         }

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

  158.         EnvManager.TerminateEnv();
  159.     }
  160. }