ERFIMacLaurinGenerator.java

  1. package org.drip.sample.erfx;

  2. import java.util.Map;

  3. import org.drip.function.e2erf.MacLaurinSeries;
  4. import org.drip.numerical.common.FormatUtil;
  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>ERFIMacLaurinGenerator</i> illustrates the MacLaurin Series Coefficient Generation for the Error
  68.  * Function Inverse. 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.  *          Chang, S. H., P. C. Cosman, L. B. Milstein (2011): Chernoff-Type Bounds for Gaussian Error
  78.  *              Function <i>IEEE Transactions on Communications</i> <b>59 (11)</b> 2939-2944
  79.  *      </li>
  80.  *      <li>
  81.  *          Cody, W. J. (1991): Algorithm 715: SPECFUN – A Portable FORTRAN Package of Special Function
  82.  *              Routines and Test Drivers <i>ACM Transactions on Mathematical Software</i> <b>19 (1)</b>
  83.  *              22-32
  84.  *      </li>
  85.  *      <li>
  86.  *          Schopf, H. M., and P. H. Supancic (2014): On Burmann’s Theorem and its Application to Problems of
  87.  *              Linear and Non-linear Heat Transfer and Diffusion
  88.  *              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/
  89.  *      </li>
  90.  *      <li>
  91.  *          Wikipedia (2019): Error Function https://en.wikipedia.org/wiki/Error_function
  92.  *      </li>
  93.  *  </ul>
  94.  *
  95.  *  <br><br>
  96.  *  <ul>
  97.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/NumericalCore.md">Numerical Core Module</a></li>
  98.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/NumericalOptimizerLibrary.md">Numerical Optimizer</a></li>
  99.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/feed/README.md">Function</a></li>
  100.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/feed/erfx/README.md">E<sup>2</sup> erfc and erfi Estimation</a></li>
  101.  *  </ul>
  102.  *
  103.  * @author Lakshmi Krishnamurthy
  104.  */

  105. public class ERFIMacLaurinGenerator
  106. {

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

  112.         int seriesTermCount = 5;
  113.         int coefficientTermCount = 15;

  114.         double[] termWeightCount =
  115.         {
  116.             Math.pow (Math.PI, 0.5) / 2.,
  117.             -Math.pow (Math.PI, 1.5) / 24.,
  118.             7. * Math.pow (Math.PI, 2.5) / 960.,
  119.             -127. * Math.pow (Math.PI, 3.5) / 80640.,
  120.             0.5 * 4369. * Math.pow (Math.PI, 4.5) / 5806080.,
  121.             -0.5 * 34807. * Math.pow (Math.PI, 5.5) / 182476800.
  122.         };

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

  124.         System.out.println ("\t|  ERFI COEFFICIENTS  ||");

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

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

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

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

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

  130.         for (int coefficientTermIndex = 0; coefficientTermIndex <= coefficientTermCount; ++coefficientTermIndex)
  131.         {
  132.             System.out.println (
  133.                 "\t|" + FormatUtil.FormatDouble (coefficientTermIndex, 2, 0, 1.) + " => " +
  134.                 FormatUtil.FormatDouble (MacLaurinSeries.ERFICoefficient (coefficientTermIndex), 2, 9, 1.) + " ||"
  135.             );
  136.         }

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

  138.         System.out.println();

  139.         Map<Integer, Double> termWeightMap = MacLaurinSeries.ERFI (seriesTermCount).termWeightMap();

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

  141.         System.out.println ("\t|   MacLaurin Series Coefficients    ||");

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

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

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

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

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

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

  148.         for (int seriesTermIndex = 0; seriesTermIndex <= seriesTermCount; ++seriesTermIndex)
  149.         {
  150.             System.out.println (
  151.                 "\t|" + FormatUtil.FormatDouble (seriesTermIndex, 1, 0, 1.) + " => " +
  152.                 FormatUtil.FormatDouble (termWeightMap.get (seriesTermIndex), 1, 10, 1.) + " | " +
  153.                 FormatUtil.FormatDouble (termWeightCount[seriesTermIndex], 1, 10, 1.) + " ||"
  154.             );
  155.         }

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

  157.         EnvManager.TerminateEnv();
  158.     }
  159. }