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

  104. public class ERFCCraig1991
  105. {

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

  111.         int nodeCount10 = 10;
  112.         int nodeCount50 = 50;
  113.         int nodeCount100 = 100;

  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|                - Newton Cotes Estimate (10 Nodes)                  ||");

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

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

  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 erfcEstimate10 = NewtonCotesQuadratureGenerator.Zero_PlusOne (
  144.                 0.,
  145.                 0.5 * Math.PI,
  146.                 nodeCount10
  147.             ).integrate (erfcIntegrand);

  148.             double erfcEstimate50 = NewtonCotesQuadratureGenerator.Zero_PlusOne (
  149.                 0.,
  150.                 0.5 * Math.PI,
  151.                 nodeCount50
  152.             ).integrate (erfcIntegrand);

  153.             double erfcEstimate100 = NewtonCotesQuadratureGenerator.Zero_PlusOne (
  154.                 0.,
  155.                 0.5 * Math.PI,
  156.                 nodeCount100
  157.             ).integrate (erfcIntegrand);

  158.             System.out.println (
  159.                 "\t| " + FormatUtil.FormatDouble (x, 1, 2, 1.) + " => " +
  160.                 FormatUtil.FormatDouble (erfcTable, 1, 9, 1.) + " | " +
  161.                 FormatUtil.FormatDouble (erfcEstimate10, 1, 9, 1.) + " | " +
  162.                 FormatUtil.FormatDouble (erfcEstimate50, 1, 9, 1.) + " | " +
  163.                 FormatUtil.FormatDouble (erfcEstimate100, 1, 9, 1.) + " ||"
  164.             );
  165.         }

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

  167.         EnvManager.TerminateEnv();
  168.     }
  169. }