NewtonCotesQuadratureGenerator.java

  1. package org.drip.numerical.integration;

  2. /*
  3.  * -*- mode: java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  4.  */

  5. /*!
  6.  * Copyright (C) 2020 Lakshmi Krishnamurthy
  7.  * Copyright (C) 2019 Lakshmi Krishnamurthy
  8.  *
  9.  *  This file is part of DROP, an open-source library targeting analytics/risk, transaction cost analytics,
  10.  *      asset liability management analytics, capital, exposure, and margin analytics, valuation adjustment
  11.  *      analytics, and portfolio construction analytics within and across fixed income, credit, commodity,
  12.  *      equity, FX, and structured products. It also includes auxiliary libraries for algorithm support,
  13.  *      numerical analysis, numerical optimization, spline builder, model validation, statistical learning,
  14.  *      and computational support.
  15.  *  
  16.  *      https://lakshmidrip.github.io/DROP/
  17.  *  
  18.  *  DROP is composed of three modules:
  19.  *  
  20.  *  - DROP Product Core - https://lakshmidrip.github.io/DROP-Product-Core/
  21.  *  - DROP Portfolio Core - https://lakshmidrip.github.io/DROP-Portfolio-Core/
  22.  *  - DROP Computational Core - https://lakshmidrip.github.io/DROP-Computational-Core/
  23.  *
  24.  *  DROP Product Core implements libraries for the following:
  25.  *  - Fixed Income Analytics
  26.  *  - Loan Analytics
  27.  *  - Transaction Cost Analytics
  28.  *
  29.  *  DROP Portfolio Core implements libraries for the following:
  30.  *  - Asset Allocation Analytics
  31.  *  - Asset Liability Management Analytics
  32.  *  - Capital Estimation Analytics
  33.  *  - Exposure Analytics
  34.  *  - Margin Analytics
  35.  *  - XVA Analytics
  36.  *
  37.  *  DROP Computational Core implements libraries for the following:
  38.  *  - Algorithm Support
  39.  *  - Computation Support
  40.  *  - Function Analysis
  41.  *  - Model Validation
  42.  *  - Numerical Analysis
  43.  *  - Numerical Optimizer
  44.  *  - Spline Builder
  45.  *  - Statistical Learning
  46.  *
  47.  *  Documentation for DROP is Spread Over:
  48.  *
  49.  *  - Main                     => https://lakshmidrip.github.io/DROP/
  50.  *  - Wiki                     => https://github.com/lakshmiDRIP/DROP/wiki
  51.  *  - GitHub                   => https://github.com/lakshmiDRIP/DROP
  52.  *  - Repo Layout Taxonomy     => https://github.com/lakshmiDRIP/DROP/blob/master/Taxonomy.md
  53.  *  - Javadoc                  => https://lakshmidrip.github.io/DROP/Javadoc/index.html
  54.  *  - Technical Specifications => https://github.com/lakshmiDRIP/DROP/tree/master/Docs/Internal
  55.  *  - Release Versions         => https://lakshmidrip.github.io/DROP/version.html
  56.  *  - Community Credits        => https://lakshmidrip.github.io/DROP/credits.html
  57.  *  - Issues Catalog           => https://github.com/lakshmiDRIP/DROP/issues
  58.  *  - JUnit                    => https://lakshmidrip.github.io/DROP/junit/index.html
  59.  *  - Jacoco                   => https://lakshmidrip.github.io/DROP/jacoco/index.html
  60.  *
  61.  *  Licensed under the Apache License, Version 2.0 (the "License");
  62.  *      you may not use this file except in compliance with the License.
  63.  *  
  64.  *  You may obtain a copy of the License at
  65.  *      http://www.apache.org/licenses/LICENSE-2.0
  66.  *  
  67.  *  Unless required by applicable law or agreed to in writing, software
  68.  *      distributed under the License is distributed on an "AS IS" BASIS,
  69.  *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  70.  *  
  71.  *  See the License for the specific language governing permissions and
  72.  *      limitations under the License.
  73.  */

  74. /**
  75.  * <i>NewtonCotesQuadratureGenerator</i> generates the Array of Newton-Cotes Based Quadrature Abscissa and
  76.  * their corresponding Weights. The References are:
  77.  *
  78.  * <br><br>
  79.  *  <ul>
  80.  *      <li>
  81.  *          Briol, F. X., C. J. Oates, M. Girolami, and M. A. Osborne (2015): <i>Frank-Wolfe Bayesian
  82.  *              Quadrature: Probabilistic Integration with Theoretical Guarantees</i> <b>arXiv</b>
  83.  *      </li>
  84.  *      <li>
  85.  *          Forsythe, G. E., M. A. Malcolm, and C. B. Moler (1977): <i>Computer Methods for Mathematical
  86.  *              Computation</i> <b>Prentice Hall</b> Englewood Cliffs NJ
  87.  *      </li>
  88.  *      <li>
  89.  *          Leader, J. J. (2004): <i>Numerical Analysis and Scientific Computation</i> <b>Addison Wesley</b>
  90.  *      </li>
  91.  *      <li>
  92.  *          Stoer, J., and R. Bulirsch (1980): <i>Introduction to Numerical Analysis</i>
  93.  *              <b>Springer-Verlag</b> New York
  94.  *      </li>
  95.  *      <li>
  96.  *          Wikipedia (2019): Numerical Integration https://en.wikipedia.org/wiki/Numerical_integration
  97.  *      </li>
  98.  *  </ul>
  99.  *
  100.  *  <br><br>
  101.  *  <ul>
  102.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ComputationalCore.md">Computational Core Module</a></li>
  103.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/NumericalAnalysisLibrary.md">Numerical Analysis Library</a></li>
  104.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/numerical/README.md">Numerical Quadrature, Differentiation, Eigenization, Linear Algebra, and Utilities</a></li>
  105.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/numerical/integration/README.md">R<sup>1</sup> R<sup>d</sup> Numerical Integration Schemes</a></li>
  106.  *  </ul>
  107.  *
  108.  * @author Lakshmi Krishnamurthy
  109.  */

  110. public class NewtonCotesQuadratureGenerator
  111. {

  112.     /**
  113.      * Generate the Newton-Cotes of Equally Spaced Quadrature over (0, +1)
  114.      *
  115.      * @param abscissaTransformer The Abscissa Transformer
  116.      * @param intermediatePointCount Number of Intermediate Points
  117.      *
  118.      * @return The Newton-Cotes of Equally Spaced Quadrature over (0, +1)
  119.      */

  120.     public static final org.drip.numerical.integration.QuadratureEstimator Zero_PlusOne (
  121.         final org.drip.numerical.integration.AbscissaTransform abscissaTransformer,
  122.         final int intermediatePointCount)
  123.     {
  124.         if (0 >= intermediatePointCount)
  125.         {
  126.             return null;
  127.         }

  128.         int nodeCount = intermediatePointCount + 2;
  129.         double width = 1. / (intermediatePointCount + 1);
  130.         double[] abscissaArray = new double[nodeCount];
  131.         double[] weightArray = new double[nodeCount];
  132.         weightArray[intermediatePointCount + 1] = 0.5 * width;
  133.         abscissaArray[intermediatePointCount + 1] = 1.;
  134.         weightArray[0] = 0.5 * width;
  135.         abscissaArray[0] = 0.;

  136.         for (int intermediatePointIndex = 0; intermediatePointIndex < intermediatePointCount;
  137.             ++intermediatePointIndex)
  138.         {
  139.             weightArray[intermediatePointIndex + 1] = width;
  140.             abscissaArray[intermediatePointIndex + 1] = width * (intermediatePointIndex + 1);
  141.         }

  142.         try
  143.         {
  144.             return new org.drip.numerical.integration.QuadratureEstimator (
  145.                 abscissaTransformer,
  146.                 org.drip.numerical.common.Array2D.FromArray (
  147.                     abscissaArray,
  148.                     weightArray
  149.                 )
  150.             );
  151.         }
  152.         catch (java.lang.Exception e)
  153.         {
  154.             e.printStackTrace();
  155.         }

  156.         return null;
  157.     }


  158.     /**
  159.      * Generate the Newton-Cotes of Equally Spaced Quadrature over (-1, +1)
  160.      *
  161.      * @param abscissaTransformer The Abscissa Transformer
  162.      * @param intermediatePointCount Number of Intermediate Points
  163.      *
  164.      * @return The Newton-Cotes of Equally Spaced Quadrature over (1, +1)
  165.      */

  166.     public static final org.drip.numerical.integration.QuadratureEstimator MinusOne_PlusOne (
  167.         final org.drip.numerical.integration.AbscissaTransform abscissaTransformer,
  168.         final int intermediatePointCount)
  169.     {
  170.         if (0 >= intermediatePointCount)
  171.         {
  172.             return null;
  173.         }

  174.         int nodeCount = intermediatePointCount + 2;
  175.         double[] weightArray = new double[nodeCount];
  176.         double[] abscissaArray = new double[nodeCount];
  177.         double width = 2. / (intermediatePointCount + 1);
  178.         weightArray[intermediatePointCount + 1] = 0.5 * width;
  179.         abscissaArray[intermediatePointCount + 1] = 1.;
  180.         weightArray[0] = 0.5 * width;
  181.         abscissaArray[0] = -1.;

  182.         for (int intermediatePointIndex = 0; intermediatePointIndex < intermediatePointCount;
  183.             ++intermediatePointIndex)
  184.         {
  185.             weightArray[intermediatePointIndex + 1] = width;
  186.             abscissaArray[intermediatePointIndex + 1] = width * (intermediatePointIndex + 1) - 1.;
  187.         }

  188.         try
  189.         {
  190.             return new org.drip.numerical.integration.QuadratureEstimator (
  191.                 abscissaTransformer,
  192.                 org.drip.numerical.common.Array2D.FromArray (
  193.                     abscissaArray,
  194.                     weightArray
  195.                 )
  196.             );
  197.         }
  198.         catch (java.lang.Exception e)
  199.         {
  200.             e.printStackTrace();
  201.         }

  202.         return null;
  203.     }

  204.     /**
  205.      * Generate the Newton-Cotes of Equally Spaced Quadrature over (a, b) onto (0, +1)
  206.      *
  207.      * @param left Left Integrand Quadrature Limit
  208.      * @param right Right Integrand Quadrature Limit
  209.      * @param intermediatePointCount Number of Intermediate Points
  210.      *
  211.      * @return The Newton-Cotes of Equally Spaced Quadrature over (a, b) onto (0, +1)
  212.      */

  213.     public static final org.drip.numerical.integration.QuadratureEstimator Zero_PlusOne (
  214.         final double left,
  215.         final double right,
  216.         final int intermediatePointCount)
  217.     {
  218.         return Zero_PlusOne (
  219.             org.drip.numerical.integration.AbscissaTransform.DisplaceAndScaleZero_PlusOne (
  220.                 left,
  221.                 right
  222.             ),
  223.             intermediatePointCount
  224.         );
  225.     }

  226.     /**
  227.      * Generate the Newton-Cotes of Equally Spaced Quadrature over (a, b) onto (-1, +1)
  228.      *
  229.      * @param left Left Integrand Quadrature Limit
  230.      * @param right Right Integrand Quadrature Limit
  231.      * @param intermediatePointCount Number of Intermediate Points
  232.      *
  233.      * @return The Newton-Cotes of Equally Spaced Quadrature over (a, b) onto (-1, +1)
  234.      */

  235.     public static final org.drip.numerical.integration.QuadratureEstimator MinusOne_PlusOne (
  236.         final double left,
  237.         final double right,
  238.         final int intermediatePointCount)
  239.     {
  240.         return MinusOne_PlusOne (
  241.             org.drip.numerical.integration.AbscissaTransform.DisplaceAndScaleMinusOne_PlusOne (
  242.                 left,
  243.                 right
  244.             ),
  245.             intermediatePointCount
  246.         );
  247.     }

  248.     /**
  249.      * Generate the Newton-Cotes Quadrature for the Gauss-Laguerre Left-Definite Integral over (a, +Infinity)
  250.      *
  251.      * @param left Left Integrand Quadrature Limit
  252.      * @param intermediatePointCount Number of Intermediate Points
  253.      *
  254.      * @return The Newton-Cotes Quadrature for the Gauss-Laguerre Left-Definite Integral over (a, +Infinity)
  255.      */

  256.     public static final org.drip.numerical.integration.QuadratureEstimator GaussLaguerreLeftDefinite (
  257.         final double left,
  258.         final int intermediatePointCount)
  259.     {
  260.         return Zero_PlusOne (
  261.             org.drip.numerical.integration.AbscissaTransform.GaussLaguerreLeftDefinite (left),
  262.             intermediatePointCount
  263.         );
  264.     }

  265.     /**
  266.      * Generate the Newton-Cotes Quadrature for the Gauss-Laguerre Left-Definite Integral over (-Infinity, a)
  267.      *
  268.      * @param right Right Integrand Quadrature Limit
  269.      * @param intermediatePointCount Number of Intermediate Points
  270.      *
  271.      * @return The Newton-Cotes Quadrature for the Gauss-Laguerre Left-Definite Integral over (-Infinity, a)
  272.      */

  273.     public static final org.drip.numerical.integration.QuadratureEstimator GaussLaguerreRightDefinite (
  274.         final double right,
  275.         final int intermediatePointCount)
  276.     {
  277.         return Zero_PlusOne (
  278.             org.drip.numerical.integration.AbscissaTransform.GaussLaguerreRightDefinite (right),
  279.             intermediatePointCount
  280.         );
  281.     }

  282.     /**
  283.      * Generate the Newton-Cotes Quadrature for the Gauss-Hermite Indefinite Integral over
  284.      *      (-Infinity, +Infinity)
  285.      *
  286.      * @param intermediatePointCount Number of Intermediate Points
  287.      *
  288.      * @return The Newton-Cotes Quadrature for the Gauss-Hermite Indefinite Integral over
  289.      *      (-Infinity, +Infinity)
  290.      */

  291.     public static final org.drip.numerical.integration.QuadratureEstimator GaussHermite (
  292.         final int intermediatePointCount)
  293.     {
  294.         return MinusOne_PlusOne (
  295.             org.drip.numerical.integration.AbscissaTransform.GaussHermite(),
  296.             intermediatePointCount
  297.         );
  298.     }
  299. }