<commit_before>/*
 * GammaDistribution.java
 *
 * Copyright (C) 2002-2006 <NAME> and <NAME>
 *
 * This file is part of BEAST.
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership and licensing.
 *
 * BEAST is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 *  BEAST is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with BEAST; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA  02110-1301  USA
 */

package dr.math.distributions;

import dr.math.GammaFunction;
import dr.math.UnivariateFunction;

/**
 * inverse gamma distribution.
 * <p/>
 * (Parameters: shape, scale; mean: ??; variance: ??)
 *
 * @author <NAME>
 * @version $Id$
 */
public class InverseGammaDistribution implements Distribution {

    private double shape, scale;

    private final double factor;
    private final double logFacor;

    public InverseGammaDistribution(double shape, double scale) {
        this.shape = shape;
        this.scale = scale;
        this.factor = Math.pow(scale, shape) / Math.exp(GammaFunction.lnGamma(shape));
        this.logFacor = shape * Math.log(scale) - GammaFunction.lnGamma(shape);
    }

    public double getShape() {
        return shape;
    }

    public void setShape(double value) {
        shape = value;
    }

    public double getScale() {
        return scale;
    }

    public void setScale(double value) {
        scale = value;
    }

    public double pdf(double x) {
        return pdf(x, shape, scale, factor);
    }

    public double logPdf(double x) {
        return logPdf(x, shape, scale, logFacor);
    }

    public double cdf(double x) {
        return cdf(x, shape, scale);
    }

    public double quantile(double y) {
        return quantile(y, shape, scale);
    }

    public double mean() {
        return mean(shape, scale);
    }

    public double variance() {
        return variance(shape, scale);
    }

    public double nextInverseGamma() {
        return nextInverseGamma(shape, scale);
    }

    public final UnivariateFunction getProbabilityDensityFunction() {
        return pdfFunction;
    }

    private final UnivariateFunction pdfFunction = new UnivariateFunction() {
        public final double evaluate(double x) {
            return pdf(x);
        }

        public final double getLowerBound() {
            return 0.0;
        }

        public final double getUpperBound() {
            return Double.POSITIVE_INFINITY;
        }
    };

    /**
     * probability density function of the Gamma distribution
     *
     * @param x     argument
     * @param shape shape parameter
     * @param scale scale parameter
     * @return pdf value
     */
    static double pdf(double x, double shape, double scale, double factor) {
        if (x <= 0)
            throw new IllegalArgumentException();

        final double a = Math.exp(-scale/x) / Math.pow(x, shape+1);

        return factor * a;
    }

    /**
     * the natural log of the probability density function of the distribution
     *
     * @param x     argument
     * @param shape shape parameter
     * @param scale scale parameter
     * @return log pdf value
     */
    static double logPdf(double x, double shape, double scale, double factor) {

        if (x <= 0)
            return Double.NEGATIVE_INFINITY;

        return  factor - (scale/x + (shape+1)*Math.log(x));
    }

    /**
     * cumulative density function of the Gamma distribution
     *
     * @param x     argument
     * @param shape shape parameter
     * @param scale scale parameter
     * @return cdf value
     */
    public static double cdf(double x, double shape, double scale) {
        return GammaFunction.incompleteGammaQ(shape, scale/x);
        //throw new RuntimeException("not implemented");
        //return GammaFunction.incompleteGammaP(shape, x / scale);
    }

    /**
     * quantile (inverse cumulative density function) of the Gamma distribution
     *
     * @param y     argument
     * @param shape shape parameter
     * @param scale scale parameter
     * @return icdf value
     */
    public static double quantile(double y, double shape, double scale) {
         throw new RuntimeException("not implemented");
        //return 0.5 * scale * pointChi2(y, 2.0 * shape);
    }

