LegendreEstimate.java

  1. package org.drip.sample.hypergeometric;

  2. import org.drip.function.definition.R1ToR1;
  3. import org.drip.function.definition.R2ToR1;
  4. import org.drip.numerical.common.FormatUtil;
  5. import org.drip.service.env.EnvManager;
  6. import org.drip.specialfunction.beta.LogGammaEstimator;
  7. import org.drip.specialfunction.definition.LegendreEstimator;
  8. import org.drip.specialfunction.derived.Legendre;
  9. import org.drip.specialfunction.gamma.EulerIntegralSecondKind;

  10. /*
  11.  * -*- mode: java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  12.  */

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

  70. /**
  71.  * <i>LegendreEstimate</i> estimates the Legendre Hyper-geometric Function. The References are:
  72.  *
  73.  * <br><br>
  74.  *  <ul>
  75.  *      <li>
  76.  *          Gessel, I., and D. Stanton (1982): Strange Evaluations of Hyper-geometric Series <i>SIAM Journal
  77.  *              on Mathematical Analysis</i> <b>13 (2)</b> 295-308
  78.  *      </li>
  79.  *      <li>
  80.  *          Koepf, W (1995): Algorithms for m-fold Hyper-geometric Summation <i>Journal of Symbolic
  81.  *              Computation</i> <b>20 (4)</b> 399-417
  82.  *      </li>
  83.  *      <li>
  84.  *          Lavoie, J. L., F. Grondin, and A. K. Rathie (1996): Generalization of Whipple’s Theorem on the
  85.  *              Sum of a (_2^3)F(a,b;c;z) <i>Journal of Computational and Applied Mathematics</i> <b>72</b>
  86.  *              293-300
  87.  *      </li>
  88.  *      <li>
  89.  *          National Institute of Standards and Technology (2019): Hyper-geometric Function
  90.  *              https://dlmf.nist.gov/15
  91.  *      </li>
  92.  *      <li>
  93.  *          Wikipedia (2019): Hyper-geometric Function https://en.wikipedia.org/wiki/Hypergeometric_function
  94.  *      </li>
  95.  *  </ul>
  96.  *
  97.  *  <br><br>
  98.  *  <ul>
  99.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/NumericalCore.md">Numerical Core Module</a></li>
  100.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/NumericalOptimizerLibrary.md">Numerical Optimizer</a></li>
  101.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/README.md">Function</a></li>
  102.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/hypergeometric/README.md">Estimates of Hyper-geometric Function</a></li>
  103.  *  </ul>
  104.  *
  105.  * @author Lakshmi Krishnamurthy
  106.  */

  107. public class LegendreEstimate
  108. {

  109.     private static final void Legendre (
  110.         final double alpha,
  111.         final double ceta,
  112.         final R2ToR1 logBetaEstimator,
  113.         final int quadratureCount,
  114.         final R1ToR1 gammaEstimator,
  115.         final double[] zArray)
  116.         throws Exception
  117.     {
  118.         LegendreEstimator legendreEstimator = new Legendre (
  119.             alpha,
  120.             ceta,
  121.             logBetaEstimator,
  122.             quadratureCount,
  123.             gammaEstimator
  124.         );

  125.         for (double z : zArray)
  126.         {
  127.             System.out.println ("\t| {alpha=" +
  128.                 FormatUtil.FormatDouble (alpha, 1, 2, 1., false) + "; ceta=" +
  129.                 FormatUtil.FormatDouble (ceta, 2, 2, 1., false) + "; z=" +
  130.                 FormatUtil.FormatDouble (z, 1, 2, 1.) + "} => " +
  131.                 FormatUtil.FormatDouble (legendreEstimator.evaluate (z), 1, 10, 1., false) + " ||"
  132.             );
  133.         }
  134.     }

  135.     public static final void main (
  136.         final String[] argumentArray)
  137.         throws Exception
  138.     {
  139.         EnvManager.InitEnv ("");

  140.         double[] alphaArray =
  141.         {
  142.              -0.25,
  143.              -0.50,
  144.              -0.75,
  145.         };
  146.         double[] cetaArray =
  147.         {
  148.             -2.,
  149.             -3.,
  150.         };
  151.         double[] zArray =
  152.         {
  153.              0.05,
  154.              0.10,
  155.              0.15,
  156.              0.20,
  157.              0.25,
  158.              0.30,
  159.              0.35,
  160.              0.40,
  161.              0.45,
  162.              0.50,
  163.         };
  164.         int logBetaTermCount = 1000;
  165.         int hypergeometricQuadratureCount = 10000;

  166.         R1ToR1 gammaEstimator = new EulerIntegralSecondKind (null);

  167.         R2ToR1 logBetaEstimator = LogGammaEstimator.Weierstrass (logBetaTermCount);

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

  169.         System.out.println ("\t|     LEGENDRE HYPER-GEOMETRIC FUNCTION ESTIMATE      ||");

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

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

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

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

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

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

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

  177.         for (double alpha : alphaArray)
  178.         {
  179.             for (double ceta : cetaArray)
  180.             {
  181.                 Legendre (
  182.                     alpha,
  183.                     ceta,
  184.                     logBetaEstimator,
  185.                     hypergeometricQuadratureCount,
  186.                     gammaEstimator,
  187.                     zArray
  188.                 );
  189.             }
  190.         }

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

  192.         EnvManager.TerminateEnv();
  193.     }
  194. }