Debian Codegraph @

src/dr/inferencexml/distribution/LogNormalDistributionModelParser.java

raw symbols SCIP
1 /*
2 * LogNormalDistributionModelParser.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.LogNormalDistributionModel;
29 import dr.inference.model.Parameter;
30 import dr.xml.*;
31
32 /**
33 * Reads a normal distribution model from a DOM Document element.
34 */
35 public class LogNormalDistributionModelParser extends AbstractXMLObjectParser {
36
37 public static final String LOGNORMAL_DISTRIBUTION_MODEL = "logNormalDistributionModel";
38 public static final String MEAN = "mean";
39 public static final String STDEV = "stdev";
40 public static final String MU = "mu";
41 public static final String SIGMA = "sigma";
42 public static final String PRECISION = "precision";
43 public static final String OFFSET = "offset";
44 public static final String MEAN_IN_REAL_SPACE = "meanInRealSpace";
45 public static final String STDEV_IN_REAL_SPACE = "stdevInRealSpace";
46
47 public String getParserName() {
48 return LOGNORMAL_DISTRIBUTION_MODEL;
49 }
50
51 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
52
53 final double offset = xo.getAttribute(OFFSET, 0.0);
54
55 if (xo.hasAttribute(MEAN_IN_REAL_SPACE) || xo.hasAttribute(STDEV_IN_REAL_SPACE)) {
56 // If either of these set then use the old logic for parsing. This means that mean
57 // means mu if meanInRealSpace is false and stdev means sigma if stdevInRealSpace
58 // is false
59
60 if (!xo.hasChildNamed(MEAN) || !xo.hasChildNamed(STDEV)) {
61 throw new XMLParseException("If meanInRealSpace attribute is given then the lognormal model must be parameterized with mean and stdev.");
62 }
63
64 final boolean meanInRealSpace = xo.getAttribute(MEAN_IN_REAL_SPACE, false);
65 if (xo.hasAttribute(STDEV_IN_REAL_SPACE)) {
66 boolean stdevInRealSpace = xo.getAttribute(STDEV_IN_REAL_SPACE, false);
67 if (!meanInRealSpace && stdevInRealSpace) {
68 throw new XMLParseException("Cannot parameterise Lognormal model with mu and stdev");
69 }
70 if (meanInRealSpace && !stdevInRealSpace) {
71 throw new XMLParseException("Cannot parameterise Lognormal model with mean and sigma");
72 }
73 }
74
75 XMLObject cxo = xo.getChild(MEAN);
76 Parameter meanParam;
77 if (cxo.getChild(0) instanceof Parameter) {
78 meanParam = (Parameter) cxo.getChild(Parameter.class);
79 } else {
80 meanParam = new Parameter.Default(cxo.getDoubleChild(0));
81 }
82
83 cxo = xo.getChild(STDEV);
84 Parameter stdevParam;
85 if (cxo.getChild(0) instanceof Parameter) {
86 stdevParam = (Parameter) cxo.getChild(Parameter.class);
87 } else {
88 stdevParam = new Parameter.Default(cxo.getDoubleChild(0));
89 }
90
91 return new LogNormalDistributionModel(meanParam, stdevParam, offset, meanInRealSpace);
92 } else {
93 // otherwise we decide the parameterization by which parameters are specified.
94
95 if (xo.hasChildNamed(MEAN) && xo.hasChildNamed(STDEV)) {
96 XMLObject cxo = xo.getChild(MEAN);
97 Parameter meanParam;
98 if (cxo.getChild(0) instanceof Parameter) {
99 meanParam = (Parameter) cxo.getChild(Parameter.class);
100 } else {
101 meanParam = new Parameter.Default(cxo.getDoubleChild(0));
102 }
103
104 cxo = xo.getChild(STDEV);
105 Parameter stdevParam;
106 if (cxo.getChild(0) instanceof Parameter) {
107 stdevParam = (Parameter) cxo.getChild(Parameter.class);
108 } else {
109 stdevParam = new Parameter.Default(cxo.getDoubleChild(0));
110 }
111 return new LogNormalDistributionModel(LogNormalDistributionModel.Parameterization.MEAN_STDEV, meanParam, stdevParam, offset);
112 } else if (xo.hasChildNamed(MU)) {
113 XMLObject cxo = xo.getChild(MU);
114 Parameter muParam;
115 if (cxo.getChild(0) instanceof Parameter) {
116 muParam = (Parameter) cxo.getChild(Parameter.class);
117 } else {
118 muParam = new Parameter.Default(cxo.getDoubleChild(0));
119 }
120
121 if (xo.hasChildNamed(SIGMA)) {
122 cxo = xo.getChild(SIGMA);
123 Parameter sigmaParam;
124 if (cxo.getChild(0) instanceof Parameter) {
125 sigmaParam = (Parameter) cxo.getChild(Parameter.class);
126 } else {
127 sigmaParam = new Parameter.Default(cxo.getDoubleChild(0));
128 }
129 return new LogNormalDistributionModel(LogNormalDistributionModel.Parameterization.MU_SIGMA, muParam, sigmaParam, offset);
130 } else if (xo.hasChildNamed(PRECISION)) {
131 cxo = xo.getChild(PRECISION);
132 Parameter precParam;
133 if (cxo.getChild(0) instanceof Parameter) {
134 precParam = (Parameter) cxo.getChild(Parameter.class);
135 } else {
136 precParam = new Parameter.Default(cxo.getDoubleChild(0));
137 }
138 return new LogNormalDistributionModel(LogNormalDistributionModel.Parameterization.MU_PRECISION, muParam, precParam, offset);
139
140 } else {
141 throw new XMLParseException("Lognormal model must be parameterized as [mean, stdev], [mu, sigma], or [mu, precision]");
142 }
143 } else {
144 throw new XMLParseException("Lognormal model must be parameterized as [mean, stdev], [mu, sigma], or [mu, precision]");
145 }
146
147 }
148 }
149
150 //************************************************************************
151 // AbstractXMLObjectParser implementation
152 //************************************************************************
153
154 public XMLSyntaxRule[] getSyntaxRules() {
155 return rules;
156 }
157
158 private final XMLSyntaxRule[] rules = {
159 AttributeRule.newBooleanRule(MEAN_IN_REAL_SPACE, true),
160 AttributeRule.newBooleanRule(STDEV_IN_REAL_SPACE, true),
161 AttributeRule.newDoubleRule(OFFSET, true),
162 new XORRule(new ElementRule(MEAN,
163 new XMLSyntaxRule[]{
164 new XORRule(
165 new ElementRule(Parameter.class),
166 new ElementRule(Double.class)
167 )}
168 ),
169 new ElementRule(MU,
170 new XMLSyntaxRule[]{
171 new XORRule(
172 new ElementRule(Parameter.class),
173 new ElementRule(Double.class)
174 )}
175 ))
176 ,
177 new XORRule(
178 new ElementRule(STDEV,
179 new XMLSyntaxRule[]{
180 new XORRule(
181 new ElementRule(Parameter.class),
182 new ElementRule(Double.class)
183 )}
184 ),
185 new ElementRule(PRECISION,
186 new XMLSyntaxRule[]{
187 new XORRule(
188 new ElementRule(Parameter.class),
189 new ElementRule(Double.class)
190 )}
191
192 ))
193 };
194
195 public String getParserDescription() {
196 return "Describes a normal distribution with a given mean and standard deviation " +
197 "that can be used in a distributionLikelihood element";
198 }
199
200 public Class getReturnType() {
201 return LogNormalDistributionModel.class;
202 }
203 }