    /**<commit_msg>maybe correct code<commit_after>/*
 * GammaDistribution.java
 *
 * Copyright (C) 2002-2006 <NAME> and <NAME>
 *
 * This file is part of BEAST.
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership and licensing.
 *
 * BEAST is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 *  BEAST is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with BEAST; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA  02110-1301  USA
 */

package dr.math.distributions;

import dr.math.GammaFunction;
import dr.math.UnivariateFunction;

/**
 * inverse gamma distribution.
 * <p/>
 * (Parameters: shape, scale; mean: ??; variance: ??)
 *
 * @author <NAME>
 * @version $Id$
 */
public class InverseGammaDistribution implements Distribution {

    private double shape, scale;

    private final double factor;
    private final double logFacor;

    public InverseGammaDistribution(double shape, double scale) {
        this.shape = shape;
        this.scale = scale;
        this.factor = Math.pow(scale, shape) / Math.exp(GammaFunction.lnGamma(shape));
        this.logFacor = shape * Math.log(scale) - GammaFunction.lnGamma(shape);
    }

    public double getShape() {
        return shape;
    }

    public void setShape(double value) {
        shape = value;
    }

    public double getScale() {
        return scale;
    }

    public void setScale(double value) {
        scale = value;
    }

    public double pdf(double x) {
        return pdf(x, shape, scale, factor);
    }

    public double logPdf(double x) {
        return logPdf(x, shape, scale, logFacor);
    }

    public double cdf(double x) {
        return cdf(x, shape, scale);
    }

    public double quantile(double y) {
        return quantile(y, shape, scale);
    }

    public double mean() {
        return mean(shape, scale);
    }

    public double variance() {
        return variance(shape, scale);
    }

    public double nextInverseGamma() {
        return nextInverseGamma(shape, scale);
    }

    public final UnivariateFunction getProbabilityDensityFunction() {
        return pdfFunction;
    }

    private final UnivariateFunction pdfFunction = new UnivariateFunction() {
        public final double evaluate(double x) {
            return pdf(x);
        }

        public final double getLowerBound() {
            return 0.0;
        }

        public final double getUpperBound() {
            return Double.POSITIVE_INFINITY;
        }
    };

    /**
     * probability density function of the Gamma distribution
     *
     * @param x     argument
     * @param shape shape parameter
     * @param scale scale parameter
     * @return pdf value
     */
    static double pdf(double x, double shape, double scale, double factor) {
        if (x <= 0)
            throw new IllegalArgumentException();

        final double a = Math.exp(-scale/x) / Math.pow(x, shape+1);

        return factor * a;
    }

    /**
     * the natural log of the probability density function of the distribution
     *
     * @param x     argument
     * @param shape shape parameter
     * @param scale scale parameter
     * @return log pdf value
     */
    static double logPdf(double x, double shape, double scale, double factor) {

        if (x <= 0)
            return Double.NEGATIVE_INFINITY;

        return  factor - (scale/x + (shape+1)*Math.log(x));
    }

    /**
     * cumulative density function of the Gamma distribution
     *
     * @param x     argument
     * @param shape shape parameter
     * @param scale scale parameter
     * @return cdf value
     */
    public static double cdf(double x, double shape, double scale) {
        return GammaFunction.incompleteGammaQ(shape, scale/x);
        //throw new RuntimeException("not implemented");
        //return GammaFunction.incompleteGammaP(shape, x / scale);
    }

    /**
     * quantile (inverse cumulative density function) of the Gamma distribution
     *
     * @param y     argument
     * @param shape shape parameter
     * @param scale scale parameter
     * @return icdf value
     */
    public static double quantile(double y, double shape, double scale) {
        final GammaDistribution g = new GammaDistribution(shape, scale);
        return 1/g.quantile(1-y);

        // throw new RuntimeException("not implemented");
        //return 0.5 * scale * pointChi2(y, 2.0 * shape);
    }

    /**
{'fraction_non_alphanumeric': 0.0885946001437224, 'fraction_numerical': 0.006570167333949287, 'mean_word_length': 2.471846044191019, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3628934', 'n_tokens_mistral': 2723, 'n_tokens_neox': 2570, 'n_words': 960}