| 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.inference.distribution; |
| 27 |
|
| 28 |
import dr.inference.model.AbstractModel; |
| 29 |
import dr.inference.model.Model; |
| 30 |
import dr.inference.model.Parameter; |
| 31 |
import dr.inference.model.Variable; |
| 32 |
import dr.inferencexml.distribution.LogNormalDistributionModelParser; |
| 33 |
import dr.math.UnivariateFunction; |
| 34 |
import dr.math.distributions.NormalDistribution; |
| 35 |
import org.w3c.dom.Document; |
| 36 |
import org.w3c.dom.Element; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
public class LogNormalDistributionModel extends AbstractModel implements ParametricDistributionModel { |
| 47 |
|
| 48 |
public enum Parameterization { |
| 49 |
MU_SIGMA, |
| 50 |
MU_PRECISION, |
| 51 |
MEAN_STDEV |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
public LogNormalDistributionModel(Parameter meanParameter, Parameter stdevParameter, double offset, boolean parametersInRealSpace) { |
| 59 |
|
| 60 |
super(LogNormalDistributionModelParser.LOGNORMAL_DISTRIBUTION_MODEL); |
| 61 |
|
| 62 |
this.offset = offset; |
| 63 |
|
| 64 |
if (parametersInRealSpace) { |
| 65 |
this.meanParameter = meanParameter; |
| 66 |
this.muParameter = null; |
| 67 |
addVariable(this.meanParameter); |
| 68 |
this.meanParameter.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, 1)); |
| 69 |
|
| 70 |
this.stdevParameter = stdevParameter; |
| 71 |
this.sigmaParameter = null; |
| 72 |
addVariable(this.stdevParameter); |
| 73 |
this.stdevParameter.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, 1)); |
| 74 |
|
| 75 |
this.parameterization = Parameterization.MEAN_STDEV; |
| 76 |
} else { |
| 77 |
this.muParameter = meanParameter; |
| 78 |
this.meanParameter = null; |
| 79 |
addVariable(this.muParameter); |
| 80 |
this.muParameter.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, 1)); |
| 81 |
|
| 82 |
this.sigmaParameter = stdevParameter; |
| 83 |
this.stdevParameter = null; |
| 84 |
addVariable(this.sigmaParameter); |
| 85 |
this.sigmaParameter.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, 1)); |
| 86 |
|
| 87 |
this.parameterization = Parameterization.MU_SIGMA; |
| 88 |
} |
| 89 |
|
| 90 |
precisionParameter = null; |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
public LogNormalDistributionModel(Parameterization parameterization, Parameter parameter1, Parameter parameter2, double offset) { |
| 103 |
|
| 104 |
super(LogNormalDistributionModelParser.LOGNORMAL_DISTRIBUTION_MODEL); |
| 105 |
|
| 106 |
switch (parameterization) { |
| 107 |
case MU_SIGMA: |
| 108 |
this.muParameter = parameter1; |
| 109 |
this.sigmaParameter = parameter2; |
| 110 |
this.meanParameter = null; |
| 111 |
this.stdevParameter = null; |
| 112 |
this.precisionParameter = null; |
| 113 |
this.parameterization = Parameterization.MU_SIGMA; |
| 114 |
break; |
| 115 |
case MU_PRECISION: |
| 116 |
this.muParameter = parameter1; |
| 117 |
this.precisionParameter = parameter2; |
| 118 |
this.meanParameter = null; |
| 119 |
this.stdevParameter = null; |
| 120 |
this.sigmaParameter = null; |
| 121 |
this.parameterization = Parameterization.MU_PRECISION; |
| 122 |
break; |
| 123 |
case MEAN_STDEV: |
| 124 |
this.meanParameter = parameter1; |
| 125 |
this.stdevParameter = parameter2; |
| 126 |
this.muParameter = null; |
| 127 |
this.sigmaParameter = null; |
| 128 |
this.precisionParameter = null; |
| 129 |
this.parameterization = Parameterization.MEAN_STDEV; |
| 130 |
break; |
| 131 |
default: |
| 132 |
throw new IllegalArgumentException("Unknow parameterization type"); |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
this.offset = offset; |
| 137 |
|
| 138 |
if (this.muParameter != null) { |
| 139 |
addVariable(this.muParameter); |
| 140 |
this.muParameter.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, 1)); |
| 141 |
} |
| 142 |
|
| 143 |
if (meanParameter != null) { |
| 144 |
addVariable(this.meanParameter); |
| 145 |
this.meanParameter.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, 1)); |
| 146 |
} |
| 147 |
|
| 148 |
if (sigmaParameter != null) { |
| 149 |
addVariable(this.sigmaParameter); |
| 150 |
this.sigmaParameter.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, 1)); |
| 151 |
} |
| 152 |
|
| 153 |
if (stdevParameter != null) { |
| 154 |
addVariable(this.stdevParameter); |
| 155 |
this.stdevParameter.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, 1)); |
| 156 |
} |
| 157 |
|
| 158 |
if (precisionParameter != null) { |
| 159 |
addVariable(this.precisionParameter); |
| 160 |
this.precisionParameter.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, 1)); |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
public Parameter getMeanParameter() { |
| 165 |
return meanParameter; |
| 166 |
} |
| 167 |
|
| 168 |
public Parameter getStdevParameter() { |
| 169 |
return stdevParameter; |
| 170 |
} |
| 171 |
|
| 172 |
public Parameter getMuParameter() { |
| 173 |
return muParameter; |
| 174 |
} |
| 175 |
|
| 176 |
public Parameter getSigmaParameter() { |
| 177 |
return sigmaParameter; |
| 178 |
} |
| 179 |
|
| 180 |
public Parameter getPrecisionParameter() { |
| 181 |
return precisionParameter; |
| 182 |
} |
| 183 |
|
| 184 |
public Parameterization getParameterization() { |
| 185 |
return parameterization; |
| 186 |
} |
| 187 |
|
| 188 |
public final double getMu() { |
| 189 |
if (muParameter != null) { |
| 190 |
return muParameter.getValue(0); |
| 191 |
} else { |
| 192 |
return calculateMu(meanParameter.getValue(0), stdevParameter.getValue(0)); |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
public final double getMean() { |
| 197 |
if (meanParameter != null) { |
| 198 |
return meanParameter.getValue(0); |
| 199 |
} else { |
| 200 |
return calculateMean(muParameter.getValue(0), getSigma()); |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
public final double getSigma() { |
| 205 |
if (sigmaParameter != null) { |
| 206 |
return sigmaParameter.getValue(0); |
| 207 |
} else if (precisionParameter != null) { |
| 208 |
return Math.sqrt(1.0 / precisionParameter.getValue(0)); |
| 209 |
} else{ |
| 210 |
return calculateSigma(meanParameter.getValue(0), stdevParameter.getValue(0)); |
| 211 |
} |
| 212 |
|
| 213 |
} |
| 214 |
|
| 215 |
public final double getStdev() { |
| 216 |
if (stdevParameter != null) { |
| 217 |
return stdevParameter.getValue(0); |
| 218 |
} else if (precisionParameter != null) { |
| 219 |
double sigma = Math.sqrt(1.0 / precisionParameter.getValue(0)); |
| 220 |
return calculateStdev(muParameter.getValue(0), sigma); |
| 221 |
} else { |
| 222 |
return calculateStdev(muParameter.getValue(0), sigmaParameter.getValue(0)); |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
public final double getPrecision() { |
| 227 |
if (precisionParameter != null) { |
| 228 |
return precisionParameter.getValue(0); |
| 229 |
} else { |
| 230 |
double sigma = getSigma(); |
| 231 |
return 1.0 / (sigma * sigma); |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
private double calculateMu(double mean, double stdev) { |
| 236 |
return Math.log(mean/Math.sqrt(1 + (stdev * stdev) / (mean * mean))); |
| 237 |
} |
| 238 |
|
| 239 |
private double calculateSigma(double mean, double stdev) { |
| 240 |
return Math.sqrt(Math.log(1 + (stdev * stdev) / (mean * mean))); |
| 241 |
} |
| 242 |
|
| 243 |
private double calculateMean(double mu, double sigma) { |
| 244 |
return Math.exp(mu + 0.5 * sigma * sigma); |
| 245 |
} |
| 246 |
|
| 247 |
private double calculateStdev(double mu, double sigma) { |
| 248 |
return Math.sqrt( (Math.exp(sigma * sigma)-1) * Math.exp(2.0 * mu + (sigma * sigma)) ); |
| 249 |
} |
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
public double pdf(double x) { |
| 315 |
if (x - offset <= 0.0) return 0.0; |
| 316 |
return NormalDistribution.pdf(Math.log(x - offset), getMu(), getSigma()) / (x - offset); |
| 317 |
} |
| 318 |
|
| 319 |
|
| 320 |
public double logPdf(double x) { |
| 321 |
if (x - offset <= 0.0) return Double.NEGATIVE_INFINITY; |
| 322 |
return NormalDistribution.logPdf(Math.log(x - offset), getMu(), getSigma()) - Math.log(x - offset); |
| 323 |
} |
| 324 |
|
| 325 |
public double cdf(double x) { |
| 326 |
if (x - offset <= 0.0) return 0.0; |
| 327 |
return NormalDistribution.cdf(Math.log(x - offset), getMu(), getSigma()); |
| 328 |
} |
| 329 |
|
| 330 |
public double quantile(double y) { |
| 331 |
return Math.exp(NormalDistribution.quantile(y, getMu(), getSigma())) + offset; |
| 332 |
} |
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
public double mean() { |
| 338 |
return getMean(); |
| 339 |
} |
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
public double variance() { |
| 346 |
return getStdev() * getStdev(); |
| 347 |
} |
| 348 |
|
| 349 |
public final UnivariateFunction getProbabilityDensityFunction() { |
| 350 |
return pdfFunction; |
| 351 |
} |
| 352 |
|
| 353 |
private final UnivariateFunction pdfFunction = new UnivariateFunction() { |
| 354 |
public final double evaluate(double x) { |
| 355 |
return pdf(Math.log(x)); |
| 356 |
} |
| 357 |
|
| 358 |
public final double getLowerBound() { |
| 359 |
return Double.NEGATIVE_INFINITY; |
| 360 |
} |
| 361 |
|
| 362 |
public final double getUpperBound() { |
| 363 |
return Double.POSITIVE_INFINITY; |
| 364 |
} |
| 365 |
}; |
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
@Override |
| 372 |
public double logPdf(double[] x) { |
| 373 |
return logPdf(x[0]); |
| 374 |
} |
| 375 |
|
| 376 |
@Override |
| 377 |
public Variable<Double> getLocationVariable() { |
| 378 |
return meanParameter; |
| 379 |
} |
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
public void handleModelChangedEvent(Model model, Object object, int index) { |
| 386 |
|
| 387 |
} |
| 388 |
|
| 389 |
public void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) { |
| 390 |
|
| 391 |
} |
| 392 |
|
| 393 |
protected void storeState() { |
| 394 |
} |
| 395 |
|
| 396 |
protected void restoreState() { |
| 397 |
} |
| 398 |
|
| 399 |
protected void acceptState() { |
| 400 |
} |
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
public Element createElement(Document document) { |
| 407 |
throw new RuntimeException("Not implemented!"); |
| 408 |
} |
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
private final Parameter meanParameter; |
| 416 |
private final Parameter stdevParameter; |
| 417 |
private final Parameter muParameter; |
| 418 |
private final Parameter sigmaParameter; |
| 419 |
private final Parameter precisionParameter; |
| 420 |
private final double offset; |
| 421 |
|
| 422 |
private Parameterization parameterization; |
| 423 |
|
| 424 |
public static void main(String[] argv) { |
| 425 |
Parameter meanParameter = new Parameter.Default(1.0); |
| 426 |
Parameter stdevParameter = new Parameter.Default(5.0); |
| 427 |
Parameter muParameter = new Parameter.Default(-1.629048); |
| 428 |
Parameter sigmaParameter = new Parameter.Default(1.80502); |
| 429 |
|
| 430 |
LogNormalDistributionModel ln1 = new LogNormalDistributionModel(Parameterization.MEAN_STDEV, meanParameter, stdevParameter, 0); |
| 431 |
System.out.println("Lognormal mean = 1.0, stdev = 5.0"); |
| 432 |
System.out.println(" mu = " + ln1.getMu() + " (correct = -1.629048)"); |
| 433 |
System.out.println(" sigma = " + ln1.getSigma() + " (correct = 1.80502)"); |
| 434 |
System.out.println(" quantile(2.5) = " + ln1.quantile(0.025) + " (correct = 0.005702663)"); |
| 435 |
System.out.println(" quantile(97.5) = " + ln1.quantile(0.975) + " (correct = 6.744487892)"); |
| 436 |
|
| 437 |
LogNormalDistributionModel ln2 = new LogNormalDistributionModel(Parameterization.MU_SIGMA, muParameter, sigmaParameter, 0); |
| 438 |
System.out.println("Lognormal mu = -1.629048, sigma = 1.80502"); |
| 439 |
System.out.println(" mean = " + ln2.getMean() + " (correct = 1.0)"); |
| 440 |
System.out.println(" sigma = " + ln2.getStdev() + " (correct = 5.0)"); |
| 441 |
System.out.println(" quantile(2.5) = " + ln2.quantile(0.025) + " (correct = 0.005702663)"); |
| 442 |
System.out.println(" quantile(97.5) = " + ln2.quantile(0.975) + " (correct = 6.744487892)"); |
| 443 |
|
| 444 |
meanParameter = new Parameter.Default(0.001); |
| 445 |
stdevParameter = new Parameter.Default(0.0005); |
| 446 |
LogNormalDistributionModel ln3 = new LogNormalDistributionModel(Parameterization.MEAN_STDEV, meanParameter, stdevParameter, 0); |
| 447 |
System.out.println("Lognormal mean = 0.001, stdev = 0.0005"); |
| 448 |
System.out.println(" mu = " + ln3.getMu()); |
| 449 |
System.out.println(" sigma = " + ln3.getSigma()); |
| 450 |
for (int i = 1; i <= 12; i++) { |
| 451 |
double y = ((double)i) / 13.0; |
| 452 |
System.out.println(i + "\t" + y + "\t" + ln3.quantile(y)); |
| 453 |
} |
| 454 |
|
| 455 |
|
| 456 |
} |
| 457 |
} |