| 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 dr.math.interfaces.OneVariableFunction; |
| 30 |
import dr.math.iterations.BisectionZeroFinder; |
| 31 |
import dr.math.iterations.NewtonZeroFinder; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
public class InverseGaussianDistribution implements Distribution { |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
public InverseGaussianDistribution(double mean, double shape) { |
| 54 |
this.m = mean; |
| 55 |
this.shape = shape; |
| 56 |
this.sd = calculateSD(mean, shape); |
| 57 |
} |
| 58 |
|
| 59 |
public double getMean() { |
| 60 |
return m; |
| 61 |
} |
| 62 |
|
| 63 |
public void setMean(double value) { |
| 64 |
m = value; |
| 65 |
} |
| 66 |
|
| 67 |
public double getShape() { |
| 68 |
return shape; |
| 69 |
} |
| 70 |
|
| 71 |
public void setShape(double value) { |
| 72 |
shape = value; |
| 73 |
sd = calculateSD(m, shape); |
| 74 |
} |
| 75 |
|
| 76 |
public static double calculateSD(double mean, double shape) { |
| 77 |
return Math.sqrt((mean * mean * mean) / shape); |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
public double pdf(double x) { |
| 88 |
return pdf(x, m, shape); |
| 89 |
} |
| 90 |
|
| 91 |
public double logPdf(double x) { |
| 92 |
return logPdf(x, m, shape); |
| 93 |
} |
| 94 |
|
| 95 |
public double cdf(double x) { |
| 96 |
return cdf(x, m, shape); |
| 97 |
} |
| 98 |
|
| 99 |
public double quantile(double y) { |
| 100 |
return quantile(y, m, shape); |
| 101 |
} |
| 102 |
|
| 103 |
public double mean() { |
| 104 |
return mean(m, shape); |
| 105 |
} |
| 106 |
|
| 107 |
public double variance() { |
| 108 |
return variance(m, shape); |
| 109 |
} |
| 110 |
|
| 111 |
public final UnivariateFunction getProbabilityDensityFunction() { |
| 112 |
return pdfFunction; |
| 113 |
} |
| 114 |
|
| 115 |
private UnivariateFunction pdfFunction = new UnivariateFunction() { |
| 116 |
public final double evaluate(double x) { |
| 117 |
return pdf(x); |
| 118 |
} |
| 119 |
|
| 120 |
public final double getLowerBound() { |
| 121 |
return 0.0; |
| 122 |
|
| 123 |
} |
| 124 |
|
| 125 |
public final double getUpperBound() { |
| 126 |
return Double.POSITIVE_INFINITY; |
| 127 |
} |
| 128 |
}; |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
public static double pdf(double x, double m, double shape) { |
| 139 |
double a = Math.sqrt(shape / (2.0 * Math.PI * x * x * x)); |
| 140 |
double b = ((-shape) * (x - m) * (x - m)) / (2.0 * m * m * x); |
| 141 |
return a * Math.exp(b); |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
public static double logPdf(double x, double m, double shape) { |
| 153 |
double a = Math.sqrt(shape / (2.0 * Math.PI * x * x * x)); |
| 154 |
double b = ((-shape) * (x - m) * (x - m)) / (2.0 * m * m * x); |
| 155 |
return Math.log(a) + b; |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
public static double cdf(double x, double m, double shape) { |
| 167 |
if (x <= 0 || m <= 0 || shape <= 0) { |
| 168 |
return Double.NaN; |
| 169 |
} |
| 170 |
|
| 171 |
double a = Math.sqrt(shape / x); |
| 172 |
double b = x / m; |
| 173 |
|
| 174 |
double p1 = NormalDistribution.cdf(a * (b - 1.0), 0.0, 1.0, false); |
| 175 |
|
| 176 |
double p2 = NormalDistribution.cdf(-a * (b + 1.0), 0.0, 1.0, false); |
| 177 |
if (p2 == 0.0) { |
| 178 |
return p1; |
| 179 |
} |
| 180 |
else { |
| 181 |
double c=2.0 * shape / m; |
| 182 |
if (c>=0x1.fffffffffffffP+1023) { |
| 183 |
return Double.POSITIVE_INFINITY; |
| 184 |
} |
| 185 |
return p1 + Math.exp(c) * p2; |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
public static double quantile(double z, double m, double shape) { |
| 222 |
if(z < 0.01 || z > 0.99) { |
| 223 |
System.err.print("Quantile is " + z); |
| 224 |
throw new RuntimeException("Quantile is too low/high to calculate (numerical estimation for extreme values is incomplete)"); |
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
double initialGuess; |
| 232 |
if (shape / m > 2.0) { |
| 233 |
initialGuess=(NormalDistribution.quantile(z,0.0,1.0)-0.5*Math.sqrt(m/shape))/Math.sqrt(shape/m); |
| 234 |
initialGuess=m*Math.exp(initialGuess); |
| 235 |
} |
| 236 |
else { |
| 237 |
initialGuess=shape/(GammaDistribution.quantile(1.0-z,0.5,1.0)*2.0); |
| 238 |
if (initialGuess > m / 2.0) { |
| 239 |
initialGuess=m*Math.exp(GammaDistribution.quantile(z,0.5,1.0)*0.1); |
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
final InverseGaussianDistribution f = new InverseGaussianDistribution(m, shape); |
| 249 |
final double y = z; |
| 250 |
NewtonZeroFinder zeroFinder = new NewtonZeroFinder(new OneVariableFunction() { |
| 251 |
public double value (double x) { |
| 252 |
return f.cdf(x) - y; |
| 253 |
} |
| 254 |
}, initialGuess); |
| 255 |
zeroFinder.evaluate(); |
| 256 |
|
| 257 |
if(Double.isNaN(zeroFinder.getResult()) || zeroFinder.getPrecision() > 0.000005) { |
| 258 |
zeroFinder = new NewtonZeroFinder(new OneVariableFunction() { |
| 259 |
public double value (double x) { |
| 260 |
return f.cdf(x) - y; |
| 261 |
} |
| 262 |
}, initialGuess); |
| 263 |
zeroFinder.initializeIterations(); |
| 264 |
int i; |
| 265 |
double previousPrecision = 0.0, previousResult = Double.NaN; |
| 266 |
double max = 10000.0, min = 0.00001; |
| 267 |
for(i=0; i < 50; i++) { |
| 268 |
zeroFinder.evaluateIteration(); |
| 269 |
double precision = f.cdf(zeroFinder.getResult()) - z; |
| 270 |
if((previousPrecision > 0 && precision < 0) || (previousPrecision < 0 && precision > 0)) { |
| 271 |
max = Math.max(previousResult, zeroFinder.getResult()); |
| 272 |
min = Math.min(previousResult, zeroFinder.getResult()); |
| 273 |
max = Math.min(10000.0, max); |
| 274 |
break; |
| 275 |
} |
| 276 |
|
| 277 |
previousPrecision = precision; |
| 278 |
previousResult = zeroFinder.getResult(); |
| 279 |
|
| 280 |
} |
| 281 |
return calculateZeroFinderApproximation(z, m, shape, min, max, initialGuess); |
| 282 |
} |
| 283 |
return zeroFinder.getResult(); |
| 284 |
} |
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
private static double calculateZeroFinderApproximation(double z, double m, double shape, double min, double max, double initialGuess) { |
| 298 |
final InverseGaussianDistribution f = new InverseGaussianDistribution(m, shape); |
| 299 |
final double y = z; |
| 300 |
BisectionZeroFinder bisectionZeroFinder = new BisectionZeroFinder(new OneVariableFunction() { |
| 301 |
public double value(double x) { |
| 302 |
return f.cdf(x) - y; |
| 303 |
} |
| 304 |
}, min, max); |
| 305 |
bisectionZeroFinder.setInitialValue(initialGuess); |
| 306 |
bisectionZeroFinder.initializeIterations(); |
| 307 |
|
| 308 |
double bestValue = Double.NaN; |
| 309 |
double bestPrecision = 10; |
| 310 |
double precision = 10; |
| 311 |
double previousPrecision = 10; |
| 312 |
int count = 0; |
| 313 |
while(precision > 0.001 && count < 10) { |
| 314 |
bisectionZeroFinder.evaluateIteration(); |
| 315 |
precision = Math.abs(f.cdf(bisectionZeroFinder.getResult()) - z); |
| 316 |
if(precision < bestPrecision) { |
| 317 |
bestPrecision = precision; |
| 318 |
bestValue = bisectionZeroFinder.getResult(); |
| 319 |
} |
| 320 |
else if(previousPrecision == precision) { |
| 321 |
count++; |
| 322 |
} |
| 323 |
previousPrecision = precision; |
| 324 |
} |
| 325 |
bisectionZeroFinder.finalizeIterations(); |
| 326 |
|
| 327 |
|
| 328 |
return bestValue; |
| 329 |
} |
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
private static double calculateShiftedGammaApproximation(double z, double m, double shape) { |
| 343 |
double a = (3 * m * m) / (4 * shape); |
| 344 |
double b = (m / 3); |
| 345 |
double nu = (8 * shape) / (9 * m); |
| 346 |
return a * ChiSquareDistribution.quantile(z, nu) + b; |
| 347 |
} |
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
private static double calculateShiftedGammaApproximationWithRIG(double z, double m, double shape) { |
| 360 |
double a = (3 * shape + 8 * m)/(4 * shape * (shape + 2 * m)); |
| 361 |
double b = (shape + 3 * m)/(m * (3 * shape + 8 * m)); |
| 362 |
double nu = (8 * Math.pow((shape + 2 * m), 3)) / (m * Math.pow((8 * m + 3 * shape), 2)); |
| 363 |
double y_hat = a * ChiSquareDistribution.quantile(z, nu) + b; |
| 364 |
return 1 / y_hat; |
| 365 |
} |
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
private static double calculateZeroFinderApproximation(double z, double m, double shape, int numIterations, double min, double max) { |
| 380 |
final InverseGaussianDistribution f = new InverseGaussianDistribution(m, shape); |
| 381 |
final double y = z; |
| 382 |
BisectionZeroFinder bisectionZeroFinder = new BisectionZeroFinder(new OneVariableFunction() { |
| 383 |
public double value(double x) { |
| 384 |
return f.cdf(x) - y; |
| 385 |
} |
| 386 |
}, min, max); |
| 387 |
|
| 388 |
|
| 389 |
bisectionZeroFinder.setMaximumIterations(numIterations); |
| 390 |
bisectionZeroFinder.evaluate(); |
| 391 |
return bisectionZeroFinder.getResult(); |
| 392 |
} |
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
public static double mean(double m, double shape) { |
| 402 |
return m; |
| 403 |
} |
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
public static double variance(double m, double shape) { |
| 413 |
double sd = calculateSD(m, shape); |
| 414 |
return sd * sd; |
| 415 |
} |
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
protected double m, sd, shape; |
| 420 |
|
| 421 |
} |