R1UnivariateScaledExponential.java

  1. package org.drip.measure.continuous;

  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>R1UnivariateScaledExponential</i> implements the Probability Density Function for the Scaled Univariate
  76.  *  R<sup>1</sup> Exponential Function. The References are:
  77.  *
  78.  * <br><br>
  79.  *  <ul>
  80.  *      <li>
  81.  *          Gradshteyn, I. S., I. M. Ryzhik, Y. V. Geronimus, M. Y. Tseytlin, and A. Jeffrey (2015):
  82.  *              <i>Tables of Integrals, Series, and Products</i> <b>Academic Press</b>
  83.  *      </li>
  84.  *      <li>
  85.  *          Hilfer, J. (2002): H-function Representations for Stretched Exponential Relaxation and non-Debye
  86.  *              Susceptibilities in Glassy Systems <i>Physical Review E</i> <b>65 (6)</b> 061510
  87.  *      </li>
  88.  *      <li>
  89.  *          Wikipedia (2019): Stretched Exponential Function
  90.  *              https://en.wikipedia.org/wiki/Stretched_exponential_function
  91.  *      </li>
  92.  *      <li>
  93.  *          Wuttke, J. (2012): Laplace-Fourier Transform of the Stretched Exponential Function: Analytic
  94.  *              Error-Bounds, Double Exponential Transform, and Open Source Implementation <i>libkw</i>
  95.  *              <i>Algorithm</i> <b>5 (4)</b> 604-628
  96.  *      </li>
  97.  *      <li>
  98.  *          Zorn, R. (2002): Logarithmic Moments of Relaxation Time Distributions <i>Journal of Chemical
  99.  *              Physics</i> <b>116 (8)</b> 3204-3209
  100.  *      </li>
  101.  *  </ul>
  102.  *
  103.  *  <br><br>
  104.  *  <ul>
  105.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ComputationalCore.md">Computational Core Module</a></li>
  106.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/NumericalAnalysisLibrary.md">Numerical Analysis Library</a></li>
  107.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/measure/README.md">R<sup>d</sup> Continuous/Discrete Probability Measures</a></li>
  108.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/measure/continuous/README.md">R<sup>1</sup> and R<sup>d</sup> Continuous Random Measure</a></li>
  109.  *  </ul>
  110.  *
  111.  * @author Lakshmi Krishnamurthy
  112.  */

  113. public class R1UnivariateScaledExponential extends org.drip.measure.continuous.R1Univariate
  114. {
  115.     private double _normalizer = java.lang.Double.NaN;
  116.     private org.drip.function.definition.R1ToR1 _gammaEstimator = null;
  117.     private org.drip.specialfunction.definition.ScaledExponentialEstimator _scaledExponentialEstimator =
  118.         null;

  119.     /**
  120.      * UnivariateScaledExponential Constructor
  121.      *
  122.      * @param scaledExponentialEstimator Scaled Exponential Estimator
  123.      * @param gammaEstimator Gamma Estimator
  124.      *
  125.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  126.      */

  127.     public R1UnivariateScaledExponential (
  128.         final org.drip.specialfunction.definition.ScaledExponentialEstimator scaledExponentialEstimator,
  129.         final org.drip.function.definition.R1ToR1 gammaEstimator)
  130.         throws java.lang.Exception
  131.     {
  132.         if (null == (_scaledExponentialEstimator = scaledExponentialEstimator) ||
  133.             null == (_gammaEstimator = gammaEstimator))
  134.         {
  135.             throw new java.lang.Exception ("R1UnivariateScaledExponential Constructor => Invalid Inputs");
  136.         }

  137.         _normalizer = 1. / _gammaEstimator.evaluate (1. + (1. / _scaledExponentialEstimator.exponent())) /
  138.             _scaledExponentialEstimator.characteristicRelaxationTime();
  139.     }

  140.     /**
  141.      * Retrieve the Scaled Exponential Estimator
  142.      *
  143.      * @return Scaled Exponential Estimator
  144.      */

  145.     public org.drip.specialfunction.definition.ScaledExponentialEstimator scaledExponentialEstimator()
  146.     {
  147.         return _scaledExponentialEstimator;
  148.     }

  149.     /**
  150.      * Retrieve the Gamma Estimator
  151.      *
  152.      * @return Gamma Estimator
  153.      */

  154.     public org.drip.function.definition.R1ToR1 gammaEstimator()
  155.     {
  156.         return _gammaEstimator;
  157.     }

  158.     @Override public double[] support()
  159.     {
  160.         return new double[]
  161.         {
  162.             0.,
  163.             java.lang.Double.POSITIVE_INFINITY
  164.         };
  165.     }

  166.     @Override public double density (
  167.         final double t)
  168.         throws java.lang.Exception
  169.     {
  170.         return _scaledExponentialEstimator.evaluate (t) * _normalizer;
  171.     }

  172.     @Override public double cumulative (
  173.         final double t)
  174.         throws java.lang.Exception
  175.     {
  176.         if (!supported (t))
  177.         {
  178.             throw new java.lang.Exception ("R1UnivariateScaledExponential::cumulative => Invalid Inputs");
  179.         }

  180.         return org.drip.numerical.integration.NewtonCotesQuadratureGenerator.Zero_PlusOne (
  181.             0.,
  182.             t,
  183.             100
  184.         ).integrate (
  185.             new org.drip.function.definition.R1ToR1 (null)
  186.             {
  187.                 @Override public double evaluate (
  188.                     final double u)
  189.                     throws java.lang.Exception
  190.                 {
  191.                     return java.lang.Double.isInfinite (u) || 0. == u ? 0. :
  192.                         _scaledExponentialEstimator.evaluate (u);
  193.                 }
  194.             }
  195.         ) * _normalizer;
  196.     }

  197.     @Override public double incremental (
  198.         final double t1,
  199.         final double t2)
  200.         throws java.lang.Exception
  201.     {
  202.         if (!org.drip.numerical.common.NumberUtil.IsValid (t1) || 0. > t1 ||
  203.             !org.drip.numerical.common.NumberUtil.IsValid (t2) || t1 > t2)
  204.         {
  205.             throw new java.lang.Exception ("R1UnivariateScaledExponential::incremental => Invalid Inputs");
  206.         }

  207.         return org.drip.numerical.integration.NewtonCotesQuadratureGenerator.Zero_PlusOne (
  208.             t1,
  209.             t2,
  210.             100
  211.         ).integrate (
  212.             new org.drip.function.definition.R1ToR1 (null)
  213.             {
  214.                 @Override public double evaluate (
  215.                     final double u)
  216.                     throws java.lang.Exception
  217.                 {
  218.                     return java.lang.Double.isInfinite (u) || 0. == u ? 0. :
  219.                         _scaledExponentialEstimator.evaluate (u);
  220.                 }
  221.             }
  222.         ) * _normalizer;
  223.     }

  224.     @Override public double mean()
  225.         throws java.lang.Exception
  226.     {
  227.         return _scaledExponentialEstimator.firstMoment (_gammaEstimator) * _normalizer;
  228.     }

  229.     @Override public double variance()
  230.         throws java.lang.Exception
  231.     {
  232.         try
  233.         {
  234.             double mean = _scaledExponentialEstimator.firstMoment (_gammaEstimator) * _normalizer;

  235.             return _scaledExponentialEstimator.higherMoment (
  236.                 2,
  237.                 _gammaEstimator
  238.             ) * _normalizer * _normalizer - mean * mean;
  239.         }
  240.         catch (java.lang.Exception e)
  241.         {
  242.             e.printStackTrace();
  243.         }

  244.         return java.lang.Double.NaN;
  245.     }

  246.     /**
  247.      * Retrieve the Normalizer
  248.      *
  249.      * @return Normalizer
  250.      */

  251.     public double normalizer()
  252.     {
  253.         return _normalizer;
  254.     }

  255.     /**
  256.      * Retrieve the "Lambda" Parameter
  257.      *
  258.      * @return "Lambda" Parameter
  259.      */

  260.     public double lambda()
  261.     {
  262.         return 1. / _scaledExponentialEstimator.characteristicRelaxationTime();
  263.     }
  264. }