Debian Codegraph @

src/dr/math/distributions/TruncatedNormalDistribution.java

raw symbols SCIP
1 /*
2 * TruncatedNormalDistribution.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.ErrorFunction;
29 import dr.math.UnivariateFunction;
30
31 /**
32 * User: Michael Defoin Platel
33 * Date: Oct 31, 2007
34 * Time: 2:32:37 PM
35 */
36 public class TruncatedNormalDistribution implements Distribution {
37
38 public double getMean() {
39 return m;
40 }
41
42 public void setMdean(double m) {
43 this.m = m;
44 }
45
46 public double getSD() {
47 return sd;
48 }
49
50 public void setSD(double sd) {
51 this.sd = sd;
52 }
53
54 public double getLower() {
55 return lower;
56 }
57
58 public void setLower(double lower) {
59 this.lower = lower;
60 }
61
62 public double getUpper() {
63 return upper;
64 }
65
66 public void setUpper(double upper) {
67 this.upper = upper;
68 }
69
70 public TruncatedNormalDistribution(double mean, double sd, double lower, double upper) {
71
72
73 if (lower == upper)
74 upper += 1.E-4;
75
76 if (sd == 0.)
77 sd = 1.E-5;
78
79 this.m = mean;
80 this.sd = sd;
81 this.lower = lower;
82 this.upper = upper;
83
84 double upperCDF;
85 double lowerCDF;
86
87 if(upper!=Double.POSITIVE_INFINITY){
88 upperCDF = standardNormalCdf((upper-mean)/sd);
89 } else {
90 upperCDF = 1;
91 }
92
93 if(lower!=Double.NEGATIVE_INFINITY){
94 lowerCDF = standardNormalCdf((lower-mean)/sd);
95 } else {
96 lowerCDF = 0;
97 }
98
99 this.T = upperCDF - lowerCDF;
100 }
101
102
103 public double pdf(double x) {
104 if (x >= upper || x < lower)
105 return 0.0;
106 else
107 return (standardNormalPdf((x - m) / sd) / sd) / T;
108 }
109
110 public double logPdf(double x) {
111 return Math.log(pdf(x));
112 }
113
114 public double cdf(double x) {
115 double cdf;
116 if (x < lower)
117 cdf = 0.;
118 else if (x >= lower && x < upper)
119 if(lower!=Double.NEGATIVE_INFINITY){
120 cdf = (standardNormalCdf((x - m) / sd) - standardNormalCdf((lower - m) / sd)) / T;
121 } else {
122 cdf = (standardNormalCdf((x - m) / sd)) / T;
123 }
124 else
125 cdf = 1.0;
126
127 return cdf;
128 }
129
130 public double quantile(double y) {
131
132 if (y == 0)
133 return lower;
134
135 if (y == 1.0)
136 return upper;
137
138 return quantileSearch(y, lower, upper, 20);
139 }
140
141 /*Implements a geometic search for the quantiles*/
142 private double quantileSearch(double y, double l, double u, int step) {
143 double q, a;
144
145 q = (u + l) / 2.0;
146
147 if (step == 0 || q == l || q == u)
148 return q;
149
150 a = cdf(q);
151
152 if (y <= a)
153 return quantileSearch(y, l, q, step - 1);
154 else
155 return quantileSearch(y, q, u, step - 1);
156 }
157
158 public double mean() {
159 return mean(m, sd, lower, upper);
160 }
161
162 public double variance() {
163 return mean(m, sd, lower, upper);
164 }
165
166 public UnivariateFunction getProbabilityDensityFunction() {
167 return pdfFunction;
168 }
169
170 private UnivariateFunction pdfFunction = new UnivariateFunction() {
171 public final double evaluate(double x) {
172 return pdf(x);
173 }
174
175 public final double getLowerBound() {
176 return lower;
177 }
178
179 public final double getUpperBound() {
180 return upper;
181 }
182 };
183
184 /**
185 * probability density function of the standard normal distribution
186 *
187 * @param x argument
188 * @return pdf at x
189 */
190 public static double standardNormalPdf(double x) {
191 double a = 1.0 / (Math.sqrt(2.0 * Math.PI));
192 double b = -(x) * (x) / (2.0);
193
194 return a * Math.exp(b);
195 }
196
197 /**
198 * the natural log of the probability density function of the standard normal distribution
199 *
200 * @param x argument
201 * @return log pdf at x
202 */
203 public static double logStandardNormalPdf(double x) {
204 /*Can throw an expcetion if x not in the range [lower,upper]*/
205 return Math.log(standardNormalPdf(x));
206 }
207
208 /**
209 * cumulative density function of the standard normal distribution
210 *
211 * @param x argument
212 * @return cdf at x
213 */
214 public static double standardNormalCdf(double x) {
215 double a = (x) / (Math.sqrt(2.0));
216
217 return 0.5 * (1.0 + ErrorFunction.erf(a));
218 }
219
220 /**
221 * mean
222 *
223 * @param m mean
224 * @param sd standard deviation
225 * @param lower the lower limit
226 * @param upper the upper limit
227 * @return mean
228 */
229 public static double mean(double m, double sd, double lower, double upper) {
230 double au, al, pu, pl, cu, cl;
231
232 au = (upper - m) / sd;
233 al = (lower - m) / sd;
234
235 pu = standardNormalPdf(au);
236 pl = standardNormalPdf(al);
237
238 cu = standardNormalCdf(au);
239 cl = standardNormalCdf(al);
240
241 return m - sd * (pu - pl) / (cu - cl);
242 }
243
244 /**
245 * variance
246 *
247 * @param m mean
248 * @param sd standard deviation
249 * @param lower the lower limit
250 * @param upper the upper limit
251 * @return variance
252 */
253 public static double variance(double m, double sd, double lower, double upper) {
254 double au, al, pu, pl, cu, cl, T1, T2;
255
256 au = (upper - m) / sd;
257 al = (lower - m) / sd;
258
259 pu = standardNormalPdf(au);
260 pl = standardNormalPdf(al);
261
262 cu = standardNormalCdf(au);
263 cl = standardNormalCdf(al);
264
265 T1 = (au * pu - al * pl) / (cu - cl);
266 T2 = (pu - pl) / (cu - cl);
267
268 return sd * sd * (1.0 - T1 - T2 * T2);
269 }
270
271
272 private double m;
273 private double sd;
274 private double lower;
275 private double upper;
276 private double T;
277 }