Debian Codegraph @

src/dr/inference/operators/NormalNormalMeanGibbsOperator.java

raw symbols SCIP
1 /*
2 * NormalNormalMeanGibbsOperator.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.inference.operators;
27
28 import dr.inference.distribution.DistributionLikelihood;
29 import dr.inference.distribution.LogNormalDistributionModel;
30 import dr.inference.distribution.NormalDistributionModel;
31 import dr.inference.model.Parameter;
32 import dr.math.MathUtils;
33 import dr.math.distributions.Distribution;
34 import dr.math.distributions.NormalDistribution;
35 import dr.util.Attribute;
36 import dr.xml.*;
37
38 import java.util.List;
39
40 /**
41 * @author Marc A. Suchard
42 * @author Philippe Lemey
43 */
44 public class NormalNormalMeanGibbsOperator extends SimpleMCMCOperator implements GibbsOperator {
45
46 public static final String OPERATOR_NAME = "normalNormalMeanGibbsOperator";
47 public static final String LIKELIHOOD = "likelihood";
48 public static final String PRIOR = "prior";
49
50 public NormalNormalMeanGibbsOperator(DistributionLikelihood inLikelihood, Distribution prior,
51 double weight) {
52
53 if (!(prior instanceof NormalDistribution || prior instanceof NormalDistributionModel))
54 throw new RuntimeException("Mean prior must be Normal");
55
56 this.likelihood = inLikelihood.getDistribution();
57 this.dataList = inLikelihood.getDataList();
58 if (likelihood instanceof NormalDistributionModel)
59 this.meanParameter = (Parameter) ((NormalDistributionModel) likelihood).getMean();
60 else if (likelihood instanceof LogNormalDistributionModel) {
61 if (((LogNormalDistributionModel) likelihood).getParameterization() == LogNormalDistributionModel.Parameterization.MEAN_STDEV) {
62 this.meanParameter = ((LogNormalDistributionModel) likelihood).getMeanParameter();
63 } else {
64 this.meanParameter = ((LogNormalDistributionModel) likelihood).getMuParameter();
65 }
66 isLog = true;
67 } else
68 throw new RuntimeException("Likelihood must be Normal or log Normal");
69
70 this.prior = prior;
71 setWeight(weight);
72 }
73
74 /**
75 * @return a short descriptive message of the performance of this operator.
76 */
77 public String getPerformanceSuggestion() {
78 return null;
79 }
80
81 public String getOperatorName() {
82 return OPERATOR_NAME;
83 }
84
85 /**
86 * Called by operate(), does the actual operation.
87 *
88 * @return the hastings ratio
89 * @throws OperatorFailedException if operator fails and should be rejected
90 */
91 public double doOperation() {
92
93 double priorPrecision = 1.0 / prior.variance();
94 double priorMean = prior.mean();
95 double likelihoodPrecision = 1.0 / likelihood.variance();
96
97 double total = 0;
98 int n = 0;
99 for ( Attribute<double[]> statistic : dataList ) {
100 for (double x : statistic.getAttributeValue()) {
101 if (isLog)
102 total += Math.log(x);
103 else
104 total += x;
105 n++;
106 }
107 }
108
109 double precision = priorPrecision + likelihoodPrecision * n;
110 double mu = (priorPrecision * priorMean + likelihoodPrecision * total) / precision;
111 meanParameter.setParameterValue(0,
112 MathUtils.nextGaussian() / Math.sqrt(precision) + mu); // N(\mu, \precision)
113 return 0;
114 }
115
116 /**
117 * @return the number of steps the operator performs in one go.
118 */
119 public int getStepCount() {
120 return 1;
121 }
122
123 public static dr.xml.XMLObjectParser PARSER = new dr.xml.AbstractXMLObjectParser() {
124
125 public String getParserName() {
126 return OPERATOR_NAME;
127 }
128
129 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
130
131 double weight = xo.getDoubleAttribute(WEIGHT);
132
133 DistributionLikelihood likelihood = (DistributionLikelihood) ((XMLObject) xo.getChild(LIKELIHOOD)).getChild(DistributionLikelihood.class);
134 DistributionLikelihood prior = (DistributionLikelihood) ((XMLObject) xo.getChild(PRIOR)).getChild(DistributionLikelihood.class);
135
136 // System.err.println("class: " + prior.getDistribution().getClass());
137
138 if (!((prior.getDistribution() instanceof NormalDistribution) ||
139 (prior.getDistribution() instanceof NormalDistributionModel)
140 ) ||
141 !((likelihood.getDistribution() instanceof NormalDistributionModel) ||
142 (likelihood.getDistribution() instanceof LogNormalDistributionModel)
143 ))
144 throw new XMLParseException("Gibbs operator assumes normal-normal model");
145
146 return new NormalNormalMeanGibbsOperator(likelihood, prior.getDistribution(), weight);
147 }
148
149 //************************************************************************
150 // AbstractXMLObjectParser implementation
151 //************************************************************************
152
153 public String getParserDescription() {
154 return "This element returns a operator on the mean parameter of a normal model with normal prior.";
155 }
156
157 public Class getReturnType() {
158 return MCMCOperator.class;
159 }
160
161 public XMLSyntaxRule[] getSyntaxRules() {
162 return rules;
163 }
164
165 private final XMLSyntaxRule[] rules = {
166 AttributeRule.newDoubleRule(WEIGHT),
167 new ElementRule(LIKELIHOOD,
168 new XMLSyntaxRule[]{
169 new ElementRule(DistributionLikelihood.class)
170 }),
171 new ElementRule(PRIOR,
172 new XMLSyntaxRule[]{
173 new ElementRule(DistributionLikelihood.class)
174 }),
175 };
176
177 };
178
179 private final Distribution likelihood;
180 private final Distribution prior;
181 private boolean isLog = false;
182
183 private final List<Attribute<double[]>> dataList;
184 private final Parameter meanParameter;
185 }