DerivativeEstimate.java

  1. package org.drip.sample.hypergeometric;

  2. import org.drip.function.definition.R2ToR1;
  3. import org.drip.numerical.common.FormatUtil;
  4. import org.drip.service.env.EnvManager;
  5. import org.drip.specialfunction.beta.LogGammaEstimator;
  6. import org.drip.specialfunction.definition.HypergeometricParameters;
  7. import org.drip.specialfunction.hypergeometric.EulerQuadratureEstimator;

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

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

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

  106. public class DerivativeEstimate
  107. {

  108.     private static final void Hypergeometric (
  109.         final double a,
  110.         final double b,
  111.         final double c,
  112.         final R2ToR1 logBetaEstimator,
  113.         final int quadratureCount,
  114.         final double[] zArray,
  115.         final int[] derivativeOrderArray)
  116.         throws Exception
  117.     {
  118.         EulerQuadratureEstimator eulerQuadratureEstimator = new EulerQuadratureEstimator (
  119.             new HypergeometricParameters (
  120.                 a,
  121.                 b,
  122.                 c
  123.             ),
  124.             logBetaEstimator,
  125.             quadratureCount
  126.         );

  127.         for (double z : zArray)
  128.         {
  129.             String display = "\t| {a=" +
  130.                 FormatUtil.FormatDouble (a, 1, 2, 1., false) + ", b=" +
  131.                 FormatUtil.FormatDouble (b, 1, 2, 1., false) + "; c=" +
  132.                 FormatUtil.FormatDouble (c, 1, 2, 1., false) + "; z=" +
  133.                 FormatUtil.FormatDouble (z, 1, 2, 1.) + "} => ";

  134.             for (int derivativeOrder : derivativeOrderArray)
  135.             {
  136.                 display = display + FormatUtil.FormatDouble (
  137.                     eulerQuadratureEstimator.derivative (
  138.                         z,
  139.                         derivativeOrder
  140.                     ), 2, 10, 1.
  141.                 ) + " |";
  142.             }

  143.             System.out.println (display + "|");
  144.         }
  145.     }

  146.     public static final void main (
  147.         final String[] argumentArray)
  148.         throws Exception
  149.     {
  150.         EnvManager.InitEnv ("");

  151.         double[] aArray =
  152.         {
  153.             1.,
  154.             2.,
  155.         };
  156.         double[] bArray =
  157.         {
  158.             3.,
  159.             4.,
  160.         };
  161.         double[] cArray =
  162.         {
  163.             5.,
  164.             6.,
  165.         };
  166.         double[] zArray =
  167.         {
  168.             -1.00,
  169.             -0.75,
  170.             -0.50,
  171.             -0.25,
  172.              0.00,
  173.              0.25,
  174.              0.50,
  175.              0.75,
  176.              1.00
  177.         };
  178.         int[] derivativeOrderArray =
  179.         {
  180.             1,
  181.             2,
  182.             3,
  183.         };
  184.         int logBetaTermCount = 1000;
  185.         int hypergeometricQuadratureCount = 10000;

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

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

  188.         System.out.println ("\t|                HYPER-GEOMETRIC FUNCTION EULER QUADRATURE DERIVATIVE                 ||");

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

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

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

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

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

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

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

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

  197.         for (double a : aArray)
  198.         {
  199.             for (double b : bArray)
  200.             {
  201.                 for (double c : cArray)
  202.                 {
  203.                     Hypergeometric (
  204.                         a,
  205.                         b,
  206.                         c,
  207.                         logBetaEstimator,
  208.                         hypergeometricQuadratureCount,
  209.                         zArray,
  210.                         derivativeOrderArray
  211.                     );
  212.                 }
  213.             }
  214.         }

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

  216.         EnvManager.TerminateEnv();
  217.     }
  218. }