Debian Codegraph @

src/dr/inferencexml/distribution/BinomialLikelihoodParser.java

raw symbols SCIP
1 /*
2 * BinomialLikelihoodParser.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.BinomialLikelihood;
29 import dr.inference.model.Likelihood;
30 import dr.inference.model.Parameter;
31 import dr.xml.*;
32
33 /**
34 *
35 */
36 public class BinomialLikelihoodParser extends AbstractXMLObjectParser {
37
38 public static final String TRIALS = "trials";
39 public static final String COUNTS = "counts";
40 public static final String PROPORTION = "proportion";
41 public static final String VALUES = "values";
42 public static final String ON_LOGIT_SCALE = "onLogitScale";
43
44 public String getParserName() {
45 return BinomialLikelihood.BINOMIAL_LIKELIHOOD;
46 }
47
48 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
49
50 final boolean onLogitScale = xo.getAttribute(ON_LOGIT_SCALE, false);
51
52 XMLObject cxo = xo.getChild(COUNTS);
53 Parameter countsParam = (Parameter) cxo.getChild(Parameter.class);
54
55 cxo = xo.getChild(PROPORTION);
56 Parameter proportionParam = (Parameter) cxo.getChild(Parameter.class);
57
58 if (proportionParam.getDimension() != 1 && proportionParam.getDimension() != countsParam.getDimension()) {
59 throw new XMLParseException("Proportion dimension (" + proportionParam.getDimension() + ") " +
60 "must equal 1 or counts dimension (" + countsParam.getDimension() + ")");
61 }
62
63 Parameter trialsParam;
64 cxo = xo.getChild(TRIALS);
65 if (cxo != null) {
66 if (cxo.hasAttribute(VALUES)) {
67 int[] tmp = cxo.getIntegerArrayAttribute(VALUES);
68 double[] v = new double[tmp.length];
69 for (int i = 0; i < tmp.length; ++i) {
70 v[i] = tmp[i];
71 }
72 trialsParam = new Parameter.Default(v);
73 } else {
74 trialsParam = (Parameter) cxo.getChild(Parameter.class);
75 }
76 } else {
77 trialsParam = new Parameter.Default(1.0);
78 }
79
80 if (trialsParam.getDimension() <= 1) {
81 try {
82 trialsParam.setDimension(countsParam.getDimension());
83 } catch (UnsupportedOperationException uoe) {
84 // This is a parameter that cannot have its dimension changed (i.e., a CompoundParameter)
85 // Just ignore this exception and the next bit will check if it is OK.
86 }
87 }
88
89 if (trialsParam.getDimension() != countsParam.getDimension()) {
90 throw new XMLParseException("Trials dimension (" + trialsParam.getDimension()
91 + ") must equal counts dimension (" + countsParam.getDimension() + ")");
92 }
93
94
95 return new BinomialLikelihood(trialsParam, proportionParam, countsParam, onLogitScale);
96
97 }
98
99 //************************************************************************
100 // AbstractXMLObjectParser implementation
101 //************************************************************************
102
103 public XMLSyntaxRule[] getSyntaxRules() {
104 return rules;
105 }
106
107 private final XMLSyntaxRule[] rules = {
108 AttributeRule.newBooleanRule(ON_LOGIT_SCALE, true),
109 new ElementRule(COUNTS,
110 new XMLSyntaxRule[]{new ElementRule(Parameter.class)}),
111 new ElementRule(PROPORTION,
112 new XMLSyntaxRule[]{new ElementRule(Parameter.class)}),
113 new XORRule(
114 new ElementRule(TRIALS,
115 new XMLSyntaxRule[]{AttributeRule.newIntegerArrayRule(VALUES, false),})
116 ,
117 new ElementRule(TRIALS,
118 new XMLSyntaxRule[]{new ElementRule(Parameter.class)}
119 )),
120 };
121
122 public String getParserDescription() {
123 return "Calculates the likelihood of some data given some parametric or empirical distribution.";
124 }
125
126 public Class getReturnType() {
127 return Likelihood.class;
128 }
129 }