Debian Codegraph @

src/dr/inferencexml/distribution/DistributionLikelihoodParser.java

raw symbols SCIP
1 /*
2 * DistributionLikelihoodParser.java
3 *
4 * Copyright (c) 2002-2015 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.DistributionLikelihood;
29 import dr.inference.distribution.ParametricDistributionModel;
30 import dr.inference.model.Statistic;
31 import dr.math.distributions.RandomGenerator;
32 import dr.xml.*;
33
34 /**
35 *
36 */
37 public class DistributionLikelihoodParser extends AbstractXMLObjectParser {
38
39 public static final String DISTRIBUTION = "distribution";
40 public static final String DATA = "data";
41 public static final String FROM = "from";
42 public static final String TO = "to";
43
44
45 public String getParserName() {
46 return DistributionLikelihood.DISTRIBUTION_LIKELIHOOD;
47 }
48
49 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
50
51 final XMLObject cxo = xo.getChild(DISTRIBUTION);
52 ParametricDistributionModel model = (ParametricDistributionModel) cxo.getChild(ParametricDistributionModel.class);
53
54 DistributionLikelihood likelihood = new DistributionLikelihood(model);
55
56 XMLObject cxo1 = xo.getChild(DATA);
57 final int from = cxo1.getAttribute(FROM, -1);
58 int to = cxo1.getAttribute(TO, -1);
59 if (from >= 0 || to >= 0) {
60 if (to < 0) {
61 to = Integer.MAX_VALUE;
62 }
63 if (!(from >= 0 && to >= 0 && from < to)) {
64 throw new XMLParseException("ill formed from-to");
65 }
66 likelihood.setRange(from, to);
67 }
68
69 for (int j = 0; j < cxo1.getChildCount(); j++) {
70 if (cxo1.getChild(j) instanceof Statistic) {
71
72 likelihood.addData((Statistic) cxo1.getChild(j));
73 } else {
74 throw new XMLParseException("illegal element in " + cxo1.getName() + " element");
75 }
76 }
77
78 return likelihood;
79 }
80
81 //************************************************************************
82 // AbstractXMLObjectParser implementation
83 //************************************************************************
84
85 public XMLSyntaxRule[] getSyntaxRules() {
86 return rules;
87 }
88
89 private final XMLSyntaxRule[] rules = {
90 new ElementRule(DISTRIBUTION,
91 new XMLSyntaxRule[]{new ElementRule(ParametricDistributionModel.class)}),
92 new ElementRule(DATA, new XMLSyntaxRule[]{
93 AttributeRule.newIntegerRule(FROM, true),
94 AttributeRule.newIntegerRule(TO, true),
95 new ElementRule(Statistic.class, 1, Integer.MAX_VALUE)
96 })
97 };
98
99 public String getParserDescription() {
100 return "Calculates the likelihood of some data given some parametric or empirical distribution.";
101 }
102
103 public Class getReturnType() {
104 return DistributionLikelihood.class;
105 }
106 }