All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.commons.math3.distribution.GammaDistribution Maven / Gradle / Ivy

Go to download

The Apache Commons Math project is a library of lightweight, self-contained mathematics and statistics components addressing the most common practical problems not immediately available in the Java programming language or commons-lang.

There is a newer version: 3.6.1
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.commons.math3.distribution;

import org.apache.commons.math3.exception.NotStrictlyPositiveException;
import org.apache.commons.math3.exception.util.LocalizedFormats;
import org.apache.commons.math3.special.Gamma;
import org.apache.commons.math3.util.FastMath;

/**
 * Implementation of the Gamma distribution.
 *
 * @see Gamma distribution (Wikipedia)
 * @see Gamma distribution (MathWorld)
 * @version $Id: GammaDistribution.java 1244107 2012-02-14 16:17:55Z erans $
 */
public class GammaDistribution extends AbstractRealDistribution {
    /**
     * Default inverse cumulative probability accuracy.
     * @since 2.1
     */
    public static final double DEFAULT_INVERSE_ABSOLUTE_ACCURACY = 1e-9;
    /** Serializable version identifier. */
    private static final long serialVersionUID = -3239549463135430361L;
    /** The shape parameter. */
    private final double alpha;
    /** The scale parameter. */
    private final double beta;
    /** Inverse cumulative probability accuracy. */
    private final double solverAbsoluteAccuracy;

    /**
     * Create a new gamma distribution with the given {@code alpha} and
     * {@code beta} values.
     * @param alpha the shape parameter.
     * @param beta the scale parameter.
     */
    public GammaDistribution(double alpha, double beta) {
        this(alpha, beta, DEFAULT_INVERSE_ABSOLUTE_ACCURACY);
    }

    /**
     * Create a new gamma distribution with the given {@code alpha} and
     * {@code beta} values.
     *
     * @param alpha Shape parameter.
     * @param beta Scale parameter.
     * @param inverseCumAccuracy Maximum absolute error in inverse
     * cumulative probability estimates (defaults to
     * {@link #DEFAULT_INVERSE_ABSOLUTE_ACCURACY}).
     * @throws NotStrictlyPositiveException if {@code alpha <= 0} or
     * {@code beta <= 0}.
     * @since 2.1
     */
    public GammaDistribution(double alpha, double beta, double inverseCumAccuracy)
        throws NotStrictlyPositiveException {
        if (alpha <= 0) {
            throw new NotStrictlyPositiveException(LocalizedFormats.ALPHA, alpha);
        }
        if (beta <= 0) {
            throw new NotStrictlyPositiveException(LocalizedFormats.BETA, beta);
        }

        this.alpha = alpha;
        this.beta = beta;
        solverAbsoluteAccuracy = inverseCumAccuracy;
    }

    /**
     * Access the {@code alpha} shape parameter.
     *
     * @return {@code alpha}.
     */
    public double getAlpha() {
        return alpha;
    }

    /**
     * Access the {@code beta} scale parameter.
     *
     * @return {@code beta}.
     */
    public double getBeta() {
        return beta;
    }

    /**
     * {@inheritDoc}
     *
     * For this distribution {@code P(X = x)} always evaluates to 0.
     *
     * @return 0
     */
    public double probability(double x) {
        return 0.0;
    }

    /** {@inheritDoc} */
    public double density(double x) {
        if (x < 0) {
            return 0;
        }
        return FastMath.pow(x / beta, alpha - 1) / beta *
               FastMath.exp(-x / beta) / FastMath.exp(Gamma.logGamma(alpha));
    }

    /**
     * {@inheritDoc}
     *
     * The implementation of this method is based on:
     * 
    *
  • * * Chi-Squared Distribution, equation (9). *
  • *
  • Casella, G., & Berger, R. (1990). Statistical Inference. * Belmont, CA: Duxbury Press. *
  • *
*/ public double cumulativeProbability(double x) { double ret; if (x <= 0) { ret = 0; } else { ret = Gamma.regularizedGammaP(alpha, x / beta); } return ret; } /** {@inheritDoc} */ @Override protected double getSolverAbsoluteAccuracy() { return solverAbsoluteAccuracy; } /** * {@inheritDoc} * * For shape parameter {@code alpha} and scale parameter {@code beta}, the * mean is {@code alpha * beta}. */ public double getNumericalMean() { return getAlpha() * getBeta(); } /** * {@inheritDoc} * * For shape parameter {@code alpha} and scale parameter {@code beta}, the * variance is {@code alpha * beta^2}. * * @return {@inheritDoc} */ public double getNumericalVariance() { final double b = getBeta(); return getAlpha() * b * b; } /** * {@inheritDoc} * * The lower bound of the support is always 0 no matter the parameters. * * @return lower bound of the support (always 0) */ public double getSupportLowerBound() { return 0; } /** * {@inheritDoc} * * The upper bound of the support is always positive infinity * no matter the parameters. * * @return upper bound of the support (always Double.POSITIVE_INFINITY) */ public double getSupportUpperBound() { return Double.POSITIVE_INFINITY; } /** {@inheritDoc} */ public boolean isSupportLowerBoundInclusive() { return true; } /** {@inheritDoc} */ public boolean isSupportUpperBoundInclusive() { return false; } /** * {@inheritDoc} * * The support of this distribution is connected. * * @return {@code true} */ public boolean isSupportConnected() { return true; } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy