EnERFMacLaurin.java

  1. package org.drip.sample.erf;

  2. import org.drip.function.enerf.GeneralizedErrorFunction;
  3. import org.drip.numerical.common.FormatUtil;
  4. import org.drip.service.env.EnvManager;

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

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

  65. /**
  66.  * <i>EnERFMacLaurin</i> illustrates the MacLaurin Series Based Estimates for E<sub>n</sub> erf. The
  67.  * References are:
  68.  *
  69.  * <br><br>
  70.  *  <ul>
  71.  *      <li>
  72.  *          Abramowitz, M., and I. A. Stegun (2007): <i>Handbook of Mathematics Functions</i> <b>Dover Book
  73.  *              on Mathematics</b>
  74.  *      </li>
  75.  *      <li>
  76.  *          Chang, S. H., P. C. Cosman, L. B. Milstein (2011): Chernoff-Type Bounds for Gaussian Error
  77.  *              Function <i>IEEE Transactions on Communications</i> <b>59 (11)</b> 2939-2944
  78.  *      </li>
  79.  *      <li>
  80.  *          Cody, W. J. (1991): Algorithm 715: SPECFUN – A Portable FORTRAN Package of Special Function
  81.  *              Routines and Test Drivers <i>ACM Transactions on Mathematical Software</i> <b>19 (1)</b>
  82.  *              22-32
  83.  *      </li>
  84.  *      <li>
  85.  *          Schopf, H. M., and P. H. Supancic (2014): On Burmann’s Theorem and its Application to Problems of
  86.  *              Linear and Non-linear Heat Transfer and Diffusion
  87.  *              https://www.mathematica-journal.com/2014/11/on-burmanns-theorem-and-its-application-to-problems-of-linear-and-nonlinear-heat-transfer-and-diffusion/#more-39602/
  88.  *      </li>
  89.  *      <li>
  90.  *          Wikipedia (2019): Error Function https://en.wikipedia.org/wiki/Error_function
  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/feed/README.md">Function</a></li>
  99.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/feed/erf/README.md">E<sup>2</sup> and E<sup>n</sup> erf Estimation</a></li>
  100.  *  </ul>
  101.  *
  102.  * @author Lakshmi Krishnamurthy
  103.  */

  104. public class EnERFMacLaurin
  105. {

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

  111.         double[] xArray =
  112.         {
  113.             -3.25,
  114.             -3.00,
  115.             -2.75,
  116.             -2.50,
  117.             -2.25,
  118.             -2.00,
  119.             -1.75,
  120.             -1.50,
  121.             -1.25,
  122.             -1.00,
  123.             -0.75,
  124.             -0.50,
  125.             -0.25,
  126.              0.00,
  127.              0.25,
  128.              0.50,
  129.              0.75,
  130.              1.00,
  131.              1.25,
  132.              1.50,
  133.              1.75,
  134.              2.00,
  135.              2.25,
  136.              2.50,
  137.              2.75,
  138.              3.00,
  139.              3.25
  140.         };
  141.         int[] degreeArray =
  142.         {
  143.             0,
  144.             1,
  145.             2,
  146.             3,
  147.             4,
  148.             5
  149.         };
  150.         int termCount = 50;

  151.         GeneralizedErrorFunction[] erfnArray = new GeneralizedErrorFunction[degreeArray.length];

  152.         for (int degree : degreeArray)
  153.         {
  154.             erfnArray[degree] = GeneralizedErrorFunction.MacLaurin (
  155.                 degree,
  156.                 termCount
  157.             );
  158.         }

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

  160.         System.out.println ("\t|                                                                  E_n MacLaurin erf Estimate                                                                     ||");

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

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

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

  164.         System.out.println ("\t|                - MacLaurin Estimate (Degree = 0)                                                                                                                ||");

  165.         System.out.println ("\t|                - MacLaurin Estimate (Degree = 1)                                                                                                                ||");

  166.         System.out.println ("\t|                - MacLaurin Estimate (Degree = 2)                                                                                                                ||");

  167.         System.out.println ("\t|                - MacLaurin Estimate (Degree = 3)                                                                                                                ||");

  168.         System.out.println ("\t|                - MacLaurin Estimate (Degree = 4)                                                                                                                ||");

  169.         System.out.println ("\t|                - MacLaurin Estimate (Degree = 5)                                                                                                                ||");

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

  171.         for (double x : xArray)
  172.         {
  173.             System.out.println (
  174.                 "\t| " + FormatUtil.FormatDouble (x, 1, 2, 1.) + " => " +
  175.                 FormatUtil.FormatDouble (erfnArray[0].evaluate (x), 1, 2, 1.) + " | " +
  176.                 FormatUtil.FormatDouble (erfnArray[1].evaluate (x), 2, 2, 1.) + " | " +
  177.                 FormatUtil.FormatDouble (erfnArray[2].evaluate (x), 1, 2, 1.) + " | " +
  178.                 FormatUtil.FormatDouble (erfnArray[3].evaluate (x), 14, 0, 1.) + " | " +
  179.                 FormatUtil.FormatDouble (erfnArray[4].evaluate (x), 38, 0, 1.) + " | " +
  180.                 FormatUtil.FormatDouble (erfnArray[5].evaluate (x), 64, 0, 1.) + " ||"
  181.             );
  182.         }

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

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