| 1 | /* |
| 2 | * ChiSquareDistribution.java |
| 3 | * |
| 4 | * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard |
| 5 | * |
| 6 | * This file is part of BEAST. |
| 7 | * See the NOTICE file distributed with this work for additional |
| 8 | * information regarding copyright ownership and licensing. |
| 9 | * |
| 10 | * BEAST is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU Lesser General Public License as |
| 12 | * published by the Free Software Foundation; either version 2 |
| 13 | * of the License, or (at your option) any later version. |
| 14 | * |
| 15 | * BEAST is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU Lesser General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU Lesser General Public |
| 21 | * License along with BEAST; if not, write to the |
| 22 | * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, |
| 23 | * Boston, MA 02110-1301 USA |
| 24 | */ |
| 25 | |
| 26 | package dr.math.distributions; |
| 27 | |
| 28 | /** |
| 29 | * chi-square distribution |
| 30 | * (distribution of sum of squares of n N(0,1) random variables) |
| 31 | * <p/> |
| 32 | * (Parameter: n; mean: n; variance: 2*n) |
| 33 | * <p/> |
| 34 | * The chi-square distribution is a special case of the Gamma distribution |
| 35 | * (shape parameter = n/2.0, scale = 2.0). |
| 36 | * |
| 37 | * @author Korbinian Strimmer |
| 38 | * @version $Id: ChiSquareDistribution.java,v 1.3 2005/05/24 20:26:00 rambaut Exp $ |
| 39 | */ |
| 40 | public class ChiSquareDistribution extends GammaDistribution { |
| 41 | // |
| 42 | // Public stuff |
| 43 | // |
| 44 | |
| 45 | /** |
| 46 | * Constructor |
| 47 | */ |
| 48 | public ChiSquareDistribution(double n) { |
| 49 | super(n / 2.0, 2.0); |
| 50 | this.n = n; |
| 51 | } |
| 52 | |
| 53 | public double pdf(double x) { |
| 54 | return pdf(x, n); |
| 55 | } |
| 56 | |
| 57 | public double cdf(double x) { |
| 58 | return cdf(x, n); |
| 59 | } |
| 60 | |
| 61 | public double quantile(double y) { |
| 62 | return quantile(y, n); |
| 63 | } |
| 64 | |
| 65 | public double mean() { |
| 66 | return mean(n); |
| 67 | } |
| 68 | |
| 69 | public double variance() { |
| 70 | return variance(n); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * probability density function of the chi-square distribution |
| 75 | * |
| 76 | * @param x argument |
| 77 | * @param n degrees of freedom |
| 78 | * @return pdf value |
| 79 | */ |
| 80 | public static double pdf(double x, double n) { |
| 81 | return pdf(x, n / 2.0, 2.0); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * cumulative density function of the chi-square distribution |
| 86 | * |
| 87 | * @param x argument |
| 88 | * @param n degrees of freedom |
| 89 | * @return cdf value |
| 90 | */ |
| 91 | public static double cdf(double x, double n) { |
| 92 | return cdf(x, n / 2.0, 2.0); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * quantile (inverse cumulative density function) of the chi-square distribution |
| 97 | * |
| 98 | * @param y argument |
| 99 | * @param n degrees of freedom |
| 100 | * @return icdf value |
| 101 | */ |
| 102 | public static double quantile(double y, double n) { |
| 103 | return quantile(y, n / 2.0, 2.0); |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * mean of the chi-square distribution |
| 108 | * |
| 109 | * @param n degrees of freedom |
| 110 | * @return mean |
| 111 | */ |
| 112 | public static double mean(double n) { |
| 113 | return n; |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * variance of the chi-square distribution |
| 118 | * |
| 119 | * @param n degrees of freedom |
| 120 | * @return variance |
| 121 | */ |
| 122 | public static double variance(double n) { |
| 123 | return 2.0 * n; |
| 124 | } |
| 125 | |
| 126 | // Private |
| 127 | |
| 128 | protected double n; |
| 129 | } |