CubicPolyGaussLobatto.java

  1. package org.drip.sample.gaussquadrature;

  2. import org.drip.function.definition.R1ToR1;
  3. import org.drip.numerical.common.FormatUtil;
  4. import org.drip.numerical.integration.GaussLobattoQuadratureGenerator;
  5. import org.drip.service.env.EnvManager;

  6. /*
  7.  * -*- mode: java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  8.  */

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

  66. /**
  67.  * <i>CubicPolyGaussLobatto</i> computes the R<sup>1</sup> Numerical Estimate of the Cubic Polynomial
  68.  * Integrand using the Gauss-Lobatto Integration Quadrature Scheme. The References are:
  69.  *
  70.  * <br><br>
  71.  *  <ul>
  72.  *      <li>
  73.  *          Abramowitz, M., and I. A. Stegun (2007): <i>Handbook of Mathematics Functions</i> <b>Dover Book
  74.  *              on Mathematics</b>
  75.  *      </li>
  76.  *      <li>
  77.  *          Gil, A., J. Segura, and N. M. Temme (2007): <i>Numerical Methods for Special Functions</i>
  78.  *              <b>Society for Industrial and Applied Mathematics</b> Philadelphia
  79.  *      </li>
  80.  *      <li>
  81.  *          Press, W. H., S. A. Teukolsky, W. T. Vetterling, and B. P. Flannery (2007): <i>Numerical Recipes:
  82.  *              The Art of Scientific Computing 3rd Edition</i> <b>Cambridge University Press</b> New York
  83.  *      </li>
  84.  *      <li>
  85.  *          Stoer, J., and R. Bulirsch (2002): <i>Introduction to Numerical Analysis 3rd Edition</i>
  86.  *              <b>Springer</b>
  87.  *      </li>
  88.  *      <li>
  89.  *          Wikipedia (2019): Gaussian Quadrature https://en.wikipedia.org/wiki/Gaussian_quadrature
  90.  *      </li>
  91.  *  </ul>
  92.  *
  93.  *  <br><br>
  94.  *  <ul>
  95.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/NumericalCore.md">Numerical Core Module</a></li>
  96.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/NumericalOptimizerLibrary.md">Numerical Optimizer</a></li>
  97.  *      <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>
  98.  *      <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>
  99.  *  </ul>
  100.  *
  101.  * @author Lakshmi Krishnamurthy
  102.  */

  103. public class CubicPolyGaussLobatto
  104. {

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

  110.         R1ToR1 cubicPolyIntegrand = new R1ToR1 (null)
  111.         {
  112.             @Override public double evaluate (
  113.                 final double x)
  114.                 throws Exception
  115.             {
  116.                 return 7. * x * x * x - 8. * x * x - 3. * x + 3;
  117.             }
  118.         };

  119.         double[] xArray =
  120.         {
  121.             -0.90,
  122.             -0.80,
  123.             -0.70,
  124.             -0.60,
  125.             -0.50,
  126.             -0.40,
  127.             -0.30,
  128.             -0.20,
  129.             -0.10,
  130.              0.00,
  131.              0.10,
  132.              0.20,
  133.              0.30,
  134.              0.40,
  135.              0.50,
  136.              0.60,
  137.              0.70,
  138.              0.80,
  139.              0.90,
  140.              1.00,
  141.         };

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

  143.         System.out.println ("\t|                             Cubic Polynomial Estimate                             ||");

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

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

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

  147.         System.out.println ("\t|                - 7P Estimate                                                      ||");

  148.         System.out.println ("\t|                - 6P Estimate                                                      ||");

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

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

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

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

  153.         for (double x : xArray)
  154.         {
  155.             double estimate7P = GaussLobattoQuadratureGenerator.SevenPoint (
  156.                 -1.,
  157.                 x
  158.             ).integrate (cubicPolyIntegrand);

  159.             double estimate6P = GaussLobattoQuadratureGenerator.SixPoint (
  160.                 -1.,
  161.                 x
  162.             ).integrate (cubicPolyIntegrand);

  163.             double estimate5P = GaussLobattoQuadratureGenerator.FivePoint (
  164.                 -1.,
  165.                 x
  166.             ).integrate (cubicPolyIntegrand);

  167.             double estimate4P = GaussLobattoQuadratureGenerator.FourPoint (
  168.                 -1.,
  169.                 x
  170.             ).integrate (cubicPolyIntegrand);

  171.             double estimate3P = GaussLobattoQuadratureGenerator.ThreePoint (
  172.                 -1.,
  173.                 x
  174.             ).integrate (cubicPolyIntegrand);

  175.             System.out.println (
  176.                 "\t| " + FormatUtil.FormatDouble (x, 1, 2, 1.) + " => " +
  177.                 FormatUtil.FormatDouble (estimate7P, 1, 9, 1.) + " | " +
  178.                 FormatUtil.FormatDouble (estimate6P, 1, 9, 1.) + " | " +
  179.                 FormatUtil.FormatDouble (estimate5P, 1, 9, 1.) + " | " +
  180.                 FormatUtil.FormatDouble (estimate4P, 1, 9, 1.) + " | " +
  181.                 FormatUtil.FormatDouble (estimate3P, 1, 9, 1.) + " ||"
  182.             );
  183.         }

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

  185.         EnvManager.TerminateEnv();
  186.     }
  187. }