ArcTangentGeneralizedMidPoint.java

  1. package org.drip.sample.newtoncotes;

  2. import org.drip.numerical.common.FormatUtil;
  3. import org.drip.service.env.EnvManager;

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

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

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

  100. public class ArcTangentGeneralizedMidPoint
  101. {

  102.     private static final double ArcTangent (
  103.         final double z,
  104.         final int termCount)
  105.         throws Exception
  106.     {
  107.         double b = 1.;
  108.         double a = 2. / z;
  109.         double arcTangent = 0.;

  110.         for (int termIndex = 1; termIndex <= termCount; ++termIndex)
  111.         {
  112.             arcTangent = arcTangent + (a / ((a * a + b * b) * (2 * termIndex - 1)));
  113.             a = a * (1. - (4. / (z * z))) + 4. * (b / z);
  114.             b = b * (1. - (4. / (z * z))) - 4. * (a / z);
  115.         }

  116.         return 2. * arcTangent;
  117.     }

  118.     public static final void main (
  119.         final String[] argumentArray)
  120.         throws Exception
  121.     {
  122.         EnvManager.InitEnv ("");

  123.         int termCount = 10;
  124.         double[] rValueArray =
  125.         {
  126.              1. / 32.,
  127.              1. / 16.,
  128.              1. /  8.,
  129.              1. /  4.,
  130.              1. /  2.,
  131.              1.,
  132.              2.,
  133.              4.,
  134.              8.,
  135.             16.,
  136.             32.,
  137.             64.
  138.         };

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

  140.         System.out.println ("\t|   ARC TANGENT SERIES ESTIMATION   ||");

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

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

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

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

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

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

  147.         for (double rValue : rValueArray)
  148.         {
  149.             System.out.println (
  150.                 "\t|" + FormatUtil.FormatDouble (rValue, 2, 5, 1.) + " => " +
  151.                 FormatUtil.FormatDouble (Math.atan (rValue), 1, 6, 1.) + " | " +
  152.                 FormatUtil.FormatDouble (
  153.                     ArcTangent (
  154.                         rValue,
  155.                         termCount
  156.                     ), 1, 6, 1.
  157.                 ) + " ||"
  158.             );
  159.         }

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

  161.         EnvManager.TerminateEnv();
  162.     }
  163. }