PSeriesSequence.java

  1. package org.drip.sample.lanczos;

  2. import java.util.Map;

  3. import org.drip.numerical.common.FormatUtil;
  4. import org.drip.service.env.EnvManager;
  5. import org.drip.specialfunction.lanczos.PSeriesGenerator;

  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>PSeriesSequence</i> illustrates the Generation of the Lanczos P Series for different Values of the g
  68.  * Control. The References are:
  69.  *
  70.  * <br><br>
  71.  *  <ul>
  72.  *      <li>
  73.  *          Godfrey, P. (2001): Lanczos Implementation of the Gamma Function
  74.  *              http://www.numericana.com/answer/info/godfrey.htm
  75.  *      </li>
  76.  *      <li>
  77.  *          Press, W. H., S. A. Teukolsky, W. T. Vetterling, and B. P. Flannery (2007): <i>Numerical Recipes:
  78.  *              The Art of Scientific Computing 3rd Edition</i> <b>Cambridge University Press</b> New York
  79.  *      </li>
  80.  *      <li>
  81.  *          Pugh, G. R. (2004): <i>An Analysis of the Lanczos Gamma Approximation</i> Ph. D. <b>University of
  82.  *              British Columbia</b>
  83.  *      </li>
  84.  *      <li>
  85.  *          Toth V. T. (2016): Programmable Calculators – The Gamma Function
  86.  *              http://www.rskey.org/CMS/index.php/the-library/11
  87.  *      </li>
  88.  *      <li>
  89.  *          Wikipedia (2019): Lanczos Approximation https://en.wikipedia.org/wiki/Lanczos_approximation
  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/sample/README.md">Sample</a></li>
  98.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/lanczos/README.md">Lanczos Gamma Calculation Scheme Illustration</a></li>
  99.  *  </ul>
  100.  *
  101.  * @author Lakshmi Krishnamurthy
  102.  */

  103. public class PSeriesSequence
  104. {

  105.     private static final void DisplaySeries (
  106.         final int g,
  107.         final Map<Integer, Double> orderedSeries)
  108.         throws Exception
  109.     {
  110.         String series = "\t|" + FormatUtil.FormatDouble (g, 2, 0, 1.) + " => ";

  111.         for (Map.Entry<Integer, Double> seriesEntry : orderedSeries.entrySet())
  112.         {
  113.             series = series + " " + FormatUtil.FormatDouble (seriesEntry.getValue(), 8, 0, 1.) + " |";
  114.         }

  115.         System.out.println (series + "|");
  116.     }

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

  122.         int termCount = 10;
  123.         int[] gArray =
  124.         {
  125.             3,
  126.             4,
  127.             5,
  128.             6,
  129.             7,
  130.             8,
  131.             9,
  132.             10
  133.         };

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

  136.         System.out.println
  137.             ("\t|                                                            P-SERIES SEQUENCE                                                             ||");

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

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

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

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

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

  148.         for (int g : gArray)
  149.         {
  150.             DisplaySeries (
  151.                 g,
  152.                 PSeriesGenerator.Standard (
  153.                     g,
  154.                     termCount
  155.                 ).generate (0.)
  156.             );
  157.         }

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

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