R1NonCentralCumulantInvariant.java

  1. package org.drip.measure.chisquare;

  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>R1NonCentralCumulantInvariant</i> implements the Cumulant Invariant Transformation for the
  76.  *  R<sup>1</sup> Non-central Chi-Square Distribution. The References are:
  77.  *
  78.  * <br><br>
  79.  *  <ul>
  80.  *      <li>
  81.  *          Johnson, N. L., S. Kotz, and N. Balakrishnan (1995): <i>Continuous Univariate Distributions
  82.  *              2<sup>nd</sup> Edition</i> <b>John Wiley and Sons</b>
  83.  *      </li>
  84.  *      <li>
  85.  *          Muirhead, R. (2005): <i>Aspects of Multivariate Statistical Theory 2<sup>nd</sup> Edition</i>
  86.  *              <b>Wiley</b>
  87.  *      </li>
  88.  *      <li>
  89.  *          Non-central Chi-Squared Distribution (2019): Chi-Squared Function
  90.  *              https://en.wikipedia.org/wiki/Noncentral_chi-squared_distribution
  91.  *      </li>
  92.  *      <li>
  93.  *          Sankaran, M. (1963): Approximations to the Non-Central Chi-Square Distribution <i>Biometrika</i>
  94.  *              <b>50 (1-2)</b> 199-204
  95.  *      </li>
  96.  *      <li>
  97.  *          Young, D. S. (2010): tolerance: An R Package for Estimating Tolerance Intervals <i>Journal of
  98.  *              Statistical Software</i> <b>36 (5)</b> 1-39
  99.  *      </li>
  100.  *  </ul>
  101.  *
  102.  *  <br><br>
  103.  *  <ul>
  104.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ComputationalCore.md">Computational Core Module</a></li>
  105.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/NumericalAnalysisLibrary.md">Numerical Analysis Library</a></li>
  106.  *      <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>
  107.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/measure/chisquare/README.md">Chi-Square Distribution Implementation/Properties</a></li>
  108.  *  </ul>
  109.  *
  110.  * @author Lakshmi Krishnamurthy
  111.  */

  112. public class R1NonCentralCumulantInvariant
  113.     extends org.drip.measure.chisquare.R1NonCentral
  114. {
  115.     private double _sankaranB = java.lang.Double.NaN;

  116.     private double transform (
  117.         final double x)
  118.     {
  119.         org.drip.measure.chisquare.R1NonCentralParameters r1NonCentralParameters = parameters();

  120.         return java.lang.Math.sqrt (
  121.             (
  122.                 x - _sankaranB
  123.             ) / (
  124.                 r1NonCentralParameters.degreesOfFreedom() + r1NonCentralParameters.nonCentralityParameter()
  125.             )
  126.         );
  127.     }

  128.     private double inverseTransform (
  129.         final double y)
  130.     {
  131.         org.drip.measure.chisquare.R1NonCentralParameters r1NonCentralParameters = parameters();

  132.         return y * y * (
  133.             r1NonCentralParameters.degreesOfFreedom() + r1NonCentralParameters.nonCentralityParameter()
  134.         ) + _sankaranB;
  135.     }

  136.     /**
  137.      * Construct the Second Cumulant Invariant Instance of R1NonCentralCumulantInvariant
  138.      *
  139.      * @param degreesOfFreedom Degrees of Freedom
  140.      * @param nonCentralityParameter Non-centrality Parameter
  141.      * @param gammaEstimator Gamma Estimator
  142.      * @param digammaEstimator Digamma Estimator
  143.      * @param lowerIncompleteGammaEstimator Lower Incomplete Gamma Estimator
  144.      * @param modifiedBesselFirstKindEstimator Modified Bessel First Kind Estimator
  145.      *
  146.      * @return The Second Cumulant Invariant Instance of R1NonCentralCumulantInvariant
  147.      */

  148.     public static final R1NonCentralCumulantInvariant InvariantSecondCumulant (
  149.         final int degreesOfFreedom,
  150.         final double nonCentralityParameter,
  151.         final org.drip.function.definition.R1ToR1 gammaEstimator,
  152.         final org.drip.function.definition.R1ToR1 digammaEstimator,
  153.         final org.drip.function.definition.R2ToR1 lowerIncompleteGammaEstimator,
  154.         final org.drip.specialfunction.definition.ModifiedBesselFirstKindEstimator
  155.             modifiedBesselFirstKindEstimator)
  156.     {
  157.         try
  158.         {
  159.             return new R1NonCentralCumulantInvariant (
  160.                 new org.drip.measure.chisquare.R1NonCentralParameters (
  161.                     degreesOfFreedom,
  162.                     nonCentralityParameter
  163.                 ),
  164.                 gammaEstimator,
  165.                 digammaEstimator,
  166.                 lowerIncompleteGammaEstimator,
  167.                 modifiedBesselFirstKindEstimator,
  168.                 (degreesOfFreedom - 1.) / 2.
  169.             );
  170.         }
  171.         catch (java.lang.Exception e)
  172.         {
  173.             e.printStackTrace();
  174.         }

  175.         return null;
  176.     }

  177.     /**
  178.      * Construct the Third Cumulant Invariant Instance of R1NonCentralCumulantInvariant
  179.      *
  180.      * @param degreesOfFreedom Degrees of Freedom
  181.      * @param nonCentralityParameter Non-centrality Parameter
  182.      * @param gammaEstimator Gamma Estimator
  183.      * @param digammaEstimator Digamma Estimator
  184.      * @param lowerIncompleteGammaEstimator Lower Incomplete Gamma Estimator
  185.      * @param modifiedBesselFirstKindEstimator Modified Bessel First Kind Estimator
  186.      *
  187.      * @return The Third Cumulant Invariant Instance of R1NonCentralCumulantInvariant
  188.      */

  189.     public static final R1NonCentralCumulantInvariant InvariantThirdCumulant (
  190.         final int degreesOfFreedom,
  191.         final double nonCentralityParameter,
  192.         final org.drip.function.definition.R1ToR1 gammaEstimator,
  193.         final org.drip.function.definition.R1ToR1 digammaEstimator,
  194.         final org.drip.function.definition.R2ToR1 lowerIncompleteGammaEstimator,
  195.         final org.drip.specialfunction.definition.ModifiedBesselFirstKindEstimator
  196.             modifiedBesselFirstKindEstimator)
  197.     {
  198.         try
  199.         {
  200.             return new R1NonCentralCumulantInvariant (
  201.                 new org.drip.measure.chisquare.R1NonCentralParameters (
  202.                     degreesOfFreedom,
  203.                     nonCentralityParameter
  204.                 ),
  205.                 gammaEstimator,
  206.                 digammaEstimator,
  207.                 lowerIncompleteGammaEstimator,
  208.                 modifiedBesselFirstKindEstimator,
  209.                 (degreesOfFreedom - 1.) / 3.
  210.             );
  211.         }
  212.         catch (java.lang.Exception e)
  213.         {
  214.             e.printStackTrace();
  215.         }

  216.         return null;
  217.     }

  218.     /**
  219.      * Construct the Fourth Cumulant Invariant Instance of R1NonCentralCumulantInvariant
  220.      *
  221.      * @param degreesOfFreedom Degrees of Freedom
  222.      * @param nonCentralityParameter Non-centrality Parameter
  223.      * @param gammaEstimator Gamma Estimator
  224.      * @param digammaEstimator Digamma Estimator
  225.      * @param lowerIncompleteGammaEstimator Lower Incomplete Gamma Estimator
  226.      * @param modifiedBesselFirstKindEstimator Modified Bessel First Kind Estimator
  227.      *
  228.      * @return The Fourth Cumulant Invariant Instance of R1NonCentralCumulantInvariant
  229.      */

  230.     public static final R1NonCentralCumulantInvariant InvariantFourthCumulant (
  231.         final int degreesOfFreedom,
  232.         final double nonCentralityParameter,
  233.         final org.drip.function.definition.R1ToR1 gammaEstimator,
  234.         final org.drip.function.definition.R1ToR1 digammaEstimator,
  235.         final org.drip.function.definition.R2ToR1 lowerIncompleteGammaEstimator,
  236.         final org.drip.specialfunction.definition.ModifiedBesselFirstKindEstimator
  237.             modifiedBesselFirstKindEstimator)
  238.     {
  239.         try
  240.         {
  241.             return new R1NonCentralCumulantInvariant (
  242.                 new org.drip.measure.chisquare.R1NonCentralParameters (
  243.                     degreesOfFreedom,
  244.                     nonCentralityParameter
  245.                 ),
  246.                 gammaEstimator,
  247.                 digammaEstimator,
  248.                 lowerIncompleteGammaEstimator,
  249.                 modifiedBesselFirstKindEstimator,
  250.                 (degreesOfFreedom - 1.) / 4.
  251.             );
  252.         }
  253.         catch (java.lang.Exception e)
  254.         {
  255.             e.printStackTrace();
  256.         }

  257.         return null;
  258.     }

  259.     /**
  260.      * R1NonCentralCumulantInvariant Constructor
  261.      *
  262.      * @param r1NonCentralParameters R<sup>1</sup> Non-central Parameters
  263.      * @param gammaEstimator Gamma Estimator
  264.      * @param digammaEstimator Digamma Estimator
  265.      * @param lowerIncompleteGammaEstimator Lower Incomplete Gamma Estimator
  266.      * @param modifiedBesselFirstKindEstimator Modified Bessel First Kind Estimator
  267.      * @param sankaranB Sankaran (1963) "B"
  268.      *
  269.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  270.      */

  271.     public R1NonCentralCumulantInvariant (
  272.         final org.drip.measure.chisquare.R1NonCentralParameters r1NonCentralParameters,
  273.         final org.drip.function.definition.R1ToR1 gammaEstimator,
  274.         final org.drip.function.definition.R1ToR1 digammaEstimator,
  275.         final org.drip.function.definition.R2ToR1 lowerIncompleteGammaEstimator,
  276.         final org.drip.specialfunction.definition.ModifiedBesselFirstKindEstimator
  277.             modifiedBesselFirstKindEstimator,
  278.         final double sankaranB)
  279.         throws java.lang.Exception
  280.     {
  281.         super (
  282.             r1NonCentralParameters,
  283.             gammaEstimator,
  284.             digammaEstimator,
  285.             lowerIncompleteGammaEstimator,
  286.             modifiedBesselFirstKindEstimator
  287.         );

  288.         if (!org.drip.numerical.common.NumberUtil.IsValid (
  289.             _sankaranB = sankaranB
  290.         ))
  291.         {
  292.             throw new java.lang.Exception (
  293.                 "R1NonCentralCumulantInvariant Constructor => Invalid Inputs"
  294.             );
  295.         }
  296.     }

  297.     /**
  298.      * Retrieve the Sankaran (1963) "B"
  299.      *
  300.      * @return Sankaran (1963) "B"
  301.      */

  302.     public double sankaranB()
  303.     {
  304.         return _sankaranB;
  305.     }

  306.     @Override public double density (
  307.         final double x)
  308.         throws java.lang.Exception
  309.     {
  310.         return super.density (
  311.             transform (
  312.                 x
  313.             )
  314.         );
  315.     }

  316.     @Override public double cumulative (
  317.         final double x)
  318.         throws java.lang.Exception
  319.     {
  320.         return super.cumulative (
  321.             transform (
  322.                 x
  323.             )
  324.         );
  325.     }

  326.     @Override public double invCumulative (
  327.         final double y)
  328.         throws java.lang.Exception
  329.     {
  330.         return inverseTransform (
  331.             super.invCumulative (
  332.                 y
  333.             )
  334.         );
  335.     }
  336. }