| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 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 |
|
| 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 |
|
| 116 |
|
| 117 |
return Double.NaN; |
| 118 |
|
| 119 |
|
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 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 |
|
| 138 |
|
| 139 |
|
| 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 |
} |