GammaDistribution.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      https://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */

  17. /*
  18.  * This is not the original file distributed by the Apache Software Foundation
  19.  * It has been modified by the Hipparchus project
  20.  */
  21. package org.hipparchus.distribution.continuous;

  22. import org.hipparchus.exception.LocalizedCoreFormats;
  23. import org.hipparchus.exception.MathIllegalArgumentException;
  24. import org.hipparchus.special.Gamma;
  25. import org.hipparchus.util.FastMath;

  26. /**
  27.  * Implementation of the Gamma distribution.
  28.  *
  29.  * @see <a href="http://en.wikipedia.org/wiki/Gamma_distribution">Gamma distribution (Wikipedia)</a>
  30.  * @see <a href="http://mathworld.wolfram.com/GammaDistribution.html">Gamma distribution (MathWorld)</a>
  31.  */
  32. public class GammaDistribution extends AbstractRealDistribution {
  33.     /** Serializable version identifier. */
  34.     private static final long serialVersionUID = 20120524L;
  35.     /** The shape parameter. */
  36.     private final double shape;
  37.     /** The scale parameter. */
  38.     private final double scale;
  39.     /**
  40.      * The constant value of {@code shape + g + 0.5}, where {@code g} is the
  41.      * Lanczos constant {@link Gamma#LANCZOS_G}.
  42.      */
  43.     private final double shiftedShape;
  44.     /**
  45.      * The constant value of
  46.      * {@code shape / scale * sqrt(e / (2 * pi * (shape + g + 0.5))) / L(shape)},
  47.      * where {@code L(shape)} is the Lanczos approximation returned by
  48.      * {@link Gamma#lanczos(double)}. This prefactor is used in
  49.      * {@link #density(double)}, when no overflow occurs with the natural
  50.      * calculation.
  51.      */
  52.     private final double densityPrefactor1;
  53.     /**
  54.      * The constant value of
  55.      * {@code log(shape / scale * sqrt(e / (2 * pi * (shape + g + 0.5))) / L(shape))},
  56.      * where {@code L(shape)} is the Lanczos approximation returned by
  57.      * {@link Gamma#lanczos(double)}. This prefactor is used in
  58.      * {@link #logDensity(double)}, when no overflow occurs with the natural
  59.      * calculation.
  60.      */
  61.     private final double logDensityPrefactor1;
  62.     /**
  63.      * The constant value of
  64.      * {@code shape * sqrt(e / (2 * pi * (shape + g + 0.5))) / L(shape)},
  65.      * where {@code L(shape)} is the Lanczos approximation returned by
  66.      * {@link Gamma#lanczos(double)}. This prefactor is used in
  67.      * {@link #density(double)}, when overflow occurs with the natural
  68.      * calculation.
  69.      */
  70.     private final double densityPrefactor2;
  71.     /**
  72.      * The constant value of
  73.      * {@code log(shape * sqrt(e / (2 * pi * (shape + g + 0.5))) / L(shape))},
  74.      * where {@code L(shape)} is the Lanczos approximation returned by
  75.      * {@link Gamma#lanczos(double)}. This prefactor is used in
  76.      * {@link #logDensity(double)}, when overflow occurs with the natural
  77.      * calculation.
  78.      */
  79.     private final double logDensityPrefactor2;
  80.     /**
  81.      * Lower bound on {@code y = x / scale} for the selection of the computation
  82.      * method in {@link #density(double)}. For {@code y <= minY}, the natural
  83.      * calculation overflows.
  84.      */
  85.     private final double minY;
  86.     /**
  87.      * Upper bound on {@code log(y)} ({@code y = x / scale}) for the selection
  88.      * of the computation method in {@link #density(double)}. For
  89.      * {@code log(y) >= maxLogY}, the natural calculation overflows.
  90.      */
  91.     private final double maxLogY;

  92.     /**
  93.      * Creates a new gamma distribution with specified values of the shape and
  94.      * scale parameters.
  95.      *
  96.      * @param shape the shape parameter
  97.      * @param scale the scale parameter
  98.      * @throws MathIllegalArgumentException if {@code shape <= 0} or
  99.      * {@code scale <= 0}.
  100.      */
  101.     public GammaDistribution(double shape, double scale) throws MathIllegalArgumentException {
  102.         this(shape, scale, DEFAULT_SOLVER_ABSOLUTE_ACCURACY);
  103.     }


  104.     /**
  105.      * Creates a Gamma distribution.
  106.      *
  107.      * @param shape the shape parameter
  108.      * @param scale the scale parameter
  109.      * @param inverseCumAccuracy the maximum absolute error in inverse
  110.      * cumulative probability estimates (defaults to
  111.      * {@link #DEFAULT_SOLVER_ABSOLUTE_ACCURACY}).
  112.      * @throws MathIllegalArgumentException if {@code shape <= 0} or
  113.      * {@code scale <= 0}.
  114.      */
  115.     public GammaDistribution(final double shape,
  116.                              final double scale,
  117.                              final double inverseCumAccuracy)
  118.         throws MathIllegalArgumentException {
  119.         super(inverseCumAccuracy);

  120.         if (shape <= 0) {
  121.             throw new MathIllegalArgumentException(LocalizedCoreFormats.SHAPE, shape);
  122.         }
  123.         if (scale <= 0) {
  124.             throw new MathIllegalArgumentException(LocalizedCoreFormats.SCALE, scale);
  125.         }

  126.         this.shape = shape;
  127.         this.scale = scale;
  128.         this.shiftedShape = shape + Gamma.LANCZOS_G + 0.5;
  129.         final double aux = FastMath.E / (2.0 * FastMath.PI * shiftedShape);
  130.         this.densityPrefactor2 = shape * FastMath.sqrt(aux) / Gamma.lanczos(shape);
  131.         this.logDensityPrefactor2 = FastMath.log(shape) + 0.5 * FastMath.log(aux) -
  132.                                     FastMath.log(Gamma.lanczos(shape));
  133.         this.densityPrefactor1 = this.densityPrefactor2 / scale *
  134.                 FastMath.pow(shiftedShape, -shape) *
  135.                 FastMath.exp(shape + Gamma.LANCZOS_G);
  136.         this.logDensityPrefactor1 = this.logDensityPrefactor2 - FastMath.log(scale) -
  137.                 FastMath.log(shiftedShape) * shape +
  138.                 shape + Gamma.LANCZOS_G;
  139.         this.minY = shape + Gamma.LANCZOS_G - FastMath.log(Double.MAX_VALUE);
  140.         this.maxLogY = FastMath.log(Double.MAX_VALUE) / (shape - 1.0);
  141.     }

  142.     /**
  143.      * Returns the shape parameter of {@code this} distribution.
  144.      *
  145.      * @return the shape parameter
  146.      */
  147.     public double getShape() {
  148.         return shape;
  149.     }

  150.     /**
  151.      * Returns the scale parameter of {@code this} distribution.
  152.      *
  153.      * @return the scale parameter
  154.      */
  155.     public double getScale() {
  156.         return scale;
  157.     }

  158.     /** {@inheritDoc} */
  159.     @Override
  160.     public double density(double x) {
  161.        /* The present method must return the value of
  162.         *
  163.         *     1       x a     - x
  164.         * ---------- (-)  exp(---)
  165.         * x Gamma(a)  b        b
  166.         *
  167.         * where a is the shape parameter, and b the scale parameter.
  168.         * Substituting the Lanczos approximation of Gamma(a) leads to the
  169.         * following expression of the density
  170.         *
  171.         * a              e            1         y      a
  172.         * - sqrt(------------------) ---- (-----------)  exp(a - y + g),
  173.         * x      2 pi (a + g + 0.5)  L(a)  a + g + 0.5
  174.         *
  175.         * where y = x / b. The above formula is the "natural" computation, which
  176.         * is implemented when no overflow is likely to occur. If overflow occurs
  177.         * with the natural computation, the following identity is used. It is
  178.         * based on the BOOST library
  179.         * http://www.boost.org/doc/libs/1_35_0/libs/math/doc/sf_and_dist/html/math_toolkit/special/sf_gamma/igamma.html
  180.         * Formula (15) needs adaptations, which are detailed below.
  181.         *
  182.         *       y      a
  183.         * (-----------)  exp(a - y + g)
  184.         *  a + g + 0.5
  185.         *                              y - a - g - 0.5    y (g + 0.5)
  186.         *               = exp(a log1pm(---------------) - ----------- + g),
  187.         *                                a + g + 0.5      a + g + 0.5
  188.         *
  189.         *  where log1pm(z) = log(1 + z) - z. Therefore, the value to be
  190.         *  returned is
  191.         *
  192.         * a              e            1
  193.         * - sqrt(------------------) ----
  194.         * x      2 pi (a + g + 0.5)  L(a)
  195.         *                              y - a - g - 0.5    y (g + 0.5)
  196.         *               * exp(a log1pm(---------------) - ----------- + g).
  197.         *                                a + g + 0.5      a + g + 0.5
  198.         */
  199.         if (x < 0) {
  200.             return 0;
  201.         }
  202.         final double y = x / scale;
  203.         if ((y <= minY) || (FastMath.log(y) >= maxLogY)) {
  204.             /*
  205.              * Overflow.
  206.              */
  207.             final double aux1 = (y - shiftedShape) / shiftedShape;
  208.             final double aux2 = shape * (FastMath.log1p(aux1) - aux1);
  209.             final double aux3 = -y * (Gamma.LANCZOS_G + 0.5) / shiftedShape +
  210.                     Gamma.LANCZOS_G + aux2;
  211.             return densityPrefactor2 / x * FastMath.exp(aux3);
  212.         }
  213.         /*
  214.          * Natural calculation.
  215.          */
  216.         return densityPrefactor1 * FastMath.exp(-y) * FastMath.pow(y, shape - 1);
  217.     }

  218.     /** {@inheritDoc} **/
  219.     @Override
  220.     public double logDensity(double x) {
  221.         /*
  222.          * see the comment in {@link #density(double)} for computation details
  223.          */
  224.         if (x < 0) {
  225.             return Double.NEGATIVE_INFINITY;
  226.         }
  227.         final double y = x / scale;
  228.         if ((y <= minY) || (FastMath.log(y) >= maxLogY)) {
  229.             /*
  230.              * Overflow.
  231.              */
  232.             final double aux1 = (y - shiftedShape) / shiftedShape;
  233.             final double aux2 = shape * (FastMath.log1p(aux1) - aux1);
  234.             final double aux3 = -y * (Gamma.LANCZOS_G + 0.5) / shiftedShape +
  235.                     Gamma.LANCZOS_G + aux2;
  236.             return logDensityPrefactor2 - FastMath.log(x) + aux3;
  237.         }
  238.         /*
  239.          * Natural calculation.
  240.          */
  241.         return logDensityPrefactor1 - y + FastMath.log(y) * (shape - 1);
  242.     }

  243.     /**
  244.      * {@inheritDoc}
  245.      *
  246.      * The implementation of this method is based on:
  247.      * <ul>
  248.      *  <li>
  249.      *   <a href="http://mathworld.wolfram.com/Chi-SquaredDistribution.html">
  250.      *    Chi-Squared Distribution</a>, equation (9).
  251.      *  </li>
  252.      *  <li>Casella, G., &amp; Berger, R. (1990). <i>Statistical Inference</i>.
  253.      *    Belmont, CA: Duxbury Press.
  254.      *  </li>
  255.      * </ul>
  256.      */
  257.     @Override
  258.     public double cumulativeProbability(double x) {
  259.         double ret;

  260.         if (x <= 0) {
  261.             ret = 0;
  262.         } else {
  263.             ret = Gamma.regularizedGammaP(shape, x / scale);
  264.         }

  265.         return ret;
  266.     }

  267.     /**
  268.      * {@inheritDoc}
  269.      *
  270.      * For shape parameter {@code alpha} and scale parameter {@code beta}, the
  271.      * mean is {@code alpha * beta}.
  272.      */
  273.     @Override
  274.     public double getNumericalMean() {
  275.         return shape * scale;
  276.     }

  277.     /**
  278.      * {@inheritDoc}
  279.      *
  280.      * For shape parameter {@code alpha} and scale parameter {@code beta}, the
  281.      * variance is {@code alpha * beta^2}.
  282.      *
  283.      * @return {@inheritDoc}
  284.      */
  285.     @Override
  286.     public double getNumericalVariance() {
  287.         return shape * scale * scale;
  288.     }

  289.     /**
  290.      * {@inheritDoc}
  291.      *
  292.      * The lower bound of the support is always 0 no matter the parameters.
  293.      *
  294.      * @return lower bound of the support (always 0)
  295.      */
  296.     @Override
  297.     public double getSupportLowerBound() {
  298.         return 0;
  299.     }

  300.     /**
  301.      * {@inheritDoc}
  302.      *
  303.      * The upper bound of the support is always positive infinity
  304.      * no matter the parameters.
  305.      *
  306.      * @return upper bound of the support (always Double.POSITIVE_INFINITY)
  307.      */
  308.     @Override
  309.     public double getSupportUpperBound() {
  310.         return Double.POSITIVE_INFINITY;
  311.     }

  312.     /**
  313.      * {@inheritDoc}
  314.      *
  315.      * The support of this distribution is connected.
  316.      *
  317.      * @return {@code true}
  318.      */
  319.     @Override
  320.     public boolean isSupportConnected() {
  321.         return true;
  322.     }
  323. }