| 1 | /* |
| 2 | * ExponentialDistribution.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 | import dr.math.UnivariateFunction; |
| 29 | |
| 30 | /** |
| 31 | * exponential distribution. |
| 32 | * <p/> |
| 33 | * (Parameter: lambda; mean: 1/lambda; variance: 1/lambda^2) |
| 34 | * <p/> |
| 35 | * The exponential distribution is a special case of the Gamma distribution |
| 36 | * (shape parameter = 1.0, scale = 1/lambda). |
| 37 | * |
| 38 | * @author Alexei Drummond |
| 39 | * @author Korbinian Strimmer |
| 40 | * @version $Id: ExponentialDistribution.java,v 1.5 2005/05/24 20:26:00 rambaut Exp $ |
| 41 | */ |
| 42 | public class ExponentialDistribution implements Distribution { |
| 43 | // |
| 44 | // Public stuff |
| 45 | // |
| 46 | |
| 47 | /** |
| 48 | * Constructor |
| 49 | * |
| 50 | * @param lambda the rate of the exponential distribution (1/mean) |
| 51 | */ |
| 52 | public ExponentialDistribution(double lambda) { |
| 53 | this.lambda = lambda; |
| 54 | } |
| 55 | |
| 56 | public double pdf(double x) { |
| 57 | return pdf(x, lambda); |
| 58 | } |
| 59 | |
| 60 | public double logPdf(double x) { |
| 61 | return logPdf(x, lambda); |
| 62 | } |
| 63 | |
| 64 | public double cdf(double x) { |
| 65 | return cdf(x, lambda); |
| 66 | } |
| 67 | |
| 68 | public double quantile(double y) { |
| 69 | return quantile(y, lambda); |
| 70 | } |
| 71 | |
| 72 | public double mean() { |
| 73 | return mean(lambda); |
| 74 | } |
| 75 | |
| 76 | public double variance() { |
| 77 | return variance(lambda); |
| 78 | } |
| 79 | |
| 80 | public UnivariateFunction getProbabilityDensityFunction() { |
| 81 | return pdfFunction; |
| 82 | } |
| 83 | |
| 84 | private UnivariateFunction pdfFunction = new UnivariateFunction() { |
| 85 | public final double evaluate(double x) { |
| 86 | return pdf(x); |
| 87 | } |
| 88 | |
| 89 | public final double getLowerBound() { |
| 90 | return 0.0; |
| 91 | } |
| 92 | |
| 93 | public final double getUpperBound() { |
| 94 | return Double.POSITIVE_INFINITY; |
| 95 | } |
| 96 | }; |
| 97 | |
| 98 | /** |
| 99 | * probability density function of the exponential distribution |
| 100 | * (mean = 1/lambda) |
| 101 | * |
| 102 | * @param x argument |
| 103 | * @param lambda parameter of exponential distribution |
| 104 | * @return pdf value |
| 105 | */ |
| 106 | public static double pdf(double x, double lambda) { |
| 107 | if (x < 0) return 0; |
| 108 | |
| 109 | return lambda * Math.exp(-lambda * x); |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * the natural log of the probability density function of the distribution |
| 114 | * (mean = 1/lambda) |
| 115 | * |
| 116 | * @param x argument |
| 117 | * @param lambda parameter of exponential distribution |
| 118 | * @return log pdf value |
| 119 | */ |
| 120 | public static double logPdf(double x, double lambda) { |
| 121 | if (x < 0) return Double.NEGATIVE_INFINITY; |
| 122 | |
| 123 | return Math.log(lambda) - (lambda * x); |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * cumulative density function of the exponential distribution |
| 128 | * |
| 129 | * @param x argument |
| 130 | * @param lambda parameter of exponential distribution |
| 131 | * @return cdf value |
| 132 | */ |
| 133 | public static double cdf(double x, double lambda) { |
| 134 | return 1.0 - Math.exp(-lambda * x); |
| 135 | } |
| 136 | |
| 137 | |
| 138 | /** |
| 139 | * quantile (inverse cumulative density function) of the exponential distribution |
| 140 | * |
| 141 | * @param y argument |
| 142 | * @param lambda parameter of exponential distribution |
| 143 | * @return icdf value |
| 144 | */ |
| 145 | public static double quantile(double y, double lambda) { |
| 146 | return -(1.0 / lambda) * Math.log(1.0 - y); |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * mean of the exponential distribution |
| 151 | * |
| 152 | * @param lambda parameter of exponential distribution |
| 153 | * @return mean |
| 154 | */ |
| 155 | public static double mean(double lambda) { |
| 156 | return 1.0 / (lambda); |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * variance of the exponential distribution |
| 161 | * |
| 162 | * @param lambda parameter of exponential distribution |
| 163 | * @return variance |
| 164 | */ |
| 165 | public static double variance(double lambda) { |
| 166 | return 1.0 / (lambda * lambda); |
| 167 | } |
| 168 | |
| 169 | // the rate parameter of this exponential distribution (1/mean) |
| 170 | double lambda; |
| 171 | } |