Debian Codegraph @

src/dr/app/beauti/components/hpm/HierarchicalModelComponentGenerator.java

raw symbols SCIP
1 /*
2 * HierarchicalModelComponentGenerator.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.app.beauti.components.hpm;
27
28 import dr.app.beauti.generator.BaseComponentGenerator;
29 import dr.app.beauti.options.BeautiOptions;
30 import dr.app.beauti.options.Parameter;
31 import dr.app.beauti.util.XMLWriter;
32 import dr.inference.distribution.DistributionLikelihood;
33 import dr.inference.model.ParameterParser;
34 import dr.inference.operators.MCMCOperator;
35 import dr.inference.operators.NormalGammaPrecisionGibbsOperator;
36 import dr.inference.operators.NormalNormalMeanGibbsOperator;
37 import dr.inferencexml.distribution.DistributionLikelihoodParser;
38 import dr.inferencexml.distribution.LogNormalDistributionModelParser;
39 import dr.inferencexml.distribution.NormalDistributionModelParser;
40 import dr.inferencexml.distribution.PriorParsers;
41 import dr.util.Attribute;
42 import dr.xml.XMLParser;
43
44 import java.util.List;
45
46 /**
47 * @author Marc A. Suchard
48 * @version $Id$
49 */
50 public class HierarchicalModelComponentGenerator extends BaseComponentGenerator {
51
52 public static final String MEAN_SUFFIX = ".mean";
53 public static final String PRECISION_SUFFIX = ".precision";
54 public static final String MODEL_SUFFIX = ".model";
55 public static final String HPM_SUFFIX = ".hpm";
56 public static final String MEAN_PRIOR_SUFFIX = ".prior.mean";
57 public static final String PRECISION_PRIOR_SUFFIX = ".prior.precision";
58
59 public HierarchicalModelComponentGenerator(final BeautiOptions options) {
60 super(options);
61 }
62
63 public boolean usesInsertionPoint(final InsertionPoint point) {
64 HierarchicalModelComponentOptions comp = (HierarchicalModelComponentOptions)
65 options.getComponentOptions(HierarchicalModelComponentOptions.class);
66
67 switch (point) {
68 case AFTER_TREE_LIKELIHOOD:
69 case IN_OPERATORS:
70 case IN_MCMC_PRIOR:
71 case IN_FILE_LOG_PARAMETERS:
72 return !comp.isEmpty();
73 default:
74 return false;
75 }
76 }
77
78 protected void generate(final InsertionPoint point, final Object item, final String prefix, final XMLWriter writer) {
79 HierarchicalModelComponentOptions comp = (HierarchicalModelComponentOptions)
80 options.getComponentOptions(HierarchicalModelComponentOptions.class);
81
82 switch (point) {
83 case AFTER_TREE_LIKELIHOOD:
84 generateDistributions(comp.getHPMList(), writer);
85 break;
86 case IN_OPERATORS:
87 generateOperators(comp.getHPMList(), writer);
88 break;
89 case IN_MCMC_PRIOR:
90 generatePriors(comp.getHPMList(), writer);
91 break;
92 case IN_FILE_LOG_PARAMETERS:
93 generateLogs(comp.getHPMList(), writer);
94 break;
95 default:
96 throw new IllegalArgumentException("This insertion point is not implemented for " + this.getClass().getName());
97 }
98 }
99
100 private void generateOperators(List<HierarchicalPhylogeneticModel> hpmList, XMLWriter writer) {
101 for (HierarchicalPhylogeneticModel hpm : hpmList) {
102 generateOperators(hpm, writer);
103 }
104 }
105
106 private void generateOperators(HierarchicalPhylogeneticModel hpm, XMLWriter writer) {
107
108 // Generate Normal-normal operator on mean
109 writer.writeOpenTag(NormalNormalMeanGibbsOperator.OPERATOR_NAME, getOperatorAttributes());
110
111 writer.writeOpenTag(NormalNormalMeanGibbsOperator.LIKELIHOOD);
112 writer.writeIDref(DistributionLikelihoodParser.DISTRIBUTION, getDistributionName(hpm));
113 writer.writeCloseTag(NormalNormalMeanGibbsOperator.LIKELIHOOD);
114
115 writer.writeOpenTag(NormalNormalMeanGibbsOperator.PRIOR);
116 writer.writeIDref(PriorParsers.NORMAL_PRIOR, getMeanPriorName(hpm));
117 writer.writeCloseTag(NormalNormalMeanGibbsOperator.PRIOR);
118
119 writer.writeCloseTag(NormalNormalMeanGibbsOperator.OPERATOR_NAME);
120
121 // Generate Gamma-normal operator on precision
122 writer.writeOpenTag(NormalGammaPrecisionGibbsOperator.OPERATOR_NAME, getOperatorAttributes());
123
124 writer.writeOpenTag(NormalGammaPrecisionGibbsOperator.LIKELIHOOD);
125 writer.writeIDref(DistributionLikelihoodParser.DISTRIBUTION, getDistributionName(hpm));
126 writer.writeCloseTag(NormalGammaPrecisionGibbsOperator.LIKELIHOOD);
127
128 writer.writeOpenTag(NormalGammaPrecisionGibbsOperator.PRIOR);
129 writer.writeIDref(PriorParsers.GAMMA_PRIOR, getPrecisionPriorName(hpm));
130 writer.writeCloseTag(NormalGammaPrecisionGibbsOperator.PRIOR);
131
132 writer.writeCloseTag(NormalGammaPrecisionGibbsOperator.OPERATOR_NAME);
133 }
134
135 private void generateLogs(List<HierarchicalPhylogeneticModel> hpmList, XMLWriter writer) {
136 for (HierarchicalPhylogeneticModel hpm : hpmList) {
137 generateLog(hpm, writer);
138 }
139 }
140
141 private void generateLog(HierarchicalPhylogeneticModel hpm, XMLWriter writer) {
142 for (Parameter parameter : hpm.getConditionalParameterList()) {
143 writer.writeIDref(ParameterParser.PARAMETER, parameter.getName());
144 }
145 }
146
147 private void generatePriors(List<HierarchicalPhylogeneticModel> hpmList, XMLWriter writer) {
148 for (HierarchicalPhylogeneticModel hpm : hpmList) {
149 generatePrior(hpm, writer);
150 }
151 }
152
153 private void generatePrior(HierarchicalPhylogeneticModel hpm, XMLWriter writer) {
154 writer.writeIDref(DistributionLikelihood.DISTRIBUTION_LIKELIHOOD, getDistributionName(hpm));
155 writer.writeIDref(PriorParsers.NORMAL_PRIOR, getMeanPriorName(hpm));
156 writer.writeIDref(PriorParsers.GAMMA_PRIOR, getPrecisionPriorName(hpm));
157 }
158
159 private void generateDistributions(List<HierarchicalPhylogeneticModel> hpmList, XMLWriter writer) {
160 for (HierarchicalPhylogeneticModel hpm : hpmList) {
161 generateDistribution(hpm, writer);
162 generateNormalAndGammaPrior(hpm, writer);
163 }
164 }
165
166 private String getModelTagName(HierarchicalPhylogeneticModel hpm) {
167 switch (hpm.getPriorType()) {
168 case NORMAL_HPM_PRIOR:
169 return NormalDistributionModelParser.NORMAL_DISTRIBUTION_MODEL;
170 case LOGNORMAL_HPM_PRIOR:
171 return LogNormalDistributionModelParser.LOGNORMAL_DISTRIBUTION_MODEL;
172 default:
173 }
174 throw new RuntimeException("Unimplemented HPM prior type");
175 }
176
177 private String getDistributionName(HierarchicalPhylogeneticModel hpm) {
178 return hpm.getName() + HPM_SUFFIX;
179 }
180
181 private String getModelName(HierarchicalPhylogeneticModel hpm) {
182 return hpm.getName() + MODEL_SUFFIX;
183 }
184
185 private Attribute[] getOperatorAttributes() {
186 return new Attribute[] {
187 new Attribute.Default<Double>(MCMCOperator.WEIGHT, 1.0),
188 };
189 }
190
191 private Attribute[] getModelAttributes(HierarchicalPhylogeneticModel hpm) {
192 switch (hpm.getPriorType()){
193 case NORMAL_HPM_PRIOR:
194 return new Attribute[] {
195 new Attribute.Default<String>(XMLParser.ID, getModelName(hpm)),
196 };
197 case LOGNORMAL_HPM_PRIOR:
198 return new Attribute[] {
199 new Attribute.Default<String>(XMLParser.ID, getModelName(hpm)),
200 //new Attribute.Default<Boolean>(LogNormalDistributionModelParser.MEAN_IN_REAL_SPACE, false),
201 };
202 default:
203 }
204 throw new RuntimeException("Unimplemented HPM prior type");
205 }
206
207 private Attribute[] getMeanPriorAttributes(HierarchicalPhylogeneticModel hpm) {
208 return new Attribute[] {
209 new Attribute.Default<String>(XMLParser.ID, getMeanPriorName(hpm)),
210 new Attribute.Default<Double>(PriorParsers.MEAN, hpm.getConditionalParameterList().get(0).mean),
211 new Attribute.Default<Double>(PriorParsers.STDEV, hpm.getConditionalParameterList().get(0).stdev),
212 };
213 }
214
215 private String getMeanPriorName(HierarchicalPhylogeneticModel hpm) {
216 return hpm.getName() + MEAN_PRIOR_SUFFIX;
217 }
218
219 private String getPrecisionPriorName(HierarchicalPhylogeneticModel hpm) {
220 return hpm.getName() + PRECISION_PRIOR_SUFFIX;
221 }
222
223 private Attribute[] getPrecisionPriorAttributes(HierarchicalPhylogeneticModel hpm) {
224 return new Attribute[] {
225 new Attribute.Default<String>(XMLParser.ID, getPrecisionPriorName(hpm)),
226 new Attribute.Default<Double>(PriorParsers.SHAPE, hpm.getConditionalParameterList().get(1).shape),
227 new Attribute.Default<Double>(PriorParsers.SCALE, hpm.getConditionalParameterList().get(1).scale),
228 new Attribute.Default<Double>(PriorParsers.OFFSET, 0.0),
229 };
230 }
231
232 private void generateDistribution(HierarchicalPhylogeneticModel hpm, XMLWriter writer) {
233
234 writer.writeOpenTag(DistributionLikelihood.DISTRIBUTION_LIKELIHOOD,
235 new Attribute[] {
236 new Attribute.Default<String>(XMLParser.ID, getDistributionName(hpm)),
237 });
238
239 // Add parameters as data
240 writer.writeOpenTag(DistributionLikelihoodParser.DATA);
241 for (Parameter parameter : hpm.getArgumentParameterList()) {
242 writeParameterRef(parameter.getName(), writer);
243 }
244 writer.writeCloseTag(DistributionLikelihoodParser.DATA);
245
246 // Add HPM model
247 writer.writeOpenTag(DistributionLikelihoodParser.DISTRIBUTION);
248 writer.writeOpenTag(getModelTagName(hpm), getModelAttributes(hpm));
249
250 writeParameter(NormalDistributionModelParser.MU,
251 hpm.getConditionalParameterList().get(0).getName(), 1, hpm.getConditionalParameterList().get(0).getInitial(),
252 Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, writer);
253 writeParameter(NormalDistributionModelParser.PREC,
254 hpm.getConditionalParameterList().get(1).getName(), 1, hpm.getConditionalParameterList().get(1).getInitial(),
255 0.0, Double.POSITIVE_INFINITY, writer);
256 writer.writeCloseTag(getModelTagName(hpm));
257
258 writer.writeCloseTag(DistributionLikelihoodParser.DISTRIBUTION);
259
260 writer.writeCloseTag(DistributionLikelihood.DISTRIBUTION_LIKELIHOOD);
261 }
262
263 private void generateNormalAndGammaPrior(HierarchicalPhylogeneticModel hpm, XMLWriter writer) {
264 // Normal prior on mean
265 writer.writeOpenTag(PriorParsers.NORMAL_PRIOR, getMeanPriorAttributes(hpm));
266 writer.writeIDref(ParameterParser.PARAMETER, hpm.getConditionalParameterList().get(0).getName());
267 writer.writeCloseTag(PriorParsers.NORMAL_PRIOR);
268
269 // Gamma prior on precision
270 writer.writeOpenTag(PriorParsers.GAMMA_PRIOR, getPrecisionPriorAttributes(hpm));
271 writer.writeIDref(ParameterParser.PARAMETER, hpm.getConditionalParameterList().get(1).getName());
272 writer.writeCloseTag(PriorParsers.GAMMA_PRIOR);
273 }
274
275 protected String getCommentLabel() {
276 return "Hierarchical phylogenetic models";
277 }
278
279 }