Debian Codegraph @

src/dr/inferencexml/distribution/PriorParsers.java

raw symbols SCIP
1 /*
2 * PriorParsers.java
3 *
4 * Copyright (c) 2002-2016 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.inferencexml.distribution;
27
28 import dr.inference.distribution.CauchyDistribution;
29 import dr.inference.distribution.DistributionLikelihood;
30 import dr.inference.distribution.MultivariateDistributionLikelihood;
31 import dr.inference.model.Likelihood;
32 import dr.inference.model.Statistic;
33 import dr.math.distributions.*;
34 import dr.util.Attribute;
35 import dr.xml.*;
36 import org.apache.commons.math.distribution.CauchyDistributionImpl;
37
38 import java.util.ArrayList;
39 import java.util.List;
40
41 /**
42 */
43 public class PriorParsers {
44 public final static boolean DEBUG = false;
45
46 public static final String TRUNCATED = "truncated";
47 public static final String UNIFORM_PRIOR = "uniformPrior";
48 public static final String EXPONENTIAL_PRIOR = "exponentialPrior";
49 public static final String POISSON_PRIOR = "poissonPrior";
50 public static final String NEGATIVE_BINOMIAL_PRIOR = "negativeBinomialPrior";
51 public static final String DISCRETE_UNIFORM_PRIOR = "discreteUniformPrior";
52 public static final String NORMAL_PRIOR = "normalPrior";
53 public static final String LOG_NORMAL_PRIOR = "logNormalPrior";
54 public static final String HALF_NORMAL_PRIOR = "halfNormalPrior";
55 public static final String GAMMA_PRIOR = "gammaPrior";
56 public static final String INVGAMMA_PRIOR = "invgammaPrior";
57 public static final String INVGAMMA_PRIOR_CORRECT = "inverseGammaPrior";
58 public static final String LAPLACE_PRIOR = "laplacePrior";
59 public static final String BETA_PRIOR = "betaPrior";
60 public static final String CAUCHY_PRIOR = "cauchyPrior";
61 public static final String UPPER = "upper";
62 public static final String LOWER = "lower";
63 public static final String MEAN = "mean";
64 public static final String MEDIAN = "median";
65 public static final String STDEV = "stdev";
66 public static final String MEAN_IN_REAL_SPACE = "meanInRealSpace";
67 public static final String MU = "mu";
68 public static final String SIGMA = "sigma";
69 public static final String SHAPE = "shape";
70 public static final String SHAPEB = "shapeB";
71 public static final String SCALE = "scale";
72 public static final String DF = "df";
73 public static final String OFFSET = "offset";
74 public static final String UNINFORMATIVE = "uninformative";
75 public static final String HALF_T_PRIOR = "halfTPrior";
76 public static final String DIRICHLET_PRIOR = "dirichletPrior";
77 public static final String ALPHA = "alpha";
78 public static final String COUNTS = "counts";
79 public static final String SUMS_TO = "sumsTo";
80
81
82 /**
83 * A special parser that reads a convenient short form of priors on parameters.
84 */
85 public static XMLObjectParser TRUNCATED_PARSER = new AbstractXMLObjectParser() {
86
87 @Override
88 public String getParserName() {
89 return TRUNCATED;
90 }
91
92 @Override
93 public String[] getParserNames() {
94 return new String[] { getParserName(), "trancated" }; // was misspelled, keep compatibility.
95 }
96
97 @Override
98 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
99
100 double lower = xo.getAttribute(LOWER, Double.NEGATIVE_INFINITY);
101 double upper = xo.getAttribute(UPPER, Double.POSITIVE_INFINITY);
102
103 DistributionLikelihood dl = (DistributionLikelihood)xo.getChild(DistributionLikelihood.class);
104
105 DistributionLikelihood likelihood = new DistributionLikelihood(new TruncatedDistribution(dl.getDistribution(), lower, upper));
106 for (Attribute<double[]> data : dl.getDataList()) {
107 likelihood.addData(data);
108 }
109
110 return likelihood;
111 }
112
113 @Override
114 public XMLSyntaxRule[] getSyntaxRules() {
115 return rules;
116 }
117
118 private final XMLSyntaxRule[] rules = {
119 AttributeRule.newDoubleRule(LOWER, true),
120 AttributeRule.newDoubleRule(UPPER, true),
121 new ElementRule(DistributionLikelihood.class)
122 };
123
124 @Override
125 public String getParserDescription() {
126 return "Truncates the enclosed distribution to the given bounds.";
127 }
128
129 @Override
130 public Class getReturnType() {
131 return Likelihood.class;
132 }
133 };
134
135 /**
136 * A special parser that reads a convenient short form of priors on parameters.
137 */
138 public static XMLObjectParser UNIFORM_PRIOR_PARSER = new AbstractXMLObjectParser() {
139
140 public String getParserName() {
141 return UNIFORM_PRIOR;
142 }
143
144 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
145
146 double lower = xo.getDoubleAttribute(LOWER);
147 double upper = xo.getDoubleAttribute(UPPER);
148
149 if (lower == Double.NEGATIVE_INFINITY || upper == Double.POSITIVE_INFINITY)
150 throw new XMLParseException("Uniform prior " + xo.getName() + " cannot take a bound at infinity, " +
151 "because it returns 1/(high-low) = 1/inf");
152
153 DistributionLikelihood likelihood = new DistributionLikelihood(new UniformDistribution(lower, upper));
154 if (DEBUG) {
155 System.out.println("Uniform prior: " + xo.getChildCount());
156 }
157 for (int j = 0; j < xo.getChildCount(); j++) {
158 if (DEBUG) {
159 System.out.println(xo.getChild(j));
160 }
161 if (xo.getChild(j) instanceof Statistic) {
162 if (DEBUG) {
163 //System.out.println((Statistic) xo.getChild(j));
164 Statistic test = (Statistic) xo.getChild(j);
165 System.out.println(test.getDimension());
166 for (int i = 0; i < test.getDimension(); i++) {
167 System.out.println(" " + test.getDimensionName(i) + " - " + test.getStatisticValue(i));
168 }
169 System.out.println(test.getClass());
170 }
171 likelihood.addData((Statistic) xo.getChild(j));
172 if (DEBUG) {
173 likelihood.calculateLogLikelihood();
174 System.out.println("likelihood: " + likelihood.getLogLikelihood());
175 }
176 } else {
177 throw new XMLParseException("illegal element in " + xo.getName() + " element");
178 }
179 }
180 return likelihood;
181 }
182
183 public XMLSyntaxRule[] getSyntaxRules() {
184 return rules;
185 }
186
187 private final XMLSyntaxRule[] rules = {
188 AttributeRule.newDoubleRule(LOWER),
189 AttributeRule.newDoubleRule(UPPER),
190 new ElementRule(Statistic.class, 1, Integer.MAX_VALUE)
191 };
192
193 public String getParserDescription() {
194 return "Calculates the prior probability of some data under a given uniform distribution.";
195 }
196
197 public Class getReturnType() {
198 return Likelihood.class;
199 }
200 };
201
202 /**
203 * A special parser that reads a convenient short form of priors on parameters.
204 */
205 public static XMLObjectParser EXPONENTIAL_PRIOR_PARSER = new AbstractXMLObjectParser() {
206
207 public String getParserName() {
208 return EXPONENTIAL_PRIOR;
209 }
210
211 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
212
213 double scale;
214
215 if (xo.hasAttribute(SCALE)) {
216 scale = xo.getDoubleAttribute(SCALE);
217 } else {
218 scale = xo.getDoubleAttribute(MEAN);
219 }
220 final double offset = xo.hasAttribute(OFFSET) ? xo.getDoubleAttribute(OFFSET) : 0.0;
221
222 DistributionLikelihood likelihood = new DistributionLikelihood(new ExponentialDistribution(1.0 / scale), offset);
223 if (DEBUG) {
224 System.out.println("Exponential prior: " + xo.getChildCount());
225 }
226 for (int j = 0; j < xo.getChildCount(); j++) {
227 if (DEBUG) {
228 System.out.println(xo.getChild(j));
229 }
230 if (xo.getChild(j) instanceof Statistic) {
231 if (DEBUG) {
232 System.out.println("scale: " + scale);
233 System.out.println("offset: " + offset);
234 //System.out.println((Statistic) xo.getChild(j));
235 Statistic test = (Statistic) xo.getChild(j);
236 System.out.println(test.getDimension());
237 for (int i = 0; i < test.getDimension(); i++) {
238 System.out.println(" " + test.getDimensionName(i) + " - " + test.getStatisticValue(i));
239 }
240 System.out.println(test.getClass());
241 }
242 likelihood.addData((Statistic) xo.getChild(j));
243 if (DEBUG) {
244 likelihood.makeDirty();
245 likelihood.calculateLogLikelihood();
246 System.out.println("likelihood: " + likelihood.getLogLikelihood());
247 }
248 } else {
249 throw new XMLParseException("illegal element in " + xo.getName() + " element");
250 }
251 }
252
253 return likelihood;
254 }
255
256 public XMLSyntaxRule[] getSyntaxRules() {
257 return rules;
258 }
259
260 private final XMLSyntaxRule[] rules = {
261 new XORRule(
262 AttributeRule.newDoubleRule(SCALE),
263 AttributeRule.newDoubleRule(MEAN)
264 ),
265 AttributeRule.newDoubleRule(OFFSET, true),
266 new ElementRule(Statistic.class, 1, Integer.MAX_VALUE)
267 };
268
269 public String getParserDescription() {
270 return "Calculates the prior probability of some data under a given exponential distribution.";
271 }
272
273 public Class getReturnType() {
274 return Likelihood.class;
275 }
276 };
277
278 /**
279 * A special parser that reads a convenient short form of priors on parameters.
280 */
281 public static XMLObjectParser POISSON_PRIOR_PARSER = new AbstractXMLObjectParser() {
282
283 public String getParserName() {
284 return POISSON_PRIOR;
285 }
286
287 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
288
289 double mean = xo.getDoubleAttribute(MEAN);
290 double offset = xo.getDoubleAttribute(OFFSET);
291
292 DistributionLikelihood likelihood = new DistributionLikelihood(new PoissonDistribution(mean), offset);
293 for (int j = 0; j < xo.getChildCount(); j++) {
294 if (xo.getChild(j) instanceof Statistic) {
295 likelihood.addData((Statistic) xo.getChild(j));
296 } else {
297 throw new XMLParseException("illegal element in " + xo.getName() + " element");
298 }
299 }
300
301 return likelihood;
302 }
303
304 public XMLSyntaxRule[] getSyntaxRules() {
305 return rules;
306 }
307
308 private final XMLSyntaxRule[] rules = {
309 AttributeRule.newDoubleRule(MEAN),
310 AttributeRule.newDoubleRule(OFFSET),
311 new ElementRule(Statistic.class, 1, Integer.MAX_VALUE)
312 };
313
314 public String getParserDescription() {
315 return "Calculates the prior probability of some data under a given poisson distribution.";
316 }
317
318 public Class getReturnType() {
319 return Likelihood.class;
320 }
321 };
322
323 /**
324 * A special parser that reads a convenient short form of priors on parameters.
325 */
326 public static XMLObjectParser NEGATIVE_BINOMIAL_PRIOR_PARSER = new AbstractXMLObjectParser() {
327
328 public String getParserName() {
329 return NEGATIVE_BINOMIAL_PRIOR;
330 }
331
332 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
333
334 double mean = xo.getDoubleAttribute(MEAN);
335 double stdev = xo.getDoubleAttribute(STDEV);
336
337 DistributionLikelihood likelihood = new DistributionLikelihood(new NegativeBinomialDistribution(mean, stdev));
338 for (int j = 0; j < xo.getChildCount(); j++) {
339 if (xo.getChild(j) instanceof Statistic) {
340 likelihood.addData((Statistic) xo.getChild(j));
341 } else {
342 throw new XMLParseException("illegal element in " + xo.getName() + " element");
343 }
344 }
345
346 return likelihood;
347 }
348
349 public XMLSyntaxRule[] getSyntaxRules() {
350 return rules;
351 }
352
353 private final XMLSyntaxRule[] rules = {
354 AttributeRule.newDoubleRule(MEAN),
355 AttributeRule.newDoubleRule(STDEV),
356 new ElementRule(Statistic.class, 1, Integer.MAX_VALUE)
357 };
358
359 public String getParserDescription() {
360 return "Calculates the prior probability of some data under a given negative binomial distribution.";
361 }
362
363 public Class getReturnType() {
364 return Likelihood.class;
365 }
366 };
367
368 /**
369 * A special parser that reads a convenient short form of priors on parameters.
370 */
371 public static XMLObjectParser DISCRETE_UNIFORM_PRIOR_PARSER = new AbstractXMLObjectParser() {
372
373 public String getParserName() {
374 return DISCRETE_UNIFORM_PRIOR;
375 }
376
377 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
378
379 double upper = xo.getDoubleAttribute(UPPER);
380 double lower = xo.getDoubleAttribute(LOWER);
381
382 DistributionLikelihood likelihood = new DistributionLikelihood(new DiscreteUniformDistribution(lower, upper));
383 for (int j = 0; j < xo.getChildCount(); j++) {
384 if (xo.getChild(j) instanceof Statistic) {
385 likelihood.addData((Statistic) xo.getChild(j));
386 } else {
387 throw new XMLParseException("illegal element in " + xo.getName() + " element");
388 }
389 }
390
391 return likelihood;
392 }
393
394 public XMLSyntaxRule[] getSyntaxRules() {
395 return rules;
396 }
397
398 private final XMLSyntaxRule[] rules = {
399 AttributeRule.newDoubleRule(LOWER),
400 AttributeRule.newDoubleRule(UPPER),
401 new ElementRule(Statistic.class, 1, Integer.MAX_VALUE)
402 };
403
404 public String getParserDescription() {
405 return "Calculates the prior probability of some data under a given discrete uniform distribution.";
406 }
407
408 public Class getReturnType() {
409 return Likelihood.class;
410 }
411 };
412
413
414
415 /**
416 * A special parser that reads a convenient short form of priors on parameters.
417 */
418 public static XMLObjectParser HALF_T_PARSER = new AbstractXMLObjectParser() {
419
420 public String getParserName() {
421 return HALF_T_PRIOR;
422 }
423
424 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
425
426 double scale = xo.getDoubleAttribute(SCALE);
427 double df = xo.getDoubleAttribute(DF);
428
429 DistributionLikelihood likelihood = new DistributionLikelihood(new HalfTDistribution(scale, df));
430 for (int j = 0; j < xo.getChildCount(); j++) {
431 if (xo.getChild(j) instanceof Statistic) {
432 likelihood.addData((Statistic) xo.getChild(j));
433 } else {
434 throw new XMLParseException("illegal element in " + xo.getName() + " element");
435 }
436 }
437
438 return likelihood;
439 }
440
441 public XMLSyntaxRule[] getSyntaxRules() {
442 return rules;
443 }
444
445 private final XMLSyntaxRule[] rules = {
446 AttributeRule.newDoubleRule(SCALE),
447 AttributeRule.newDoubleRule(DF),
448 new ElementRule(Statistic.class, 1, Integer.MAX_VALUE)
449 };
450
451 public String getParserDescription() {
452 return "Calculates the prior probability of some data under a given half-T distribution.";
453 }
454
455 public Class getReturnType() {
456 return Likelihood.class;
457 }
458 };
459
460 /**
461 * A special parser that reads a convenient short form of priors on parameters.
462 */
463 public static XMLObjectParser NORMAL_PRIOR_PARSER = new AbstractXMLObjectParser() {
464
465 public String getParserName() {
466 return NORMAL_PRIOR;
467 }
468
469 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
470
471 double mean = xo.getDoubleAttribute(MEAN);
472 double stdev = xo.getDoubleAttribute(STDEV);
473
474 DistributionLikelihood likelihood = new DistributionLikelihood(new NormalDistribution(mean, stdev));
475 for (int j = 0; j < xo.getChildCount(); j++) {
476 if (xo.getChild(j) instanceof Statistic) {
477 likelihood.addData((Statistic) xo.getChild(j));
478 } else {
479 throw new XMLParseException("illegal element in " + xo.getName() + " element");
480 }
481 }
482
483 return likelihood;
484 }
485
486 public XMLSyntaxRule[] getSyntaxRules() {
487 return rules;
488 }
489
490 private final XMLSyntaxRule[] rules = {
491 AttributeRule.newDoubleRule(MEAN),
492 AttributeRule.newDoubleRule(STDEV),
493 new ElementRule(Statistic.class, 1, Integer.MAX_VALUE)
494 };
495
496 public String getParserDescription() {
497 return "Calculates the prior probability of some data under a given normal distribution.";
498 }
499
500 public Class getReturnType() {
501 return Likelihood.class;
502 }
503 };
504
505 public static XMLObjectParser HALF_NORMAL_PRIOR_PARSER = new AbstractXMLObjectParser() {
506
507 public String getParserName() {
508 return HALF_NORMAL_PRIOR;
509 }
510
511 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
512
513 double mean = xo.getDoubleAttribute(MEAN);
514 double stdev = xo.getDoubleAttribute(STDEV);
515
516 DistributionLikelihood likelihood = new DistributionLikelihood(new HalfNormalDistribution(mean, stdev));
517 for (int j = 0; j < xo.getChildCount(); j++) {
518 if (xo.getChild(j) instanceof Statistic) {
519 likelihood.addData((Statistic) xo.getChild(j));
520 } else {
521 throw new XMLParseException("illegal element in " + xo.getName() + " element");
522 }
523 }
524
525 return likelihood;
526 }
527
528 public XMLSyntaxRule[] getSyntaxRules() {
529 return rules;
530 }
531
532 private final XMLSyntaxRule[] rules = {
533 AttributeRule.newDoubleRule(MEAN),
534 AttributeRule.newDoubleRule(STDEV),
535 new ElementRule(Statistic.class, 1, Integer.MAX_VALUE)
536 };
537
538 public String getParserDescription() {
539 return "Calculates the prior probability of some data under a given half-normal distribution.";
540 }
541
542 public Class getReturnType() {
543 return Likelihood.class;
544 }
545 };
546
547
548 /**
549 * A special parser that reads a convenient short form of priors on parameters.
550 * <p/>
551 * If X ~ logNormal, then log(X) ~ Normal.
552 * <br>
553 * <br>
554 * If meanInRealSpace=false, <code>mean</code> specifies the mean of log(X) and
555 * <code>stdev</code> specifies the standard deviation of log(X).
556 * <br>
557 * <br>
558 * If meanInRealSpace=true, <code>mean</code> specifies the mean of X, but <code>
559 * stdev</code> specifies the standard deviation of log(X).
560 * <br>
561 */
562 public static XMLObjectParser LOG_NORMAL_PRIOR_PARSER = new AbstractXMLObjectParser() {
563
564 public String getParserName() {
565 return LOG_NORMAL_PRIOR;
566 }
567
568 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
569
570 double mu;
571 double sigma;
572
573 final boolean meanInRealSpace = xo.getAttribute(MEAN_IN_REAL_SPACE, false);
574
575 if (xo.hasAttribute(MEAN_IN_REAL_SPACE)) {
576 // if the meanInRealSpace attribute is given then respect the old
577 // parser choices (i.e., specify mean and stdev but change their
578 // meaning to mu and sigma).
579
580 final double mean = xo.getDoubleAttribute(MEAN);
581 final double stdev = xo.getDoubleAttribute(STDEV);
582
583 if (meanInRealSpace) {
584 mu = Math.log(mean / Math.sqrt(1 + (stdev * stdev) / (mean * mean)));
585 } else {
586 mu = mean;
587 }
588
589 // in the previous version, the stdev (or 'Log(stdev)') parameter is sigma.
590 sigma = stdev;
591 } else {
592 // decide parameterization by whether mean, stdev or mu, sigma are given.
593 if (xo.hasAttribute(MEAN)) {
594 final double mean = xo.getDoubleAttribute(MEAN);
595 if (!xo.hasAttribute(STDEV)) {
596 throw new XMLParseException("Lognormal should be specified either with mean and stdev or mu and sigma.");
597 }
598 final double stdev = xo.getDoubleAttribute(STDEV);
599 if (mean <= 0) {
600 throw new XMLParseException("If specified with a mean in real space, the value should be positive.");
601 }
602 mu = Math.log(mean / Math.sqrt(1 + (stdev * stdev) / (mean * mean)));
603 sigma = Math.sqrt(Math.log(1 + (stdev * stdev) / (mean * mean)));
604 } else {
605 if (meanInRealSpace) {
606 throw new XMLParseException("Lognormal with 'meanInRealSpace' should be specified with mean and stdev.");
607 }
608 mu = xo.getDoubleAttribute(MU);
609 if (!xo.hasAttribute(SIGMA)) {
610 throw new XMLParseException("Lognormal should be specified either with mean and stdev or mu and sigma.");
611 }
612 sigma = xo.getDoubleAttribute(SIGMA);
613 }
614 }
615 final double offset = xo.getAttribute(OFFSET, 0.0);
616
617 final DistributionLikelihood likelihood = new DistributionLikelihood(new LogNormalDistribution(mu, sigma), offset);
618
619 for (int j = 0; j < xo.getChildCount(); j++) {
620 if (xo.getChild(j) instanceof Statistic) {
621 likelihood.addData((Statistic) xo.getChild(j));
622 } else {
623 throw new XMLParseException("illegal element in " + xo.getName() + " element");
624 }
625 }
626
627 return likelihood;
628 }
629
630 public XMLSyntaxRule[] getSyntaxRules() {
631 return rules;
632 }
633
634 private final XMLSyntaxRule[] rules = {
635 new XORRule(
636 AttributeRule.newDoubleRule(MEAN),
637 AttributeRule.newDoubleRule(MU)
638 ),
639 new XORRule(
640 AttributeRule.newDoubleRule(STDEV),
641 AttributeRule.newDoubleRule(SIGMA)
642 ),
643 AttributeRule.newDoubleRule(OFFSET, true),
644 AttributeRule.newBooleanRule(MEAN_IN_REAL_SPACE, true),
645 new ElementRule(Statistic.class, 1, Integer.MAX_VALUE)
646 };
647
648 public String getParserDescription() {
649 return "Calculates the prior probability of some data under a given lognormal distribution.";
650 }
651
652 public Class getReturnType() {
653 return Likelihood.class;
654 }
655 };
656
657
658 /**
659 * A special parser that reads a convenient short form of priors on parameters.
660 */
661 public static XMLObjectParser GAMMA_PRIOR_PARSER = new AbstractXMLObjectParser() {
662
663 public String getParserName() {
664 return GAMMA_PRIOR;
665 }
666
667 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
668
669 final double shape = xo.getDoubleAttribute(SHAPE);
670 final double scale = xo.getDoubleAttribute(SCALE);
671 final double offset = xo.getAttribute(OFFSET, 0.0);
672
673 DistributionLikelihood likelihood = new DistributionLikelihood(new GammaDistribution(shape, scale), offset);
674 for (int j = 0; j < xo.getChildCount(); j++) {
675 if (xo.getChild(j) instanceof Statistic) {
676 likelihood.addData((Statistic) xo.getChild(j));
677 } else {
678 throw new XMLParseException("illegal element in " + xo.getName() + " element");
679 }
680 }
681
682 return likelihood;
683 }
684
685 public XMLSyntaxRule[] getSyntaxRules() {
686 return rules;
687 }
688
689 private final XMLSyntaxRule[] rules = {
690 AttributeRule.newDoubleRule(SHAPE),
691 AttributeRule.newDoubleRule(SCALE),
692 AttributeRule.newDoubleRule(OFFSET, true),
693 // AttributeRule.newBooleanRule(UNINFORMATIVE, true),
694 new ElementRule(Statistic.class, 1, Integer.MAX_VALUE)
695 };
696
697 public String getParserDescription() {
698 return "Calculates the prior probability of some data under a given gamma distribution.";
699 }
700
701 public Class getReturnType() {
702 return Likelihood.class;
703 }
704 };
705
706 /**
707 * A special parser that reads a convenient short form of priors on parameters.
708 */
709 public static XMLObjectParser INVGAMMA_PRIOR_PARSER = new AbstractXMLObjectParser() {
710
711 public String getParserName() {
712 return INVGAMMA_PRIOR;
713 }
714
715 public String[] getParserNames() {
716 return new String[]{INVGAMMA_PRIOR, INVGAMMA_PRIOR_CORRECT};
717 }
718
719 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
720
721 final double shape = xo.getDoubleAttribute(SHAPE);
722 final double scale = xo.getDoubleAttribute(SCALE);
723 final double offset = xo.getAttribute(OFFSET, 0.0);
724
725 DistributionLikelihood likelihood = new DistributionLikelihood(new InverseGammaDistribution(shape, scale), offset);
726
727 for (int j = 0; j < xo.getChildCount(); j++) {
728 if (xo.getChild(j) instanceof Statistic) {
729 likelihood.addData((Statistic) xo.getChild(j));
730 } else {
731 throw new XMLParseException("illegal element in " + xo.getName() + " element");
732 }
733 }
734
735 return likelihood;
736 }
737
738 public XMLSyntaxRule[] getSyntaxRules() {
739 return rules;
740 }
741
742 private final XMLSyntaxRule[] rules = {
743 AttributeRule.newDoubleRule(SHAPE),
744 AttributeRule.newDoubleRule(SCALE),
745 AttributeRule.newDoubleRule(OFFSET, true),
746 new ElementRule(Statistic.class, 1, Integer.MAX_VALUE)
747 };
748
749 public String getParserDescription() {
750 return "Calculates the prior probability of some data under a given inverse gamma distribution.";
751 }
752
753 public Class getReturnType() {
754 return Likelihood.class;
755 }
756 };
757
758 public static XMLObjectParser LAPLACE_PRIOR_PARSER = new AbstractXMLObjectParser() {
759
760 public String getParserName() {
761 return LAPLACE_PRIOR;
762 }
763
764 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
765
766 double mean = xo.getDoubleAttribute(MEAN);
767 double scale = xo.getDoubleAttribute(SCALE);
768
769 DistributionLikelihood likelihood = new DistributionLikelihood(new LaplaceDistribution(mean, scale));
770 for (int j = 0; j < xo.getChildCount(); j++) {
771 if (xo.getChild(j) instanceof Statistic) {
772 likelihood.addData((Statistic) xo.getChild(j));
773 } else {
774 throw new XMLParseException("illegal element in " + xo.getName() + " element");
775 }
776 }
777
778 return likelihood;
779 }
780
781 public XMLSyntaxRule[] getSyntaxRules() {
782 return rules;
783 }
784
785 private final XMLSyntaxRule[] rules = {
786 AttributeRule.newDoubleRule(MEAN),
787 AttributeRule.newDoubleRule(SCALE),
788 new ElementRule(Statistic.class, 1, Integer.MAX_VALUE)
789 };
790
791 public String getParserDescription() {
792 return "Calculates the prior probability of some data under a given laplace distribution.";
793 }
794
795 public Class getReturnType() {
796 return Likelihood.class;
797 }
798 };
799
800 /**
801 * A special parser that reads a convenient short form of priors on parameters.
802 */
803 public static XMLObjectParser BETA_PRIOR_PARSER = new AbstractXMLObjectParser() {
804
805 public String getParserName() {
806 return BETA_PRIOR;
807 }
808
809 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
810
811 final double shape = xo.getDoubleAttribute(SHAPE);
812 final double shapeB = xo.getDoubleAttribute(SHAPEB);
813 final double offset = xo.getAttribute(OFFSET, 0.0);
814 final double scale = xo.getAttribute(SCALE, 1.0);
815
816 DistributionLikelihood likelihood = new DistributionLikelihood(new BetaDistribution(shape, shapeB), offset, scale);
817 for (int j = 0; j < xo.getChildCount(); j++) {
818 if (xo.getChild(j) instanceof Statistic) {
819 likelihood.addData((Statistic) xo.getChild(j));
820 } else {
821 throw new XMLParseException("illegal element in " + xo.getName() + " element");
822 }
823 }
824
825 return likelihood;
826 }
827
828 public XMLSyntaxRule[] getSyntaxRules() {
829 return rules;
830 }
831
832 private final XMLSyntaxRule[] rules = {
833 AttributeRule.newDoubleRule(SHAPE),
834 AttributeRule.newDoubleRule(SHAPEB),
835 AttributeRule.newDoubleRule(OFFSET, true),
836 new ElementRule(Statistic.class, 1, Integer.MAX_VALUE)
837 };
838
839 public String getParserDescription() {
840 return "Calculates the prior probability of some data under a given beta distribution.";
841 }
842
843 public Class getReturnType() {
844 return Likelihood.class;
845 }
846 };
847
848 /**
849 * A special parser that reads a convenient short form of priors on parameters.
850 */
851 public static XMLObjectParser DIRICHLET_PRIOR_PARSER = new AbstractXMLObjectParser() {
852
853 public String getParserName() {
854 return DIRICHLET_PRIOR;
855 }
856
857 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
858
859 List<Statistic> data = new ArrayList<Statistic>();
860 int dim = 0;
861 double sum = 0;
862 for (int j = 0; j < xo.getChildCount(); j++) {
863 if (xo.getChild(j) instanceof Statistic) {
864 Statistic statistic = (Statistic) xo.getChild(j);
865 dim += statistic.getDimension();
866 sum += statistic.getValueSum();
867 data.add(statistic);
868 } else {
869 throw new XMLParseException("Illegal element in " + xo.getName() + " element.");
870 }
871 }
872
873 double[] counts;
874
875 if (xo.hasAttribute(ALPHA)) {
876 // alpha is for when the Dirichlet distribution is symmetrical
877 double alpha = xo.getDoubleAttribute(ALPHA);
878 counts = new double[dim];
879 for (int i = 0; i < dim; i++) {
880 counts[i] = alpha;
881 }
882 } else {
883 counts = xo.getDoubleArrayAttribute(COUNTS);
884 if (counts.length != dim) {
885 throw new XMLParseException("Counts attribute in " + xo.getName() + " should have the same dimension as the data.");
886 }
887 }
888
889 double sumsTo = 1.0;
890 if (xo.hasAttribute(SUMS_TO)) {
891 sumsTo = xo.getDoubleAttribute(SUMS_TO);
892 }
893 if (Math.abs(sumsTo - sum) > DirichletDistribution.ACCURACY_THRESHOLD) {
894 throw new XMLParseException("The starting values of the data should sum to " + sumsTo + " (actually sums to " + sum + ").");
895 }
896
897 MultivariateDistributionLikelihood likelihood = new MultivariateDistributionLikelihood(new DirichletDistribution(counts, sumsTo));
898
899 for (Statistic statistic : data) {
900 likelihood.addData(statistic);
901 }
902
903 return likelihood;
904 }
905
906 public XMLSyntaxRule[] getSyntaxRules() {
907 return rules;
908 }
909
910 private final XMLSyntaxRule[] rules = {
911 new XORRule(
912 AttributeRule.newDoubleArrayRule(ALPHA),
913 AttributeRule.newDoubleArrayRule(COUNTS)
914 ),
915 AttributeRule.newDoubleRule(SUMS_TO, true),
916 new ElementRule(Statistic.class, 1, Integer.MAX_VALUE)
917 };
918
919 public String getParserDescription() {
920 return "Calculates the prior probability of some data under a Dirichlet distribution.";
921 }
922
923 public Class getReturnType() {
924 return MultivariateDistributionLikelihood.class;
925 }
926 };
927
928
929
930 public static XMLObjectParser CAUCH_PRIOR_PARSER = new AbstractXMLObjectParser() {
931
932 public String getParserName() {
933 return CAUCHY_PRIOR;
934 }
935
936 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
937
938 final double median = xo.getDoubleAttribute(MEDIAN);
939 final double scale = xo.getDoubleAttribute(SCALE);
940
941 DistributionLikelihood likelihood = new DistributionLikelihood(new CauchyDistribution(median, scale));
942 for (int j = 0; j < xo.getChildCount(); j++) {
943 if (xo.getChild(j) instanceof Statistic) {
944 likelihood.addData((Statistic) xo.getChild(j));
945 } else {
946 throw new XMLParseException("illegal element in " + xo.getName() + " element");
947 }
948 }
949
950 return likelihood;
951 }
952
953 public XMLSyntaxRule[] getSyntaxRules() {
954 return rules;
955 }
956
957 private final XMLSyntaxRule[] rules = {
958 AttributeRule.newDoubleRule(MEDIAN),
959 AttributeRule.newDoubleRule(SCALE),
960 new ElementRule(Statistic.class, 1, Integer.MAX_VALUE)
961 };
962 public String getParserDescription() {
963 return "Calculates the prior probability of some data under a Cauchy distribution.";
964 }
965
966 public Class getReturnType() {
967 return DistributionLikelihood.class;
968 }
969 };
970
971 }