FactorialEstimate.java

  1. package org.drip.sample.stirling;

  2. import org.drip.numerical.common.FormatUtil;
  3. import org.drip.numerical.common.NumberUtil;
  4. import org.drip.numerical.estimation.R1Estimate;
  5. import org.drip.service.env.EnvManager;
  6. import org.drip.specialfunction.gamma.StirlingSeries;

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

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

  67. /**
  68.  * <i>FactorialEstimate</i> illustrates the Stirling's Approximation of the Factorial Function. The
  69.  * References are:
  70.  *
  71.  * <br><br>
  72.  *  <ul>
  73.  *      <li>
  74.  *          Mortici, C. (2011): Improved Asymptotic Formulas for the Gamma Function <i>Computers and
  75.  *              Mathematics with Applications</i> <b>61 (11)</b> 3364-3369
  76.  *      </li>
  77.  *      <li>
  78.  *          National Institute of Standards and Technology (2018): NIST Digital Library of Mathematical
  79.  *              Functions https://dlmf.nist.gov/5.11
  80.  *      </li>
  81.  *      <li>
  82.  *          Nemes, G. (2010): On the Coefficients of the Asymptotic Expansion of n!
  83.  *              https://arxiv.org/abs/1003.2907 <b>arXiv</b>
  84.  *      </li>
  85.  *      <li>
  86.  *          Toth V. T. (2016): Programmable Calculators – The Gamma Function
  87.  *              http://www.rskey.org/CMS/index.php/the-library/11
  88.  *      </li>
  89.  *      <li>
  90.  *          Wikipedia (2019): Stirling's Approximation
  91.  *              https://en.wikipedia.org/wiki/Stirling%27s_approximation
  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/stirling/README.md">Stirling Approximation Based Gamma Estimates</a></li>
  101.  *  </ul>
  102.  *
  103.  * @author Lakshmi Krishnamurthy
  104.  */

  105. public class FactorialEstimate
  106. {

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

  112.         int factorialCount = 12;

  113.         StirlingSeries stirlingFactorial = new StirlingSeries (null);

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

  115.         System.out.println ("\t|              STIRLING FACTORIAL APPROXIMATION              ||");

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

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

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

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

  120.         System.out.println ("\t|              - Stirling's Estimate                         ||");

  121.         System.out.println ("\t|              - Stirling's Estimate Bounds [Lower - Upper]  ||");

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

  123.         for (int factorialIndex = 0; factorialIndex <= factorialCount; ++factorialIndex)
  124.         {
  125.             R1Estimate numericalApproximation = stirlingFactorial.boundedEstimate (factorialIndex);

  126.             System.out.println (
  127.                 "\t| " + factorialIndex + " => " +
  128.                 NumberUtil.Factorial (factorialIndex) + " |" +
  129.                 FormatUtil.FormatDouble (numericalApproximation.baseline() + 0.5, 1, 0, 1.) + " | [" +
  130.                 FormatUtil.FormatDouble (numericalApproximation.lowerBound() + 0.5, 1, 0, 1.) + " -" +
  131.                 FormatUtil.FormatDouble (numericalApproximation.upperBound() + 0.5, 1, 0, 1.) + " ||"
  132.             );
  133.         }

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

  135.         EnvManager.TerminateEnv();
  136.     }
  137. }