IntegrandEstimator.java

  1. package org.drip.specialfunction.beta;

  2. /*
  3.  * -*- mode: java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  4.  */

  5. /*!
  6.  * Copyright (C) 2020 Lakshmi Krishnamurthy
  7.  * Copyright (C) 2019 Lakshmi Krishnamurthy
  8.  *
  9.  *  This file is part of DROP, an open-source library targeting analytics/risk, transaction cost analytics,
  10.  *      asset liability management analytics, capital, exposure, and margin analytics, valuation adjustment
  11.  *      analytics, and portfolio construction analytics within and across fixed income, credit, commodity,
  12.  *      equity, FX, and structured products. It also includes auxiliary libraries for algorithm support,
  13.  *      numerical analysis, numerical optimization, spline builder, model validation, statistical learning,
  14.  *      and computational support.
  15.  *  
  16.  *      https://lakshmidrip.github.io/DROP/
  17.  *  
  18.  *  DROP is composed of three modules:
  19.  *  
  20.  *  - DROP Product Core - https://lakshmidrip.github.io/DROP-Product-Core/
  21.  *  - DROP Portfolio Core - https://lakshmidrip.github.io/DROP-Portfolio-Core/
  22.  *  - DROP Computational Core - https://lakshmidrip.github.io/DROP-Computational-Core/
  23.  *
  24.  *  DROP Product Core implements libraries for the following:
  25.  *  - Fixed Income Analytics
  26.  *  - Loan Analytics
  27.  *  - Transaction Cost Analytics
  28.  *
  29.  *  DROP Portfolio Core implements libraries for the following:
  30.  *  - Asset Allocation Analytics
  31.  *  - Asset Liability Management Analytics
  32.  *  - Capital Estimation Analytics
  33.  *  - Exposure Analytics
  34.  *  - Margin Analytics
  35.  *  - XVA Analytics
  36.  *
  37.  *  DROP Computational Core implements libraries for the following:
  38.  *  - Algorithm Support
  39.  *  - Computation Support
  40.  *  - Function Analysis
  41.  *  - Model Validation
  42.  *  - Numerical Analysis
  43.  *  - Numerical Optimizer
  44.  *  - Spline Builder
  45.  *  - Statistical Learning
  46.  *
  47.  *  Documentation for DROP is Spread Over:
  48.  *
  49.  *  - Main                     => https://lakshmidrip.github.io/DROP/
  50.  *  - Wiki                     => https://github.com/lakshmiDRIP/DROP/wiki
  51.  *  - GitHub                   => https://github.com/lakshmiDRIP/DROP
  52.  *  - Repo Layout Taxonomy     => https://github.com/lakshmiDRIP/DROP/blob/master/Taxonomy.md
  53.  *  - Javadoc                  => https://lakshmidrip.github.io/DROP/Javadoc/index.html
  54.  *  - Technical Specifications => https://github.com/lakshmiDRIP/DROP/tree/master/Docs/Internal
  55.  *  - Release Versions         => https://lakshmidrip.github.io/DROP/version.html
  56.  *  - Community Credits        => https://lakshmidrip.github.io/DROP/credits.html
  57.  *  - Issues Catalog           => https://github.com/lakshmiDRIP/DROP/issues
  58.  *  - JUnit                    => https://lakshmidrip.github.io/DROP/junit/index.html
  59.  *  - Jacoco                   => https://lakshmidrip.github.io/DROP/jacoco/index.html
  60.  *
  61.  *  Licensed under the Apache License, Version 2.0 (the "License");
  62.  *      you may not use this file except in compliance with the License.
  63.  *  
  64.  *  You may obtain a copy of the License at
  65.  *      http://www.apache.org/licenses/LICENSE-2.0
  66.  *  
  67.  *  Unless required by applicable law or agreed to in writing, software
  68.  *      distributed under the License is distributed on an "AS IS" BASIS,
  69.  *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  70.  *  
  71.  *  See the License for the specific language governing permissions and
  72.  *      limitations under the License.
  73.  */

  74. /**
  75.  * <i>IntegrandEstimator</i> implements the Beta Function using Integrand Estimation Schemes. The References
  76.  * are:
  77.  *
  78.  * <br><br>
  79.  *  <ul>
  80.  *      <li>
  81.  *          Abramowitz, M., and I. A. Stegun (2007): <i>Handbook of Mathematics Functions</i> <b>Dover Book
  82.  *              on Mathematics</b>
  83.  *      </li>
  84.  *      <li>
  85.  *          Davis, P. J. (1959): Leonhard Euler's Integral: A Historical Profile of the Gamma Function
  86.  *              <i>American Mathematical Monthly</i> <b>66 (10)</b> 849-869
  87.  *      </li>
  88.  *      <li>
  89.  *          Whitaker, E. T., and G. N. Watson (1996): <i>A Course on Modern Analysis</i> <b>Cambridge
  90.  *              University Press</b> New York
  91.  *      </li>
  92.  *      <li>
  93.  *          Wikipedia (2019): Beta Function https://en.wikipedia.org/wiki/Beta_function
  94.  *      </li>
  95.  *      <li>
  96.  *          Wikipedia (2019): Gamma Function https://en.wikipedia.org/wiki/Gamma_function
  97.  *      </li>
  98.  *  </ul>
  99.  *
  100.  *  <br><br>
  101.  *  <ul>
  102.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ComputationalCore.md">Computational Core Module</a></li>
  103.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/FunctionAnalysisLibrary.md">Function Analysis Library</a></li>
  104.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/specialfunction/README.md">Special Function Implementation Analysis</a></li>
  105.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/specialfunction/beta/README.md">Estimation Techniques for Beta Function</a></li>
  106.  *  </ul>
  107.  *
  108.  * @author Lakshmi Krishnamurthy
  109.  */

  110. public abstract class IntegrandEstimator extends org.drip.specialfunction.definition.BetaEstimator
  111. {
  112.     private int _quadratureCount = -1;

  113.     /**
  114.      * Construct the Beta Estimator from the Trigonometric Integral
  115.      *
  116.      * @param quadratureCount Count of the Integrand Quadrature
  117.      *
  118.      * @return Beta Estimator from the Trigonometric Integral
  119.      */

  120.     public static final IntegrandEstimator Trigonometric (
  121.         final int quadratureCount)
  122.     {
  123.         try
  124.         {
  125.             return new IntegrandEstimator (quadratureCount)
  126.             {
  127.                 @Override public double evaluate (
  128.                     final double x,
  129.                     final double y)
  130.                     throws java.lang.Exception
  131.                 {
  132.                     if (!org.drip.numerical.common.NumberUtil.IsValid (x) ||
  133.                         !org.drip.numerical.common.NumberUtil.IsValid (y))
  134.                     {
  135.                         throw new java.lang.Exception
  136.                             ("IntegrandEstimator::Trigonometric::evaluate => Invalid Inputs");
  137.                     }

  138.                     return 2. * org.drip.numerical.integration.NewtonCotesQuadratureGenerator.Zero_PlusOne (
  139.                         0.,
  140.                         0.5 * java.lang.Math.PI,
  141.                         quadratureCount
  142.                     ).integrate (
  143.                         new org.drip.function.definition.R1ToR1 (null)
  144.                         {
  145.                             @Override public double evaluate (
  146.                                 final double theta)
  147.                                 throws java.lang.Exception
  148.                             {
  149.                                 return 0. == theta || 0.5 * java.lang.Math.PI == theta ? 0. :
  150.                                     java.lang.Math.pow (
  151.                                         java.lang.Math.sin (theta),
  152.                                         2. * x - 1.
  153.                                     ) * java.lang.Math.pow (
  154.                                         java.lang.Math.cos (theta),
  155.                                         2. * y - 1.
  156.                                     );
  157.                             }
  158.                         }
  159.                     );
  160.                 }
  161.             };
  162.         }
  163.         catch (java.lang.Exception e)
  164.         {
  165.             e.printStackTrace();
  166.         }

  167.         return null;
  168.     }

  169.     /**
  170.      * Construct the Beta Estimator from the Euler Integral of the First Kind
  171.      *
  172.      * @param quadratureCount Count of the Integrand Quadrature
  173.      *
  174.      * @return Beta Estimator from the Euler Integral of the First Kind
  175.      */

  176.     public static final IntegrandEstimator EulerFirst (
  177.         final int quadratureCount)
  178.     {
  179.         try
  180.         {
  181.             return new IntegrandEstimator (quadratureCount)
  182.             {
  183.                 @Override public double evaluate (
  184.                     final double x,
  185.                     final double y)
  186.                     throws java.lang.Exception
  187.                 {
  188.                     if (!org.drip.numerical.common.NumberUtil.IsValid (x) ||
  189.                         !org.drip.numerical.common.NumberUtil.IsValid (y))
  190.                     {
  191.                         throw new java.lang.Exception
  192.                             ("IntegrandEstimator::EulerFirst::evaluate => Invalid Inputs");
  193.                     }

  194.                     return org.drip.numerical.integration.NewtonCotesQuadratureGenerator.Zero_PlusOne (
  195.                         0.,
  196.                         1.,
  197.                         1000000
  198.                     ).integrate (
  199.                         new org.drip.function.definition.R1ToR1 (null)
  200.                         {
  201.                             @Override public double evaluate (
  202.                                 final double t)
  203.                                 throws java.lang.Exception
  204.                             {
  205.                                 return 0. == t || 1. == t ? 0. : java.lang.Math.pow (
  206.                                     t,
  207.                                     x - 1.
  208.                                 ) * java.lang.Math.pow (
  209.                                     1. - t,
  210.                                     y - 1.
  211.                                 );
  212.                             }
  213.                         }
  214.                     );
  215.                 }
  216.             };
  217.         }
  218.         catch (java.lang.Exception e)
  219.         {
  220.             e.printStackTrace();
  221.         }

  222.         return null;
  223.     }

  224.     /**
  225.      * Construct the Beta Estimator from the Euler Integral of the First Kind
  226.      *
  227.      * @param quadratureCount Count of the Integrand Quadrature
  228.      * @param exponent Exponent
  229.      *
  230.      * @return Beta Estimator from the Euler Integral of the First Kind
  231.      */

  232.     public static final IntegrandEstimator EulerFirstN (
  233.         final int quadratureCount,
  234.         final double exponent)
  235.     {
  236.         if (!org.drip.numerical.common.NumberUtil.IsValid (exponent) || 0. >= exponent)
  237.         {
  238.             return null;
  239.         }

  240.         try
  241.         {
  242.             return new IntegrandEstimator (quadratureCount)
  243.             {
  244.                 @Override public double evaluate (
  245.                     final double x,
  246.                     final double y)
  247.                     throws java.lang.Exception
  248.                 {
  249.                     if (!org.drip.numerical.common.NumberUtil.IsValid (x) ||
  250.                         !org.drip.numerical.common.NumberUtil.IsValid (y))
  251.                     {
  252.                         throw new java.lang.Exception
  253.                             ("IntegrandEstimator::EulerFirstN::evaluate => Invalid Inputs");
  254.                     }

  255.                     return exponent * org.drip.numerical.integration.NewtonCotesQuadratureGenerator.Zero_PlusOne (
  256.                         0.,
  257.                         1.,
  258.                         quadratureCount
  259.                     ).integrate (
  260.                         new org.drip.function.definition.R1ToR1 (null)
  261.                         {
  262.                             @Override public double evaluate (
  263.                                 final double t)
  264.                                 throws java.lang.Exception
  265.                             {
  266.                                 return 0. == t || 1. == t ? 0. : java.lang.Math.pow (
  267.                                     t,
  268.                                     exponent * x - 1.
  269.                                 ) * java.lang.Math.pow (
  270.                                     java.lang.Math.pow (
  271.                                         1. - t,
  272.                                         exponent
  273.                                     ),
  274.                                     y - 1.
  275.                                 );
  276.                             }
  277.                         }
  278.                     );
  279.                 }
  280.             };
  281.         }
  282.         catch (java.lang.Exception e)
  283.         {
  284.             e.printStackTrace();
  285.         }

  286.         return null;
  287.     }

  288.     /**
  289.      * Construct the Beta Estimator from the Euler Integral of the First Kind over the Right Half Plane
  290.      *
  291.      * @param quadratureCount Count of the Integrand Quadrature
  292.      *
  293.      * @return Beta Estimator from the Euler Integral of the First Kind over the Right Half Plane
  294.      */

  295.     public static final IntegrandEstimator EulerFirstRightPlane (
  296.         final int quadratureCount)
  297.     {
  298.         try
  299.         {
  300.             return new IntegrandEstimator (quadratureCount)
  301.             {
  302.                 @Override public double evaluate (
  303.                     final double x,
  304.                     final double y)
  305.                     throws java.lang.Exception
  306.                 {
  307.                     if (!org.drip.numerical.common.NumberUtil.IsValid (x) ||
  308.                         !org.drip.numerical.common.NumberUtil.IsValid (y))
  309.                     {
  310.                         throw new java.lang.Exception
  311.                             ("IntegrandEstimator::EulerFirstRightPlane::evaluate => Invalid Inputs");
  312.                     }

  313.                     return org.drip.numerical.integration.NewtonCotesQuadratureGenerator.GaussLaguerreLeftDefinite (
  314.                         0.,
  315.                         quadratureCount
  316.                     ).integrate (
  317.                         new org.drip.function.definition.R1ToR1 (null)
  318.                         {
  319.                             @Override public double evaluate (
  320.                                 final double t)
  321.                                 throws java.lang.Exception
  322.                             {
  323.                                 return 0. == t || java.lang.Double.isInfinite (t) ? 0. :
  324.                                     java.lang.Math.pow (
  325.                                         t,
  326.                                         x - 1.
  327.                                     ) / java.lang.Math.pow (
  328.                                         1. + t,
  329.                                         x + y
  330.                                     );
  331.                             }
  332.                         }
  333.                     );
  334.                 }
  335.             };
  336.         }
  337.         catch (java.lang.Exception e)
  338.         {
  339.             e.printStackTrace();
  340.         }

  341.         return null;
  342.     }

  343.     protected IntegrandEstimator (
  344.         final int quadratureCount)
  345.         throws java.lang.Exception
  346.     {
  347.         if (0 >= (_quadratureCount = quadratureCount))
  348.         {
  349.             throw new java.lang.Exception ("IntegrandEstimator Constructor => Invalid Inputs");
  350.         }
  351.     }

  352.     /**
  353.      * Retrieve the Quadrature Count
  354.      *
  355.      * @return The Quadrature Count
  356.      */

  357.     public int quadratureCount()
  358.     {
  359.         return _quadratureCount;
  360.     }
  361. }