WeightFunctionBuilder.java

  1. package org.drip.numerical.quadrature;

  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>WeightFunctionBuilder</i> builds the Weight Function associated with Different Kinds of Orthogonal
  76.  * Basis Polynomials. The References 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.  *          Gil, A., J. Segura, and N. M. Temme (2007): <i>Numerical Methods for Special Functions</i>
  86.  *              <b>Society for Industrial and Applied Mathematics</b> Philadelphia
  87.  *      </li>
  88.  *      <li>
  89.  *          Press, W. H., S. A. Teukolsky, W. T. Vetterling, and B. P. Flannery (2007): <i>Numerical Recipes:
  90.  *              The Art of Scientific Computing 3rd Edition</i> <b>Cambridge University Press</b> New York
  91.  *      </li>
  92.  *      <li>
  93.  *          Stoer, J., and R. Bulirsch (2002): <i>Introduction to Numerical Analysis 3rd Edition</i>
  94.  *              <b>Springer</b>
  95.  *      </li>
  96.  *      <li>
  97.  *          Wikipedia (2019): Gaussian Quadrature https://en.wikipedia.org/wiki/Gaussian_quadrature
  98.  *      </li>
  99.  *  </ul>
  100.  *
  101.  *  <br><br>
  102.  *  <ul>
  103.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ComputationalCore.md">Computational Core Module</a></li>
  104.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/NumericalAnalysisLibrary.md">Numerical Analysis Library</a></li>
  105.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/numerical/README.md">Numerical Quadrature, Differentiation, Eigenization, Linear Algebra, and Utilities</a></li>
  106.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/numerical/quadrature/README.md">R<sup>1</sup> Gaussian Integration Quadrature Schemes</a></li>
  107.  *  </ul>
  108.  *
  109.  * @author Lakshmi Krishnamurthy
  110.  */

  111. public class WeightFunctionBuilder
  112. {

  113.     /**
  114.      * Generate the Legendre Polynomial Weight Function
  115.      *
  116.      * @return The Legendre Polynomial Weight Function
  117.      */

  118.     public static final org.drip.function.definition.R1ToR1 Legendre()
  119.     {
  120.         return new org.drip.function.definition.R1ToR1 (null)
  121.         {
  122.             @Override public double evaluate (
  123.                 final double x)
  124.                 throws java.lang.Exception
  125.             {
  126.                 return 1.;
  127.             }
  128.         };
  129.     }

  130.     /**
  131.      * Generate the Jacobi Polynomial Weight Function
  132.      *
  133.      * @param alpha Jacobi Alpha
  134.      * @param beta Jacobi Beta
  135.      *
  136.      * @return The Jacobi Polynomial Weight Function
  137.      */

  138.     public static final org.drip.function.definition.R1ToR1 Jacobi (
  139.         final double alpha,
  140.         final double beta)
  141.     {
  142.         if (!org.drip.numerical.common.NumberUtil.IsValid (alpha) || -1. >= alpha || -1. >= beta)
  143.         {
  144.             return null;
  145.         }

  146.         return new org.drip.function.definition.R1ToR1 (null)
  147.         {
  148.             @Override public double evaluate (
  149.                 final double x)
  150.                 throws java.lang.Exception
  151.             {
  152.                 if (!org.drip.numerical.common.NumberUtil.IsValid (x))
  153.                 {
  154.                     throw new java.lang.Exception
  155.                         ("WeightFunctionBuilder::Jacobi::evaluate => Invalid Inputs");
  156.                 }

  157.                 return java.lang.Math.pow (
  158.                     1. - x,
  159.                     alpha
  160.                 ) * java.lang.Math.pow (
  161.                     1. + x,
  162.                     beta
  163.                 ) * java.lang.Math.exp (-1. * x);
  164.             }
  165.         };
  166.     }

  167.     /**
  168.      * Generate the Chebyshev Polynomial (First-Kind) Weight Function
  169.      *
  170.      * @return The Chebyshev Polynomial (First-Kind) Weight Function
  171.      */

  172.     public static final org.drip.function.definition.R1ToR1 ChebyshevFirstKind()
  173.     {
  174.         return new org.drip.function.definition.R1ToR1 (null)
  175.         {
  176.             @Override public double evaluate (
  177.                 final double x)
  178.                 throws java.lang.Exception
  179.             {
  180.                 if (!org.drip.numerical.common.NumberUtil.IsValid (x))
  181.                 {
  182.                     throw new java.lang.Exception
  183.                         ("WeightFunctionBuilder::ChebyshevFirstKind::evaluate => Invalid Inputs");
  184.                 }

  185.                 return 1. / java.lang.Math.sqrt (1. - x * x);
  186.             }
  187.         };
  188.     }

  189.     /**
  190.      * Generate the Chebyshev Polynomial (Second-Kind) Weight Function
  191.      *
  192.      * @return The Chebyshev Polynomial (Second-Kind) Weight Function
  193.      */

  194.     public static final org.drip.function.definition.R1ToR1 ChebyshevSecondKind()
  195.     {
  196.         return new org.drip.function.definition.R1ToR1 (null)
  197.         {
  198.             @Override public double evaluate (
  199.                 final double x)
  200.                 throws java.lang.Exception
  201.             {
  202.                 if (!org.drip.numerical.common.NumberUtil.IsValid (x))
  203.                 {
  204.                     throw new java.lang.Exception
  205.                         ("WeightFunctionBuilder::ChebyshevSecondKind::evaluate => Invalid Inputs");
  206.                 }

  207.                 return java.lang.Math.sqrt (1. - x * x);
  208.             }
  209.         };
  210.     }

  211.     /**
  212.      * Generate the Laguerre Polynomial Weight Function
  213.      *
  214.      * @return The Laguerre Polynomial Weight Function
  215.      */

  216.     public static final org.drip.function.definition.R1ToR1 Laguerre()
  217.     {
  218.         return new org.drip.function.definition.R1ToR1 (null)
  219.         {
  220.             @Override public double evaluate (
  221.                 final double x)
  222.                 throws java.lang.Exception
  223.             {
  224.                 if (!org.drip.numerical.common.NumberUtil.IsValid (x))
  225.                 {
  226.                     throw new java.lang.Exception
  227.                         ("WeightFunctionBuilder::Laguerre::evaluate => Invalid Inputs");
  228.                 }

  229.                 return java.lang.Math.exp (-1. * x);
  230.             }
  231.         };
  232.     }

  233.     /**
  234.      * Generate the Generalized Laguerre Polynomial Weight Function
  235.      *
  236.      * @param alpha Generalized Laguerre Alpha
  237.      *
  238.      * @return The Generalized Laguerre Polynomial Weight Function
  239.      */

  240.     public static final org.drip.function.definition.R1ToR1 GeneralizedLaguerre (
  241.         final double alpha)
  242.     {
  243.         if (!org.drip.numerical.common.NumberUtil.IsValid (alpha) || -1. >= alpha)
  244.         {
  245.             return null;
  246.         }

  247.         return new org.drip.function.definition.R1ToR1 (null)
  248.         {
  249.             @Override public double evaluate (
  250.                 final double x)
  251.                 throws java.lang.Exception
  252.             {
  253.                 if (!org.drip.numerical.common.NumberUtil.IsValid (x))
  254.                 {
  255.                     throw new java.lang.Exception
  256.                         ("WeightFunctionBuilder::GeneralizedLaguerre::evaluate => Invalid Inputs");
  257.                 }

  258.                 return java.lang.Math.pow (
  259.                     x,
  260.                     alpha
  261.                 ) * java.lang.Math.exp (-1. * x);
  262.             }
  263.         };
  264.     }

  265.     /**
  266.      * Generate the Hermite Polynomial Weight Function
  267.      *
  268.      * @return The Hermite Polynomial Weight Function
  269.      */

  270.     public static final org.drip.function.definition.R1ToR1 Hermite()
  271.     {
  272.         return new org.drip.function.definition.R1ToR1 (null)
  273.         {
  274.             @Override public double evaluate (
  275.                 final double x)
  276.                 throws java.lang.Exception
  277.             {
  278.                 if (!org.drip.numerical.common.NumberUtil.IsValid (x))
  279.                 {
  280.                     throw new java.lang.Exception
  281.                         ("WeightFunctionBuilder::Hermite::evaluate => Invalid Inputs");
  282.                 }

  283.                 return java.lang.Math.exp (-1. * x * x);
  284.             }
  285.         };
  286.     }
  287. }