Debian Codegraph @

src/dr/math/distributions/UniformDistribution.java

raw symbols SCIP
1 /*
2 * UniformDistribution.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 import dr.util.DataTable;
30
31 /**
32 * uniform distribution.
33 *
34 * @author Andrew Rambaut
35 * @author Alexei Drummond
36 * @version $Id: UniformDistribution.java,v 1.3 2005/07/11 14:06:25 rambaut Exp $
37 */
38 public class UniformDistribution implements Distribution {
39 //
40 // Public stuff
41 //
42
43 /*
44 * Constructor
45 */
46
47 public UniformDistribution(double lower, double upper) {
48 this.lower = lower;
49 this.upper = upper;
50
51 assert !Double.isInfinite(lower) : "bounds on a uniform distribution cannot be infinite";
52 assert !Double.isInfinite(upper) : "bounds on a uniform distribution cannot be infinite";
53 }
54
55 public double pdf(double x) {
56 return pdf(x, lower, upper);
57 }
58
59 public double logPdf(double x) {
60 return logPdf(x, lower, upper);
61 }
62
63 public double cdf(double x) {
64 return cdf(x, lower, upper);
65 }
66
67 public double quantile(double y) {
68 return quantile(y, lower, upper);
69 }
70
71 public double mean() {
72 return mean(lower, upper);
73 }
74
75 public double variance() {
76 return variance(lower, upper);
77 }
78
79 public final UnivariateFunction getProbabilityDensityFunction() {
80 return pdfFunction;
81 }
82
83 private final UnivariateFunction pdfFunction = new UnivariateFunction() {
84 public final double evaluate(double x) {
85 return pdf(x);
86 }
87
88 public final double getLowerBound() {
89 return lower;
90 }
91
92 public final double getUpperBound() {
93 return upper;
94 }
95 };
96
97
98 /**
99 * probability density function of the uniform distribution
100 *
101 * @param x argument
102 * @param lower the lower bound of the uniform distribution
103 * @param upper the upper bound of the uniform distribution
104 * @return pdf value
105 */
106 public static double pdf(double x, double lower, double upper) {
107 return (x >= lower && x <= upper ? 1.0 / (upper - lower) : 0.0);
108 }
109
110 /**
111 * the natural log of the probability density function of the uniform distribution
112 *
113 * @param x argument
114 * @param lower the lower bound of the uniform distribution
115 * @param upper the upper bound of the uniform distribution
116 * @return log pdf value
117 */
118 public static double logPdf(double x, double lower, double upper) {
119 if (x < lower || x > upper) return Double.NEGATIVE_INFINITY;
120
121 // improve numerical stability:
122 return - Math.log(upper - lower);
123 // return Math.log(pdf(x, lower, upper));
124 }
125
126 /**
127 * cumulative density function of the uniform distribution
128 *
129 * @param x argument
130 * @param lower the lower bound of the uniform distribution
131 * @param upper the upper bound of the uniform distribution
132 * @return cdf value
133 */
134 public static double cdf(double x, double lower, double upper) {
135 if (x < lower) return 0.0;
136 if (x > upper) return 1.0;
137 return (x - lower) / (upper - lower);
138 }
139
140
141 /**
142 * quantile (inverse cumulative density function) of the uniform distribution
143 *
144 * @param y argument
145 * @param lower the lower bound of the uniform distribution
146 * @param upper the upper bound of the uniform distribution
147 * @return icdf value
148 */
149 public static double quantile(double y, double lower, double upper) {
150 if (!(y >= 0.0 && y <= 1.0)) throw new IllegalArgumentException("y must in range [0,1]");
151 return (y * (upper - lower)) + lower;
152 }
153
154 /**
155 * mean of the uniform distribution
156 *
157 * @param lower the lower bound of the uniform distribution
158 * @param upper the upper bound of the uniform distribution
159 * @return mean
160 */
161 public static double mean(double lower, double upper) {
162 return (upper + lower) / 2;
163 }
164
165 /**
166 * variance of the uniform distribution
167 *
168 * @param lower the lower bound of the uniform distribution
169 * @param upper the upper bound of the uniform distribution
170 * @return variance
171 */
172 public static double variance(double lower, double upper) {
173 return (upper - lower) * (upper - lower) / 12;
174 }
175
176 // Private
177
178 private final double upper;
179 private final double lower;
180 }