| 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.ErrorFunction; |
| 29 |
import dr.math.UnivariateFunction; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 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 |
|
| 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 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 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 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
public static double logStandardNormalPdf(double x) { |
| 204 |
|
| 205 |
return Math.log(standardNormalPdf(x)); |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 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 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 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 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 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 |
} |