Debian Codegraph @

src/dr/math/distributions/TruncatedDistribution.java

raw symbols SCIP
1 /*
2 * TruncatedDistribution.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 org.apache.commons.math.MathException;
30 import org.apache.commons.math.distribution.AbstractContinuousDistribution;
31
32 /**
33 * @author Andrew Rambaut
34 */
35 public class TruncatedDistribution extends AbstractContinuousDistribution implements Distribution {
36
37 public double getLower() {
38 return lower;
39 }
40
41 public double getUpper() {
42 return upper;
43 }
44
45 public TruncatedDistribution(Distribution source, double lower, double upper) {
46 this.source = source;
47
48 if (lower == upper) {
49 throw new IllegalArgumentException("upper equals lower");
50 }
51
52 if (source.getProbabilityDensityFunction().getLowerBound() > lower) {
53 lower = source.getProbabilityDensityFunction().getLowerBound();
54 }
55
56 if (source.getProbabilityDensityFunction().getUpperBound() < upper) {
57 upper = source.getProbabilityDensityFunction().getUpperBound();
58 }
59
60 this.lower = lower;
61 this.upper = upper;
62
63 if (!Double.isInfinite(this.lower)) {
64 this.lowerCDF = source.cdf(lower);
65 } else {
66 this.lowerCDF = 0;
67 }
68
69 double upperCDF = 1.0;
70 if (!Double.isInfinite(this.upper)) {
71 upperCDF = source.cdf(upper);
72 }
73
74 this.normalization = upperCDF - lowerCDF;
75 }
76
77 public double pdf(double x) {
78 if (x >= upper && x < lower)
79 return 0.0;
80 else
81 return source.pdf(x) / normalization;
82 }
83
84 public double logPdf(double x) {
85 return Math.log(pdf(x));
86 }
87
88 public double cdf(double x) {
89 double cdf;
90 if (x < lower)
91 cdf = 0.0;
92 else if (x >= lower && x < upper)
93 cdf = (source.cdf(x) - lowerCDF) / normalization;
94 else
95 cdf = 1.0;
96
97 return cdf;
98 }
99
100 public double quantile(double y) {
101
102 if (y == 0)
103 return lower;
104
105 if (y == 1.0)
106 return upper;
107
108 if (Double.isInfinite(lower) && Double.isInfinite(upper)) {
109 return source.quantile(y);
110 }
111
112 try {
113 return super.inverseCumulativeProbability(y);
114 } catch (MathException e) {
115 // throw MathRuntimeException.createIllegalArgumentException( // AR - throwing exceptions deep in numerical code causes trouble. Catching runtime
116 // exceptions is bad. Better to return NaN and let the calling code deal with it.
117 return Double.NaN;
118
119 // "Couldn't calculate beta quantile for alpha = " + alpha + ", beta = " + beta + ": " +e.getMessage());
120 }
121 }
122
123 /**
124 * mean of the distribution
125 *
126 * @return mean
127 */
128 public double mean() {
129 if (source != null) {
130 return source.mean();
131 } else {
132 throw new IllegalArgumentException("Distribution is null");
133 }
134 }
135
136 /**
137 * variance of the distribution
138 *
139 * @return variance
140 */
141 public double variance() {
142 if (source != null) {
143 return source.variance();
144 } else {
145 throw new IllegalArgumentException("Distribution is null");
146 }
147
148 }
149
150 public UnivariateFunction getProbabilityDensityFunction() {
151 return pdfFunction;
152 }
153
154 private UnivariateFunction pdfFunction = new UnivariateFunction() {
155 public final double evaluate(double x) {
156 return pdf(x);
157 }
158
159 public final double getLowerBound() {
160 return lower;
161 }
162
163 public final double getUpperBound() {
164 return upper;
165 }
166 };
167
168 private final Distribution source;
169 private final double lower;
170 private final double upper;
171 private final double normalization;
172 private final double lowerCDF;
173
174 @Override
175 protected double getInitialDomain(double v) {
176 if (!Double.isInfinite(lower) && !Double.isInfinite(upper)) {
177 return (upper + lower) / 2;
178 } else if (!Double.isInfinite(upper)) {
179 return upper / 2;
180 } else if (!Double.isInfinite(lower)) {
181 return lower * 2;
182 }
183 return v;
184 }
185
186 @Override
187 protected double getDomainLowerBound(double v) {
188 return lower;
189 }
190
191 @Override
192 protected double getDomainUpperBound(double v) {
193 return upper;
194 }
195
196 public double cumulativeProbability(double v) throws MathException {
197 return cdf(v);
198 }
199 }